SQL Server 12 general operations on tables

Source: Internet
Author: User

01.

Create

CREATE TABLE Strings (ID int);
Go

02.

Add columns to a table

ALTER TABLE Strings
Add String nvarchar (32);
Go

03.

Adding calculated columns

ALTER TABLE Strings
Add ID2 as (id+1);
Go---See there's no need to specify type here.

04.

Modify Definition

ALTER TABLE Strings
ALTER column ID int NOT null;
Go---Drop and add for computed columns first

05.

Delete Column

ALTER TABLE Strings
Drop column ID2;
Go---Delete when you want to add column do not column because according to the added content can see what is added.

06.

Add a primary key to a table

ALTER TABLE Strings
Add constraint pk_id primary key (ID);
Go

07.

Add foreign key to table

ALTER TABLE Strings
Add Constraint Fk_a
Foreign KEY (String) references T (S);
Go---CREATE table T (S nvarchar (+) NOT null primary key);

ALTER TABLE Strings
Add Constraint Fk_a
Foreign KEY (String) references T (S) on the DELETE cascade on UPDATE cascade;
Go---No action, cascade,set null,set default

08, for the table plus uniqu constraints

ALTER TABLE Strings
Add constraint unique_a unique (String);
Go

09.

To add a check constraint to a table

ALTER TABLE Strings
Add Constraint Ck_a
Check (String! = ' 007 ');
Go

10.

Add a DEFAULT constraint to a table

ALTER TABLE Strings
Add Constraint df_a
Default ' 1234656 ' for String;
Go

11.

disabling constraints

ALTER TABLE Strings
Nocheck constraint fk_a;
Go

ALTER TABLE Strings

Nocheck constraint all; ---Disable all constraints

ALTER TABLE Strings

Check constraint all; ---enable all constraints

12.

Delete Constraint

ALTER TABLE Strings
Drop constraint fk_a;
Go

SQL Server 12 general operations on tables

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.