'table in ms sql with primary key and auto_increment problem

hi i found an answer here and used the exemple

CREATE TABLE Persons (
    Personid int IDENTITY(1,1) PRIMARY KEY,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int
);

but when i enter records, it does not allow duplicates in column lastname?!

table persons

i would expect id 3 and "hans" but it makes NULL?! it shouldnt be a problem that there is again name hans in row 3 ...

what do i wrong?

enter image description here



Solution 1:[1]

If do you want to do autoincrement on Personid, you should do:

CREATE TABLE Persons (
Personid int AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid) );

And if do you want insert something into:

INSERT INTO Persons (LastName, FirstName, Age int) VALUES (..,..,..)

I hope I've helped.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Macia Salva