SQL DROP TABLE and TRUNCATE TABLE Keywords

DROP TABLE

The DROP TABLE command deletes a table in the database.

The following SQL deletes the table “Shippers”:

Example

DROP TABLE Shippers;

Note: Be careful before deleting a table. Deleting a table results in loss of all information stored in the table!

Continue reading SQL DROP TABLE and TRUNCATE TABLE Keywords

SQL DROP INDEX Keyword

DROP INDEX

The DROP INDEX command is used to delete an index in a table.

MS Access:

DROP INDEX index_name ON table_name;

SQL Server:

DROP INDEX table_name.index_name;

Continue reading SQL DROP INDEX Keyword

SQL DROP DEFAULT Keyword

DROP DEFAULT

The DROP DEFAULT command is used to delete a DEFAULT constraint.

To drop a DEFAULT constraint, use the following SQL:

SQL Server / Oracle / MS Access:

ALTER TABLE Persons
ALTER COLUMN City DROP DEFAULT;

Continue reading SQL DROP DEFAULT Keyword

SQL DROP DATABASE Keyword

DROP DATABASE

The DROP DATABASE command is used to delete an existing SQL database.

The following SQL drops a database named “testDB”:

Example

DROP DATABASE testDB;

Note: Be careful before dropping a database. Deleting a database will result in loss of complete information stored in the database!

Continue reading SQL DROP DATABASE Keyword

SQL DROP CONSTRAINT Keyword

DROP CONSTRAINT

The DROP CONSTRAINT command is used to delete a UNIQUE, PRIMARY KEY, FOREIGN KEY, or CHECK constraint.


DROP a UNIQUE Constraint

To drop a UNIQUE constraint, use the following SQL :

SQL Server / Oracle / MS Access:

ALTER TABLE Persons
DROP CONSTRAINT UC_Person;

MySQL:

ALTER TABLE Persons
DROP INDEX UC_Person;

Continue reading SQL DROP CONSTRAINT Keyword

SQL DROP COLUMN Keyword

DROP COLUMN

The DROP COLUMN command is used to delete a column in an existing table.

The following SQL deletes the “ContactName” column from the “Customers” table:

Example

ALTER TABLE Customers
DROP COLUMN ContactName;

Continue reading SQL DROP COLUMN Keyword

SQL DROP Keyword

DROP COLUMN

The DROP COLUMN command is used to delete a column in an existing table.

The following SQL deletes the “ContactName” column from the “Customers” table:

Example

ALTER TABLE Customers
DROP COLUMN ContactName;

Continue reading SQL DROP Keyword

SQL DESC Keyword

DESC

The DESC command is used to sort the data returned in descending order.

The following SQL statement selects all the columns from the “Customers” table, sorted descending by the “CustomerName” column : Continue reading SQL DESC Keyword

SQL DELETE Keyword

DELETE

The DELETE command is used to delete existing records in a table.

The following SQL statement deletes the customer “Alfreds Futterkiste” from the “Customers” table:

Example

DELETE FROM Customers WHERE CustomerName=‘Alfreds Futterkiste’;

Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE statement. The WHERE clause specifies which record(s) should be deleted. If you omit the WHERE clause, all records in the table will be deleted!

It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be intact:

The following SQL statement deletes all rows in the “Customers” table, without deleting the table. This means that the table structure, attributes, and indexes will be intact: Continue reading SQL DELETE Keyword