當(dāng)創(chuàng)建數(shù)據(jù)表時(shí),忘記添加約束航邢,或者需要修改數(shù)據(jù)表的數(shù)據(jù)類型卤恳,那么就涉及到了數(shù)據(jù)表中列的增刪改查
增
alter table 表名 add 列名 數(shù)據(jù)類型? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //添加列
alter table 表名 add primary key(列名)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//添加主鍵約束
alter table 當(dāng)前表名 add foreign key(當(dāng)前表中列名) references 外表名(外表列)? ? ? ? ? ?//添加外健約束
alter table 表名 add default '內(nèi)容' for 列名? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//添加默認(rèn)值約束
alter table 表名 add check(條件)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //添加檢查(條件)約束
alter table 表名 add unique(列名)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//添加唯一約束
alter table 表名 alter column 列名 數(shù)據(jù)類型 not null? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//添加非空約束
注:? ? ? ? 完整的應(yīng)該是? ? ? ?alter table 名稱 add constraint 約束名 關(guān)鍵字 列名? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 以上的所有添加主鍵,全部都省略了這一步
刪
alter table 表名 drop column 列名? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//刪除列
alter table 表名 drop constraint 主鍵名? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//刪除主鍵約束
alter table 表名 drop constraint 外鍵名? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//刪除外鍵約束
alter table 表名 drop constraint 默認(rèn)鍵名? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //刪除默認(rèn)約束
alter table 表名 drop constraint 檢查約束名? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//刪除條件約束
alter table 表名 drop constraint 唯一約束名? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//刪除唯一約束
alter table 表名 alter column 列名 數(shù)據(jù)類型 null? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //刪除非空約束
改
alter table 表名 alter column 列名 數(shù)據(jù)類型? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改列