5、數據庫之完整性約束

 1、介紹

約束條件與數據類型的寬度同樣,都是可選參數mysql

做用:用於保證數據的完整性和一致性
主要分爲:nginx

PRIMARY KEY (PK)    標識該字段爲該表的主鍵,能夠惟一的標識記錄
FOREIGN KEY (FK)    標識該字段爲該表的外鍵
NOT NULL    標識該字段不能爲空
UNIQUE KEY (UK)    標識該字段的值是惟一的
AUTO_INCREMENT    標識該字段的值自動增加(整數類型,並且爲主鍵)
DEFAULT    爲該字段設置默認值

UNSIGNED 無符號
ZEROFILL 使用0填充

說明:sql

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

2、not null 和default  

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

 

default默認值,建立列時能夠指定默認值,當插入數據時若是未主動設置,則自動添加默認值
create table tb1(
id int not null defalut  2 ,
num int not null
)ide

3、unique約束(惟一性約束)

單列惟一post

-----1.單列惟一---------
create table t2(
id int not null unique,
name char(10)
);
insert into t2 values(1,'egon');
insert into t2 values(1,'alex');
#上面建立表的時候把id設置了惟一約束。那麼在插入id=1,就會出錯了

多列惟一spa

-----2.多列惟一---------
#255.255.255.255
create table server(
id int primary key auto_increment,
name char(10),
host char(15), #主機ip
port int, #端口
constraint host_port unique(host,port) #constraint host_port這個只是用來設置惟一約束的名字的,也能夠不設置默認就有了
);
insert into server(name,host,port) values('ftp','192.168.20.11',8080);
insert into server(name,host,port) values('https','192.168.20.11',8081); #ip和端口合起來惟一
select * from server;

4、primary key (主鍵約束)

primary key字段的值不爲空且惟一code

一個表中能夠:server

單列作主鍵
多列作主鍵(複合主鍵)blog

但一個表內只能有一個主鍵primary key

 1 ============單列作主鍵===============
 2 #方法一:not null+unique
 3 create table department1(
 4 id int not null unique, #主鍵
 5 name varchar(20) not null unique,
 6 comment varchar(100)
 7 );
 8 
 9 mysql> desc department1;
