<tfoot id='Nlc7y'></tfoot>
        • <bdo id='Nlc7y'></bdo><ul id='Nlc7y'></ul>

        <small id='Nlc7y'></small><noframes id='Nlc7y'>

        <i id='Nlc7y'><tr id='Nlc7y'><dt id='Nlc7y'><q id='Nlc7y'><span id='Nlc7y'><b id='Nlc7y'><form id='Nlc7y'><ins id='Nlc7y'></ins><ul id='Nlc7y'></ul><sub id='Nlc7y'></sub></form><legend id='Nlc7y'></legend><bdo id='Nlc7y'><pre id='Nlc7y'><center id='Nlc7y'></center></pre></bdo></b><th id='Nlc7y'></th></span></q></dt></tr></i><div id='Nlc7y'><tfoot id='Nlc7y'></tfoot><dl id='Nlc7y'><fieldset id='Nlc7y'></fieldset></dl></div>
        <legend id='Nlc7y'><style id='Nlc7y'><dir id='Nlc7y'><q id='Nlc7y'></q></dir></style></legend>
      1. SQL Server,不能在主键字段中插入空值?

        SQL Server, can#39;t insert null into primary key field?(SQL Server,不能在主键字段中插入空值?)
        <tfoot id='w4sTL'></tfoot>

            <small id='w4sTL'></small><noframes id='w4sTL'>

              <tbody id='w4sTL'></tbody>
            <legend id='w4sTL'><style id='w4sTL'><dir id='w4sTL'><q id='w4sTL'></q></dir></style></legend>
              • <i id='w4sTL'><tr id='w4sTL'><dt id='w4sTL'><q id='w4sTL'><span id='w4sTL'><b id='w4sTL'><form id='w4sTL'><ins id='w4sTL'></ins><ul id='w4sTL'></ul><sub id='w4sTL'></sub></form><legend id='w4sTL'></legend><bdo id='w4sTL'><pre id='w4sTL'><center id='w4sTL'></center></pre></bdo></b><th id='w4sTL'></th></span></q></dt></tr></i><div id='w4sTL'><tfoot id='w4sTL'></tfoot><dl id='w4sTL'><fieldset id='w4sTL'></fieldset></dl></div>

                  <bdo id='w4sTL'></bdo><ul id='w4sTL'></ul>

                • 本文介绍了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,不能在主键字段中插入空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How do I split flat file data and load into parent-child tables in database?(如何拆分平面文件数据并加载到数据库中的父子表中?)
                  How to import CSV into sqlite using RSqlite?(如何使用 RSqlite 将 CSV 导入 sqlite?)
                  Import CSV to Update rows in table(导入 CSV 以更新表中的行)
                  Importing MaxMind#39;s GeoLite2 to MySQL(将 MaxMind 的 GeoLite2 导入 MySQL)
                  Import / Export database with SQL Server Server Management Studio(使用 SQL Server Server Management Studio 导入/导出数据库)
                  How do you import a large MS SQL .sql file?(如何导入大型 MS SQL .sql 文件?)

                    <small id='7E8Uo'></small><noframes id='7E8Uo'>

                      1. <i id='7E8Uo'><tr id='7E8Uo'><dt id='7E8Uo'><q id='7E8Uo'><span id='7E8Uo'><b id='7E8Uo'><form id='7E8Uo'><ins id='7E8Uo'></ins><ul id='7E8Uo'></ul><sub id='7E8Uo'></sub></form><legend id='7E8Uo'></legend><bdo id='7E8Uo'><pre id='7E8Uo'><center id='7E8Uo'></center></pre></bdo></b><th id='7E8Uo'></th></span></q></dt></tr></i><div id='7E8Uo'><tfoot id='7E8Uo'></tfoot><dl id='7E8Uo'><fieldset id='7E8Uo'></fieldset></dl></div>
                          <bdo id='7E8Uo'></bdo><ul id='7E8Uo'></ul>
                            <tbody id='7E8Uo'></tbody>
                          <tfoot id='7E8Uo'></tfoot>

                          • <legend id='7E8Uo'><style id='7E8Uo'><dir id='7E8Uo'><q id='7E8Uo'></q></dir></style></legend>