码迷,mamicode.com
首页 > 其他好文 > 详细

完整性约束

时间:2018-10-15 23:09:16      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:error   化学   key   完整性   duplicate   才有   services   onclick   unsigned   

1.介绍:

约束条件与数据类型的宽度一样,都是可选参数

作用:用于保证数据的完整性和一致性

主要分为:

PRIMARY KEY (PK)    #标识该字段为该表的主键,可以唯一的标识记录
FOREIGN KEY (FK)    #标识该字段为该表的外键
NOT NULL    #标识该字段不能为空
UNIQUE KEY (UK)    #标识该字段的值是唯一的
AUTO_INCREMENT    #标识该字段的值自动增长(整数类型,而且为主键)
DEFAULT    #为该字段设置默认值

UNSIGNED #无符号
ZEROFILL #使用0填充

 说明:

#1. 是否允许为空,默认NULL,可设置NOT NULL,字段不允许为空,必须赋值
#2. 字段是否有默认值,缺省的默认值是NULL,如果插入记录时不给字段赋值,此字段使用默认值
sex enum(male,female) not null default male

#必须为正值(无符号) 不允许为空 默认是20
age int unsigned NOT NULL default 20 
# 3. 是否是key
主键 primary key
外键 foreign key
索引 (index,unique...)

 2.not null 和default

 是否可空,null表示空,非字符串

not null - 不可空
null - 可空

默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值

 验证1

技术分享图片
mysql> create table t11(id int);# id字段默认可以为空
Query OK, 0 rows affected (0.05 sec)

mysql> desc t11;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES   |          | NULL    |       |
+-------+---------+------+-----+---------+-------+
row in set (0.03 sec)
mysql> insert into t11 values(); #给t11表插一个空的值
Query OK, 1 row affected (0.00 sec)

#查询结果如下
mysql> select * from t11;
+------+
| id   |
+------+
| NULL |
+------+
row in set (0.00 sec)

默认值可以为空
View Code

 

验证2 

技术分享图片
mysql> create table t12(id int not null);#设置字段id不为空
Query OK, 0 rows affected (0.03 sec)

mysql> desc t12;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
row in set (0.01 sec)

mysql> insert into t12 values();#不能插入空
ERROR 1364 (HY000): Field id doesnt have a default value

设置not null,插入值时不能为空
View Code

 

 验证3

技术分享图片
# 第一种情况
mysql> create table t13(id int default 1);
Query OK, 0 rows affected (0.03 sec)

mysql> desc t13;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |           | 1              |              |
+-------+---------+------+-----+---------+-------+
row in set (0.01 sec)

mysql> insert into t13 values();
Query OK, 1 row affected (0.00 sec)

mysql> select * from t13;
+------+
| id   |
+------+
|    1  |
+------+
row in set (0.00 sec)


# 第二种情况
mysql> create table t14(id int not null default 2);
Query OK, 0 rows affected (0.02 sec)

mysql> desc t14;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO      |         | 2               |             |
+-------+---------+------+-----+---------+-------+
row in set (0.01 sec)

mysql> select * from t14;
+----+
| id |
+----+
|  2 |
+----+
row in set (0.00 sec)

设置id字段有默认值后,则无论id字段是null还是not null,都可以插入空,插入空默认填入default指定的默认值
View Code

 

练习:创建学生表student2,设置每个字段的约束条件

技术分享图片
mysql> create table student2(
    -> id int not null,
    -> name varchar(50) not null,
    -> age int(3) unsigned not null default 18,
    -> sex enum(male,female) default male,
    -> fav set(smoke,drink,tangtou) default drink,tangtou
    -> );
Query OK, 0 rows affected (0.01 sec)

# 只插入了not null约束条件的字段对应的值
mysql> insert into student2(id,name) values(1,mjj);
Query OK, 1 row affected (0.00 sec)

# 查询结果如下
mysql> select * from student2;
+----+------+-----+------+---------------+
| id | name | age | sex  | fav           |
+----+------+-----+------+---------------+
|  1 | mjj  |  18 | male | drink,tangtou |
+----+------+-----+------+---------------+
row in set (0.00 sec)
View Code

 

 3.unique:单列唯一 

技术分享图片
mysql> create table student2(
    -> id int not null,
    -> name varchar(50) not null,
    -> age int(3) unsigned not null default 18,
    -> sex enum(male,female) default male,
    -> fav set(smoke,drink,tangtou) default drink,tangtou
    -> );
