MySQL修改約束

2022-05-06 02:30:07 字數 1034 閱讀 4650

alter

table 表名 modify column 欄位名 字段型別 新約束

例如:alter table student modify column age int not null;

新增字段唯一:alter table student modify column stu_nu varchar(20) unique;

alter

table 表名 add

[constraint 約束名

] 約束型別(欄位名) [

外來鍵的引用

]

例如新增外來鍵:alter table student add constraint fk_stu_class foreign key(class_id) references class(id);

新增主鍵:alter table student add constraint pk_stu primary key(id);

新增字段唯一:alter table student add unique(stu_nu);

例如刪除非空約束

alter

table student modify column age int

null;

刪除主鍵:

alter

table student drop

primary

key;

刪除唯一鍵(可以使用show index from student檢視唯一)

alter

table student drop

index index_name;

刪除外來鍵:

alter

table student drop

foreign

key fk_name;

MySQL修改約束

新增主鍵約束 alter table tbl name add constraint symbol索引名 primary key index type index col name1,index col name2,刪除主鍵約束 alter table tbl name drop primary k...

MySQL 新增約束,修改約束,刪除約束

alter table 新增,修改,刪除表的列,約束等表的定義。檢視列 desc 表名 修改表名 alter table t book rename to bbb 新增列 alter table 表名 add column 列名 varchar 30 刪除列 alter table 表名 drop ...

Oracel 修改約束命令

增加修改列的約束 alter table dep 1 modify dep id number 5 default 8 constraint dep 1 id nn not null 非空約束 修改表的約束 例如設定主鍵 alter table dep 1 add constraint dep 1 ...