无法将 null 插入主键字段

发布于 2024-09-26 13:27:34 字数 357 浏览 5 评论 0原文

当我尝试做这样的声明时:

INSERT INTO qcRawMatTestCharacteristic 
VALUES (NULL, 1, 1, 1, 1, 1, 1, 1, '', '', '', GETDATE(), 1)

我得到以下信息:

无法将 NULL 值插入 列“iRawMatTestCharacteristicId”, 桌子 'Intranet.dbo.qcRawMatTestCharacteristic'; 列不允许为空。插入 失败。

我理解该错误,但 null 值适用于具有 int 数据类型的主字段。

有什么想法吗?

When I try to do a statement like this:

INSERT INTO qcRawMatTestCharacteristic 
VALUES (NULL, 1, 1, 1, 1, 1, 1, 1, '', '', '', GETDATE(), 1)

I get the following:

Cannot insert the value NULL into
column 'iRawMatTestCharacteristicId',
table
'Intranet.dbo.qcRawMatTestCharacteristic';
column does not allow nulls. INSERT
fails.

I understand the error, but the null value is for my primary field with an int data type.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

飘逸的'云 2024-10-03 13:27:34

任何关系数据库中的主键都不允许为 NULL - 这是主键的主要基本特征之一。

请参阅:SQL按设计:如何选择主键

永不为空
主键值不能为空,也不能做任何事情
使主键为空。 这是
不可违反的关系规则
ANSI 支持的模型,
关系数据库管理系统
(RDBMS) 设计和 SQL Server。


更新: 好的,所以您需要在 SQL Server 中使用“自动增量”主键。

您需要在 CREATE TABLE 语句中将其定义为 INT IDENTITY:

 CREATE TABLE dbo.YourTable(ID INT IDENTITY, col1 INT, ..., colN INT)

然后当您执行 INSERT 时,您需要显式指定要插入的列,但不要在该列表中指定“ID”列 - 然后是 SQL服务器将自动查找正确的值:

 INSERT INTO dbo.YourTable(col1, col2, ..., colN) -- anything **except** `ID`      
 VALUES(va1l, val2, ..., valN)

如果您想在创建表后执行此操作,可以在 SQL Server Management Studio 的表设计器中执行此操作:

替代文字

Primary keys in any relational database are not allowed to be NULL - it's one of the main, fundamental characteristics of a primary key.

See: SQL by Design: how to Choose the primary key

Never Null
No primary key value can be null, nor can you do anything to
render the primary key null. This is
an inviolate rule of the relational
model as supported by ANSI
, of
relational database management system
(RDBMS) design, and of SQL Server.

UPDATE: ok, so you want an "auto-increment" primary key in SQL Server.

You need to define it as an INT IDENTITY in your CREATE TABLE statement:

 CREATE TABLE dbo.YourTable(ID INT IDENTITY, col1 INT, ..., colN INT)

and then when you do an INSERT, you need to explicitly specify the columns to insert, but just don't specify the "ID" column in that list - then SQL Server will handle finding the proper value automagically:

 INSERT INTO dbo.YourTable(col1, col2, ..., colN) -- anything **except** `ID`      
 VALUES(va1l, val2, ..., valN)

If you want to do this after having created the table already, you can do so in the SQL Server Management Studio's table designer:

alt text

雾里花 2024-10-03 13:27:34

SQL Server 中主键字段不能包含空值。如果您想要填充 SQL 表并且不知道要为基于整数的主键字段输入什么内容,请将 PK 设置为身份字段。另外,在指定 Insert 语句时,最好使用 insert 语句的列映射部分,例如:

insert into (field1, field2, field3)
values
(value1, value2, value3)

这样做的原因是它确保列顺序是您开发的,因为 SQL 管理员可以修改列顺序。它还允许您插入带有标识主键的行,而无需指定主键的值示例

CREATE TABLE [dbo].[foo](
    [fooid] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](50) NULL,
CONSTRAINT [PK_foo] PRIMARY KEY
(
    [fooid] ASC
)

现在我的插入语句很简单,

