Question

I am just learning some SQL Server with SQL Server Management Studio Express and in code I wish to add an auto-increment to this part stu_id integer not null primary key

So the code below is what I have tried and doesn't work.

Also once I do have it successfully added, how do I write the value into the table? Since it is an auto-increment do I just leave that part blank - like so?

values('', 'James', 'DACLV6', '$2000');

==================The Full Code here=========================
create database firstTest
use firstTest

create table studentDetails
(stu_id integer not null primary key SQL AUTO INCREMENT, stu_name varchar(50), stu_course     varchar(20), stu_fees varchar(20));

select * from studentDetails

Insert into studentDetails
(stu_id, stu_name, stu_course, stu_fees)
values('1', 'James', 'DACLV6', '$2000');

Thanks in advance.

Était-ce utile?

La solution

To get an auto increment column it would be something like

Create Table Test(
id int not null Identity(1,1),
desc varchar(50) null,
Constraint PK_test Primary Key(id)
)

You can use the short form syntax if you want, I just like any constraints to be out of the way in my sql. The arguments in the identity function are start value and increment, so you start it 107 and increment by 13 if you were really strange. :)

you then insert with

Insert Test(desc) Values('a description')

Autres conseils

I would imagine the answer is to not supply a value for the column in the same way as if it were an identity column - because it's value will be generated by auto increment:

INSERT INTO studentDetails (stu_name, stu_course, stu_fees)
VALUES ('James', 'DACLV6', '$2000');
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top