由于sql语句不区分大小写,为了书写方便,本文所有命令统一使用小写

 往期内容

  • day 01 初识Mysql以及DDL数据定义语言 
  • day 02 DML数据操作语言​​​​​​
  • day 03 DQL数据查询语言---初窥门径
  • day 04 DQL数据查询语言---单表查询
  • day 05 DQL数据查询语言---连接查询

目录

一、简介

二、非空约束:not null

三、唯一性约束:unique

四、主键约束:primary key

五、外键约束:foreign key


一、简介

约束就是在创建表的时候,给表中的字段加上一些约束,来保证这个表中数据的完整性、有效性!!!

约束的作用就是为了保证:表中的数据有效!!

 约束有哪些?

  • 非空约束:not null
  • 唯一性约束: unique
  • 主键约束: primary key
  • 外键约束:foreign key

约束的用法一般都是直接写在字段的数据类型之后

二、非空约束:not null

非空约束not null约束的字段不能为NULL

可以在多个字段上使用,但不能多个字段联合起来使用!!!

即只有列级约束,没有表级约束!!!

#name字段非空约束mysql> create table t_students(-> id int,-> name varchar(32) not null);
Query OK, 0 rows affected (0.10 sec)mysql> insert into t_students(id,name) values(1,'张三');
Query OK, 1 row affected (0.01 sec)mysql> insert into t_students(id) values(2);
ERROR 1364 (HY000): Field 'name' doesn't have a default value

  • 增加非空约束:alter table 表名 add 字段名 数据类型 not null;  
  • 修改非空约束:alter table 表名 modify 字段名 数据类型 新约束; 

#新约束为null时,为取消约束

三、唯一性约束:unique 

not null 与 unique  联合使用

案例:建立一个表,使id不能重复且不能为null

mysql> create table t_students(-> id int not null unique,-> name varchar(32));
Query OK, 0 rows affected (0.06 sec)mysql> desc t_students;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int         | NO   | PRI | NULL    |       |
| name  | varchar(32) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.04 sec)mysql> insert into t_students(id,name) values(1,'张三');
Query OK, 1 row affected (0.04 sec)mysql> insert into t_students(id,name) values(1,'李四');
ERROR 1062 (23000): Duplicate entry '1' for key 't_students.id'mysql> insert into t_students(name) values('李四');
ERROR 1364 (HY000): Field 'id' doesn't have a default value

not null 与 unique 在联合使用时,书写顺序没有限制。

not null 与 unique 在联合使用时,该字段自动变成主键!!!

  • 修改唯一约束:alter table 表名 modify 字段名 数据类型 unique;   #列级约束
  • 添加唯一约束:alter table 表名 add unique(字段名1,.....);   #表级约束
  • 删除约束:alter table 表名 drop index 字段名;

四、主键约束:primary key 

  • 主键约束:就是一种约束。
  • 主键字段:添加了主键约束的字段
  • 主键值:主键字段中的每一个值都叫做主键值,主键值是每一行记录的唯一标识。

主键约束的特征:

  • 主键值不能重复也不能为NULL!!!
  • 一张表,主键约束只能添加1个!!!
  • 主键值一般都是数字,一般都是定长的!!!

案例:建立一个表,使 id 为主键

mysql> create table t_students(-> id int primary key,-> name varchar(32));
Query OK, 0 rows affected (0.08 sec)mysql> insert into t_students(id,name) values(1,'张三');
Query OK, 1 row affected (0.01 sec)mysql> insert into t_students(id,name) values(1,'李四');
ERROR 1062 (23000): Duplicate entry '1' for key 't_students.PRIMARY'mysql> insert into t_students(name) values('李四');
ERROR 1364 (HY000): Field 'id' doesn't have a default value

复合主键:多个字段联合起来作为主键,称为复合主键。

案例:建立一个表,使 id 和 name 联合起来作为复合主键

