SQL 语句语法简介(一)
语句分类
SQL 命令一般分为三类:DQL、DML、DDL。
一、DDL语句。
1.1建表语句
CREATE TABLE table_name(
col01_name data_type,
col02_name data_type,
);
实例:
student_name varchar(40),
chinese_score int,
math_score int,
test_date date
);
CREATE TABLE
\d 显示表
postgres=# \d score
Table "public.score"
Column | Type | Modifiers
---------------+-----------------------+-----------
student_name | character varying(40) |
chinese_score | integer |
math_score | integer |
test_date | date |
主键
创建表的时候可以指定主键primary key,标识这一列唯一,不能重复。
postgres=# create table student(
postgres(# no int primary key,
postgres(# student_name varchar(40),
postgres(# age int);
CREATE TABLE
1.2 删除表
drop table table_name;
二、DML语句。
DML用于插入、更新和删除数据。insert、update、delete。
2.1 插入语句:
postgres=# insert into student(no,age,student_name) values(2,13,'steven');
INSERT 0 1
2.2 更新语句:
postgres=# update student set age = 15;
UPDATE 1
使用where过滤:
postgres=# update student set age=14 where no = 3;
UPDATE 1
2.3 删除语句:
postgres=# delete from student where no = 3;
DELETE 1
2.4.1 查询语句:
postgres=# select no,student_name,age from student;
no | student_name | age
----+--------------+-----
2 | haha | 15
1 | steven | 13
(2 rows)
2.4.2 过滤查询:
postgres=# select * from student where age > 14;
no | student_name | age
----+--------------+-----
2 | haha | 15
(1 row)
2.4.3排序
postgres=# select * from student order by age;
no | student_name | age
----+--------------+-----
1 | steven | 13
2 | haha | 15
(2 rows)
2.4.4倒序
postgres=# select * from student order by no desc;
no | student_name | age
----+--------------+-----
2 | haha | 15
1 | steven | 13
(2 rows)
2.4.5分组
postgres=# select age,count(*) from student group by age;
age | count
-----+-------
15 | 1
13 | 1
(2 rows)
2.4.6多表联查
postgres=# create table class(no int primary key,
class_name varchar(40));
CREATE TABLE
postgres=# insert into class values(1,'初二(1)班');
INSERT 0 1
postgres=# insert into class values(2,'初二(2)班');
INSERT 0 1
postgres=# insert into class values(3,'初二(3)班');
INSERT 0 1
postgres=# insert into class values(4,'初二(4)班');
INSERT 0 1
postgres=# select * from class;
no | class_name
----+-------------
1 | 初二(1)班
2 | 初二(2)班
3 | 初二(3)班
4 | 初二(4)班
(4 rows)
postgres=# create table student2(no int primary key,
student_name varchar(40),
age int,
class_no int);
CREATE TABLE postgres=# insert into student2 values(1,'a',12,1);
INSERT 0 1
postgres=# insert into student2 values(2,'b',13,1);
INSERT 0 1
postgres=# insert into student2 values(3,'c',15,2);
INSERT 0 1
postgres=# insert into student2 values(4,'d',15,2);
INSERT 0 1
postgres=# insert into student2 values(5,'e',15,3);
INSERT 0 1
postgres=# insert into student2 values(6,'f',15,2);
INSERT 0 1
postgres=#
postgres=# select student_name,class_name from student2,class
where student2.class_no = class.no;
student_name | class_name
--------------+-------------
a | 初二(1)班
b | 初二(1)班
c | 初二(2)班
d | 初二(2)班
e | 初二(3)班
f | 初二(2)班
(6 rows)
三、其它SQL语句
insert into ....select语句,可以把一个表插入到另外一张表中。
postgres=# create table student_bak(no int primary key,
postgres(# sutdent_name varchar(40),
postgres(# age int,
postgres(# class_no int);
CREATE TABLE
postgres=# insert into student_bak select * from student;
INSERT 0 2
postgres=# select * from student_bak;
no | sutdent_name | age | class_no
----+--------------+-----+----------
2 | haha | 15 |
1 | steven | 13 |
(2 rows)
union语句将两个表查询的数据整合在一起
postgres=# select * from student2 where no = 1 union select * from student_bak
where no =2;
no | student_name | age | class_no
----+--------------+-----+----------
1 | a | 12 | 1
2 | haha | 15 |
(2 rows)
truncate table 清楚表数据
postgres=# truncate table student_bak;
SQL 语句语法简介(一)的更多相关文章
- SQL语句语法简介
SQL命令一般分为DQL.DML.DDL几类: DQL:数据查询语句,基本就是SELECT查询命令,用于数据查询 DML:Data Manipulation Language的简称,即数据操纵语言,主 ...
- 【知识库】-数据库_MySQL常用SQL语句语法大全示例
简书作者:seay 文章出处: 关系数据库常用SQL语句语法大全 Learn [已经过测试校验] 一.创建数据库 二.创建表 三.删除表 四.清空表 五.修改表 六.SQL查询语句 七.SQL插入语句 ...
- 关系数据库常用SQL语句语法大全
创建表 语法 CREATE TABLE <表名>(<列名> <数据类型>[列级完整性约束条件] [,<列名> <数据类型>[列级完整性约束条 ...
- 基础SQL语句/语法
SQL是现在进入互联网工作人们的必须技能之一,下面分享自己觉得很nice的SQL基本语句,从网上找了,觉得很不错,就分享给大家!简要介绍基础语句: 1.说明:创建数据库 Create DATABAS ...
- mysql数据库之基础SQL语句/语法
SQL是现在进入互联网工作人们的必须技能之一,下面分享自己觉得很nice的SQL基本语句,从网上找了,觉得很不错,就分享给大家!简要介绍基础语句: 1.说明:创建数据库 Create DATABAS ...
- sql语句语法大全
一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...
- sql语句语法
13.1. 数据定义语句 . ALTER DATABASE语法 . ALTER TABLE语法 . CREATE DATABASE语法 . CREATE INDEX语法 . CREATE TABLE语 ...
- 转载-增删改查sql语句语法
一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> 例:insert into Strdent ...
- 简单实用 “易忘” 的SQL 语句语法,新老皆宜
--创建数据库 create database 数据库名 on primary ( name='数据库名_data', filename='数据库储存路径', size=数据库初始大小(MB), ...
随机推荐
- java基础-day22
第11天 多线程 今日内容介绍 u 多线程概述 u 线程实现 u 多线程安全问题产生 & 解决方案 第1章 多线程概述 学习多线程之前,我们先要了解几个关于多线程有关的概念. A ...
- PAT甲级 1122. Hamiltonian Cycle (25)
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
- Codeforces822 B. Crossword solving
B. Crossword solving time limit per test 1 second memory limit per test 256 megabytes input standard ...
- PAT甲级 1126. Eulerian Path (25)
1126. Eulerian Path (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue In grap ...
- RPC、RMI、SOAP、WSDL的区别详解
RPC与RMI的区别============================================================================RPC:(Remote Pr ...
- swift能干什么,不能干什么及相关概念
1.swift 是什么?OpenStackObject Storage (Swift) 是开源的,用来创建可扩展的.冗余的.对象存储(引擎). swift使用标准化的服务器存储 PB 级可用数据.但它 ...
- phpstudy 配置 memcached / memcache
https://blog.csdn.net/zql898626913/article/details/77309269
- 没那么难,谈CSS的设计模式
没那么难,谈CSS的设计模式 来源: 灵感的小窝 发布时间: 2016-09-09 16:46 阅读: 8949 次 推荐: 27 原文链接 [收藏] 什么是设计模式? 曾有人调侃, ...
- Android x86模拟器Intel Atom x86 System Image配置与使用方法
Android x86模拟器Intel Atom x86 System Image配置与使用方法 前言: 大家现在开发使用的Android 模拟器模拟的是 ARM 的体系结构(ar ...
- Idea14 生成webservices
一直为idea生成soap协议的webservices而纠结,当初做axis2的时候,用的是eclipse,用它的插件来生成.这次做短信平台,决定要换一下,因为eclipse用axis2生成的接口,会 ...