我正在使用以下查询:
INSERT INTO role (name, created) VALUES ('Content Coordinator', GETDATE()), ('Content Viewer', GETDATE())
Run Code Online (Sandbox Code Playgroud)
但是,我没有指定主键(即id).所以我的问题是,为什么sql server会出现这个错误:
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'id', table 'CMT_DEV.dbo.role'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Run Code Online (Sandbox Code Playgroud)
Cur*_*urt 122
我假设这id应该是一个递增的值.
您需要设置它,否则如果您有一个不可为空的列,没有默认值,如果您没有提供任何值,它将会出错.
在SQL Server Management Studio中设置自动增量:
DesignColumn PropertiesIndentity Specification,set (Is Identity)=Yes和Indentity Increment=1小智 7
IDENTITY(1,1)在创建表时使用,例如
CREATE TABLE SAMPLE(
[Id] [int] IDENTITY(1,1) NOT NULL,
[Status] [smallint] NOT NULL,
CONSTRAINT [PK_SAMPLE] PRIMARY KEY CLUSTERED
(
[Id] ASC
)
)
Run Code Online (Sandbox Code Playgroud)