当创建数据表时,忘记添加约束,或者需要修改数据表的数据类型,那么就涉及到了数据表中列的增删改查
增
alter table 表名 add 列名 数据类型 //添加列
alter table 表名 add primary key(列名) //添加主键约束
alter table 当前表名 add foreign key(当前表中列名) references 外表名(外表列) //添加外健约束
alter table 表名 add default '内容' for 列名 //添加默认值约束
alter table 表名 add check(条件) //添加检查(条件)约束
alter table 表名 add unique(列名) //添加唯一约束
alter table 表名 alter column 列名 数据类型 not null //添加非空约束
注: 完整的应该是 alter table 名称 add constraint 约束名 关键字 列名 以上的所有添加主键,全部都省略了这一步
删
alter table 表名 drop column 列名 //删除列
alter table 表名 drop constraint 主键名 //删除主键约束
alter table 表名 drop constraint 外键名 //删除外键约束
alter table 表名 drop constraint 默认键名 //删除默认约束
alter table 表名 drop constraint 检查约束名 //删除条件约束
alter table 表名 drop constraint 唯一约束名 //删除唯一约束
alter table 表名 alter column 列名 数据类型 null //删除非空约束
改
alter table 表名 alter column 列名 数据类型 //修改列