亚洲精品久久久久久第一页-人妻少妇精彩视品一区二区三区-91国产自拍免费视频-免费一级a在线播放视频正片-少妇天天日天天射天天爽-国产大屁股喷水视频在线观看-操美女骚穴抽插性爱视频-亚洲 欧美 中文字幕 丝袜-成人免费无码片在线观看

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


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

文章插圖
摘要:一篇文章帶你徹底了解MySQL各種約束 。
MySQL約束<1> 概念
是一種限制,它是對表的行和列的數(shù)據(jù)做出約束,確保表中數(shù)據(jù)的完整性和唯一性 。
<2> 使用場景
創(chuàng)建表的時候,添加約束
<3> 分類
  • default: 默認(rèn)約束, 域完整性
  • not null: 非空約束,域完整性
  • unique: 唯一約束,實(shí)體完整性
  • primary key: 主鍵約束,實(shí)體完整性
  • foreign key: 外鍵約束,參照完整性
  • check: 檢查約束(MySQL不支持),域完整性
  • auto_increment: 自增長約束
  • unsigned: 無符號約束
  • zerofill: 零填充約束
數(shù)據(jù)庫中有三個完整性: 域、實(shí)體、參照完整性
  • 域(列)完整性:
o 域完整性是對數(shù)據(jù)表中字段屬性的約束
  • 實(shí)體完整性在MySQL中實(shí)現(xiàn):
o 通過主鍵約束和候選鍵約束實(shí)現(xiàn)的
  • 參照完整性:
o 也就是說是MySQL的外鍵
1. default
  • 概念
o 指定某列的默認(rèn)值,插入數(shù)據(jù)時候,此列沒有值,則用default指定的值來填充
  • 添加
o 在創(chuàng)建表的時候添加: 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 指定某列的值不為空,在插入數(shù)據(jù)的時候必須非空 ‘’ 不等于null, 0不等于 null
  • 添加
o 在創(chuàng)建表的時候添加: 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 指定列或者列組合不能重復(fù),保證數(shù)據(jù)的唯一性
o 不能出現(xiàn)重復(fù)的值,但是可以有多個null
o 同一張表可以有多個唯一的約束
  • 添加唯一約束
o 在創(chuàng)建表的時候添加: 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)– 添加復(fù)合約束
);
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 當(dāng)前行的數(shù)據(jù)不為空并且不能重復(fù)
o 相當(dāng)于:唯一約束+非空約束
  • 添加主鍵約束
o 在創(chuàng)建表的時候添加: create … primary key


以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時就醫(yī)或請專業(yè)人士給予相關(guān)指導(dǎo)!

「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助: