mysql約束,主鍵,外來鍵

2021-10-10 00:22:52 字數 3144 閱讀 4173

資料庫表約束:對錶中的資料進行限制,保證資料的正確性、有效性和完整性,乙個表如果新增了約束,不正確的資料將無法插入到表中。約束在建立表的時候新增比較合適。

約束名約束關鍵字

主鍵primary key

唯一unique

非空not null

外來鍵foreign key

檢查約束

check 注:mysql 不支援

用來唯一標識資料庫中的每一條記錄;

主鍵關鍵字: primary key

主鍵的特點:

非空 not null唯一 unique

主鍵的增刪改語法:

建立表時設定主鍵:

(倆種:一種直接跟在字段後面加上primary key 第二種 在最後一句加上primary key (id)):

create table st4 (

id int primary key auto_increment,

name varchar(20)

--primary key(id)

) auto_increment = 1000;

建立好以後修改(increment)起始值:

alter table st4 auto_increment = 2000;

-- 刪除st5表的主鍵

alter table st5 drop primary key;

-- 和新增主鍵

alter table st5 add primary key(id);

注意:設定auto_increment自增後,delete 和truncate對自增是倆種影響

delete:刪除所有的記錄之後,自增長沒有影響,自增長不重置,

truncate:刪除以後,自增長又重新開始。自增長重置為初始,

主鍵數在乙個表中,只能有乙個。但這乙個主鍵可以單列,也可以是多列。

自增長只能用在主鍵上;

唯一約束:unique

建立表的時候新增唯一約束:在字段後面直接加上unique

create table st7 (

id int,

name varchar(20) unique

) 注意:多個null並不重複,因為null沒有資料

非空約束:not null

建立表學生表st8, 包含字段(id,name,gender)其中name不能為null 

create table st8 (

id int,

name varchar(20) not null,

gender char(1)

)

預設值:default

建立乙個學生表 st9,包含字段(id,name,address), 位址預設值是廣州 

create table st9 (

id int,

name varchar(20),

address varchar(20) default '廣州' )

-- 新增一條記錄,使用預設位址

insert into st9 values (1, '李四', default);

select * from st9;

insert into st9 (id,name) values (2, '李白');

-- 新增一條記錄,不使用預設位址

當表內資料需要借助其他表資料來表示的時候 ,應對該錶內的資料進行外來鍵約束(防止設定非法資料,在另乙個表內無法查到) 但也可以不設定外來鍵約束…依舊還可以認為他是外來鍵

建立外來鍵約束的語法:

新建表時增加外來鍵:(在[ ]內的字段屬於可填可不填)

[constraint] [外來鍵約束名稱] foreign key已有表增加外來鍵:

alter table 從表 add [constraint] [外來鍵約束名稱] foreign key (外來鍵欄位名) references 主表(主 鍵欄位名);

建立乙個dep_id的外來鍵約束,主表是department:

create table employee(

id int primary key auto_increment,

name varchar(20),

age int,

dep_id int, -- 外來鍵對應主表的主鍵

-- 建立外來鍵約束

constraint emp_depid_fk foreign key (dep_id) references department(id)

) -- 刪除employee表的emp_depid_fk外來鍵

alter table employee drop foreign key emp_depid_fk;

-- 在employee表情存在的情況下新增外來鍵

alter table employee add constraint emp_depid_fk foreign key (dep_id) references department(id);

外來鍵的級聯
create table employee(

id int primary key auto_increment,

name varchar(20),

age int,

dep_id int, -- 外來鍵對應主表的主鍵

-- 建立外來鍵約束

constraint emp_depid_fk foreign key (dep_id) references department(id) on update cascade

) 結尾加上on update cascade或on delete cascade倆種情況,同時級聯的關係有四種;no action,cascade,...

alter table 參照表

add constraint 外來鍵

foreign key( 外鍵名) references 被參照表(主鍵)

on update cascade --級聯更新

on delete cascade --級聯刪除

mysql的主鍵 外來鍵約束 MySQL 主鍵外來鍵

笛卡兒積 多表查詢 多個表變成乙個表 完整性約束條件 primary key 標識該屬性為該錶的主鍵,可以唯一的標識對應的元組 foreign key 標識該屬性為該錶的外來鍵,是與之聯絡的某錶的主鍵 not null 標識該屬性不能為空 unique 標識該屬性的值是唯一的 auto increm...

Sql Server 主鍵 外來鍵約束

主鍵約束 表通常具有包含唯一標識表中每一行的值的一列或一組列。這樣的一列或多列稱為表的主鍵 pk 用於強制表的實體完整性。由於主鍵約束可保證資料的唯一性,因此經常對標識列定義這種約束。如果為表指定了主鍵約束,資料庫引擎 將通過為主鍵列自動建立唯一索引來強制資料的唯一性。當在查詢中使用主鍵時,此索引還...

mysql 約束基本概念 主鍵約束 外來鍵約束

四種條件約束 非空 唯一性 主鍵約束 外來鍵約束 create table t user id int 4 name varchar 32 not null,class bigint 1000 這裡的id不可以是空值 列級約束 create table t user id int 4 unique,...