导致错误的默认约束

Default Constraint causing an error

我有以下 SQL 来创建 table,但是第一个 CONSTRAINT 中的 "DEFAULT" 给我一个错误:"A Default constraint can exist only at the column level in a CREATE or ALTER TABLE statement."

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

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]

您没有指定您使用的是什么数据库,但根据语法我认为它是 MS SQL 服务器。如果是这样,您可以将内联约束添加为列定义的一部分,如下所示:

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

试试这个

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]

或者先创建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 
    )
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