mysql> create table t_students(-> id int,-> name varchar(32),-> primary key(id,name));
Query OK, 0 rows affected (0.07 sec)mysql> insert into t_students(id,name) values(1,'张三');
Query OK, 1 row affected (0.01 sec)mysql> insert into t_students(id,name) values(2,'张三');
Query OK, 1 row affected (0.04 sec)mysql> insert into t_students(id,name) values(1,'张三');
ERROR 1062 (23000): Duplicate entry '1-张三' for key 't_students.PRIMARY'mysql> insert into t_students(name) values('张三');
ERROR 1364 (HY000): Field 'id' doesn't have a default value

自增长:auto_increment

auto_increment表示自增,从1开始,以1递增;自增长字段必须是整型数据!!

mysql> create table t_students(-> id int primary key auto_increment,-> name varchar(32));
Query OK, 0 rows affected (0.13 sec)mysql> insert into t_students(name) values('张三'),('李四'),('王五');
Query OK, 3 rows affected (0.05 sec)
Records: 3  Duplicates: 0  Warnings: 0mysql> select * from t_students;
+----+------+
| id | name |
+----+------+
|  1 | 张三 |
|  2 | 李四 |
|  3 | 王五 |
+----+------+
3 rows in set (0.00 sec)

删除主键:alter table 表名 drop primary key;

修改主键:alter table 表名 modify 字段名 数据类型 primary key;

五、外键约束:foreign key 

语法:constraint 外键名 foreign key(外键字段) references 父表名(引用字段)

位置:与创建复合主键的语句的位置相同。

  • 外键约束:一种约束
  • 外键字段:该字段上添加了外键约束
  • 外键值:外键字段当中的每一个值,可以为NULL。

foreign key 被约束的表叫做子表,外键引用主键字段所在的表叫做父表。

外键值中的数据一定是父表中出现的数据!!

子表中的外键引用的这个字段不一定是主键,但至少具有unique约束!!

注(添加外键约束后)

  • 创建表的时候,先创建父表,再创建子表
  • 添加数据的时候,先添加父表数据,再添加子表数据
  • 修改数据的时候,先修改子表的数据,再修改父表的数据
  • 删除数据的时候,先删除子表的数据,再删除父表的数据

案例:父表为t_f,子表为t_z,父表的为教师表,子表为学生表,外键为教师id,外键名为fz

mysql> create table t_f(-> fid int unique,-> fname varchar(32));
Query OK, 0 rows affected (0.11 sec)mysql> create table t_z(-> zid int,-> zname varchar(32),-> fid int,-> constraint fz foreign key(fid) references t_f(fid));
Query OK, 0 rows affected (0.14 sec)mysql> desc t_f;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| fid   | int         | YES  | UNI | NULL    |       |
| fname | varchar(32) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.04 sec) mysql> desc t_z;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| zid   | int         | YES  |     | NULL    |       |
| zname | varchar(32) | YES  |     | NULL    |       |
| fid   | int         | YES  | MUL | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

尝试插入数据

由于父表中没有fid=3,所以会报错!!

mysql> insert into t_f(fid,fname) values(1,'张三'),(2,'李四');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0mysql> insert into t_z(zid,zname,fid) values(10,'李华',1);
Query OK, 1 row affected (0.00 sec)mysql> insert into t_z(zid,zname,fid) values(10,'李华',3);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`studydata`.`t_z`, CONSTRAINT `fz` FOREIGN KEY (`fid`) REFERENCES `t_f` (`fid`))

建表完成后添加外键

alter table  数据表名 add constraint 外键名 foreign key(外键字段名)  references 父表名 (引用字段); 


删除外键

alter table 表名 drop foreign key 外键名;

 未完待续。。。。。。。。。

