How to obtain an automatic number field when designing a table in SQL Server 2000

Source: Internet
Author: User
Tags filegroup

The procedure is as follows:
① Automatically numbered field in Access
Right-click your table --> design table --> Find your id field (int type) --> id --> yes --> id seed (initial value) --> incremental id --> OK
② Use the IDENTITY (SEED, INCREMENT) parameter
Seed-start value
Increment-increment
Create table Name (
Your id identity (1, 1) not null, your other fields ...)
Create table Name (
Your field id autoincrement (), other fields ...)
③ Modify the start value and step value
Alter table name alter column your field id counter (2000,50)
④ Let the field's start value be automatically added to a deleted table and start from 1 again
Alter table name alter column your field id counter (1, 1)
The preceding 3 and 4 are applicable only to Access. COUNTER is a data type. You can specify a field not automatically numbered as an automatically numbered field in Access, you can also set an automatic number field to be automatically numbered from the specified value according to the specified step size. However, if the table contains data, you cannot use this statement to change the data type of this column to COUNTER. SQL Server is not supported.
For SQL Server, we may always want to use the Alter Table name Alter Column. Your field IDENTITY)
To specify the field to start counting again from 1, but this sentence itself is incorrect. For a long time, I wonder why this sentence cannot be executed. If we look at the definition of the Alter Table statement in MS, this statement is completely incorrect. The following is the definition of the Alter Table statement by MS.
Alter table table
{[Alter column column_name
{New_data_type [(precision [, scale])]
[COLLATE <collation_name>]
[NULL │ not null]
│ {ADD │ DROP} ROWGUIDCOL}
]
│ ADD
{[<Column_definition>]
│ Column_name AS computed_column_expression
} [,... N]
│ [With check │ with nocheck] ADD
{<Table_constraint>} [,... n]
│ DROP
{[CONSTRAINT] constraint_name
│ COLUMN column} [,... n]
│ {CHECK │ NOCHECK} CONSTRAINT
{ALL │ constraint_name [,... n]}
│ {ENABLE │ DISABLE} TRIGGER
{ALL │ trigger_name [,... n]}
}
<Column_definition >::=
{Column_name data_type}
[[DEFAULT constant_expression] [with values]
│ [IDENTITY [(seed, increment) [not for replication]
]
[ROWGUIDCOL]
[COLLATE <collation_name>]
[<Column_constraint>] [... n]
<Column_constraint >:: =
[CONSTRAINT constraint_name]
{[NULL │ not null]
│ [{Primary key │ UNIQUE}
[CLUSTERED │ NONCLUSTERED]
[With fillfactor = fillfactor]
[ON {filegroup │ DEFAULT}]
]
│ [[Foreign key]
REFERENCES ref_table [(ref_column)]
[On delete {CASCADE │ no action}]
[On update {CASCADE │ no action}]
[Not for replication]
]
│ CHECK [not for replication]
(Logical_expression)
}
<Table_constraint >:: =
[CONSTRAINT constraint_name]
{[{Primary key │ UNIQUE}
[CLUSTERED │ NONCLUSTERED]
{(Column [,... n])}
[With fillfactor = fillfactor]
[ON {filegroup │ DEFAULT}]
]
│ FOREIGN KEY
[(Column [,... n])]
REFERENCES ref_table [(ref_column [,... n])]
[On delete {CASCADE │ no action}]
[On update {CASCADE │ no action}]
[Not for replication]
│ DEFAULT constant_expression
[FOR column] [with values]
│ CHECK [not for replication]
(Search_conditions)
}
We can see that IDENTITY is only in <column_definition>, that is, we can use
Alter Table name Add field name Int IDENTITY (1, 1)
That is, we can add a field and specify it as an automatically numbered field. However, you cannot change a field to an automatically numbered field (or perhaps I have not found a method ). That is, if we want to add an automatic number field to a table, we can only use the method of adding a field, but cannot change an existing field to an automatic number field.
If you need to change the start value of the automatic number field count, you can use the DBCC command:
Dbcc checkident (table, RESEED, 100)
The value starts from 101 next to the automatically numbered field.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.