MySQL数据库--外键约束

MySQL数据库--约束  

  首先我们把建立数据库表,讲下列内容k到数据库表中

create table student(
id char(10),
name varchar(30),
mobile char(11),
address varchar(120)
)

#添加数据

insert into student (id,name,mobile,address) values ('0000000001','Jimmy','18898098765','河南省郑州市高新技术开发区莲花街11号')
insert into student (id,name,mobile,address) values ('0000000001','Tonny','13378909876','新疆自治区乌鲁木齐海拉尔区葵花街130号')
insert into student (id,name,mobile,address) values (null,'Katty','13589760976','吉林省长春市宽平区1356号')

 

#为什么?默认情况下,如果字段没有约束条件,则用户添加的数据可能存在错误 = 约束限制用户的行为,确保数据有效性


#1、not null 不能为空

drop table student
create table student(
id char(10) not null,
name varchar(30),
mobile char(11),
address varchar(120)
)
insert into student (id,name,mobile,address) values (null,'Katty','13589760976','吉林省长春市宽平区1356号')
insert into student (id,name,mobile,address) values ('0000000001','Jimmy','18898098765','河南省郑州市高新技术开发区莲花街11号')
insert into student (id,name,mobile,address) values ('0000000001','Tonny','13378909876','新疆自治区乌鲁木齐海拉尔区葵花街130号')

 

#2、unique 数据必须唯一,但是可以为null,并且可以为多个null

drop table student
create table student(
id char(10) unique,
name varchar(30),
mobile char(11),
address varchar(120)
)
insert into student (id,name,mobile,address) values ('0000000001','Jimmy','18898098765','河南省郑州市高新技术开发区莲花街11号')
insert into student (id,name,mobile,address) values ('0000000001','Tonny','13378909876','新疆自治区乌鲁木齐海拉尔区葵花街130号')
insert into student (id,name,mobile,address) values (null,'Katty','13589760976','吉林省长春市宽平区1356号')
insert into student (id,name,mobile,address) values (null,'Lucy','12298076675','北京北京市海淀区五道口')

 

#3、primary key 主键:不允许为null且不能重复

drop table student
create table student(
id char(10) primary key,
name varchar(30),
mobile char(11),
address varchar(120)
)
insert into student (id,name,mobile,address) values ('0000000001','Jimmy','18898098765','河南省郑州市高新技术开发区莲花街11号')
insert into student (id,name,mobile,address) values ('0000000001','Tonny','13378909876','新疆自治区乌鲁木齐海拉尔区葵花街130号')
insert into student (id,name,mobile,address) values (null,'Katty','13589760976','吉林省长春市宽平区1356号')
insert into student (id,name,mobile,address) values (null,'Lucy','12298076675','北京北京市海淀区五道口')

 

#3、auto_increment 自增长,注意:a、此时数据类型不能使字符型;b、必须是主键

drop table student
create table student(
id int(10) primary key auto_increment,
name varchar(30),
mobile char(11),
address varchar(120)
)
insert into student (name,mobile,address) values ('Jimmy','18898098765','河南省郑州市高新技术开发区莲花街11号')
insert into student (name,mobile,address) values ('Tonny','13378909876','新疆自治区乌鲁木齐海拉尔区葵花街130号')

 

#4、default 设置字段默认值,插入数据时如果没有指定值,则使用默认值;

drop table student
create table student(
id char(10),
name varchar(30),
sex int(1) default 0,
mobile char(11),
address varchar(120)
)
insert into student (id,name,mobile,address) values ('0000000001','Jimmy','18898098765','河南省郑州市高新技术开发区莲花街11号')
insert into student (id,name,mobile,address) values ('0000000001','Tonny','13378909876','新疆自治区乌鲁木齐海拉尔区葵花街130号')

 

#5、注释:comment  对数据库表中的元素进行注释

drop table student
create table student(
id char(10) primary key comment '学号,主键',
name varchar(30) comment '姓名',
sex int(1) default 0 comment '性别:0-男,1-女',
mobile char(11) comment '手机号',
address varchar(120) comment '家庭地址'

 

posted @ 2021-04-02 17:54  好人有好报  阅读(123)  评论(0)    收藏  举报