问题描述
在 SQL Server 中,我在表中有一个新列:
In SQL Server, I have a new column on a table:
ALTER TABLE t_tableName
ADD newColumn NOT NULL
这失败了,因为我在没有指定默认约束的情况下指定了 NOT NULL.该表不应具有默认约束.
This fails because I specify NOT NULL without specifying a default constraint. The table should not have a default constraint.
为了解决这个问题,我可以使用默认约束创建表,然后将其删除.
To get around this, I could create the table with the default constraint and then remove it.
但是,似乎没有任何方法可以指定默认约束应命名为该语句的一部分,因此我摆脱它的唯一方法是使用存储过程在sys.default_constraints 表.
However, there doesn't appear to be any way to specify that the default constraint should be named as part of this statement, so my only way to get rid of it is to have a stored procedure which looks it up in the sys.default_constraints table.
对于可能经常发生的操作来说,这有点混乱/冗长.有没有人对此有更好的解决方案?
This is a bit messy/verbose for an operation which is likely to happen a lot. Does anyone have any better solutions for this?
推荐答案
这应该有效:
ALTER TABLE t_tableName
ADD newColumn VARCHAR(50)
CONSTRAINT YourContraintName DEFAULT '' NOT NULL
这篇关于我可以在 SQL Server 的添加列语句中创建命名默认约束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)