DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL ALTER TABLE Statement


The ALTER TABLE Statement

The ALTER TABLE proclamation is utilized to include, erase, or adjust segments in a current table.

SQL ALTER TABLE Syntax

To include a section in a table, utilize the accompanying syntax:

Modify TABLE table_name
Include column_name datatype

To erase a section in a table, utilize the accompanying grammar (see that a few database frameworks don't permit erasing a column):

Modify TABLE table_name
DROP COLUMN column_name

To change the information sort of a segment in a table, utilize the accompanying syntax:

SQL Server/MS Access:

Change TABLE table_name
Change COLUMN column_name datatype

My SQL/Oracle (earlier form 10G):

Adjust TABLE table_name
Adjust COLUMN column_name datatype

Oracle 10G and later:

Adjust TABLE table_name
Adjust column_name datatype

SQL ALTER TABLE Example

Look at the "Persons" table:

Id LastName FirstName City
1 Ray Barnett OK
2 Tara Allen NY
3 Austin McCollum CT

Now we need to include a segment named "DateOfBirth" in the "Persons" table.

We utilize the accompanying SQL statement:

Change TABLE Persons
Include DateOfBirth date
The accompanying SQL code includes another section named DateOfBirth:
 

Change TABLE People ADD DateOfBirth date; 

The "Persons" table will currently look like this:

Id LastName FirstName City DateOfBirth
1 Ray Barnett OK  
2 Tara Allen NY  
3 Austin McCollum CT  

Change Data Type Example

Now we need to change the information sort of the segment named "DateOfBirth" in the "Persons" table.

We utilize the accompanying SQL statement:

Adjust TABLE Persons
Adjust COLUMN DateOfBirth year

Notice that the "DateOfBirth" segment is presently of sort year and is going to hold a year in a two-digit or four-digit format.


DROP COLUMN Example

Next, we need to erase the section named "DateOfBirth" in the "Persons" table.

We utilize the accompanying SQL statement:

Modify TABLE Persons
DROP COLUMN DateOfBirth

The "Persons" table will currently look like this:

Id LastName FirstName City
1 Ray Barnett OK
2 Tara Allen NY
3 Austin McCollum CT