SQL语句查询
数据查询:select
数据定义:create,drop,alter
数据操纵:insert,update,delete
数据控制:grant,revoke
1、数据定义(create drop,alter)
(1)定义模式:
Create schema <模式名> authorization <用户名>;
(2)删除模式:
Drop schema <模式名> <cascade|restrict>;
(3)定义基本表:
Create Table Student(
Sno char(10)primary key,
Sname char(20) unique,
Foreign key 外键名 reference Course(Cno),
);
(3)修改表
①新加一列:
Alter table 表名 Add 新加列名 列名类型;
②更改数据类型:
Alter table 表名 alter column 列名 新类型;
③增加条件是唯一:
Alter table 表名 Add unique(列名);
(4)删除基本表E
Drop table <表名> [cascade|restrict]
(5)建立索引
Create [unique] [cluster] index <索引名>
On <表名>(<列名>[ASC|DESC]…);
(6)修改索引
①重新命名:
Alter index <旧索引名> Rename To <新索引名>;
(7)删除索引
Drop index <索引名>;
(8)定义视图
Create view <视图名> [<列名1>…]
AS <子查询>
With check option;
(8)删除视图
Drop view <视图名> [cascade];
2、数据查询(select)
Select [All|Distinct] <目标列表达式>…
From <表名或视图名>
[where <条件表达式>]
[Group by <列名1>] [Having <条件表达式>]
[Order by <列名2>] [ASC|DESC]
注:
①Distinct可以取消重复的行
②Eg1:
select *
From student
Where sno like ‘20222043’;
[Where sno =‘20222043’;]
当like后面的匹配串不带有通配符%和_,则可以用=/!=/</>表示。
Where sname like ‘吴%’;
Where sname not like ‘吴_ _’;
这样的就只能用like
③查询DB_Design(本事查询的内容带有通配符符号)
Where Cname like ‘DB\_Design’Escape ‘\’;
④涉及空值的查询
Where Grade Is NULL;
Where Grade Is Not NULL;
Is不能用=替
⑤Order by
⑥Group by
3、数据操纵(insert,update,delete)
(1)插入元组
Insert
into <表名> [(<属性列1>[属性列2])…]
Values(常量1,常量2…)
[子查询];
(2)修改数据
Update <表名>
set <列名>=<表达式> [<列名>=<表达式>]
[Where <条件>];
①修改某个元组:
Update Student
Set Sage=18
Where Sno=’20222043’;
②修改多个元组:
Update Student
Set Sage=18
Where Sno In(
Select Sno
From Student
Where Sdept=’Cs’
);
(3)删除数据
Delete
From <表名>
[Where <条件>]