create table t4(
id int primary key,
name varchar(20)
);
create table t3(
id int,
name varchar(20),
[constraint id_primary] primary(id,name) – 聯(lián)合約束
o 通過alter語句添加: alter … modify/change … / alter … addprimary key …
alter table t4 modify id intprimary key;
alter table t3 add constraintun_primary primary key(id, name);
- 刪除主鍵
o altertable t4 drop primary key;
- 注意:如果刪除的主鍵約束具有自增長約束,則必須先刪除自增長約束,再去刪除主鍵約束 。
- 概述
o 通常給主鍵添加自增長約束
- 添加
create table t5(
id int primary key auto_increment,
name varchar(20)
);
o 通過alter語句添加: alter … modify/change …auto_increment
o alter table t5 change id idint auto_increment;
- 刪除自增長
o alter table t5 modify id int;
- 注意:
6. unsigned: 無符號約束
- 概念
o age tinyint 1 -128~127 unsigned 0~255
- 添加
create table t6(
id int,
age tinyint unsigned
);
o 通過alter語句添加: alter … unsigned modify/change …
o alter table t6 change age agetinyint unsigned;
o alter table t6 modify agetinyint unsigned;
- 刪除
o alter table t6 modify agetinyint;
o alter table t6 change age agetinyint;
7. zerofill: 零填充約束
- 概念
- 添加
create table t7(
id int,
age int(6) zerofill
);
- 刪除
o alter table t7 modify ageint;
o alter table t7 change age ageint;
8. foreign key: 外鍵約束
- 通過建立外鍵,設(shè)置表與表之間的約束性,限制數(shù)據(jù)的錄入
- 概述
o 被外鍵約束的列,取值必須參照其主表列中的值
o 注意:通常先創(chuàng)建主表,再創(chuàng)建從表
- 添加外鍵約束
empno int promary key auto_increment,
ename varchar(32) not null,
deptno int,
[constraint fk_name] foreignkey(deptno) references dept(deptno) – 添加外鍵約束
);
createtable dept(
deptno int primary keyauto_increment,
dname varchar(32),
loc varchar(32)
);
o 使用alteradd constraint …
o altertable emp add constraint fk_name foreign key(deptno) references dept (deptno);
- 刪除外鍵約束
o alter table emp drop foreignkey fk_name;
- 注意:
o 使用 show create table 表名 查看具體的外鍵名稱
- 設(shè)置外鍵中的級聯(lián)關(guān)系
o on update cascase: 更新主表中的數(shù)據(jù)時(shí),從表中的數(shù)據(jù)隨之更新
o on delete set null: 刪除主表中的數(shù)據(jù)時(shí),從表中的數(shù)據(jù)置空
- 級聯(lián)刪除
empno int promary key auto_increment,
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時(shí)就醫(yī)或請專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助:- irr公式excel的用法 irr公式excel怎么用
- 天干相生相克的作用關(guān)系
- 社交app用戶需求分析 app需求分析模板
- 計(jì)算機(jī)rank函數(shù)操作方法 rank函數(shù)怎么用
- 阿里云ssh使用教程 阿里云ssh怎么連接
- 二學(xué)歷考研有用嗎
- 在職考研也要用學(xué)信網(wǎng)嗎
- 物聯(lián)網(wǎng)應(yīng)用了解 物聯(lián)網(wǎng)有哪些應(yīng)用
- mysql安裝教程分享 mysql的安裝包在哪個(gè)文件
- 產(chǎn)業(yè)發(fā)展理論及應(yīng)用 產(chǎn)業(yè)發(fā)展理論有哪些