day 06 非空约束、唯一约束、主键约束、外键约束相关推荐

  1. MySQL设置主键、联合主键、外键、唯一约束、非空约束、默认约束

    MySQL登录.查看记录等基本操作 MySQL设置表的属性值自动增加 auto_increment 1.主键.联合主键.外键 1.什么是主键 主键是在多条记录中用于确定一条记录时使用的标识符.主键具备 ...

  2. 约束,MySQL约束,非空默认值,主键外键唯一自增,完整详细可收藏

    文章目录 1. 约束(constraint)概述 2. 非空约束 3. 唯一性约束 4. PRIMARY KEY 约束 5. 自增列:AUTO_INCREMENT 6. FOREIGN KEY 约束 ...

  3. SQL数据库语言基础之SqlServer数据表的六大约束(主键、外键、检查、非空、唯一性、默认值约束)的创建

    文章目录 一.主键约束(primary key) 二.外键约束(foreign key) 三.检查约束(check) 四.非空约束(not null) 五.唯一性约束(unique) 六.默认值约束( ...

  4. mysql 未能启用约束_未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值。...

    来源:http://www.cnblogs.com/JuneZhang/archive/2013/01/10/2853981.html 今天运行项目,提示"未能启用约束.一行或多行中包含违反 ...

  5. 关于未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值的解决方法...

    今天在编写C#程序时,VS提示了"未能启用约束.一行或多行中包含违反非空.唯一或外键约束的值."异常,没有慌乱,因为前几天遇到过这个异常,现在只有悔恨,为什么当时没有写一下博客记录 ...

  6. 未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值。

    来源:http://www.cnblogs.com/JuneZhang/archive/2013/01/10/2853981.html 今天运行项目,提示"未能启用约束.一行或多行中包含违反 ...

  7. SQL的主键和外键约束

    SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...

  8. SQL Server的主键与外键约束

    SQL Server的主键与外键约束SQL Server有许多的重要知识,开始也说过许多的SQL知识了.SQL Server中的约束也是一个重要性的知识,下面我来说说关于SQL Server约束的知识 ...

  9. 数据库元数据数据字典查询_5_列出给定表的主键、外键约束

    列出给定表的主键.外键约束 需求描述 需求:查询出给定的表emp的外键约束信息. 解决方法:通过各个数据库里提供的与外键约束相关的数据字典进行查询. 注: 数据库数据集SQL脚本详见如下链接地址 员工 ...

最新文章

  1. [转]python的requests发送/上传多个文件
  2. Spring MVC 3 深入总结
  3. [基础算法]通过后缀表达式实现表达式的计算
  4. 打开.net web项目,出现Unable to get the project ile from the web server错误
  5. 使用python模拟实现PID控制算法
  6. python中文字体奇怪_“matplotlib”中对字体设置的奇怪响应
  7. Java异常 #Class path contains multiple SLF4J bindings.警告解决
  8. 《痞子衡嵌入式半月刊》 第 48 期
  9. 调用泛微OA接口的一些基本常识
  10. UVa 10934 Dropping water balloons:dp(递推)
  11. 火星转债上市价格预测
  12. TDengine 单节点Cluster not ready( 群集未就绪) 异常问题分析及解决方案
  13. ch340 win7 64位驱动下载
  14. 彩虹显IP 后两位星号 解决方案 全显IP
  15. css 利用动画实现旋转卡片
  16. 地下城与勇士(DNF)诺斯玛尔副本(堕落的盗贼、“迷乱之村”哈穆林、血蝴蝶之舞、疑惑之村、痛苦之村列瑟芬)(童年的回忆)
  17. 中软国际携手深开鸿发布(1+1) x N 战略,以数字化、智慧化改变人类生产和生活方式
  18. 深圳市彬戈科技有限公司告诉你,什么是青蛙刷脸
  19. HTML--超链接--a标签-跳转
  20. oracle显示员工的名字和姓氏,如何在sql中以a和b开头显示员工姓名

热门文章

  1. IntelliJ IDEA创建Servlet最新方法 Idea版本2021以及IntelliJ IDEA创建Servlet 404问题,找不到对应的路径。
  2. HINSTANCE初了解
  3. C语言——从文本文件中读入及向文本文件中写入内容
  4. Newstart HA常见使用场景
  5. PIV(粒子成像测速)
  6. openfass学习及使用
  7. 各种进制转换通用代码
  8. [人脸活体检测] 人脸活体检测简介
  9. 双11投影仪推荐,什么样的投影仪才是年轻人最爱的?
  10. 【mySQL】mySQL动态语句(SQL语句中有变量)