identity 自增长
primary key 主键
unique 唯一键
not null 非空
references 外键(引用)
1.删除表
drop table Student
2.修改表
alter table RenYuan add CC int
alter table RenYuan drop column CC
3.删除数据库
drop database CeShi
CRUD操作
insert添加数据 read 读取数据 update 修改数据 delete 删除数据
1.添加数据
insert into Nation values(‘n002‘,‘回族‘)
insert into Nation values(‘n003‘,‘‘)
insert into Nation(code,name) values(‘n004‘,‘维吾尔族‘)
insert into Friends values(‘p001‘,‘p007‘)
2.删除数据
delete from Nation 删除所有
delete from Friends where ids = 5
3.修改数据
update Friends set fcode=‘p016‘ 修改所有
update Friends set fcode=‘p006‘,mcode=‘p002‘ where ids=4
查询:
1.简单查询
select * from Info --查所有数据
select Code,Name from Info --查指定列的数据
select Code as ‘代号‘,Name as ‘姓名‘ from Info --给列指定别名
2.条件查询
select * from Info where Code=‘p001‘
select * from Info where Sex=‘true‘ and Nation=‘n001‘ --多条件并的关系
select * from Info where Sex=‘true‘ or Nation=‘n001‘ --多条件或的关系
3.范围查询
select * from Car where Price>40 and Price<50
select * from Car where Price between 40 and 50
4.离散查询
select * from Car where Code in (‘c001‘,‘c005‘,‘c010‘,‘c015‘)
select * from Car where Code not in (‘c001‘,‘c005‘,‘c010‘,‘c015‘)
5.模糊查询
select * from Car where Name like ‘%宝马%‘ --查包含宝马的
select * from Car where Name like ‘宝马%‘ --查以宝马开头的
select * from Car where Name like ‘%宝马‘ --查以宝马结尾的
select * from Car where Name like ‘宝马‘ --查等于宝马的
select * from Car where Name like ‘__E%‘ --查第三个字符是E的
% 代表是任意多个字符
_ 代表是一个字符
6.排序查询
select * from Car order by Price asc --以价格升序排列
select * from Car order by Price desc --以价格降序排列
select * from Car order by Oil desc,Price asc --以两个字段排序,前面的是主条件后面的是次要条件
7.分页查询
select top 5 * from Car
select top 5 * from Car where Code not in (select top 5 Code from Car)
当前页:page = 2; 每页显示:row = 10;
select top row * from Car where Code not in (select top (page-1)*row Code from Car)
8.去重查询
select distinct Brand from Car
9.分组查询
select Brand from Car group by Brand having count(*)>2
10.聚合函数(统计查询)
select count(*) from Car --查询所有数据条数
select count(Code) from Car --查询所有数据条数
select sum(Price) from Car --求和
select avg(Price) from Car --求平均
select max(Price) from Car --求最大值
select min(Price) from Car --求最小值