sql server how to achieve auto-increment field? Common Operations

Methods as below:

E.g:

create table student(

Sno int identity(1,1) primary key,

Sname varchar(100)

)

Sno field so that we can achieve self-growth, the first parameter is the identity seed (that is, the start value), and the second parameter is the identification value (how many increments)

Here we begin to 1, increments of 1

If you do insert, then:

insert into student values ​​( 'John Doe')

Note values ​​which do not write from value-added field name, because the database will automatically help you generate.

1 example, the first behavior Xiaoming

insert into student values ​​( 'John Doe')

Xiao Ming to 1

John Doe 2

Extended Information:

SQL Server: Modify field properties summary

1: add a field to the table

Alter table [table] the Add [Column Name] Type

2: Remove fields

Alter table [table name] drop column [Column Name]

3: Modify table field types (column type can be modified, is empty)

Alter table [table] alter column [Column Name] Type

4: Add the primary key

Alter table [table] add constraint [constraint name] primary key ([Column Name])

5: Adding a unique constraint

Alter table [table] add constraint [constraint name] UNIQUE ([Column Name])

6: Add a list of default values

Alter table [table] add constraint [constraint name] default (default value) for [Column Name]

7: Adding constraints

Alter table [table] add constraint [constraint name] Check (Content)

8: add foreign key constraint

Alter table [table] add constraint [constraint name] foreign key (column names) referencese another table (column names)

9: Remove the constraint

Alter table [table] add constraint [constraint name]

10: rename table

exec sp_rename '[original table name]', '[new table name]'

11: Rename the column name

exec sp_rename '[table]. [column name]', '[table name]. [new column name]'

Guess you like

Origin www.cnblogs.com/lydg/p/11363019.html