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