Allen 2020-10-21 14:08:15 10953 0 0 0 0

MYSQL添加/删除主键,约束,索引


第一种:创建表的时候
create table table_name(
列名1  数据类型 (int) primary key auto_increment,
列名2 数据类型  not null,
列名3 数据类型   unique,
列名4 数据类型  default '值',
constraint  索引名 foreign key(外键列)  references 主键表(主键列)
on delete cascade | on delete set null

+++
第二种:建表完成之后
1.主键约束
添加:alter table  table_name add primary key (字段)
删除:alter table table_name drop primary key

2.非空约束
添加:alter  table table_name modify 列名 数据类型  not null 
删除:alter table table_name modify 列名 数据类型 null

3.唯一约束
添加:alter table table_name add unique 约束名(字段)
删除:alter table table_name drop key 约束名

4.自动增长
添加:alter table table_name  modify 列名 int  auto_increment
删除:alter table table_name modify 列名 int

5.外键约束
添加:alter table table_name add constraint 约束名 foreign key(外键列) 
references 主键表(主键列)

+++
删除:
第一步:删除外键
alter table table_name drop foreign key 约束名

第二步:删除索引
alter  table table_name drop  index 索引名

[1]: 约束名和索引名一样

6.默认值
添加:alter table table_name alter 列名  set default '值'
删除:alter table table_name alter 列名  drop default

+++
多列唯一性约束
alter table ty_forum add constraint uk_forum_alias unique(site_id,forum_alias);


以上


【版權聲明】
本文爲原創,遵循CC 4.0 BY-SA版權協議!轉載時請附上原文鏈接及本聲明。
原文鏈接:https://tdlib.com/am.php?t=mXXrSG6TGERu
No Tags
我也要發一個   ·   返回首頁   ·   返回[Mysql]   ·   前一個   ·   下一個
相關內容
歡迎評論
未登錄,
請先 [ 註冊 ] or [ 登錄 ]
(一分鍾即可完成註冊!)
返回首頁     ·   返回[Mysql]   ·   返回頂部