10 +---------+--------------+------+-----+---------+-------+
11 | Field   | Type         | Null | Key | Default | Extra |
12 +---------+--------------+------+-----+---------+-------+
13 | id      | int(11)      | NO   | PRI | NULL    |       |
14 | name    | varchar(20)  | NO   | UNI | NULL    |       |
15 | comment | varchar(100) | YES  |     | NULL    |       |
16 +---------+--------------+------+-----+---------+-------+
17 rows in set (0.01 sec)
18 
19 #方法二:在某一個字段後用primary key
20 create table department2(
21 id int primary key, #主鍵
22 name varchar(20),
23 comment varchar(100)
24 );
25 
26 mysql> desc department2;
27 +---------+--------------+------+-----+---------+-------+
28 | Field   | Type         | Null | Key | Default | Extra |
29 +---------+--------------+------+-----+---------+-------+
30 | id      | int(11)      | NO   | PRI | NULL    |       |
31 | name    | varchar(20)  | YES  |     | NULL    |       |
32 | comment | varchar(100) | YES  |     | NULL    |       |
33 +---------+--------------+------+-----+---------+-------+
34 rows in set (0.00 sec)
35 
36 #方法三:在全部字段後單獨定義primary key
37 create table department3(
38 id int,
39 name varchar(20),
40 comment varchar(100),
41 constraint pk_name primary key(id); #建立主鍵併爲其命名pk_name
42 
43 mysql> desc department3;
44 +---------+--------------+------+-----+---------+-------+
45 | Field   | Type         | Null | Key | Default | Extra |
46 +---------+--------------+------+-----+---------+-------+
47 | id      | int(11)      | NO   | PRI | NULL    |       |
48 | name    | varchar(20)  | YES  |     | NULL    |       |
49 | comment | varchar(100) | YES  |     | NULL    |       |
50 +---------+--------------+------+-----+---------+-------+
51 rows in set (0.01 sec)
52 
53 單列主鍵
單列主鍵
 1 ==================多列作主鍵================
 2 create table service(
 3 ip varchar(15),
 4 port char(5),
 5 service_name varchar(10) not null,
 6 primary key(ip,port)
 7 );
 8 
 9 
10 mysql> desc service;
11 +--------------+-------------+------+-----+---------+-------+
12 | Field        | Type        | Null | Key | Default | Extra |
13 +--------------+-------------+------+-----+---------+-------+
14 | ip           | varchar(15) | NO   | PRI | NULL    |       |
15 | port         | char(5)     | NO   | PRI | NULL    |       |
16 | service_name | varchar(10) | NO   |     | NULL    |       |
17 +--------------+-------------+------+-----+---------+-------+
18 rows in set (0.00 sec)
19 
20 mysql> insert into service values
21     -> ('172.16.45.10','3306','mysqld'),
22     -> ('172.16.45.11','3306','mariadb')
23     -> ;
24 Query OK, 2 rows affected (0.00 sec)
25 Records: 2  Duplicates: 0  Warnings: 0
26 
27 mysql> insert into service values ('172.16.45.10','3306','nginx');
28 ERROR 1062 (23000): Duplicate entry '172.16.45.10-3306' for key 'PRIMARY'
多列主鍵

5、auto_increment (自增約束)

步長increment與起始偏移量offset:auto_increment_increment,auto_increment_offset

 

3.--------偏移量:auto_increment_offset---------
==============沒有設置偏移量的時候
create table dep(
id int primary key auto_increment,
name char(10)
);
insert into dep(name) values('IT'),('HR'),('EFO');
select * from dep;

================設置自增的時候以10開頭
create table dep1(
id int primary key auto_increment,
name char(10)
)auto_increment = 10;
insert into dep1(name) values('IT'),('HR'),('EFO');
select * from dep1;

===============auto_increment_increment:自增步長
create table dep3(
id int primary key auto_increment,
 name char(10)
);
會話:經過客戶端連到服務端(一次連接稱爲一次會話)
set session auto_increment_increment = 2; #會話級,只對當前會話有效
set global auto_increment_increment=2; #全局,對全部的會話都有效
insert into dep3(name) values('IT'),('HR'),('SALE'),('Boss');

-----------查看變量----------
show variables like '%auto_in%';#查看變量。只要包含auto_in就都查出來了

=========auto_increment_offset:偏移量+auto_increment_increment:步長===========
注意:若是auto_increment_offset的值大於auto_increment_increment的值,
則auto_increment_offset的值會被忽略

set session auto_increment_offset=2;
set session auto_increment_increment=3;
show variables like '%auto_in%';

create table dep4(
id int primary key auto_increment,
name char(10)
);
insert into dep4(name) values('IT'),('HR'),('SALE'),('Boss');

 6、foreign key (外鍵約束)

員工信息表有三個字段:工號  姓名  部門

公司有3個部門,可是有1個億的員工,那意味着部門這個字段須要重複存儲,部門名字越長,越浪費

解決方法:

咱們徹底能夠定義一個部門表

而後讓員工信息表關聯該表,如何關聯,即foreign key

以下圖簡單的表示了一下員工表與部門表的關係,即員工表的(dep_id)要關聯部門表的id字段

多對一(一個表多條記錄的某一字段關聯另外一張表的惟一一個字段):員工有部門,部門又有好多信息,因此
分開建了一張部門表,部門表的id 和員工表裏面
的dep_id相關聯。(dep_id要關聯部門表的id字段
(注意:1.先建被關聯的表, 2.被關聯的字段必須惟一 3.先給被關聯的表插入記錄 ) 

先建張部門表(被關聯表)
create table dep(
id int not null unique,
#id int primary key auto_increment,
name varchar(50),
comment varchar(100)
);

再建張員工表(關聯表)
create table emp_info(
id int primary key auto_increment,
name varchar(20),
dep_id int,
constraint FK_depid_id foreign key(dep_id) references dep(id) #references :關聯
on delete cascade  #關聯的表刪了,被關聯的表也刪了
on update cascade  #關聯的表修改了,被關聯的表也修改了
);
#先給被關聯的表初始化記錄
insert into dep values
(1,'歐德博愛技術有限事業部','說的好...'),
(2,'艾利克斯人力資源部','招不到人'),
(3,'銷售部','賣不出東西');


insert into emp_info values
(1,'egon',1),
(2,'alex1',2),
(3,'alex2',2),
(4,'alex3',2),
(5,'李坦克',3),
(6,'劉飛機',3),
(7,'張火箭',3),
(8,'林子彈',3),
(9,'加特林',3);

#修改
update dep set id =301 where id = 2;
select * from dep;
delect * from em_info;


若是部門解散了,員工也就走吧,就是部門表沒了,
員工表也就沒有了。

 

 運行結果以下圖:

 查看建立的表

 修改id=301

查看被關聯表和關聯表

相關文章
相關標籤/搜索