Receive all updates via Facebook. Just Click the Like Button Below

Blogger Widgets
/

Watch Intro About My Website ! ! !

Monday, 4 November 2013


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


The DROP TABLE statement is used to delete a table.


DROP TABLE table_name

The DROP DATABASE Statement


The DROP DATABASE statement is used to delete a database.


DROP DATABASE database_name


The TRUNCATE TABLE Statement


What if we only want to delete the data inside the table, and not the table itself?

Then, use the TRUNCATE TABLE statement:


TRUNCATE TABLE table_name

The ALTER TABLE Statement


The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.

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


Look at the "Persons" table:


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


Now we want to change the data type of the column named "DateOfBirth" in the "Persons" table.

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


Next, we want to delete the column named "DateOfBirth" in the "Persons" table.

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

Tagged:

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...