SQL Server ALTER statement

Source: Internet
Author: User

1: Add a field to the table

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

2: Delete Field

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

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

ALTER TABLE [table name] ALTER COLUMN [column name] Type

4: Add primary key

Alter table [table name] add constraint [constraint name] primary KEY ([column name])

5: Add UNIQUE Constraint

Alter table [table name] add constraint [constraint name] Unique ([column name])

6: Add default value for a column in a table

Alter table [table name] add constraint [constraint name] Default (defaults) for [column name]
7: Add constraint

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 Constraint

Alter table [table name] drop constraint [constraint name]

10: Renaming 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] '

SQL Server ALTER statement

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.