本節重點:
- not null 與 default
- unique
- primary
- auto_increment
- foreign key
?
?
一、介紹
約束條件與數據類型的寬度一樣,都是可選參數
作用:用于保證數據的完整性和一致性
主要分為:
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...)
?
二、not null 與default
是否可空,null表示空,非字符串
not null - 不可空
null - 可空
默認值,創建列時可以指定默認值,當插入數據時如果未主動設置,則自動添加默認值
create table tb1(nid int not null defalut 2,num int not null);
?
驗證1:
?

?
?
驗證2:

?
驗證3:

?
小練習:
創建學生表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 | +----+------+-----+------+---------------+ 1 row in set (0.00 sec)
?
3、unique
中文翻譯:不同的。在mysql中稱為單列唯一
?
舉例說明:創建公司部門表(每個公司都有唯一的一個部門)。

?
接下來,使用約束條件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');
?
聯合唯一:
# 創建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: 0mysql> 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'
?
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 | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.02 sec)
?
復合主鍵

?
5.auto_increment
約束:約束的字段為自動增長,約束的字段必須同時被key約束
?
(重點)驗證:

?

?

?
了解:

?
清空表區分delete和truncate的區別:
delete from t1; #如果有自增id,新增的數據,仍然是以刪除前的最后一樣作為起始。
truncate table t1;數據量大,刪除速度比上一條快,且直接從零開始。
?
6.foreign key
?
一 快速理解foreign key
之前創建表的時候都是在一張表中添加記錄,比如如下表:
?
公司有3個部門,但是有1個億的員工,那意味著部門這個字段需要重復存儲,部門名字越長,越浪費。
這個時候,
解決方法:
我們完全可以定義一個部門表
然后讓員工信息表關聯該表,如何關聯,即foreign key
我們可以將上表改為如下結構:
?
此時有兩張表,一張是employee表,簡稱emp表(關聯表,也就從表)。一張是department表,簡稱dep表(被關聯表,也叫主表)。
?
創建兩張表操作:

?
上面的刪除表記錄的操作比較繁瑣,按道理講,裁掉一個部門,該部門的員工也會被裁掉。其實呢,在建表的時候還有個很重要的內容,叫同步刪除,同步更新
接下來將剛建好的兩張表全部刪除,先刪除關聯表(emp),再刪除被關聯表(dep)
接下來:
重復上面的操作建表
注意:在關聯表中加入
on delete cascade #同步刪除
on update cascade #同步更新
修改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) on delete cascade #同步刪除on update cascade #同步更新 );
接下來的操作,就復合我們正常的生活中的情況了。
#再去刪被關聯表(dep)的記錄,關聯表(emp)中的記錄也跟著刪除 mysql> delete from dep where id=3; Query OK, 1 row affected (0.00 sec)mysql> select * from dep; +----+-----------+----------------------+ | id | name | descripe | +----+-----------+----------------------+ | 1 | IT | IT技術有限部門 | | 2 | 銷售部 | 銷售部門 | +----+-----------+----------------------+ 2 rows in set (0.00 sec)mysql> select * from emp; +----+----------+-----+--------+ | id | name | age | dep_id | +----+----------+-----+--------+ | 1 | zhangsan | 18 | 1 | | 2 | lisi | 19 | 1 | | 3 | egon | 20 | 2 | | 5 | alex | 18 | 2 | +----+----------+-----+--------+ 4 rows in set (0.00 sec)#再去更改被關聯表(dep)的記錄,關聯表(emp)中的記錄也跟著更改mysql> update dep set id=222 where id=2; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0# 趕緊去查看一下兩張表是否都被刪除了,是否都被更改了 mysql> select * from dep; +-----+-----------+----------------------+ | id | name | descripe | +-----+-----------+----------------------+ | 1 | IT | IT技術有限部門 | | 222 | 銷售部 | 銷售部門 | +-----+-----------+----------------------+ 2 rows in set (0.00 sec)mysql> select * from emp; +----+----------+-----+--------+ | id | name | age | dep_id | +----+----------+-----+--------+ | 1 | zhangsan | 18 | 1 | | 2 | lisi | 19 | 1 | | 3 | egon | 20 | 222 | | 5 | alex | 18 | 222 | +----+----------+-----+--------+ 4 rows in set (0.00 sec)