您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

删除表中字段的sql语句有哪些?

2024/7/24 17:51:47发布33次查看
删除表中字段的sql语句有:1、删除没有默认值的列【alter table test drop column bazaartype】;2、删除有默认值的列,【alter table test drop column bazaartype】。
删除表中字段的sql语句
1.删除没有默认值的列:
alter table test drop column bazaartype
2.删除有默认值的列:
先删除约束(默认值)
alter table test drop constraint df__test__bazaartype__3c4acb5f
(alter table test drop column bazaartype 提示报错就是 df__sheettest__attac__0f8d3381)
然后在删除列
alter table test drop column bazaartype
3.修改字段名称
alter table 表名 add 字段名 int default (0) not null;
修改主键字段类型
alter table [tablename] alter column [colname] [newdatatype])
修改某表的字段类型时,会报错是因为存在约束。
a. 将表中的字段设置为not null(不为空)、给字段增加default值(默认值)这样的操作都会给该字段添加约束,增加了这些约束后,在用sql脚本修改字段类型、删除字段的时候均会发生类似错误.
b.查找该字段上已存在的约束,并删除存在的约束.
c.再次执行修改/删除脚本即可。
解决方法:
1.查找出表中该字段的约束名称(或根据已有的提示及对象'df__******')
declare @name varchar(50)select @name =b.name from sysobjects b join syscolumns a on b.id = a.cdefault where a.id = object_id('tablename') and a.name ='columname'
2. 删除存在的约束
exec('alter table tablename drop constraint ' + @name)
例如:
exec('alter table t_tablename drop constraint 报错信息的约束名' )
3. 再执行修改字段类型的脚本即可
alter table dbo.t_tablename alter column id bigint not nullalter table dbo.t_tablename add constraint pk_id primary key(id)
相关学习推荐:sql视频教程
以上就是删除表中字段的sql语句有哪些?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product