Reputation: 62394
I'm using the following query:
INSERT INTO role (name, created) VALUES ('Content Coordinator', GETDATE()), ('Content Viewer', GETDATE())
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.
Upvotes: 100
Views: 453876
Reputation: 12777
Encountered the same issue. This is something to do with your table creation. When you created table you have not indicate 'ID
' column to be Auto Increment
hence you get this error. By making the column Primary Key
it cannot be null
or contain duplicates hence without Auto Increment
pretty obvious to throw column does not allow nulls. INSERT fails
.
There are two ways you could fix this issue.
1). via MS SQL Server Management Studio
Got to MS SQL Server Management Studio
Locate your table and right click and select Design
Locate your column and go to Column Properties
Under Indentity Specification: set (Is Identity)=Yes and Indentity Increment=1
2). via ALTER SQLs
ALTER TABLE table DROP COLUMN id; // drop the existing ID
ALTER TABLE table ADD id int IDENTITY(1, 1) NOT NULL; // add new column ID with auto-increment
ALTER TABLE table ADD CONSTRAINT PK_ident_test PRIMARY KEY CLUSTERED (id); // make it primary key
Upvotes: 3
Reputation: 2397
RULE: You cannot IGNORE those colums that do not allow null values, when inserting new data.
id
column, which does not allow nulls. Obviously this won't work.id
values for you (see selected answer), when you later execute the insert query.Cannot insert the value NULL into column 'id_foreign', table 'MyDataBase.dbo.Tmp_ThisTable'; column does not allow nulls. INSERT fails. The statement has been terminated.
id_foreign
, allowing nulls.id_foreign
.Upvotes: 0
Reputation: 2007
WARNING! Make sure the target table is locked when using this method (As per @OnurOmer's comment)
if you can't or don't want to set the autoincrement property of the id, you can set value for the id for each row like this:
INSERT INTO role (id, name, created)
SELECT
(select max(id) from role) + ROW_NUMBER() OVER (ORDER BY name)
, name
, created
FROM (
VALUES
('Content Coordinator', GETDATE())
, ('Content Viewer', GETDATE())
) AS x(name, created)
Upvotes: 1
Reputation: 9
I had a similar problem and upon looking into it, it was simply a field in the actual table missing id
(id
was empty/null
) - meaning when you try to make the id
field the primary key
it will result in error because the table contains a row with null
value for the primary key
.
This could be the fix if you see a temp table associated with the error. I was using SQL Server Management Studio.
Upvotes: 0
Reputation: 121
use IDENTITY(1,1)
while creating the table
eg
CREATE TABLE SAMPLE(
[Id] [int] IDENTITY(1,1) NOT NULL,
[Status] [smallint] NOT NULL,
CONSTRAINT [PK_SAMPLE] PRIMARY KEY CLUSTERED
(
[Id] ASC
)
)
Upvotes: 12
Reputation: 83
you didn't give a value for id. Try this :
INSERT INTO role (id, name, created) VALUES ('example1','Content Coordinator', GETDATE()), ('example2', 'Content Viewer', GETDATE())
Or you can set the auto increment on id field, if you need the id value added automatically.
Upvotes: 0
Reputation: 103358
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.
To set up auto-increment in SQL Server Management Studio:
Design
Column Properties
Indentity Specification
, set (Is Identity)=Yes
and Indentity Increment=1
Upvotes: 185
Reputation: 11
You need to set autoincrement property of id column to true when you create the table or you can alter your existing table to do this.
Upvotes: 0
Reputation: 2697
If the id
column has no default value, but has NOT NULL
constraint, then you have to provide a value yourself
INSERT INTO dbo.role (id, name, created) VALUES ('something', 'Content Coordinator', GETDATE()), ('Content Viewer', GETDATE())
Upvotes: 4
Reputation: 285
As id is PK it MUST be unique and not null. If you do not mention any field in the fields list for insert it'll be supposed to be null or default value. Set identity (i.e. autoincrement) for this field if you do not want to set it manualy every time.
Upvotes: 0
Reputation: 328
You either need to specify an ID in the insert, or you need to configure the id column in the database to have Identity Specification = Yes.
Upvotes: 0