SQL Server Modifying Table Structure

The keyword modified in SQL Server is Alter (change; make) change; Modify (clothes to fit better); Change).

Column operation

Add Column

Add Column Operation.

alter tabel table name add column name data type 
 --add an email to the employee table alter the translation is (changed) 
alter table people add PeopleMail varchar(200);

After successful operation, an additional column named PeopleMail can be seen in the People table

Delete Column

alter table table name drop column column names 
 --delete the newly added mailbox column 
alter table People drop column PeopleMail

Modify the data type of a column

alter table table name alter column column name data type 
 --modify PeopleAddress of varchar the length has changed from 300 to 200 
 --modify table People modify columns PeopleAddress
alter table people alter column PeopleAddress varchar(200)

After modification

Modify column names

alter table table name rename column column names to new column name 
alter table people rename column PeopleMail to QQemil

If there is a grammar error prompt, I don't know why. Good friends who know can tell everyone in the comments

Constraint operation

Delete constraint

alter table table name drop constraint constraint name 

If the constraint name was set when adding columns before, use the
previously set
If not, you can find the constraint name of the field you want to modify by following these steps
Right click table - click on design Then this interface will appear
Right click on the check constraint in the blank space to check the constraint This is the constraint name
(It seems like it's not bad to manually delete it here).

alter table people drop constraint CK__people__PeopleSa__4316F928

Add (expression) constraints

alter table table name add constraint constraint name check (expression) 
 --just removed the constraint on salary and added it here 
alter table people add constraint CK__people__PeopleSa1 check(PeopleSalary >=1000 and PeopleSalary <=10000000);

After adding it, you can also see the expression and name when checking its constraints on the page just now

Add (primary key) constraint

alter table table name add constraint constraint name primary key (column name) 

Add (unique) constraint

alter table table name add constraint constraint name unique (column name) 

Add (default) constraint

alter table table name add constraint constraint name default default value for (column name) 

Add (foreign key) constraints

alter table table name add constraint constraint name foreign key (column name) references associated table name (column name (primary key))