mysql三-3:完整性條件約束

來源:互聯網
上載者:User

標籤:har   聯合   定義   word   業務   sed   nbsp   ble   主鍵   

閱讀目錄

  • 一 介紹
  • 二 not null與default
  • 三 unique
  • 四 primary key
  • 五 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‘age int unsigned NOT NULL default 20 必須為正值(無符號) 不允許為空白 預設是203. 是否是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
)

驗證三 uniqueView Codenot null+unique的化學反應聯合唯一四 primary key

primary key欄位的值不為空白且唯一

一個表中可以:

單列做主鍵
多列做主鍵(複合主鍵)

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

單列主鍵多列主鍵五 auto_increment

限制欄位為自動成長,被約束的欄位必須同時被key約束

View Code步長increment與起始位移量offset:auto_increment_increment,auto_increment_offset六 foreign key

一 快速理解foreign key

員工資訊表有三個欄位:工號  姓名  部門

公司有3個部門,但是有1個億的員工,那意味著部門這個欄位需要重複儲存,部門名字越長,越浪費

解決方案:

我們完全可以定義一個部門表

然後讓員工資訊表關聯該表,如何關聯,即foreign key

示範 

二 如何找出兩張表之間的關係 

分析步驟:#1、先站在左表的角度去找是否左表的多條記錄可以對應右表的一條記錄,如果是,則證明左表的一個欄位foreign key 右表一個欄位(通常是id)#2、再站在右表的角度去找是否右表的多條記錄可以對應左表的一條記錄,如果是,則證明右表的一個欄位foreign key 左表一個欄位(通常是id)#3、總結:#多對一:如果只有步驟1成立,則是左表多對一右表如果只有步驟2成立,則是右表多對一左表#多對多如果步驟1和2同時成立,則證明這兩張表時一個雙向的多對一,即多對多,需要定義一個這兩張表的關係表來專門存放二者的關係#一對一:如果1和2都不成立,而是左表的一條記錄唯一對應右表的一條記錄,反之亦然。這種情況很簡單,就是在左表foreign key右表的基礎上,將左表的外鍵欄位設定成unique即可

三 建立表之間的關係

#一對多或稱為多對一三張表:出版社,作者資訊,書一對多(或多對一):一個出版社可以出版多本書關聯方式:foreign key
View Code其他例子

  

#多對多三張表:出版社,作者資訊,書多對多:一個作者可以寫多本書,一本書也可以有多個作者,雙向的一對多,即多對多  關聯方式:foreign key+一張新的表
View Code其他例子

 

#一對一兩張表:學生表和客戶表一對一:一個學生是一個客戶,一個客戶有可能變成一個學校,即一對一的關係關聯方式:foreign key+unique
View Code其他例子七 作業

練習:帳號資訊表,使用者組,主機表,主機群組

#使用者表create table user(id int not null unique auto_increment,username varchar(20) not null,password varchar(50) not null,primary key(username,password));insert into user(username,password) values(‘root‘,‘123‘),(‘egon‘,‘456‘),(‘alex‘,‘alex3714‘);#使用者組表create table usergroup(id int primary key auto_increment,groupname varchar(20) not null unique);insert into usergroup(groupname) values(‘IT‘),(‘Sale‘),(‘Finance‘),(‘boss‘);#主機表create table host(id int primary key auto_increment,ip char(15) not null unique default ‘127.0.0.1‘);insert into host(ip) values(‘172.16.45.2‘),(‘172.16.31.10‘),(‘172.16.45.3‘),(‘172.16.31.11‘),(‘172.10.45.3‘),(‘172.10.45.4‘),(‘172.10.45.5‘),(‘192.168.1.20‘),(‘192.168.1.21‘),(‘192.168.1.22‘),(‘192.168.2.23‘),(‘192.168.2.223‘),(‘192.168.2.24‘),(‘192.168.3.22‘),(‘192.168.3.23‘),(‘192.168.3.24‘);#業務線表create table business(id int primary key auto_increment,business varchar(20) not null unique);insert into business(business) values(‘輕鬆貸‘),(‘隨便花‘),(‘大富翁‘),(‘窮一生‘);#建關係:user與usergroupcreate table user2usergroup(id int not null unique auto_increment,user_id int not null,group_id int not null,primary key(user_id,group_id),foreign key(user_id) references user(id),foreign key(group_id) references usergroup(id));insert into user2usergroup(user_id,group_id) values(1,1),(1,2),(1,3),(1,4),(2,3),(2,4),(3,4);#建關係:host與businesscreate table host2business(id int not null unique auto_increment,host_id int not null,business_id int not null,primary key(host_id,business_id),foreign key(host_id) references host(id),foreign key(business_id) references business(id));insert into host2business(host_id,business_id) values(1,1),(1,2),(1,3),(2,2),(2,3),(3,4);#建關係:user與hostcreate table user2host(id int not null unique auto_increment,user_id int not null,host_id int not null,primary key(user_id,host_id),foreign key(user_id) references user(id),foreign key(host_id) references host(id));insert into user2host(user_id,host_id) values(1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8),(1,9),(1,10),(1,11),(1,12),(1,13),(1,14),(1,15),(1,16),(2,2),(2,3),(2,4),(2,5),(3,10),(3,11),(3,12);
View Code

 

作業:

 

mysql三-3:完整性條件約束

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.