SQL DROP Statements: Deleting Database Objects

SQL provides several DROP commands to remove database objects. These are powerful commands that permanently delete data; always back up your data before using them.



DROP COLUMN

Deleting a Column from a Table

The DROP COLUMN command removes a column from an existing table. The data within the column is lost permanently. (Note: Not all database systems support this command directly; some might require alternative approaches.)

Syntax

ALTER TABLE table_name
DROP COLUMN column_name;
      

Example: Removing the ContactName Column

Syntax

ALTER TABLE Customers
DROP COLUMN ContactName;
      
Output

(The ContactName column and its data are permanently deleted from the Customers table.)
      

Dropping Constraints

Dropping a UNIQUE Constraint

Removes a unique constraint. The syntax varies slightly depending on the database system.

Syntax (SQL Server, Oracle, MS Access)

ALTER TABLE Persons
DROP CONSTRAINT UC_Person;
      
Syntax (MySQL)

ALTER TABLE Persons
DROP INDEX UC_Person;
      
Output

(The unique constraint UC_Person is dropped.)
      

Dropping a PRIMARY KEY Constraint

Removes a primary key constraint.

Syntax (SQL Server, Oracle, MS Access)

ALTER TABLE Persons
DROP CONSTRAINT PK_Person;
      
Syntax (MySQL)

ALTER TABLE Persons
DROP PRIMARY KEY;
      
Output

(The primary key constraint PK_Person is dropped.)
      

Dropping a FOREIGN KEY Constraint

Removes a foreign key constraint.

Syntax (SQL Server, Oracle, MS Access)

ALTER TABLE Orders
DROP CONSTRAINT FK_PersonOrder;
      
Syntax (MySQL)

ALTER TABLE Orders
DROP FOREIGN KEY FK_PersonOrder;
      
Output

(The foreign key constraint FK_PersonOrder is dropped.)
      

Dropping a CHECK Constraint

Removes a check constraint.

Syntax (SQL Server, Oracle, MS Access)

ALTER TABLE Persons
DROP CONSTRAINT CHK_PersonAge;
      
Syntax (MySQL)

ALTER TABLE Persons
DROP CHECK CHK_PersonAge;
      
Output

(The CHECK constraint CHK_PersonAge is dropped.)
      

Dropping a DEFAULT Constraint

Removes a default value constraint.

Syntax (SQL Server, Oracle, MS Access)

ALTER TABLE Persons
ALTER COLUMN City DROP DEFAULT;
      
Syntax (MySQL)

ALTER TABLE Persons
ALTER City DROP DEFAULT;
      
Output

(The default constraint on the City column is dropped.)
      

Dropping an Index

Removes an index from a table.

Syntax (MS Access)

DROP INDEX index_name ON table_name;
      
Syntax (SQL Server)

DROP INDEX table_name.index_name;
      
Syntax (DB2/Oracle)

DROP INDEX index_name;
      
Syntax (MySQL)

ALTER TABLE table_name DROP INDEX index_name;
      
Output

(The specified index is dropped from the table.)
      

Dropping a Database

Deletes an entire database. Use with extreme caution!

Syntax

DROP DATABASE database_name;
      
Output

(The database and all its contents are permanently deleted.)
      

Dropping a Table

Deletes a table and all its data. Use with caution!

Syntax

DROP TABLE table_name;
      
Output

(The table and its data are permanently deleted.)
      

Dropping a View

Deletes a view; the underlying data is unaffected.

Syntax

DROP VIEW view_name;
      
Output

(The view is deleted.)
      

**Important Note:** Always back up your database before running any `DROP` commands to prevent accidental data loss. These actions are typically not easily reversible without a backup. The specific syntax may vary slightly depending on your database system (MySQL, PostgreSQL, SQL Server, etc.). Always refer to your database system's documentation.