Use SQL to change the data type of a table's columns and add new columns and constraints

Source: Internet
Author: User

Use SQL to change the data type of a table's columns and add new columns and Constraints

Modify a column of a database table or add a column

--Add a column

ALTER table name ADD column name VARCHAR (+) NULL

--Delete a column

ALTER table table name drop column name

--Modify a column

ALTER TABLE name ALTER COLUMN name VARCHAR (+) NULL

--Modify the type of a column

ALTER TABLE name ALTER COLUMN name VARCHAR (40)-

-Add a PRIMARY KEY constraint

ALTER TABLE name ADD CONSTRAINT constraint name primary key (column name)

--Add UNIQUE constraints

ALTER TABLE name ADD CONSTRAINT constraint name unique (column name)

--Add a DEFAULT constraint

ALTER TABLE name ADD CONSTRAINT constraint name default (content) for column name

--Add a CHECK constraint

ALTER TABLE name ADD constraint constraint name check (content)

--Add a FOREIGN KEY constraint

ALTER TABLE name ADD CONSTRAINT constraint name foreign key (column name) references another table name (column name)

--Delete Constraint

ALTER TABLE name DROP CONSTRAINT constraint name

--vertical connection of two tables

Select *from stuinfo unionselect *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 '

Use SQL to change the data type of a table's columns and add new columns and constraints

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.