Query OK, 0 rows affected (0.01 sec)

# 只插入了not null约束条件的字段对应的值
mysql> insert into student2(id,name) values(1,mjj);
Query OK, 1 row affected (0.00 sec)

# 查询结果如下
mysql> select * from student2;
+----+------+-----+------+---------------+
| id | name | age | sex  | fav           |
+----+------+-----+------+---------------+
|  1 | mjj  |  18 | male | drink,tangtou |
+----+------+-----+------+---------------+
row in set (0.00 sec)


# 发现: 同时插入两个IT部门也是可以的,但这是不合理的,所以我们要设置name字段为unique 解决这种不合理的现象。

验证之前重复插入记录的操作是可行的,但是不符合场景
View Code

 

 接下来,使用约束条件unique,来对公司部门的字段进行设置

技术分享图片
#第一种创建unique的方式
#例子1:
create table department(
    id int,
    name char(10) unique
);
mysql> insert into department values(1,it),(2,it);
ERROR 1062 (23000): Duplicate entry it for key name

#例子2:
create table department(
    id int unique,
    name char(10) unique
);
insert into department values(1,it),(2,sale);

#第二种创建unique的方式
create table department(
    id int,
    name char(10) ,
    unique(id),
    unique(name)
);
insert into department values(1,it),(2,sale);
View Code

 

 联合唯一

技术分享图片
# 创建services表
mysql> create table services(
    -> id int,
    -> ip char(15),
    -> port int,
    -> unique(id),
    -> unique(ip,port)
    -> );
Query OK, 0 rows affected (0.05 sec)

mysql> desc services;
+-------+----------+------+-----+---------+-------+
| Field | Type      | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id        | int(11)   | YES   | UNI  | NULL       |             |
| ip        | char(15) | YES   | MUL  | NULL       |             |
| port    | int(11) | YES   |          | NULL       |             |
+-------+----------+------+-----+---------+-------+
rows in set (0.01 sec)

#联合唯一,只要两列记录,有一列不同,既符合联合唯一的约束
mysql> insert into services values
    -> (1,192,168,11,23,80),
    -> (2,192,168,11,23,81),
    -> (3,192,168,11,25,80);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from services;
+------+---------------+------+
| id   | ip            | port |
+------+---------------+------+
|    1 | 192,168,11,23 |   80 |
|    2 | 192,168,11,23 |   81 |
|    3 | 192,168,11,25 |   80 |
+------+---------------+------+
rows in set (0.00 sec)

mysql> insert into services values (4,192,168,11,23,80);
ERROR 1062 (23000): Duplicate entry 192,168,11,23-80 for key ip
View Code

 

 4.primary key

 一个表中可以:

单列做主键
多列做主键(复合主键)

约束:等价于 not null unique,字段的值不为空且唯一

存储引擎默认是(innodb):对于innodb存储引擎来说,一张表必须有一个主键。

单列主键

技术分享图片
# 创建t14表,为id字段设置主键,唯一的不同的记录
create table t14(
    id int primary key,
    name char(16)
);

insert into t14 values
(1,xiaoma),
(2,xiaohong);

mysql> insert into t14 values(2,wxxx);
ERROR 1062 (23000): Duplicate entry 6 for key PRIMARY


#   not null + unique的化学反应,相当于给id设置primary key
create table t15(
    id int not null unique,
    name char(16)
);
mysql> create table t15(
    -> id int not null unique,
    -> name char(16)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc t15;
+-------+----------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id        | int(11)  | NO     | PRI | NULL       |             |
| name   | char(16) | YES  |         | NULL       |             |
+-------+----------+------+-----+---------+-------+
rows in set (0.02 sec)
View Code

 

 复合主键

技术分享图片
create table t16(
    ip char(15),
    port int,
    primary key(ip,port)
);

insert into t16 values
(1.1.1.2,80),
(1.1.1.2,81);

验证复合主键的使用
View Code

 

 5.auto_increment

约束:约束的字段为自动增长,约束的字段必须同时被key约束 

技术分享图片
# 创建student
create table student(
id int primary key auto_increment,
name varchar(20),
sex enum(male,female) default male
);

mysql>  desc student;
+-------+-----------------------+------+-----+---------+----------------+
| Field | Type                  | Null | Key | Default | Extra          |
+-------+-----------------------+------+-----+---------+----------------+
| id    | int(11)               | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20)           | YES  |     | NULL    |                |
| sex   | enum(male,female) | YES  |     | male    |                |
+-------+-----------------------+------+-----+---------+----------------+
rows in set (0.17 sec)

