表約束
一、概念
? 表中一定要有各種約束,通過各種約束使得未來插入到數據庫中的數據是合法的,在語法上是沒有問題的;
? 約束本質就是通過技術手段,倒逼著程序員插入正確的數據,換句話說就是,插入進來的數據一定是符合數據約束的;最終保證數據的完整性和可預期性;
二、非空約束
select null;#NULL表示沒有,不區分大小寫;
select 1+null;#實際上null是不會參與計算的
? 空屬性有兩個值,分別是null(默認值)和not null;創建表時除了寫表屬性和類型之外還可以添加null和not null分別表示允許為空和不允許為空;
? Null列表示是否啟用空屬性約束;默認約束是Null,由于不啟用空約束所以就不允許插入缺省值了;
三、默認約束
? 常用的某一個具體值,可以一開始就指定好,用戶需要顯式傳遞時允許用戶傳入其需求值;
? default和not NULL并不沖突,當用戶顯示插入對應屬性時,可以插入NULL或者合法數據,當不插入數據時,如果設置了default就會自動插入缺省值,沒設置就必須用戶顯示插入;
? MySQL默認的default值是NULL;
四、comment約束
? 相當于屬性字段的注釋,用于程序員和DBA進行了解;
create table 表名(屬性名 屬性類型 非空約束 默認約束 comment '屬性注釋');
五、zerofill約束
create table 表名(屬性名 屬性類型 非空約束 默認約束 comment '屬性注釋'zerofill);
#zerofill表示是否啟用;
? 關于顯示方面的約束;如果顯示的寬度小于指定的寬度,使用zerofill填充之后就會使用0填充指定的寬度;如果數據的寬度大于zerofill約束指定寬度,還是可以正常顯示,所以zerofill是一種至少的行為;
? int是4個字節,無符號顯示出來就是十個位,有符號還有符號位所以是十一位;
六、主鍵約束
? 主鍵:primary key用來唯一約束該字段里面的數據,不能重復,不能為空,一張表里面只能有一個主鍵;
create table 表名(屬性名 屬性類型 非空約束 默認約束 zerofill primary key comment '屬性注釋');
create table 表名(屬性名 屬性類型 非空約束 默認約束 zerofill comment '屬性注釋',primary key('屬性名'));
? 存在的意義就是:使得屬性唯一標識,不可以被修改,便于增刪查改;
alter table 表名 add primary key(屬性字段);
alter table 表名 drop primary key;
? 主鍵也可以添加到多列,即復合主鍵,仍然是一個主鍵;
? 嚴格的按照一對一的關系,不允許同一個記錄重復出現;
6.1自增長約束
? auto_increment是主鍵的一種,插入數據時可以不用考慮它,每次在插入數據時都會自動地進行最大數據加一;**自增長約束一般都必須設置成主鍵;自增長字段必須是整數;一張表只能有一個自增長;**常用于索引;
create table if not exists t2(id int unsigned primary key auto_increment,name varchar(20) not null
)auto_increment=5;Create Table: CREATE TABLE `t2` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`name` varchar(20) NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
#除了表內屬性進行了優化,表外屬性也自動添加AUTO_INCREMENT=5,表明下一次插入時會使用的主鍵值;每次插入玩=完成之后會更新AUTO_INCREMENT字段;
? 補充:索引的本質就是以空間換時間,為kv映射關系提供了專門的空間,便于快速定位;索引和主鍵相關;
七、唯一鍵約束
create table t3(id char(20) unique comment '這是一個學生的唯一鍵學號',name varchar(32) not null
);Create Table: CREATE TABLE `t3` (`id` char(20) DEFAULT NULL COMMENT '這是一個學生的唯一鍵學號',`name` varchar(32) NOT NULL,UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | char(20) | YES | UNI | NULL | |
| name | varchar(32) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
? 唯一鍵與逐漸最大的不同就是可以為空,并且空可以重復;
? 他們之間互為補充,其中選擇了主鍵作為索引,但是其他的屬性邏輯上也要保證唯一性,所以需要有唯一鍵來保證唯一性;
八、外鍵約束
? 外鍵常用于主表和從表之間建立聯系;即從表中有屬性與主表的主鍵或者唯一鍵屬性相關聯,此時就需要使用外鍵建立聯系;
create table if not exists student(id int primary key,name varchar(20) not null,telephone varchar(20) unique key,class_id int,foreign key(class_id) references class(id)
);Create Table: CREATE TABLE `student` (`id` int(11) NOT NULL,`name` varchar(20) NOT NULL,`telephone` varchar(20) DEFAULT NULL,`class_id` int(10) unsigned DEFAULT NULL,PRIMARY KEY (`id`),UNIQUE KEY `telephone` (`telephone`),KEY `class_id` (`class_id`),CONSTRAINT `student_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8+-----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| telephone | varchar(20) | YES | UNI | NULL | |
| class_id | int(10) unsigned | YES | MUL | NULL | |
+-----------+------------------+------+-----+---------+-------+
? 外鍵既可以保證從表插入時,需要符合約束條件,也可以保證從表外鍵約束有數據時不可以刪除主表內容;
? 定義外鍵時必須保證外鍵列數據在主表中存在或者為空;
九、綜合案例
? 約束會保證凡是進入mysql中的數據一定是符合規定的;
一個商店的數據記錄客戶端及客戶購物情況,由三個表組成;
? 商品goods(商品編號goods_id,商品名goods——name,單價unitprice,商品類別category,供應商provider);
? 客戶customer(客戶號customer_id,姓名name,住址address,郵箱email,性別sex,身份證card_id);
? 購買purchase(購買訂單號order_id,客戶號customer_id,商品號goods_id,購買數量nums);
要求:
? 每個表的主外鍵;
? 客戶的姓名不能為空值;
? 郵箱不能重復;
? 客戶的性別(男,女);
-- 創建數據庫
create database if not exists bit32mall
default character set utf8 ;
-- 選擇數據庫
use bit32mall;
-- 創建數據庫表
-- 商品
create table if not exists goods
(goods_id int primary key auto_increment comment '商品編號',goods_name varchar(32) not null comment '商品名稱',unitprice int not null default 0 comment '單價,單位分',category varchar(12) comment '商品分類',provider varchar(64) not null comment '供應商名稱'
);
-- 客戶
create table if not exists customer
(customer_id int primary key auto_increment comment '客戶編號',name varchar(32) not null comment '客戶姓名',address varchar(256) comment '客戶地址',email varchar(64) unique key comment '電子郵箱',sex enum('男','女') not null comment '性別',card_id char(18) unique key comment '身份證'
);
-- 購買
create table if not exists purchase
(order_id int primary key auto_increment comment '訂單號',customer_id int comment '客戶編號',goods_id int comment '商品編號',nums int default 0 comment '購買數量',foreign key (customer_id) references customer(customer_id),foreign key (goods_id) references goods(goods_id)
);