SQL > ALTER TABLE
Once a table is created in the database, there are many occasions where one may wish to change the structure of the table. In general, the SQL syntax for ALTER TABLE is,
ALTER TABLE "table_name"
[alter specification];
[alter specification];
[alter specification] is dependent on the type of alteration we wish to perform. We list a number of common changes below:
- Add Column
- Modify Column
- Rename Column
- Drop Column
- Add Index
- Drop Index
- Add Constraint
- Drop Constraint
For all cases, examples are provided for MySQL, Oracle, and SQL Server.
SQL > SQL ALTER TABLE > Add Column SyntaxSometimes we wish to add a column to a table. This can be achieved in SQL. To do this, we specify that we want to change the table structure via the ALTER TABLE command, followed by the ADD command to tell the RDBMS that we want to add a column. The SQL syntax for ALTER TABLE Add Column is,
ALTER TABLE "table_name"
Let's look at the example. Assuming our starting point is the Customer table created in theCREATE TABLE section:ADD "column_name" "Data Type"; Table Customer
MySQL:
ALTER TABLE Customer ADD Gender char(1);
Oracle:
ALTER TABLE Customer ADD Gender char(1);
SQL Server:
ALTER TABLE Customer ADD Gender char(1);
The resulting table structure is:Table Customer
It is also possible to add multiple columns. For example, if we want to add a column called "Email" and another column called "Telephone", we will type the following: MySQL:
ALTER TABLE Customer ADD (Email char(30), Telephone char(20) );
Oracle:
ALTER TABLE Customer ADD (Email char(30), Telephone char(20) );
SQL Server:
ALTER TABLE Customer ADD (Email char(30), Telephone char(20) );
The table now becomes:Table Customer
SQL > SQL ALTER TABLE > Modify Column SyntaxSometimes 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"
For SQL Server, the syntax is,MODIFY "column_name" "New Data Type";
ALTER TABLE "table_name"
Let's look at the example. Assuming our starting point is the Customer table created in theCREATE TABLE section:ALTER COLUMN "column_name" "New Data Type"; Table Customer
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
In MySQL, the syntax for ALTER TABLE Drop Column is,
ALTER TABLE "table_name"
In Oracle and SQL Server, the syntax for ALTER TABLE Drop Column is,DROP "column_name";
ALTER TABLE "table_name"
Let's look at the example. Assuming our starting point is the Customer table created in theCREATE TABLE section:DROP COLUMN "column_name"; Table Customer
MySQL:
ALTER TABLE Customer DROP Birth_Date;
SQL Server:
ALTER TABLE Customer DROP COLUMN Birth_Date;
Oracle:
ALTER TABLE Customer DROP COLUMN Birth_Date;
The resulting table structure is:Table Customer
SQL > ALTER TABLE > Add Index SyntaxSometimes after a table has been created in a database, we find that it is advantageous to add an index to that table to speed up queries involving this table (to understand what an index is and how it can help speed up SQL queries, please see the INDEX section). To do this in SQL, we specify that we want to change the table structure via the ALTER TABLE command, followed by the ADD INDEX command to tell the RDBMS that we want to add an index. The syntax for adding an index to an existing table is,
ALTER TABLE "table_name"
Let's look at the example. Assuming our starting point is the Customer table created in theCREATE TABLE section:ADD INDEX "index_name" (column_name); Table Customer
ALTER TABLE Customer ADD INDEX IDX_COUNTRY (Country);
Please note that using ALTER TABLE to add an index is supported in MySQL but not in Oracle or SQL Server.SQL > ALTER TABLE > Drop Index Syntax Sometimes we may decide that a particular index is no longer needed for a table. In those cases, that index should be removed to free up storage. To drop an index in SQL, we specify that we want to change the table structure via the ALTER TABLE command, followed by theDROP INDEX command. The SQL syntax to drop an index from a table is,
ALTER TABLE "table_name"
Let's look at the example. Assuming our starting point is the Customer table created in theCREATE TABLE section:DROP INDEX "index_name"; Table Customer
ALTER TABLE Customer DROP INDEX IDX_COUNTRY;
Please note that using ALTER TABLE to drop an index in supported in MySQL but not in Oracle or SQL Server.SQL > ALTER TABLE > Add Constraint Syntax Sometimes we may decide to add a new constraint to an existing table (to see what are the different types of constraints that can be placed on a database table, please refer to theCONSTRAINT section). The syntax for adding a constraint in SQL is,
ALTER TABLE "table_name"
Let's look at the example. Assuming our starting point is the Customer table created in theCREATE TABLE section:ADD [CONSTRAINT_NAME] [CONSTRAINT_TYPE] [CONSTRAINT_CONDITION]; Table Customer
MySQL:
ALTER TABLE Customer ADD CONSTRAINT Con_First UNIQUE (Address);
Oracle:
ALTER TABLE Customer ADD CONSTRAINT Con_First UNIQUE (Address);
SQL Server:
ALTER TABLE Customer ADD CONSTRAINT Con_First UNIQUE (Address);
where Con_First is the name of the constraint.SQL > ALTER TABLE > Drop Constraint Syntax Constraints can be placed on a table to limit the type of data that can go into a table. Since we can specify constraints on a table, there needs to be a way to remove this constraint as well. In SQL, this is done via the ALTER TABLE statement. The SQL syntax to remove a constraint from a table is,
ALTER TABLE "table_name"
Let's look at the example. Assuming our starting point is the Customer table created in theCREATE TABLE section:DROP [CONSTRAINT|INDEX] "CONSTRAINT_NAME"; Table Customer
MySQL:
ALTER TABLE Customer DROP INDEX Con_First;
Note that MySQL uses DROP INDEX for index-type constraints such as UNIQUE.Oracle:
ALTER TABLE Customer DROP CONSTRAINT Con_First;
SQL Server:
ALTER TABLE Customer DROP CONSTRAINT Con_First;
|
No comments:
Post a Comment