not null 不能存储null值
unique 某列或者多个列,每列必须有唯一值,可以有多个唯一,多个列的唯一用逗号分隔
default 默认字段名,可以起别名,规定没有赋值时的默认值
primary key 相当于not null 和unique的结合,确保多个列的组合只能有一个
foreign key 保证一个表中的数据匹配另外一个表中的值的参照完整性
创建表时的写法:
create table 表名(
字段1 字段类型 primary key,
字段2 字段类型 not null,
字段3 字段类型 ,
字段4 字段类型 default "默认值",
字段5 字段类型 ,
字段6 字段类型 ,
.....
unique(字段3),(如果的难度命名或者多个列唯一constraint自定义名 unique(字段2,字段3,。。。))
primary key(字段1,字段2,。。。。)
);
添加,修改,删除not null default
其实就是修改字段
alter table 表名 modify 字段名 字段类型 not null default "默认值";
常见的约束
/*
含义:一种限制,用于限制表中的数据,为了保证表中数据的准确性和可靠性
例如对于学号不能重复:需要给与约束
分类:6大约束
not null:非空,用于保证该字段的值不能为空
default:默认,用于保证该字段有默认值
比如性别
Primary key:主键,用于保证该字段的值是唯一的,且非空
unique:唯一,用于保证该字段的值是唯一的,可以为空
比如座位号
check:检查约束【mysql不支持】,用于加条件,类似枚举
比如年龄、性别
foriegn key:外键,用于限制两个表的关系,保证该字段的值必须来自主表的关联列的值
例如:有2个表,一个表是学生的信息(包含学号,姓名,性别,专业代号等),另外一个表
是专业信息(包含专业代号,专业名称);我们对学生信息表的专业代号需要
添加外键约束,使它里面的专业代号的编码来自于专业表格里面的内容,如果
不是严格来自专业表格,则有些专业代号是查不到名称的。
在从表中添加外键约束,用于引用主表中某列的值
比如学生表的专业编号,员工表的部门编号,员工表的工种编号
添加约束的时机(均在插入数据之前):
1.创建表时
2.修改表时
约束的添加分类
列级约束:
6大约束,语法上均支持,但是外键约束没有效果
表级约束:
除了非空,默认,其他都支持
主键和唯一的对比:
保证唯一性 是否允许为空 一个表中可以有多少个 是否允许组合
主键 是 否 至多有一个 允许,不推荐
唯一 是 是 可以有多个 允许,不推荐
组合主键 primary key(id,stuNmae);
1,"john"
1,"lily"
2,"john"
以上三种均支持
但是不支持这种 1,"john"
1,"john"
完全重复
外键:
1.要求在从表设置外键关系
2.从表的外键列的类型和主表的类型要求一致或兼容,名称无所谓
3.主表的关联列必须是一个key(一般是主键或者唯一)
4.插入数据时,应该先插入主表,再插入从表
删除数据时,先删除从表,再删除主表
insert into major values(1,"java");
insert into major values(2,"python");
insert into major values(3,"c++");
insert into major values(4,"html");
select * from major;
select * from student;
insert into student values(1,"john","男",3,14,4);
insert into student values(2,"lily","女",4,14,4);
insert into student values(3,"mike","男",null,13,1);
insert into student values(4,"disy","女",null,14,2);
select * from student;
*/
create table 表名(
字段名 字段类型 列级约束,
字段名 字段类型,
表级约束);
#一.创建表时如何添加约束
create database students;
#1.添加列级约束
/*
语法:
直接在字段名和类型后面追加 约束类型即可。
仅支持:默认,唯一,主键,非空
*/
use students;
create table student(
id int primary key,#主键
stuName varchar(20) not null, #非空
gender char(1) check(gender in("男","女")),#检查约束
seat int unique,#唯一约束
age int default 14,#默认约束
majorId int references major(id)#外键约束
);
create table major(
id int primary key,
majorName varchar(20));
desc student;
#查看表中所有的索引,包括主键、外键
show index from student;
#2.添加表级约束
/*
语法:在各个字段的最下面
【constraint 约束名】 约束类型(字段名)
*/
drop table if exists student; #删除表
create table student(
id int,
stuName varchar(20) ,
gender char(1),
seat int,
age int,
majorId int,
constraint pk primary key(id),#添加表级约束,标记约束的名称为pk,为id字段添加主键约束
constraint uq unique(seat),#为字段seat添加唯一键
constraint ck check(gender in("男","女")),#添加检查约束
constraint fk_student_major foreign key(majorid) references major(id)#外键
);
show index from student;
#【constraint 约束名】 约束类型(字段名)
drop table if exists student; #删除表
create table student(
id int,
stuName varchar(20) ,
gender char(1),
seat int,
age int,
majorId int,
primary key(id),#添加表级约束,标记约束的名称为pk,为id字段添加主键约束
unique(seat),#为字段seat添加唯一键
check(gender in("男","女")),#添加检查约束
foreign key(majorid) references major(id)#外键
);
show index from student;
#效果和上述加了constraint一致
#通用的写法
create table student(
id int primary key,
stuName varchar(20) not null,
gender char(1),
seat int unique,
age int default 14,
majorId int,
constraint fk_student_major foreign key(majorid) references major(id)#外键
);
show index from student;
#二、修改表时添加约束
/*
总结:
1.添加列级约束
alter table 表名 modify column 字段名 字段类型 新约束;
2.添加表级约束
alter table 表名 add【constraint 约束名】 约束类型(字段名)【reference 主表名(主表字段)只适合外键】;
*/
重新删除表,建一个没有约束的表
drop table if exists student; #删除表
create table student(
id int,
stuName varchar(20) ,
gender char(1),
seat int,
age int,
majorId int
);
#1. 添加非空约束
alter table student modify column stuName varchar(20) not null;
# 显示添加的索引
show index from student;
# 显示表中的内容数据
select * from student;
# 显示表中的结构
desc student;
#2.添加默认约束
alter table student modify column age int default 14;
#3.添加主键
方式一:列级约束
alter table student modify column id int primary key;
方式二:表级约束
alter table student add primary key(id);
#4.添加唯一键
方式一:列级约束
alter table student modify column seat int unique;
方式二:表级约束
alter table student add unique(seat);
#5.添加外键:只适合表级约束
alter table student add 【constraint fk_student_major】foreign key(majorid) references major(id);
#三、修改表时删除约束
#1. 删除非空约束
alter table student modify column stuName varchar(20) null;
# 显示表中的结构
desc student;
#2.删除默认约束
alter table student modify column age int;
#3.删除主键
方式一,直接不写主键
alter table student modify column id int;
方式二,直接删除
alter table student drop primary key;
#4.删除唯一
alter table student drop index seat;
show index from student;
#5.删除外键约束
alter table student add constraint fk_student_major foreign key(majorid) references major(id);
alter table student drop foreign key fk_student_major;