Common SQL Server commands

Source: Internet
Author: User
Tags how to use sql

-- How to use SQL to change the data type of columns in a table and add new columns and constraints

-- Add a column
Alter table table name Add column name varchar (20) null
-- Delete a column
Alter table Table Name drop column name
-- Modify a column
Alter table table name alter column name varchar (40) null
-- Modify the type of a column
Alter table table name alter column name varchar (40)

-- Add primary key constraints
Alter table table name
Add constraint name primary key (column name)

-- Add a unique constraint
Alter table table name
Add constraint name unique (column name)

-- Add default Constraints
Alter table table name
Add constraint name default (content) for column name

-- Add check Constraints
Alter table table name
Add constraint name check (content)

-- Add a foreign key constraint
Alter table table name
Add constraint name foreign key (column name) references another table name (column name)

-- Delete Constraints
Alter table table name
Drop constraint name

-- Vertically join two tables
Select * From stuinfo Union
Select * From stumarks

-- Rename, table name
Exec sp_rename 'original table name', 'change table name'
-- Rename, column name
Exec sp_rename 'table name. Original column name', 'changed column name', 'column'

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.