Yara H
Yara H

Reputation: 11

SQL insert error on column part of primary key

I am trying to insert the following into SQL

INSERT INTO Customers (contactname, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');

When I do this, I get an error :

Cannot insert the value NULL into column 'CustomerID', table 'Northwind.dbo.Customers'; column does not allow nulls. INSERT fails.

I tried to change the properties of the customer id to allow null values but it says "null property cannot be set on a column which is part of the primary key"?

Does anyone have an idea?

Upvotes: 1

Views: 493

Answers (2)

Michael Waterman
Michael Waterman

Reputation: 81

Primary keys must be unique and cannot be null. They're the individuating column(s) for a given table.

You won't need to worry about manually setting the CustomerID column if you set it to auto increment, which is best practice for integer keys.

Upvotes: 0

ishak
ishak

Reputation: 141

you can move primary to contact name if you want to try make customerID null, but always remember one of caracteristics from primary key is must be uniq value

Upvotes: 2

Related Questions