SQL Server,不能在主键字段中插入空值?

2023-11-28数据库问题
10

本文介绍了SQL Server,不能在主键字段中插入空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正准备在这个上撕掉我的头发.我是 MS SQL 的新手,还没有在任何地方看到过类似的帖子.

I'm about ready to rip my hair out on this one. I'm fairly new to MS SQL, and haven't seen a similar post anywhere.

当我尝试做这样的陈述时:

When I try to do a statement like this:

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

我得到以下信息:

不能插入 NULL 值列iRawMatTestCharacteristicId",桌子'Intranet.dbo.qcRawMatTestCharacteristic';列不允许空值.插入失败.

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

我理解错误,但空值是我的主字段的 int 数据类型.

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

有什么想法!?

推荐答案

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

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

参见:SQL by Design:如何选择主键

从不为空
主键值不能为空,也不能做任何事情将主键呈现为空.这是不可侵犯的关系规则ANSI 支持的模型,关系数据库管理系统(RDBMS) 设计,以及 SQL Server.

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.

更新:好的,所以您需要 SQL Server 中的自动增量"主键.

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

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

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)

然后当您执行 INSERT 时,您需要明确指定要插入的列,但不要在该列表中指定ID"列 - 然后 SQL Server 将自动处理查找正确的值:

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)

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

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:

这篇关于SQL Server,不能在主键字段中插入空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12