为什么 SQL 服务器会抛出此错误:无法将值 NULL 插入列“id"? [英] Why is SQL server throwing this error: Cannot insert the value NULL into column 'id'?

查看:18
本文介绍了为什么 SQL 服务器会抛出此错误:无法将值 NULL 插入列“id"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下查询:

INSERT INTO role (name, created) VALUES ('Content Coordinator', GETDATE()), ('Content Viewer', GETDATE())

但是,我没有指定主键(即 id).所以我的问题是,为什么 sql server 会返回此错误:

However, I'm not specifying the primary key (which is id). So my questions is, why is sql server coming back with this error:

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.

推荐答案

我假设 id 应该是一个递增的值.

I'm assuming that id is supposed to be an incrementing value.

你需要设置这个,否则如果你有一个不可为空的列,没有默认值,如果你不提供值就会出错.

You need to set this, or else if you have a non-nullable column, with no default value, if you provide no value it will error.

在 SQL Server Management Studio 中设置自动增量:

To set up auto-increment in SQL Server Management Studio:

  • Design中打开你的表格
  • 选择您的列并转到Column Properties
  • Indentity Specification下,设置(Is Identity)=YesIndentity Increment=1
  • Open your table in Design
  • Select your column and go to Column Properties
  • Under Indentity Specification, set (Is Identity)=Yes and Indentity Increment=1

这篇关于为什么 SQL 服务器会抛出此错误:无法将值 NULL 插入列“id"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