2013-09-24 54 views
0

添加自動遞增我剛學與SQL Server Management Studio中快速和代碼,我想一個自動遞增到這個部分stu_id integer not null primary keySQL Server Management Studio中 - 在代碼

所以下面的代碼是某些SQL Server我已經嘗試過,並沒有工作。

另外,一旦我確實已經成功添加,我該如何將值寫入表中? 因爲它是一個自動增量,我只是把這個部分留空 - 就像這樣?

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'); 

在此先感謝。

回答

1

爲了獲得自動遞增列它會是這樣的

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

您可以使用簡寫形式語法,如果你願意,我就像任何限制是出於對我的SQL的方式。 身份函數中的參數是開始值和增量,所以如果你真的很奇怪,你就開始它,然後增加13。 :)

你再用

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

這確實是工作插入 - 沒有時間之前對此發表評論,但非常感謝:-) –

0

我猜想,答案是相同的方式爲列不提供值就好像它是一個標識列 - 因爲它會自動遞增生成值:

INSERT INTO studentDetails (stu_name, stu_course, stu_fees) 
VALUES ('James', 'DACLV6', '$2000'); 
相關問題