How To Delete a Column in an Existing Table?
Page 1 of 1
How To Delete a Column in an Existing Table?
If you have an existing column in a table and you need that column any more, you can delete it
with ALTER TABLE ... DROP COLUMN statement. Here is an example SQL script:
SQL> CREATE TABLE emp_dept_90
2 AS SELECT * FROM employees WHERE department_id=90;
Table created.
SQL> SELECT last_name FROM emp_dept_90;
LAST_NAME
-------------------------
King
Kochhar
De Haan
SQL> ALTER TABLE emp_dept_90 DROP COLUMN last_name;
Table altered.
SQL> SELECT last_name FROM emp_dept_90;
ERROR at line 1:
ORA-00904: "LAST_NAME": invalid identifier
As you can see the column "last_name" is gone.
How To Drop an Existing Table?
If you want to delete an existing table and its data rows, you can use the DROP TABLE statement
as shown in this script:
SQL> connect HR/fyicenter
Connected.
SQL> CREATE TABLE emp_dept_10
2 AS SELECT * FROM employees WHERE department_id=10;
Table created.
SQL> DROP TABLE emp_dept_10;
Table dropped.
Be careful, when you use the DROP TABLE statement. All data rows are gone too.
with ALTER TABLE ... DROP COLUMN statement. Here is an example SQL script:
SQL> CREATE TABLE emp_dept_90
2 AS SELECT * FROM employees WHERE department_id=90;
Table created.
SQL> SELECT last_name FROM emp_dept_90;
LAST_NAME
-------------------------
King
Kochhar
De Haan
SQL> ALTER TABLE emp_dept_90 DROP COLUMN last_name;
Table altered.
SQL> SELECT last_name FROM emp_dept_90;
ERROR at line 1:
ORA-00904: "LAST_NAME": invalid identifier
As you can see the column "last_name" is gone.
How To Drop an Existing Table?
If you want to delete an existing table and its data rows, you can use the DROP TABLE statement
as shown in this script:
SQL> connect HR/fyicenter
Connected.
SQL> CREATE TABLE emp_dept_10
2 AS SELECT * FROM employees WHERE department_id=10;
Table created.
SQL> DROP TABLE emp_dept_10;
Table dropped.
Be careful, when you use the DROP TABLE statement. All data rows are gone too.
Similar topics
» How To Delete a Column in an Existing Table?
» How To Add a New Column to an Existing Table?
» How To Add a New Column to an Existing Table?
» How To Add a New Column to an Existing Table with a Default Value?
» How To Create a New Table by Selecting Rows from Another Table?
» How To Add a New Column to an Existing Table?
» How To Add a New Column to an Existing Table?
» How To Add a New Column to an Existing Table with a Default Value?
» How To Create a New Table by Selecting Rows from Another Table?
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Sun Jan 31, 2010 12:35 am by Micheal
» Accessing tables of other users in SQL
Sun Jan 31, 2010 12:34 am by Micheal
» Grouping commands in SQL
Sun Jan 31, 2010 12:32 am by Micheal
» Operations on Result Sets in SQL
Sun Jan 31, 2010 12:31 am by Micheal
» Subqueries in SQL
Sun Jan 31, 2010 12:30 am by Micheal
» Joining Relations in SQL
Sun Jan 31, 2010 12:28 am by Micheal
» Delete comand in SQL
Sun Jan 31, 2010 12:27 am by Micheal
» Update command in SQL
Sun Jan 31, 2010 12:26 am by Micheal
» Insert command in SQL
Sun Jan 31, 2010 12:23 am by Micheal