sql – 所以我不能在时态表的列定义中使用NOT NULL?

我正在使用SQL Server 2016并尝试创建Temporal表.这是定义:

USE zbachoreTest
GO`enter code here`
IF EXISTS (SELECT 1 FROM sys.tables WHERE name = 'People' AND temporal_type_desc ='SYSTEM_VERSIONED_TEMPORAL_TABLE' )
ALTER TABLE dbo.People SET( SYSTEM_VERSIONING = OFF)
DROP TABLE IF EXISTS dbo.People 
CREATE TABLE dbo.People(
    PersonID INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_People PRIMARY KEY(PersonID),
    FirstName VARCHAR(50) NOT  NULL,
    LastName VARCHAR(50)  NULL,
    StartTime DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL
        CONSTRAINT DF_People_StartTime DEFAULT SYSDATETIME(),
    EndTime DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL
        CONSTRAINT DF_People_EndTime DEFAULT CONVERT(DATETIME2,'9999-12-31 23:59:59.9999999'),
    PERIOD FOR SYSTEM_TIME(StartTime,EndTime)
    ) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.PeopleHistory))

当我运行上面的代码时,我收到以下错误消息:
Msg 13531,Level 16,State 1,Line 7
将SYSTEM_VERSIONING设置为ON失败,因为列’FirstName’在表’zbachoreTest.dbo.People’和’zbachoreTest.dbo.PeopleHistory’中没有相同的nullability属性.

任何聪明的人都请帮忙.谢谢!

最佳答案 是的你可以.

请先丢弃表dbo.PeopleHistory.如果它已经存在,它将仅被验证,而不是重新创建.

来自:https://docs.microsoft.com/en-us/sql/relational-databases/tables/creating-a-system-versioned-temporal-table

The history table must always be schema-aligned with the current or
temporal table, in terms of number of columns, column names, ordering
and data types.

If the table specified by the HISTORY_TABLE parameter already exists,
it will be validated against the newly created temporal table in terms
of schema consistency and temporal data consistency. If you specify an
invalid history table, the CREATE TABLE statement will fail.

点赞