1.mysql约束的作用:表在设计的时候加入约束目的就是为了保证表中的记录完整性和有效性,比如用户表有些列的值(手机号)不能为空,有些列的值(身份证号)不能重复
分类:
主键约束(primary key)PK
自增长约束(auto_increment)
非空约束(not null)
唯一性约束(unique)
默认约束(default)
零填充约束(zerofill)
外键约束(foreign key)FK
2.主键约束
1)MySql主键约束是一个列或者多个列的组合
2)主键约束相当于唯一约束+非空约束
3)每个表做多只允许一个主键
4)主键约束的关键字:primarty key
5)当创建主键约束时,系统默认会在所在的列或者列组合建立对应的唯一索引
--添加单列主键
--方式1:
create table 表名(
...
<字段名> <数据类型> primary key
...
)
--方式2:
create table student(
sid ind primary key,
name varchar(20),
dept int,
salary double
)
--添加多列主键(联合主键)
create table 表名(
...
primary key (字段1,字段2,...,字段n)
);
--例子
create table student(
sid int,
name varchar(20),
dept int,
salary double,
constraint stuId primary key (name,dept) --constraint给主键起一个别名
)
--通过修改表结构添加主键
create table 表名(
...
);
alter table <表名> add primary key (字段列表);
--添加单列主键
create table student(
sid int,
name varchar(20),
dept int,
salary double
)
alter table student add primary key (sid);
--删除主键约束
alter table <数据表名> drop primary key;
3.自增长约束
概念:当主键定义为自增长后,主键的值就不需要用户输入数据了,每增加一条记录,主键自动以相同的步长进行增长
特点:
1)默认情况下,auto_increment的初始值是1,每新增一条记录,字段值自动加1
2)一个表中只能有一个字段使用auto_increment约束,且该字段必须有唯一索引,以避免序号重复
3)auto_increment约束的字段必须具备not null属性
4)auto_increment约束的字段只能是整数类型(tinyint、smallint、int、bigint)
5)auto_increment约束字段的最大值为该字段的数据类型最大值,如果达到上限,auto_increment会失效
--自增长约束
字段名 数据类型 auto_increment
--例子
create table student(
id int primary key auto_increment,
name varchar(20)
);
--指定自增字段初始值
create table student(
id int primary key auto_increment,
name varchar(20)
)auto_increment=100;
delete和truncate在删除数据后自增列的变化:
1)delete数据之后自动增长从断点开始
2)truncate数据之后自动增长从默认起始值开始
4.非空约束(not null)
--非空约束
方式1:<字段名><数据类型> not null;
方式2:alter table 表名 modify 字段 类型 not null;
--例子1
create table student(
id int,
name varchar(20) not null,
address varchar(20) not null
);
--例子2
create table student(
id int,
name varchar(20),
address varchar(20)
);
alter table student modify name varchar(20) not null;
--删除非空约束
alter table student modify name varchar(20);
4.唯一约束(unique)
概念:所有记录中字段的值不能重复出现
--新增唯一约束
方式1:<字段名><数据类型> unique
方式2:alter table 表名 add constraint 约束名 unique(列)
--例子1:
create table student(
id int,
name varchar(20),
phone_number varchar(20) unique --指定唯一约束
);
--例子2:
create table student(
id int,
name varchar(20),
phone_number varchar(20)
);
alter table student add constraint temp unique(phone_number);
--删除唯一约束
alter table <表名> drop index <唯一约束名>
alter table student drop index temp;
--注:如果没有约束名字,默认为该列的名字
5.默认约束(default)
概念:指定某列的默认值
--默认约束
方式1:<字段名><数据类型> default <默认值>;
方式2:alter table 表名 modify 列名 类型 default 默认值;
--例子1:
create table student(
id int,
name varchar(20),
address varchar(20) default '北京' --指定默认约束
);
--例子2:
create table student(
id int,
name varchar(20),
address vachar(20)
);
alter table student modify address varchar(20) default '北京';
--删除默认约束
alter table <表名> change column <字段名><类型> default null;
alter table student change column address varchar(20) default null;
6.零填充约束(zerofill)
概念:
1)插入数据时,当该字段值的长度小于定义的长度时,会在该值的前面补上相应的0
2)zerofill默认为int(10)
3)当使用zerofill时,默认会自动加unsigned(无符号)属性,使用unsigned属性后,数值范围是原值的2倍
--零填充约束
create table student(
id int zerofill, --零填充约束
name varchar(20)
);
--删除零填充约束
alter table student modify id int;