深圳市网站建设科技,织带东莞网站建设技术支持,常州专业网站建设费用,室内设计联盟官方网站入口目录 前言
1.什么是约束
2.空属性
3.默认值
4.列描述
5.zerofill
6.主键
7.自增长
8.唯一键
9.外键
总结 前言 hello#xff0c;各位小伙伴大家好#xff0c;本章内容为大家介绍关于MySQL约束的相关内容#xff0c;关于约束这个概念#xff0c;如果是第一次接触可…目录 前言
1.什么是约束
2.空属性
3.默认值
4.列描述
5.zerofill
6.主键
7.自增长
8.唯一键
9.外键
总结 前言 hello各位小伙伴大家好本章内容为大家介绍关于MySQL约束的相关内容关于约束这个概念如果是第一次接触可能会有一点难以理解不过也不用担心相信看完这篇文章对约束这个概念会有一个清晰的认识。 1.什么是约束 在谈MySQL表的约束之前先举一个生活中的例子我们在学校中读书会受到学校中各种各样规矩的约束本质上在学校制定许多的规矩是为了更好的管理学生而在MySQL中也有许多的规矩这些规矩是为了规范程序员在向数据库插入数据的时候正确操作体现为对程序员的一种约束下面我们就具体来看看建表的时候都有哪些约束。
2.空属性
两个值null(默认的) 和 not null(不为空)
数据库默认字段基本都是为空但是在实际开发中尽可能保证字段不为空因为数据为空没办法参与运算
mysql select null;
------
| NULL |
------
| NULL |
------
1 row in set (0.00 sec)
mysql select 1null;
--------
| 1null |
--------
| NULL |
--------
1 row in set (0.00 sec)
案例 创建一个班级表包含班级名和班级所在的教室。 站在正常的业务逻辑中 如果班级没有名字你不知道你在哪个班级 如果教室名字可以为空就不知道在哪上课 所以我们在设计数据库表的时候一定要在表中进行限制班级名和教室名都不能为空这就是“约束”。
//创建表
mysql create table myclass( class_name varchar(20) not null, class_room varchar(20) not null);
Query OK, 0 rows affected (0.03 sec)//表结构
mysql desc myclass;
----------------------------------------------------
| Field | Type | Null | Key | Default | Extra |
----------------------------------------------------
| class_name | varchar(20) | NO | | NULL | |
| class_room | varchar(20) | NO | | NULL | |
----------------------------------------------------
2 rows in set (0.00 sec)//正常插入数据
mysql insert into myclass values(通信211,4406);
Query OK, 1 row affected (0.00 sec)
//给非空属性字段不插入值报错
mysql insert into myclass (class_name) values(通信211);
ERROR 1364 (HY000): Field class_room doesnt have a default value总结在创建属性字段的时候默认情况是null可以为空指定为not null在插入数据的时候不能为空必须有值而必须有值则体现为MySQL建表的一种约束
3.默认值
默认值某一种数据会经常性的出现某个具体的值可以一开始就指定好在需要真实数据的时候用户可以选择性的使用默认值。
//创建表
mysql create table if not exists stu(- name varchar(12) not null,- age tinyint unsigned default 18,- sex char(2) default 男- );
Query OK, 0 rows affected (0.03 sec)//表结构
mysql desc stu;
-------------------------------------------------------
| Field | Type | Null | Key | Default | Extra |
-------------------------------------------------------
| name | varchar(12) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | 18 | |
| sex | char(2) | YES | | 男 | |
-------------------------------------------------------
3 rows in set (0.00 sec)//全插入
mysql insert into stu (name,age,sex) values (张三,20,男);
Query OK, 1 row affected (0.00 sec)//按照默认值进行插入
mysql insert into stu (name) values (李四);
Query OK, 1 row affected (0.01 sec)//插入后的数据
mysql select* from stu;
--------------------
| name | age | sex |
--------------------
| 张三 | 20 | 男 |
| 李四 | 18 | 男 |
--------------------
2 rows in set (0.00 sec)设置了default的列在插入数据的时候可以对列进行省略。
4.列描述
列描述comment,没有实际含义专门用来描述字段体现为一种软性约束
//创建表通过comment对每个字段进行描述
mysql create table if not exists t7(- name varchar(20) not null comment 姓名,- age int default 18 comment 年龄- );
Query OK, 0 rows affected (0.03 sec)//查看表结构
mysql show create table t7\G;
*************************** 1. row ***************************Table: t7
Create Table: CREATE TABLE t7 (name varchar(20) NOT NULL COMMENT 姓名,age int(11) DEFAULT 18 COMMENT 年龄
) ENGINEInnoDB DEFAULT CHARSETutf8
1 row in set (0.00 sec)
5.zerofill
zerofill对数据进行格式化显示如果宽度小于设定的宽度自动填充0
mysql create table if not exists t8(- id int zerofill,- name varchar(20) not null- );
Query OK, 0 rows affected (0.04 sec)//插入数据
mysql insert into t8 values (1,张三);
Query OK, 1 row affected (0.00 sec)mysql insert into t8 values (2,李四);
Query OK, 1 row affected (0.01 sec)//插入数据的时候宽度小于设定的宽度用0填充。
mysql select* from t8;
--------------------
| id | name |
--------------------
| 0000000001 | 张三 |
| 0000000002 | 李四 |
--------------------
2 rows in set (0.00 sec)//int类型数据宽度为10位
mysql show create table t8\G;
*************************** 1. row ***************************Table: t8
Create Table: CREATE TABLE t8 (id int(10) unsigned zerofill DEFAULT NULL,name varchar(20) NOT NULL
) ENGINEInnoDB DEFAULT CHARSETutf8
1 row in set (0.00 sec)注自动填充0这是最后显示的结果在MySQL中实际存储的还是1和2。
作用在一些对数据格式有要求的地方使用使得数据更加规范。
6.主键
主键primary key用来唯一约束该字段里面的数据不能重复不能为空一张表最多只能有一个主键主键所在的列通常是整数类型
案例
给学生的id添加主键用id唯一标识一个学生
//创建表指定id为主键
mysql create table if not exists student(- id int primary key comment 学号,- name varchar(20) not null,- age tinyint default 18- );
Query OK, 0 rows affected (0.04 sec)//插入数据
mysql insert into student values(1,张三,20);
Query OK, 1 row affected (0.01 sec)mysql insert into student values(2,张三,20);
Query OK, 1 row affected (0.00 sec)//当id值出现重复或者为空时不能向表中插入数据
mysql insert into student values(2,张三,20);
ERROR 1062 (23000): Duplicate entry 2 for key PRIMARY
mysql insert into student values(1,张三,20);
ERROR 1062 (23000): Duplicate entry 1 for key PRIMARY
mysql insert into student (name,age)values(张三,20);
ERROR 1364 (HY000): Field id doesnt have a default value当表创建好以后但是没有主键的时候可以再次追加主键
alter table 表名 add primary key(字段列表);
删除主键
alter table 表名 drop primary key;
复合主键
在创建表的时候有多个属性字段作为主键这里的多个属性字段作为主键不是说一张表中有多个主键而是多个主键共同约束作为一个主键
案例
创建一张表该表中有多个属性字段作为主键
mysql create table if not exists t9(- id int unsigned,- course char(10) comment 课程代码,- score tinyint default 60 comment 成绩,- primary key(id,score) --id和score共同作为复合主键- );
Query OK, 0 rows affected (0.03 sec)mysql desc t9;
-----------------------------------------------------
| Field | Type | Null | Key | Default | Extra |
-----------------------------------------------------
| id | int(10) unsigned | NO | PRI | NULL | |
| course | char(10) | YES | | NULL | |
| score | tinyint(4) | NO | PRI | 60 | |
-----------------------------------------------------
3 rows in set (0.00 sec)mysql insert into t9 values(1,数学,65);
Query OK, 1 row affected (0.00 sec)mysql insert into t9 values(2,数学,65);
Query OK, 1 row affected (0.00 sec)mysql insert into t9 values(2,数学,65);
ERROR 1062 (23000): Duplicate entry 2-65 for key PRIMARY --主键冲突
7.自增长
auto_increment:当对应的字段不给值会自动的被系统触发系统从当前字段中已经有的最大值1操作得到一个新的不同的值。通常和主键搭配使用作为逻辑主键
自增长的特点
任何一个字段要做自增长前提本身是一个索引
自增长字段必须是整数
一张表最多只能有一个自增长
案例
mysql system clear;
mysql create table if not exists t10(- id int unsigned primary key auto_increment,- name varchar(20) not null- );
Query OK, 0 rows affected (0.03 sec)mysql insert into t10 (name) values(张三);
Query OK, 1 row affected (0.01 sec)mysql insert into t10 (name) values(李四);
Query OK, 1 row affected (0.01 sec)//默认从1开始自增长
mysql select* from t10;
------------
| id | name |
------------
| 1 | 张三 |
| 2 | 李四 |
------------
2 rows in set (0.00 sec)自增长实现的原理 获取上次插入的auto_increment的值
mysql select last_insert_id();
------------------
| last_insert_id() |
------------------
| 2 |
------------------
1 row in set (0.00 sec)8.唯一键
一张表中往往有很多字段需要唯一性数据不能重复但是一张表中只能有一个主键而唯一键的存在可以解决表中有很多字段需要唯一性约束的问题。
唯一键和主键不同在于唯一键允许为空而主键不允许为空。另外主键更多的是标识唯一性的而唯一键更多是保证在业务上不要和别的信息产生冲突。
mysql create table if not exists t11( - id tinyint primary key,- name varchar(20) not null,- class_name varchar(20) unique comment 班级名不能为空- );
Query OK, 0 rows affected (0.03 sec)
//唯一键约束不能重复
mysql insert into t11 values (1,zhangsan,A);
Query OK, 1 row affected (0.00 sec)mysql insert into t11 values (2,zhangsan,A);
ERROR 1062 (23000): Duplicate entry A for key class_name
//可以为空
mysql insert into t11 (id,name) values (2,zhangsan);
Query OK, 1 row affected (0.00 sec)mysql select* from t11;
--------------------------
| id | name | class_name |
--------------------------
| 1 | zhangsan | A |
| 2 | zhangsan | NULL |
--------------------------
2 rows in set (0.00 sec)9.外键
MySQL是关系型数据库一般表跟表之间是有关联关系的举个简单的例子有两张表一张表是班级表另一张是学生表因为学生是属于班级的所以一般将班级表称为是主表而学生表称为是从表。
外键就是用于定义主表和从表之间的关系外键约束主要定义在从表上主表则必须是主键约束或者是唯一键约束。当定义外键之后要求外键列数据必须在主表的主键列存在或为null。
语法
foreign key 字段名 references 主表(列)
案例 a.创建主表
mysql create table if not exists myclass(- id int primary key,- name varchar(20) not null comment 班级名- );
Query OK, 0 rows affected (0.03 sec)b.创建从表
mysql create table if not exists stu(- id int primary key,- name varchar(20) not null comment 学生名,- class_id int,- foreign key (class_id) references myclass(id)- );
Query OK, 0 rows affected (0.06 sec)c.正常插入数据
mysql insert into myclass values (10,C大牛班),(20,java大神班);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0mysql insert into stu values (100,张三,10);
Query OK, 1 row affected (0.01 sec)mysql insert into stu values (200,李四,20);
Query OK, 1 row affected (0.01 sec)mysql insert into stu values (300,王五,null);
Query OK, 1 row affected (0.01 sec)d.插入一个班级号为30的同学因为没有这个班级所以插入不成功
mysql insert into stu values (400,赵六,30);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (test.stu, CONSTRAINT stu_ibfk_1 FOREIGN KEY (class_id) REFERENCES myclass (id))e.数据显示
mysql select* from stu;
-----------------------
| id | name | class_id |
-----------------------
| 100 | 张三 | 10 |
| 200 | 李四 | 20 |
| 300 | 王五 | NULL |
-----------------------
3 rows in set (0.00 sec)mysql select* from myclass;
-------------------
| id | name |
-------------------
| 10 | C大牛班 |
| 20 | java大神班 |
-------------------
2 rows in set (0.00 sec)总结
本篇文章为大家介绍了MySQL中约束相关的话题总结为一句话约束是在建表的时候进行的目的是为了防止在后续数据插入的时候出现不合法的数据实现这个目的方法就是上面介绍的这些规则相信掌握了这些规则之后对数据库的操作又有了一个不小的提升。 我们下次再见。