Insert into foo (name)
values
("John")

表中的结果将是

1, "John"

Primary Key fields cannot contain null values in SQL Server. If you want to populate a SQL table and don't know what to enter for a integer based primary key field then set the PK to an Identity field. Also when specifying Insert statements its wise to use the column mapping portion of the insert statement for example:

insert into (field1, field2, field3)
values
(value1, value2, value3)

The reason for this is it insures that the column order is what you developed for as a SQL administrator can modify column order. It also allows you to insert a row with an identity Primary key with out specifying the value of the Primary Key Example

CREATE TABLE [dbo].[foo](
    [fooid] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](50) NULL,
CONSTRAINT [PK_foo] PRIMARY KEY
(
    [fooid] ASC
)

now my insert statement is simple

Insert into foo (name)
values
("John")

the result in the table would be

1, "John"
素衣风尘叹 2024-10-03 13:27:34

您可能没有在整数主键上设置(您忘记添加)自动增量。

You probably don't have (you forgot to add) autoincrement set on your integer primary key.

千纸鹤 2024-10-03 13:27:34

主键不应接受空值。为什么要向主键字段插入空值?主键字段应该有一个不可为空的唯一值,这将使表中的每条记录都是唯一的。

Primary keys shouldn't accept null value. Why you are inserting null values to a primary key field? Primary key field should have a non-nullable, unique value which will make each of your record in the table unique.

不弃不离 2024-10-03 13:27:34

您可以仅对 1 个唯一行使用 0 而不是 null,对于 PK 不可能使用 null。或者您可以省略 PK 并使用自动增加 P​​K 字段

you can use 0 instead of null for only 1 unique row, null is not possible for PK. Or you can omit PK and use and auto increament PK field

蒗幽 2024-10-03 13:27:34

假设您的主键有一个自动增量字段,您需要在插入中包含字段列表,而不是为该字段输入值,例如

INSERT INTO qcRawMatTestCharacteristic 
(Answer1,Answer2,...SomeDateField)
VALUES(1,1,1,1,1,1,1,'','','', GETDATE(), 1)

Assuming you have an autoincrement field for your primary Key you'll need to include the field list on your insert and not put a value for that field e.g.

INSERT INTO qcRawMatTestCharacteristic 
(Answer1,Answer2,...SomeDateField)
VALUES(1,1,1,1,1,1,1,'','','', GETDATE(), 1)
川水往事 2024-10-03 13:27:34

我假设您真正的问题是您不确定如何编写插入语句以便自动正确填充 PK?您需要命名要为其设置值的字段,看起来您正在尝试设置所有这些字段,但只是排除 PK 字段,如下所示:

INSERT INTO someTable
(fieldName1, fieldName2) 
VALUES(1,1)

其中 sometable 是包含三个字段的表。 PK、字段名称 1 和字段名称 2。您还需要确保 PK 字段上的身份属性设置为 true。

I'm assuming your real issue is that you're not sure how to write an insert statement so that the PK is auto populated correct? You need to name the fields you're setting values for, it looks like you're trying to set all of them but just exclude the PK field like so:

INSERT INTO someTable
(fieldName1, fieldName2) 
VALUES(1,1)

Where sometable is a table with three fields. PK, fieldName1, and fieldName2. You also need to make sure that the identity property on the PK field is set to true.

浪荡不羁 2024-10-03 13:27:34

如果您有标识列,则无需在插入语句中指定它。

INSERT INTO qcRawMatTestCharacteristic  
VALUES(1,1,1,1,1,1,1,'','','', GETDATE(), 1) 

但是,如果您的主键不是标识列,那么您确实需要指定它,否则它将尝试插入空值,并且默认情况下主键不可为空。

if you have an identity column, you don't need to specify it in the insert statement.

INSERT INTO qcRawMatTestCharacteristic  
VALUES(1,1,1,1,1,1,1,'','','', GETDATE(), 1) 

However, if you have a primary key that isn't an identity column, then you do need to specify it, because otherwise it'll try to insert a null and primary keys by default are non-nullable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文