Can I create a named default constraint in an add column statement in SQL Server?(我可以在 SQL Server 的添加列语句中创建命名默认约束吗?)
问题描述
在 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 的添加列语句中创建命名默认约束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以在 SQL Server 的添加列语句中创建命名默认约束吗?
基础教程推荐
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 在多列上分布任意行 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- oracle区分大小写的原因? 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
