mysql三-3:完整性约束

一、完整性约束介绍

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

  作用:用于保证数据的完整性和一致性
主要分为:

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'

  age int unsigned NOT NULL default 20 必须为正值(无符号) 不允许为空 默认是20

  3. 是否是key

    主键 primary key

    外键 foreign key

    索引 (index,unique...)

二、约束条件not null与default

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

默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
  create table tb1(
    nid int not null defalut 2,
    num int not null
  )

==================not null====================
mysql> create table t1(id int); #id字段默认可以插入空
mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
mysql> insert into t1 values(); #可以插入空


mysql> create table t2(id int not null); #设置字段id不为空
mysql> desc t2;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
mysql> insert into t2 values(); #不能插入空
ERROR 1364 (HY000): Field 'id' doesn't have a default value



==================default====================
#设置id字段有默认值后,则无论id字段是null还是not null,都可以插入空,插入空默认填入default指定的默认值
mysql> create table t3(id int default 1);
mysql> alter table t3 modify id int not null default 1;
not null和default用法介绍
mysql> create table t15( 
    -> id int(11)  unsigned zerofill   # id为字段,必须为字段指定数据类型(int);在数据条件下添加约束条件:unsigned限制id只 能传入无符号
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc t15
    -> ;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type                      | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id    | int(11) unsigned zerofill | YES  |     | NULL    |       |
+-------+---------------------------+------+-----+---------+-------+
1 row in set (0.00 sec)


# not null 不能为空,default设置默认值
mysql> create table t16(
    -> id int,
    -> name char(6),
    -> sex enum('male', 'female') not null default 'male'   # 不能为空,传空就用默认值'male'
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc t16;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | char(6)               | YES  |     | NULL    |       |
| sex   | enum('male','female') | NO   |     | male    |       |
+-------+-----------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> insert into t16(id,name) values(1,'egon');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t16;
+------+--------+------+
| id   | name   | sex  |
+------+--------+------+
|    1 | egon   | male |
+------+--------+------+
1 row in set (0.00 sec)
操作验证,not null和default配合使用
三、约束条件unique key

  unique key指的就是限制字段传入的值是唯一的,不能重复的。

# 创建部门表,且部门名称不设置unique,因此可以创建多个同名部门名称,这明显不合理
mysql> create table department(
    -> id int,
    -> name char(10)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc department;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  |     | NULL    |       |
| name  | char(10) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into department values
    -> (1,'IT'),
    -> (2,'IT');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from department;
+------+------------+
| id   | name       |
+------+------------+
|    1 | IT         |
|    2 | IT         |
+------+------------+
2 rows in set (0.00 sec)

# 设置唯一约束unique
# 方式一:
mysql> drop table department;
Query OK, 0 rows affected (0.01 sec)

mysql> create table department(
    -> id int,
    -> name char(10) unique
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc department;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  |     | NULL    |       |
| name  | char(10) | YES  | UNI | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into department values
    -> (1, 'IT'),
    -> (2, 'IT');
ERROR 1062 (23000): Duplicate entry 'IT        ' for key 'name'

# 方式二:
mysql> drop table department;
Query OK, 0 rows affected (0.01 sec)

mysql> create table department(
    -> id int,
    -> name char(10),
    -> unique(id),
    -> unique(name)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc department;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  | UNI | NULL    |       |
| name  | char(10) | YES  | UNI | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into department values
    -> (1,'IT'),
    -> (2,'IT');
ERROR 1062 (23000): Duplicate entry 'IT        ' for key 'name'
mysql> insert into department values (1,'IT'), (2,'Sale');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> selcet * from department;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'selcet * from department' at line 1
mysql> select * from department;  
+------+------------+
| id   | name       |
+------+------------+
|    1 | IT         |
|    2 | Sale       |
+------+------------+
2 rows in set (0.00 sec)
单列唯一设置的两种方法
mysql> create table services(
    -> id int,
    -> ip char(15),
    -> port int,
    -> unique(id),    # 单列唯一
    -> unique(ip,port)    # 联合唯一:ip+port唯一 
    -> );
Query OK, 0 rows affected (0.01 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    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into services values
    -> (1,'192.168.1.101',80),
    -> (2,'192.168.1.101',88),
    -> (3,'192.168.1.103',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.1.101   |   80 |
|    2 | 192.168.1.101   |   88 |
|    3 | 192.168.1.103   |   80 |
+------+-----------------+------+
3 rows in set (0.00 sec)

mysql> insert into services values (4,'192.168.1.101',80);
ERROR 1062 (23000): Duplicate entry '192.168.1.101  -80' for key 'ip'
联合唯一
四、约束条件primary key
五、约束条件auto_increment
六、约束条件foreign key

猜你喜欢

转载自www.cnblogs.com/xiugeng/p/9022545.html