#插入记录
mysql>  insert into student(name) values (老白),(小白);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+--------+------+
| id | name   | sex  |
+----+--------+------+
|  1 | 老白   | male |
|  2 | 小白   | male |
+----+--------+------+
rows in set (0.00 sec)

不指定id,则自动增长
View Code
技术分享图片
mysql> insert into student values(4,asb,female);
Query OK, 1 row affected (0.00 sec)

mysql> insert into student values(7,wsb,female);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+--------+--------+
| id | name   | sex    |
+----+--------+--------+
|  1 | 老白   | male   |
|  2 | 小白   | male   |
|  4 | asb    | female |
|  7 | wsb    | female |
+----+--------+--------+
rows in set (0.00 sec)

# 再次插入一条不指定id的记录,会在之前的最后一条记录继续增长
mysql>  insert into student(name) values (大白);
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+----+--------+--------+
| id | name   | sex    |
+----+--------+--------+
|  1 | 老白   | male   |
|  2 | 小白   | male   |
|  4 | asb    | female |
|  7 | wsb    | female |
|  8 | 大白   | male   |
+----+--------+--------+
rows in set (0.00 sec)

也可以指定id
View Code
技术分享图片
mysql> delete from student;
Query OK, 5 rows affected (0.00 sec)

mysql> select * from student;
Empty set (0.00 sec)

mysql> select * from student;
Empty set (0.00 sec)

mysql> insert into student(name) values(ysb);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+----+------+------+
| id | name | sex  |
+----+------+------+
|  9 | ysb  | male |
+----+------+------+
row in set (0.00 sec)

#应该用truncate清空表,比起delete一条一条地删除记录,truncate是直接清空表,在删除大表时用它
mysql> truncate student;
Query OK, 0 rows affected (0.03 sec)

mysql>  insert into student(name) values(xiaobai);
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+----+---------+------+
| id | name    | sex  |
+----+---------+------+
|  1 | xiaobai | male |
+----+---------+------+
row in set (0.00 sec)

mysql>

对于自增的字段,在用delete删除后,再插入值,该字段仍按照删除前的位置继续增长
View Code

 

 了解:

技术分享图片
查看可用的 开头auto_inc的词
mysql> show variables like auto_inc%;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
rows in set (0.02 sec)
# 步长auto_increment_increment,默认为1
# 起始的偏移量auto_increment_offset, 默认是1

 # 设置步长 为会话设置,只在本次连接中有效
 set session auto_increment_increment=5;

 #全局设置步长 都有效。
 set global auto_increment_increment=5;

 # 设置起始偏移量
 set global  auto_increment_offset=3;

#强调:If the value of auto_increment_offset is greater than that of auto_increment_increment, the value of auto_increment_offset is ignored. 
翻译:如果auto_increment_offset的值大于auto_increment_increment的值,则auto_increment_offset的值会被忽略 

# 设置完起始偏移量和步长之后,再次执行show variables likeauto_inc%;
发现跟之前一样,必须先exit,再登录才有效。

mysql> show variables likeauto_inc%;
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 5     |
| auto_increment_offset    | 3     |
+--------------------------+-------+
rows in set (0.00 sec)

#因为之前有一条记录id=1
mysql> select * from student;
+----+---------+------+
| id | name    | sex  |
+----+---------+------+
|  1 | xiaobai | male |
+----+---------+------+
row in set (0.00 sec)
# 下次插入的时候,从起始位置3开始,每次插入记录id+5
mysql> insert into student(name) values(ma1),(ma2),(ma3);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+---------+------+
| id | name    | sex  |
+----+---------+------+
|  1 | xiaobai | male |
|  3 | ma1     | male |
|  8 | ma2     | male |
| 13 | ma3     | male |
+----+---------+------+

auto_increment_increment和 auto_increment_offset
View Code

 

 清空表区分delete和truncate的区别:

delete from t1; #如果有自增id,新增的数据,仍然是以删除前的最后一样作为起始。

truncate table t1;数据量大,删除速度比上一条快,且直接从零开始。

 

完整性约束

标签:error   化学   key   完整性   duplicate   才有   services   onclick   unsigned   

原文地址:https://www.cnblogs.com/wqzn/p/9794994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!