约束是为了限制表中的数据,为了保证表中的数据的准确和可靠性

约束分类

NOT NULL

非空,确保字段不为空
比如姓名,学号等

DEFAULT

默认值,为了确保每一个字段都有值

PRIMARY KEY

主键,确保该字段是唯一的,并且非空
举例:学号,身份证号

UNIQUE

唯一,用于保证该字段的值是唯一的,但是可以为空(有点像unique key)

check

检查约束(mysql中不支持)

foregin key

外键约束,为了限制两个表之间的关系

  1. # 添加列级约束
  2. create table stuinfo(
  3. id INT primary key,# 主键
  4. stuName varchar(20) not null,# 非空
  5. gender char(1) check(gender='男' or gender ='女'),# 检查
  6. seat int unique,# 唯一
  7. age int default 18,# 默认
  8. marjorId int references major(id) # 外键
  9. );
  10. create table major(
  11. id int primary key,
  12. majorName varchar(20)
  13. );
  14. desc stuinfo;
  15. show index from stuinfo;
  16. # 查看stuinfo中的索引
  17. CREATE TABLE `stuinfo` (
  18. `id` int(11) NOT NULL,
  19. `stuName` varchar(20) COLLATE utf8_bin NOT NULL,
  20. `gender` char(1) COLLATE utf8_bin DEFAULT NULL,
  21. `seat` int(11) DEFAULT NULL,
  22. `age` int(11) DEFAULT '18',
  23. `marjorId` int(11) DEFAULT NULL,
  24. PRIMARY KEY (`id`),
  25. UNIQUE KEY `seat` (`seat`),
  26. CONSTRAINT `stuinfo_chk_1` CHECK (((`gender` = _utf8mb4'男') or (`gender` = _utf8mb4'女')))
  27. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
  28. # 添加表级约束
  29. drop table if exists stuinfo;
  30. create table stuinfo(
  31. id int,
  32. stuname varchar(20),
  33. gender char(1),
  34. seat int,
  35. majorid int,
  36. CONSTRAINT pk PRIMARY key(id),# 主键
  37. CONSTRAINT uq UNIQUE(seat),# 唯一
  38. CONSTRAINT CK check(gender = '男' or gender = '女'),
  39. CONSTRAINT fk_stuinfo_major FOREIGN KEY (majorid) references major(id)#外键
  40. );
  41. CREATE TABLE `stuinfo` (
  42. `id` int(11) NOT NULL,
  43. `stuname` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  44. `gender` char(1) COLLATE utf8_bin DEFAULT NULL,
  45. `seat` int(11) DEFAULT NULL,
  46. `majorid` int(11) DEFAULT NULL,
  47. PRIMARY KEY (`id`),
  48. UNIQUE KEY `uq` (`seat`),
  49. KEY `fk_stuinfo_major` (`majorid`),
  50. CONSTRAINT `fk_stuinfo_major` FOREIGN KEY (`majorid`) REFERENCES `major` (`id`),
  51. CONSTRAINT `CK` CHECK (((`gender` = _utf8mb4'男') or (`gender` = _utf8mb4'女')))
  52. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

主键和唯一的区别

主键和唯一都可以保证为唯一性
主键不能为空,但唯一可以为空
主键表中只能有一个,唯一可以有多个
主键可以组合(多个字段组合在一起)唯一也支持组合()

需要注意的是在唯一中,null是互相不相等的。

外键关联的是主键或者唯一

修改表时,添加约束

## 添加非空
alter table tableName modify column column_name 类型 not null;
### 添加默认约束
alter table tableName modify column column_name 类型 default 10;
## 添加主键
#### 列级添加
alter table stuinfo column id int(11) primary key;
# 或者表级添加
alter table stuinfo add primary key(id);


## 添加唯一
alter table stuinfo modify column seat int unique;
alter table stuinfo add unique(seat);
alter table stuinfo add unique index(seat);
### 添加外键
alter table stuinfo add constraint fk_stuinfo_major foreign key(majorid) references major(id);

修改表时,删除约束

## 添加非空
alter table tableName modify column column_name 类型 null;
### 添加默认约束
alter table tableName modify column column_name 类型;
## 添加主键
#### 列级添加
alter table stuinfo column id int(11);
# 或者表级添加
alter table stuinfo drop primary key;


## 添加唯一
alter table stuinfo modify column seat int;
alter table stuinfo drop index seat;

### 添加外键
alter table stuinfo drop foreign key fk_stuinfo_major;

查看表中的索引

show index from table_name;