Indexes, tables, and
databases can easily be deleted/removed with the DROP statement.
The DROP INDEX Statement
The DROP INDEX statement is used to delete an index in a table.
DROP INDEX Syntax for MS Access:
DROP INDEX index_name ON table_name
|
DROP INDEX Syntax for MS SQL Server:
DROP INDEX table_name.index_name
|
DROP INDEX Syntax for DB2/Oracle:
DROP INDEX index_name
|
DROP INDEX Syntax for MySQL:
ALTER TABLE table_name DROP INDEX
index_name
|
The DROP TABLE Statement
DROP TABLE table_name
|
The DROP DATABASE Statement
DROP DATABASE database_name
|
The TRUNCATE TABLE Statement
Then, use the TRUNCATE TABLE statement:
TRUNCATE TABLE table_name
|
The ALTER TABLE Statement
SQL ALTER TABLE Syntax
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype |
To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column)
ALTER TABLE table_name
DROP COLUMN column_name |
To change the data type of a column in a table, use the following syntax:
ALTER TABLE table_name
ALTER COLUMN column_name datatype |
SQL ALTER TABLE Example
Id
|
LastName
|
FirstName
|
Address
|
City
|
1
|
Navis
|
Anto
|
Madras
|
TVL
|
2
|
Ji
|
Bala
|
Bombay
|
TVL
|
3
|
Christopher
|
Franklin
|
America
|
KK
|
Now we want to add a column named "DateOfBirth" in the "Persons" table.
We use the following SQL statement:
ALTER TABLE Persons
ADD DateOfBirth date |
Notice that the new column, "DateOfBirth", is of type date and is going to hold a date. The data type specifies what type of data the column can hold.
The "Persons" table will now like this:
Id
|
LastName
|
FirstName
|
Address
|
City
|
DateOfBirth
|
1
|
Navis
|
Anto
|
Madras
|
TVL
|
|
2
|
Ji
|
Bala
|
Bombay
|
TVL
|
|
3
|
Christopher
|
Franklin
|
America
|
KK
|
Change Data Type Example
We use the following SQL statement:
ALTER TABLE Persons
ALTER COLUMN DateOfBirth year |
Notice that the "DateOfBirth" column is now of type year and is going to hold a year in a two-digit or four-digit format.
DROP COLUMN Example
We use the following SQL statement:
ALTER TABLE Persons
DROP COLUMN DateOfBirth |
The "Persons" table will now like this:
Id
|
LastName
|
FirstName
|
Address
|
City
|
1
|
Navis
|
Anto
|
Madras
|
TVL
|
2
|
Ji
|
Bala
|
Bombay
|
TVL
|
3
|
Christopher
|
Franklin
|
America
|
KK
|
0 comments:
Post a Comment