在access中新增自動編號欄位

有關資料庫的討論, 都可以在這發表哦~~
回覆文章
頭像
tim
文章: 1380
註冊時間: 2008年 11月 26日, 00:49

在access中新增自動編號欄位

文章 tim »

如何在 ACCESS 中新增自動編號欄位呢...

在 MS-SQL 中使用的是
Alter Table t1 Add Column c1 int identity

在 Access 中則不行, Access 不認識 identity (雖然也有 @@identity 變數可用), 但是自動編號的欄位新增並不是用 int 附加 identity property 上去的, 可以用下面兩種語法來進行:

Alter Table t1 Add Column c1 counter
Alter Table t1 Add Column c1 autoincrement

用這兩種方式才能正常的新增自動編號欄位 (AutoNumber)

相關連結:

http://msdn.microsoft.com/library/defau ... intsql.asp

注意... identity 在 ACCESS 是不行的
如:
alter table t1 add column tt8 identity(1,1)
alter table t1 add column tt8 identity
CREATE TABLE tblCodeCounterDataTypes(
Field1_IDENTITY IDENTITY(10,5),
Field2 TEXT(10))
都是不能在 access 下使用的.

此篇說明在 delphi 程式中可以使用 identity 但是在 access 中不可使用 identity, 僅能使用 counter 或 autoincrement.
http://forum.vclxx.org/topic.asp?TOPIC_ ... e=Database

代碼: 選擇全部

    在 delphi 中可以使用的方式: 
    CREATE TABLE AutoNumberFieldTest 
    ( 
       fld1 varchar(5), 
       fld2 integer IDENTITY 
    ) 
    也可以使用: 
    CREATE TABLE AutoNumberFieldTest 
    ( 
       fld1 varchar(5), 
       fld2 counter 
    ) 
    或 
    CREATE TABLE AutoNumberFieldTest 
    ( 
       fld1 varchar(5), 
       fld2 autoincrement 
    ) 
    但在 access 中就只能使用後兩者了. 

此篇為 alter 的語法
http://delphi.ktop.com.tw/TOPIC.ASP?TOPIC_ID=28662
ps. 在 delphi 中可用
alter TABLE test add column tt int identity
的語法.

所以在 delphi 中可以使用 identity, 但在 access 中是不可以的, 只能使用 counter 或是 autoincrement.
多多留言, 整理文章, 把經驗累積下來.....
回覆文章