|
Sometimes we need to change the data type of a column. To do this, we use the ALTER TABLE Modify Column command. For Oracle and MySQL, the SQL syntax for ALTER TABLE Modify Column is,
ALTER TABLE "table_name"
MODIFY "column 1" "New Data Type"
For SQL Server, the syntax is,
ALTER TABLE "table_name"
ALTER COLUMN "column 1" "New Data Type"
Let's look at the example. Assuming our starting point is the "customer" table created in the CREATE TABLE section:
Table customer
| Column Name | Data Type |
| First_Name | char(50) |
| Last_Name | char(50) |
| Address | char(50) |
| City | char(50) |
| Country | char(25) |
| Birth_Date | date |
Our goal is to alter the data type of the "Address" column to char(100). To do this, we key in:
MySQL:
ALTER TABLE customer MODIFY Address char(100);
Oracle:
ALTER TABLE customer MODIFY Address char(100);
SQL Server:
ALTER TABLE customer ALTER COLUMN Address char(100);
Resulting table structure:
Table customer
| Column Name | Data Type |
| First_Name | char(50) |
| Last_Name | char(50) |
| Address | char(100) |
| City | char(50) |
| Country | char(25) |
ALTER TABLE RENAME COLUMN >>
Link to this page: If you find this page useful, we encourage you to link to this page. Simply copy and paste the code below to your website, blog, or profile.
Copyright 2010 1keydata.com. All Rights Reserved. Privacy Policy
|