SQL-W3School- High: SQL ALTER TABLE statement

ylbtech-SQL-W3School- High: SQL ALTER TABLE statement

 

1. Back to top
1、

ALTER TABLE statement

ALTER TABLE statement is used to add an existing table, modify, or delete columns .

SQL ALTER TABLE Syntax

To add columns in a table, use the following syntax:

ALTER TABLE table_name
ADD column_name datatype

To delete a column in a table, use the following syntax:

ALTER TABLE table_name 
DROP COLUMN column_name

Note: Some database systems do not allow this to delete columns in a database table mode (DROP COLUMN column_name).

To change the data type of columns in the table, use the following syntax:

ALTER TABLE table_name
ALTER COLUMN column_name datatype

The original table (used in the example):

Persons table:

Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing

SQL ALTER TABLE example

Now we want to add a new column called "Birthday" in the table "Persons" in.

We use the following SQL statement:

ALTER TABLE Persons
ADD Birthday date

Note that the new column "Birthday" is the type of date, the date can be stored. Data type column specifies the type of data can be stored.

The new "Persons" table like this:

Id LastName FirstName Address City Birthday
1 Adams John Oxford Street London  
2 Bush George Fifth Avenue New York  
3 Carter Thomas Changan Street Beijing  

Changing the data type instance

Now we want to change the "Persons" table "Birthday" column data type.

We use the following SQL statement:

ALTER TABLE Persons
ALTER COLUMN Birthday year

Note that, "Birthday" data type column is year, can store two or four year format.

Examples of DROP COLUMN

Next, we remove the "Person" table "Birthday" column:

ALTER TABLE Person
DROP COLUMN Birthday

Persons table will be like this:

Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing
2、
2. Return to top
 
3. Back to top
 
4. Top
 
5. Top
1、
2、
 
6. Back to top
 
warn Author: ylbtech
Source: http://ylbtech.cnblogs.com/
This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise reserves the right to pursue legal responsibilities.

Guess you like

Origin www.cnblogs.com/storebook/p/11827291.html