一、create语句
建表的语法:
create table 表名(
列名 类型,
列名 类型,
列名 类型
........
)
在同一个数据库中表名不能重复
-- 创建学生表
create table student(
id int, -- 不重复的id值
sno char(10), -- 学号
sname varchar(16),
birthday date,
tel char(11),
height float(3,2) -- 单位是米,一共3位数,1位整数部分,2位小数部分
)
二、约束
为了保证数据的完整性,要为表添加一些约束。约束一般都在建表时创建,若是表中有数据后再创建约束,有可能约束创建失败。
约束分类:
(1)主键约束
(2)外键约束
(3)唯一约束
(4)非空约束
(5)检查约束
1.主键约束
具有非空且唯一的特性,可以为一个列添加主键约束,那么这列的数据不能重复且不能为空;也 可以为多列添加一个主键约束(复合主键),那么这些列的组合不能重复的且都不能为空。一个表只能加一个主键约束,可以通过主键约束的列的值确定唯一的一条数据。
写法一:在需要约束的属性后直接加 primary key
create table student(
id int primary key, -- 不重复的id值
sno char(10), -- 学号
sname varchar(16),
birthday date,
tel char(11),
height float(3,2) -- 单位是米
)
写法二:在建表时添加主键约束 constraint pk_student primary key(id)
create table student(
id int, -- 不重复的id值
sno char(10), -- 学号
sname varchar(16),
birthday date,
tel char(11),
height float(3,2), -- 单位是米\
-- 添加了一个主键约束,约束的列是id ,约束的名字是pk_student
constraint pk_student primary key(id)
)
写法三:使用alter语句,通过更新表结构的方式添加约束
alter table student add constraint pk_student primary key(id)
alter table student add constraint fk_student_classid foreign key(classid) REFERENCES class(id)
不推荐使用复合主键
2.外键约束:
为一列添加外键约束,这列的值一定是另一个表主键的值。外键约束关联了两个表的关系
写法:
create table student(
id int, -- 不重复的id值
sno char(10), -- 学号
sname varchar(16),
birthday date,
tel char(11),
height float(3,2), -- 单位是米\
classid int,
-- 添加了一个主键约束,约束的列是id ,约束的名字是pk_student
constraint pk_student primary key(id,sno),
constraint fk_student_classid foreign key(classid) REFERENCES class(id)
)
将表pk_student和表fk_student_classid进行关联
注意事项:
(1)外键约束的列值可以是空
(2)在添加两个表的数据时,先加主键表的数据,再加外键表的数据。
(3)在删除两个表的数据时,先删除外键表的数据,在删除主键表的数据。
3.唯一约束:
唯一且允许为空,为表添加多个唯一约束,从业务的角度上,有些列的值不能重复,就要添加一些唯一约束了。一个唯一约束可以约束一列或多列,多列的组合不能重复的。
写法:
create table student(
id int, -- 不重复的id值
sno char(10), -- 学号
sname varchar(16),
birthday date,
tel char(11),
height float(3,2), -- 单位是米\
classid int,
-- 添加了一个主键约束,约束的列是id ,约束的名字是pk_student
constraint pk_student primary key(id,sno),
constraint fk_student_classid foreign key(classid) REFERENCES class(id),
constraint unique_student1 unique(sno),-- 唯一约束
constraint unique_student2 unique(tel)
)
4.非空约束:约束列的值不能为空,只能写在列的后面(跟not null)
create table student(
id int, -- 不重复的id值
sno char(10), -- 学号
sname varchar(16) not null,
birthday date,
tel char(11),
height float(3,2) -- 单位是米\
)
5.检查约束:check ,在mysql中没有检查约束,为列约束条件,例如数值范围的条件
constraint check_student check(height>1 and height<3)
三、建表语句,列的属性设置
1. default 属性: 设置默认值
height float(3,2) default 1.7
2. binary属性:列在存储值时以二进制存储的,区分大小写。
sname varchar(16) binary
3. auto_increment属性:自增长的列的值自动+1的,只能为主键约束或唯一约束的列设置自增长属性,一个表只能有一列设置自增长属性。
copy表结构
-- 根据student表 创建 student2
create table student2
select * from student
-- 约束不会创建到student2中
四、alter 语句
更改(更新)数据库对象的结构,目前可以用来更改表结构。
更改表结构的语法:
alter table 表名 动作
其中的动作包括:
1. 增加列: add 列定义语句
alter table student add address varchar(255);
2. 修改列:change 或 modify 修改列的属性,包括列名、类型、其他属性,change 列名列定义语句
alter table student change address address varchar(128);
3. 删除列:drop 列名
alter table student drop address
4. 改表名:rename to 新表名
alter table student rename to stu
5. 增加、删除约束: alter table 表名 add 约束定义语句
alter table student add constraint pk_student primary key(id)
alter table student add constraint fk_student_classid foreign key(classid) REFERENCES class(id)
删除主键约束
alter table student drop primary key
删除外键约束
alter table student drop FOREIGN key fk_student_classid
五、drop语句
删除表的语法:
drop table [if exists] 表名
删除表结构和数据。
六、insert语句
插入语句,向表中插入一条或多条数据
语法:
insert into 表名 [(列名,列名)]
values(值,值) [,(值,值),(值,值)....]
如果没有指定要插入的列名,要插入这条数据为所有列,默认所有列的顺序是建表时列的顺序
值和列的个数要对应,顺序要对应
values后面的每个小括号,代表插入的一条数据
-- 向学生表插入数据
insert into student (id,sno,sname)
values(default,'10','jack')
insert into student -- 为所有列插入数据
values(default,'11','rose','2000-01-01','13311111111',default,null)
七、update语句
更新表中的数据
语法:
update student set 列名=值,列名=值... [where 布尔表达式]
例如:
-- 将jack的身高更新为1.75 ,jack的id是1
update student set height = 1.75 where id= 1
-- 将jack的身高更新为1.76 ,amt 更新为 1000 ,jack的id是1
update student set height = 1.76,amt = 1000 where id = 1
-- 将jack的,amt减900 ,jack的id是1
update student set amt = amt - 900 where id = 1
八、delete语句
删除表中的数据
语法:
delete from 表名 [where 布尔表达式]
例如:
delete from student where height is null
-- 删除id是8的学生
delete from student where id = 8