默认约束导致错误

时间:2015-01-13 15:29:27

标签: sql sql-server tsql constraints default

我有以下SQL来创建表,但第一个CONSTRAINT中的“DEFAULT”给出了一个错误:“默认约束只能存在于CREATE或ALTER TABLE语句的列级别。”

我之前从未使用默认设置,因此我通过互联网研究对此进行了一些研究,但没有任何帮助我解决错误,甚至没有对我进行过解释。

CREATE TABLE [RuleEngine].[NCCIImportHistory](
                [NCCIImportHistoryID] [int] IDENTITY(1,1) NOT NULL,
                [StartTime] [datetimeoffset](7) NOT NULL,
                [EndTime] [datetimeoffset](7) NOT NULL,
                [CreatedOn] [datetimeoffset](7) NOT NULL,
                [CreatedBy_UserID] [int] NOT NULL,
CONSTRAINT [DF_NCCIImportHistory_CreatedOn]  DEFAULT (getutcdate()) FOR [CreatedOn],
CONSTRAINT [FK_NCCIImportHistory_User_CreatedBy] FOREIGN KEY([CreatedBy_UserID]) REFERENCES [Security].[User] ([UserID]),
CONSTRAINT [PK_NCCIImportHistoryID] PRIMARY KEY CLUSTERED ([NCCIImportHistoryID] ASC) 
    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]) ON [PRIMARY]

2 个答案:

答案 0 :(得分:2)

您没有指定您正在使用的数据库,但基于语法我认为它是MS SQL Server。如果是这样,您可以将内联约束作为列定义的一部分添加,如下所示:

[CreatedOn] [datetimeoffset](7) NOT NULL CONSTRAINT [DF_NCCIImportHistory_CreatedOn] DEFAULT (getutcdate()),

答案 1 :(得分:0)

试试这个

CREATE TABLE [RuleEngine].[NCCIImportHistory]
    (
      [NCCIImportHistoryID] [INT] IDENTITY(1, 1)
                                  NOT NULL ,
      [StartTime] [DATETIMEOFFSET](7) NOT NULL ,
      [EndTime] [DATETIMEOFFSET](7) NOT NULL ,
      [CreatedOn] [DATETIMEOFFSET](7) NOT NULL CONSTRAINT [DF_NCCIImportHistory_CreatedOn] DEFAULT ( GETUTCDATE() ),
      [CreatedBy_UserID] [INT] NOT NULL ,
      CONSTRAINT [FK_NCCIImportHistory_User_CreatedBy] FOREIGN KEY ( [CreatedBy_UserID] ) REFERENCES [Security].[User] ( [UserID] ) ,
      CONSTRAINT [PK_NCCIImportHistoryID] PRIMARY KEY CLUSTERED
        ( [NCCIImportHistoryID] ASC )

        WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
               IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
               ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90 ) ON [PRIMARY]
    )
ON  [PRIMARY]

或首先创建表然后像这样的约束

CREATE TABLE [RuleEngine].[NCCIImportHistory]
    (
      [NCCIImportHistoryID] [INT] IDENTITY(1, 1) NOT NULL ,
      [StartTime] [DATETIMEOFFSET](7) NOT NULL ,
      [EndTime] [DATETIMEOFFSET](7) NOT NULL ,
      [CreatedOn] [DATETIMEOFFSET](7) NOT NULL,
      [CreatedBy_UserID] [INT] NOT NULL 
    )
ON  [PRIMARY]
GO


ALTER TABLE  [RuleEngine].[NCCIImportHistory] ADD CONSTRAINT [DF_NCCIImportHistory_CreatedOn] DEFAULT( GETUTCDATE() ) FOR [CreatedOn]
GO

ALTER TABLE  [RuleEngine].[NCCIImportHistory] ADD CONSTRAINT [FK_NCCIImportHistory_User_CreatedBy]  FOREIGN KEY ( [CreatedBy_UserID] ) REFERENCES [Security].[User] ( [UserID] )
GO

ALTER TABLE  [RuleEngine].[NCCIImportHistory] ADD CONSTRAINT [PK_NCCIImportHistoryID] PRIMARY KEY CLUSTERED( [NCCIImportHistoryID] ASC ) WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
               IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
               ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90 ) ON [PRIMARY]
GO