Mysql数据库约束

数据库约束

主键约束 primary
唯一约束 unique
非空约束  not null
外键约束 Foreign Key

主键约束

在MySQL的一个表中只有唯一的一个主键,不能有多列主键,但可以有复合主键。

一个表中可以:单列做主键、多列做主键(复合主键)

单列主键

# 创建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)

复合主键

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);

唯一约束

单列唯一

#第一种创建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');

联合唯一:

# 创建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       |             |
+-------+----------+------+-----+---------+-------+
3 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 |
+------+---------------+------+
3 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'

非空约束 

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

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

create table tb1(
    nid int not null defalut 2,
    num int not null

);

验证

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    |       |
+-------+---------+------+-----+---------+-------+
1 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 |
+------+
1 row in set (0.00 sec)

设置not null值不能为空

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    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)

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

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

# 第一种情况
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指定的默认值

 外键约束

一 快速理解foreign key

之前创建表的时候都是在一张表中添加记录,比如如下表:

公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费。

这个时候,

解决方法:

我们完全可以定义一个部门表

然后让员工信息表关联该表,如何关联,即foreign key

我们可以将上表改为如下结构:

此时有两张表,一张是employee表,简称emp表(关联表,也就从表)。一张是department表,简称dep表(被关联表,也叫主表)

创建两张表操作:

#1.创建表时先创建被关联表,再创建关联表
# 先创建被关联表(dep表)
create table dep(
    id int primary key,
    department varchar(20) not null
);

#再创建关联表(emp表)
create table emp(
    id int primary key,
    name varchar(20) not null,
    age int not null,
    dep_id int,
    constraint fk_dep foreign key(dep_id) references dep(id) 
);

#2.插入记录时,先往被关联表中插入记录,再往关联表中插入记录
insert into dep values(1,'IT'),(2,'财务部门'),(3,'销售部门'); 
insert into emp values(1,'张三',18,1),(2,'李四',20,2),(3,'王五',22,1),(4,'李杨',22,3),(5,'吴昊',23,1);

#按道理来说,删除了部门表中的某个部门,员工表的有关联的记录相继删除
mysql> delete from dep where id=3;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`db01`.`emp`, CONSTRAINT `fk_dep` FOREIGN KEY (`dep_id`) REFERENCES `dep` (`id`))

#但是先删除员工表的记录之后,再删除当前部门就没有任何问题
mysql> delete from emp where dep_id=3;
Query OK, 1 row affected (0.00 sec)

mysql> select * from emp;
+----+--------+-----+--------+
| id | name   | age | dep_id |
+----+--------+-----+--------+
|  1 | 张三   |  18 |      1 |
|  2 | 李四   |  20 |      2 |
|  3 | 王五   |  22 |      1 |
|  5 | 吴昊   |  23 |      1 |
+----+--------+-----+--------+
4 rows in set (0.00 sec)

mysql> delete from dep where id=3;
Query OK, 1 row affected (0.00 sec)

mysql> select * from dep;
+----+--------------+
| id | department   |
+----+--------------+
|  1 | IT           |
|  2 | 财务部门     |
+----+--------------+
2 rows in set (0.00 sec)

mysql> 

上面的删除表记录的操作比较繁琐,按道理讲,裁掉一个部门,该部门的员工也会被裁掉。其实呢,在建表的时候还有个很重要的内容,叫同步删除,同步更新

接下来将刚建好的两张表全部删除,先删除关联表(emp),再删除被关联表(dep)

接下来:
重复上面的操作建表
注意:在关联表中加入
on delete cascade #同步删除
on update cascade #同步更新

create table emp(
    id int primary key,
    name varchar(20) not null,
    age int not null,
    dep_id int,
    constraint fk_dep foreign key(dep_id) references dep(id) 
    on delete cascade #同步删除
    on update cascade #同步更新
);

接下来的操作,就复合我们正常的生活中的情况了。

 

posted @ 2019-09-01 13:50  杨灏  阅读(339)  评论(0)    收藏  举报