Tec*_*ave 11 sql sql-server constraints alter-table
我想ALTER TABLE在SQL Server中添加默认约束,但我收到了以下错误消息.
表'Register'中的列'StartDate'对于创建默认约束无效.
我知道我可以在创建表时声明约束,但这不是我所处的情况.我希望有人可以帮助我;)
这是我的alter发言:
ALTER TABLE [dbo].[Register]
ADD CONSTRAINT [Register_StartDate] DEFAULT (GETDATE()) FOR StartDate
Run Code Online (Sandbox Code Playgroud)
这是我的创建表脚本:
CREATE TABLE [dbo].[Register]
(
[ID] [INT] IDENTITY(1, 1) NOT NULL,
/* ....*/
[StartDate] [DATETIME] NULL
)
Run Code Online (Sandbox Code Playgroud)
编辑:修复:我忘记了[StartDate]字段甚至不存在于表中.我的错!
Mar*_*ith 10
据我所知,有两种可能导致此错误的情况.
试图将默认约束添加到:
对于表定义
CREATE TABLE dbo.Register
(
ID INT IDENTITY(1, 1) NOT NULL,
Computed AS 'Foo'
)
Run Code Online (Sandbox Code Playgroud)
以下两个语句都失败,错误1752.
ALTER TABLE dbo.Register
ADD CONSTRAINT C1 DEFAULT 'X' FOR Computed
ALTER TABLE [dbo].[Register]
ADD CONSTRAINT [Register_StartDate] DEFAULT (GETDATE()) FOR StartDate
Run Code Online (Sandbox Code Playgroud)
有许多其他条件,不允许向列添加默认约束,但这些条件都有自己唯一的错误号和消息.
+------------------------------------+--------------+
| Reason | Error number |
+------------------------------------+--------------+
| Column is IDENTITY | 1754 |
| Column is timestamp/rowversion | 1755 |
| Sparse Column | 1791 |
| Default constraint already present | 1781 |
+------------------------------------+--------------+
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8420 次 |
| 最近记录: |