语句分类
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;