数据库主要关键字

一、约束关键字
1.primary key 主键

主键约束字段不能为空,不能重复,一张表只能有一个主键

2.foreign key 外键

eg:foreign key(Sno)
一般会和references一起出现,如下:

3.references

A表Sno需要B表Sno数据,结合外键
eg:
foreign key(Sno)  references B(Sno),

4.unique 唯一键
5.check 约束

eg:
check(Ssex=‘男’ or Ssex='女’)
check(age>=1 and age<=100)
此处也可以用 age between ** and**
not between ** and **

6.default 默认值

未添加数据时的默认值

7.not null

不能为空

8.indentity(种子值,增量值) 自增
9.zerofill 若长度未达到规定,自动在字段前填充0
二、增删查改关键字
1.insert 插入

insert into 表名(列名以逗号隔开) values(以逗号隔开,字符型带‘’);
eg:
insert into Student(Sno,Ssex) values(10,'男');

2.update 修改

1.update 表名  set  将要修改的列名=修改的数据;    将表中此列所有数据都修改
2.update 表名  set  将要修改的列名=修改的数据   where 列名=?;   此列中列名=?的修改数据
eg:
update goods set price=price+10;
update orders set quantity=5  where customerid=2;

3.delete 删除

delete from orders where customerid=2;  删除了orders表中customerid=2的记录

4.select 查询
(1)

1.select  需要查找的列名;(>=2以逗号隔开)  from 表名;
2.select*  from 表名;    查找此表所有数据
3.select 2014-sage from student;    查询结果是2014-sage的值
4.select ‘year of birth:’,2014-sage from student;   ''内的内容单独作为一列原样输出
5.select sname NAME from student;   将原列名sname改为NAME

(2)distinct消除重复

eg:
select distinct sno from sc;  消除sc表中sno重复行

(3)where 查询条件

在这里插入代码片

(4)lower 小写

eg:
select lower sdept from student; 将student表sdept列以小写显示

(5)like 和not like

若like后不含通配符,则可以取代= 
eg:
select* from student where sno like '2015486';  
若含有通配符(%:a%b y以a开头,b结尾任意长度的字符串,和_:a_b以a开头 b结尾的长度为3的字符串)
select sname from student  wher sname like '_阳%'; 查找名字中第二个字为阳的学生姓名  

(6)as

在这里插入代码片

(7)in 和 not in

eg:
select sname from student  where sdept in('SC'); 查找student表中sdept列中在SC系的学生名字
与之对应 not in

(8)escape’’

跟在\后的_  只代表_  不作为通配符

(9)order by 列名 asc升序 /desc降序
(10)group by 列名 按值分组

猜你喜欢

转载自blog.csdn.net/Cenny_/article/details/90348785