SQL Server alter statement

Source: Internet
Author: User

When modifying the SQL Server table structure, we often use the alter statement to list some commonly used alter statements as follows.

1: add fields to the table

Alter table [Table name] add [column name] Type

2: delete a field

Alter table [Table name] Drop column [column name]

3: Modify the field type in the table (you can modify the column type, whether it is null)

Alter table [Table name] alter column [column name] Type

4: Add a primary key

Alter table [Table name] add constraint [constraint name] primary key ([column name])

5. Add a unique constraint

Alter table [Table name] add constraint [constraint name] unique ([column name])

6: add the default value of a column in the table

Alter table [Table name] add constraint [constraint name] default (default) for [column name]
7. Add Constraints

Alter table [Table name] add constraint [constraint name] Check (content)

8: Add a foreign key constraint

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

9: delete Constraints

Alter table [Table name] Drop constraint [constraint name]

10: rename a table

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

11. Rename the column name.

Exec sp_rename '[Table name]. [column name]', '[Table name]. [new column name]'

 

Create comments (N 'user', N 'dbo', N 'table' is a fixed Statement)

12: Add description information for the table
Execute sp_addextendedproperty n 'Ms _ description', 'personnel info table ', N 'user', N 'dbo', N 'table', N 'table name', null, null

13: Add description information for the field Username
Execute sp_addextendedproperty n 'Ms _ description', 'name', N 'user', N 'dbo', N 'table', N 'table name', N 'column ', N 'username'

14: Add description information for the sex field
Execute sp_addextendedproperty n 'Ms _ description', 'gender', N 'user', N 'dbo', N 'table', N 'table name', N 'column ', N 'sex'

15: update the description attribute of the username column in the table:
Exec sp_updateextendedproperty 'Ms _ description', 'new name', 'user', DBO, 'table', 'table name', 'column', 'username'

16: Delete the description attribute of the username column in the table:
Exec sp_dropextendedproperty 'Ms _ description', 'user', DBO, 'table', 'table name', 'column ', 'username'

 

 

 

 

 

 

 

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.