Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert value into primary key column in SQL Server?

insert into Student 
values('1', 'joedio', 'newyark', GETDATE())

I get this error message when trying to run this SQL:

An explicit value for the identity column in table 'Student' can only be specified when a column list is used and IDENTITY_INSERT is ON.

like image 637
divikdiya Avatar asked Feb 01 '18 05:02

divikdiya


People also ask

Can we insert value in primary key column?

The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE values, and cannot contain NULL values.

How do you add values into an identity column?

To manually insert a new value into the Id column, we first must set the IDENTITY_INSERT flag ON as follows: SET IDENTITY_INSERT Students ON; To set the IDENTIT_INSERT flag ON we need to use the SET statement followed by the flag name and the name of the table.

How do I change the primary key value of a column in SQL Server?

Syntax: Alter table table_name add primary key (column_name); To change the Primary key column in the SQL Server, follow these steps: Drop already defined primary key.

Can we insert value in identity column in SQL?

Identity field is usually used as a primary key. When you insert a new record into your table, this field automatically assign an incremented value from the previous entry. Usually, you can't insert your own value to this field.


2 Answers

If your table has Identity Column, then you have to mention all the other columns while inserting.

Just it is for sample:

INSERT INTO Student (ID, Name, State, Date) VALUES('1','joedio','newyark',GETDATE())

If the First column is Identity, then skip the value while inserting:

INSERT INTO Student (Name, State, Date) VALUES('joedio','newyark',GETDATE())

And If you want to Insert Values into an Identity Column in SQL Server

SET IDENTITY_INSERT IdentityTable ON

INSERT INTO Student (ID, Name, State, Date) VALUES('1','joedio','newyark',GETDATE())

SET IDENTITY_INSERT IdentityTable OFF

And also refer the link How to Insert Values into an Identity Column in SQL Server for More information.

like image 20
DineshDB Avatar answered Sep 18 '22 12:09

DineshDB


If you wants to insert primary key by query even it is auto increment then you have to set IDENTITY_INSERT ON as below, it will allow you to insert unique value by query:

SET IDENTITY_INSERT [Tablename] ON;

Your query will be now:

SET IDENTITY_INSERT Student ON;

INSERT INTO Student VALUES('1','joedio','newyark',GETDATE());

SET IDENTITY_INSERT Student OFF;

If you wants that SQL manage it self then default it set to IDENTITY_INSERT OFF and Auto increment is set, means on every insert the new value is assigned to that PK column.

Better to execute with default setting SET IDENTITY_INSERT Student OFF because by manual inset possibility to insert duplicate value and it will throw an error.

like image 53
Sandip - Frontend Developer Avatar answered Sep 18 '22 12:09

Sandip - Frontend Developer