What command do you use to make changes to existing data in a table?
ALTER TABLEThe SQL ALTER TABLE command is used to change the structure of an existing table. It helps to add or delete columns, create or destroy indexes, change the type of existing columns, or rename columns or the table itself. Show
It can also be used to change the comment for the table and type of the table. Syntax: ALTER TABLE Parameters:
SQL ALTER TABLE statement to add a column to a tableIn the following topic, we are discussing the SQL ALTER TABLE statement, which adds a column to a table. If not specified otherwise, the column will be added at the end of the table. Sample table: agent1 To add a new column 'email' at the end of the table 'agent1' with field name and data type
the following SQL statement can be used : SQL Code:
Output: To see the modified structure of the said table: SQL Code:
StructureSQL ALTER TABLE statement to drop a columnIn the following example, we are discussing, how a column can be dropped from a table if it exists in the table, using the SQL ALTER TABLE statement. Sample table: agent1 To drop the existing column 'country' from the table 'agent1', the following SQL statement can be used: SQL Code:
Output: To see the modified structure of the said table: SQL Code:
StructureSQL ALTER TABLE statement to change or drop default value of a columnIn the following example, we are going to discuss how to drop the default value of a column from a table if the column exists in the table using SQL ALTER TABLE statement. Example: Sample table: agent1 To modify the existing column 'commission' of 'agent1' table with a default value .05, the following SQL statement can be used: SQL Code:
Output: To see the modified structure of the said table : SQL Code:
StructureSQL ALTER TABLE statement to drop default value of a columnTo drop the existing default value of column 'commission' of 'agent1' table, the following SQL statement can be used: SQL Code:
Output: SQL ALTER TABLE statement to add individual column constraintIn the following example, we are going to discuss about the usage of SQL ALTER TABLE statement to add a constraint for a column and also drop an existing constraint of a column from a table if the column exists in the table. Sample table: agent1 To add a UNIQUE CONSTRAINT named 'dup_che_con' for the the existing column 'agent_code' of 'agent1' table, the following SQL statement can be used: SQL Code:
Output: SQL ALTER TABLE statement to drop individual column constraintTo drop the existing UNIQUE CONSTRAINT 'dup_che_con' from the table 'agent1', the following SQL statement can be used: SQL Code:
Output: SQL ALTER TABLE statement to change size and data type of a columnIn the following example, we are going to discuss about the usage of SQL ALTER TABLE statement to change the size and data type of a column in an existing table, if the column exists in the table. Example: Sample table: agent1 To modify the datatype and size of the column 'country' of 'agent1' table, the following SQL statement can be used: SQL Code:
Output: SQL ALTER TABLE statement to add or drop PRIMARY KEY of a tableIn the following example, we are going to discuss about the usage of SQL ALTER TABLE statement to add and drop primary key of a table. Sample table: agent1 To add a PRIMARY KEY CONSTRAINT named 'pk_ag_code' for the column 'agent_code' of the 'agent1' table, the following SQL statement can be used: SQL Code:
Output: SQL ALTER TABLE statement to drop existing PRIMARY KEY of a tableTo drop the existing PRIMARY KEY CONSTRAINT named 'pk_ag_code' form the 'agent1' table, the following SQL statement can be used: SQL Code:
Output: SQL ALTER TABLE statement to add or drop FOREIGN KEY of a tableIn the following example, we are going to discuss about the usage of SQL ALTER TABLE statement to add and drop foreign key of a table. Sample table: customer1 To add a FOREIGN KEY CONSTRAINT named 'fk_ag_code' for the column 'agent_code' of the 'customer1' table, the following SQL statement can be used: SQL Code:
Output: SQL ALTER TABLE statement to drop existing FOREIGN KEY of a tableTo drop the existing FOREIGN KEY CONSTRAINT named 'fk_ag_code' form the 'customer1' table, the following SQL statement can be used : SQL Code:
Output: SQL ALTER TABLE statement to add CHECK CONSTRAINTIn the following example, we are going to discuss about the usage of SQL ALTER TABLE statement to add and drop CHECK CONSTRAINT of column(s) of a table. Sample table : customer1To add a CHECK CONSTRAINT named 'du_che_con' for the column 'grade' of the 'customer1' table, which checks whether the values of 'grade' are within the range 1 to 3 at the time of inserting rows into the table, the following SQL statement can be used : SQL Code:
Output: SQL ALTER TABLE statement to drop CHECK CONSTRAINTTo drop the existing CHECK CONSTRAINT named 'du_che_con' form the 'customer1' table, the following SQL statement can be used : SQL Code:
Output: SQL ALTER TABLE statement to change PRIMARY KEY CONSTRAINTIn the following example, we are going to discuss about the usage of SQL ALTER TABLE statement to modify the PRIMARY KEY and FOREIGN KEY constraint. To modify a PRIMARY KEY and FOREIGN KEY constraint, firstly it is needed to remove the existing PRIMARY KEY and FOREIGN KEY constraint and then re-create it. Sample table : agent1 Suppose there is a PRIMARY KEY CONSTRAINT named 'pk_ag_code' for the column 'agent_code' of the 'agent1' table. To modify the PRIMARY KEY CONSTRAINT named 'pk_ag_code, the following SQL statements can be used : SQL Code:
SQL Code:
SQL ALTER TABLE statement to change FOREIGN KEY CONSTRAINTSample table: customer1 Suppose, there is a FOREIGN KEY CONSTRAINT named 'fk_ag_code' for the column 'agent_code' of the 'customer1' table To modify the FOREIGN KEY CONSTRAINT named 'fk_ag_code', the following SQL statements can be used : SQL Code:
SQL Code:
Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition. Practice SQL Exercises
Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus. Previous:
Constraints What command changes data in a table?The SQL UPDATE query is used to modify the existing records in a table.
Which command is used to UPDATE existing data of a record within a table?The UPDATE statement is used to modify the existing records in a table.
Which command is used to modify the existing data?Answer: The UPDATE command is used to modify the records of a table.
|