用sql語句刪除主鍵約束,sql刪除約束的語句

摘要:一篇文章帶你徹底了解MySQL各種約束。

MySQL約束

<1> 概念

是一種限制,它是對錶的行和列的數據做出約束,確保表中數據的完整性和唯一性。

<2> 使用場景

創建表的時候,添加約束

<3> 分類

  • default: 默認約束, 域完整性
  • not null: 非空約束,域完整性
  • unique: 唯一約束,實體完整性
  • primary key: 主鍵約束,實體完整性
  • foreign key: 外鍵約束,參照完整性
  • check: 檢查約束(MySQL不支持),域完整性
  • auto_increment: 自增長約束
  • unsigned: 無符號約束
  • zerofill: 零填充約束

數據庫中有三個完整性: 域、實體、參照完整性

  • 域(列)完整性:

o 域完整性是對數據表中字段屬性的約束

  • 實體完整性在MySQL中實現:

o 通過主鍵約束和候選鍵約束實現的

  • 參照完整性:

o 也就是說是MySQL的外鍵

1. default

  • 概念

o 指定某列的默認值,插入數據時候,此列沒有值,則用default指定的值來填充

  • 添加

o 在創建表的時候添加: create … default

create table t1(
id int default 1,
name varchar(20) default 『老王』
);

o 通過alter語句添加: alter … modify/change …

alter table t1 modify id intdefault 2;

alter table t1 change namename varchar(20) default 『若塵』;

  • 刪除

o alter … modify/change

o alter table t1 modify id int;

o alter table t1 change namename varchar(20);

2. not null

  • 概念

o 指定某列的值不為空,在插入數據的時候必須非空 『』 不等於null, 0不等於 null

  • 添加

o 在創建表的時候添加: create … not null

create table t2(
id int not null,
name varchar(20) not null
);

o 通過alter語句添加: alter … modify/change …

alter table t2 modify id intnot null;

alter table t2 change namename varchar(20) not null;

  • 刪除

o alter … modify/change

o alter table t2 modify id int;

o alter table t2 change namename varchar(20);

3. unique

  • 概念

o 指定列或者列組合不能重複,保證數據的唯一性

o 不能出現重複的值,但是可以有多個null

o 同一張表可以有多個唯一的約束

  • 添加唯一約束

o 在創建表的時候添加: create … unique

create table t3(
id int unique,
name varchar(20) not null
);

insertt3 value (1, 『老王』);
insert t3 value (1, 『老李』); – id唯一約束,添加異常

create table t3(
id int,
name varchar(20) not null,
constraint id_unique unique(id, name)– 添加複合約束
);

insertt3 value (1, 『老王』);
insert t3 value (1, 『老李』);
select * from t3;
insert t3 value (1, 『老王』);

o 通過alter語句添加: alter … modify/change … / alter … addunique

alter table t3 modify id intunique;

alter table t3 addunique(name);

alter table t3 add constraintun_id unique(id);

3. 刪除唯一約束

  • alter … drop … index 名稱
  • drop index on 表名
  • alter table t3 drop index id_unique;
  • 注意:如果刪除的唯一約束列具有自增長約束,則必須先刪除自增長約束,再去刪除唯一約束

4. 主鍵約束

  • 概念

o 當前行的數據不為空並且不能重複

o 相當於:唯一約束+非空約束

  • 添加主鍵約束

o 在創建表的時候添加: create … primary key

create table t4(
id int primary key,
name varchar(20)
);

create table t3(
id int,
name varchar(20),
[constraint id_primary] primary(id,name) – 聯合約束

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 alter … drop primary key

o altertable t4 drop primary key;

  • 注意:如果刪除的主鍵約束具有自增長約束,則必須先刪除自增長約束,再去刪除主鍵約束。

5. auto_increment: 自增長約束

  • 概述

o 列的數值自動增長,列的類型只能是整數類型

o 通常給主鍵添加自增長約束

  • 添加

o 在創建表的時候添加: create … auto_increment

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 … modify/change…

o alter table t5 modify id int;

  • 注意:

o 一張表只能有一個自增長列,並且該列需要定義約束

6. unsigned: 無符號約束

  • 概念

o 指定當前列的數值為非負數

o age tinyint 1 -128~127 unsigned 0~255

  • 添加

o 在創建表的時候添加: create … unsigned

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 … modify/change …

o alter table t6 modify agetinyint;

o alter table t6 change age agetinyint;

7. zerofill: 零填充約束

  • 概念

o 指定當前列的數值的顯示格式,不影響當前列顯示範圍

  • 添加

o 在創建表的時候添加: create … zerofill

create table t7(
id int,
age int(6) zerofill
);

  • 刪除

o alter … modify/change …

o alter table t7 modify ageint;

o alter table t7 change age ageint;

8. foreign key: 外鍵約束

  • 通過建立外鍵,設置表與表之間的約束性,限制數據的錄入
常見的6種MySQL約束
  • 概述

o 建立表與表之間的關係,建立參照完整性,一個表可以有多個外鍵,每個外鍵必須參照另一個主鍵。

o 被外鍵約束的列,取值必須參照其主表列中的值

o 注意:通常先創建主表,再創建從表

  • 添加外鍵約束

createtable emp(
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 … drop foreign key fk_name

o alter table emp drop foreignkey fk_name;

  • 注意:

o 在創建表時,不去明確指定外鍵約束的名稱,系統會自動地生成一個外鍵的名稱。

o 使用 show create table 表名 查看具體的外鍵名稱

  • 設置外鍵中的級聯關係

o on delete cascade: 刪除主表中的數據時,從表中的數據隨之刪除

o on update cascase: 更新主表中的數據時,從表中的數據隨之更新

o on delete set null: 刪除主表中的數據時,從表中的數據置空

  • 級聯刪除

create table emp(
empno int promary key auto_increment,
ename varchar(32) not null,
deptno int,
[constraint fk_name] foreignkey(deptno) references dept(deptno) on delete cascade– 添加外鍵約束
);

  • 注意:

o 插入數據時,先插入主表的數據,再插入從表的數據

o 刪除數據時,先刪除從表的數據,再刪除主表的數據

數據庫的設計

  • 主鍵約束
  • 自增長約束
  • 外鍵約束(慎用)
  • 唯一約束
  • 非空約束
  • 默認約束

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/230931.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-10 18:44
下一篇 2024-12-10 18:44

相關推薦

發表回復

登錄後才能評論