8.4. Dropping Tables


To remove a table when you no longer need it, use the DROP TABLE statement:

 DROP TABLE t; 

In MySQL, a single DROP TABLE statement can name several tables to be dropped simultaneously:

 DROP TABLE t1, t2, t3; 

Normally, an error occurs if you attempt to drop a table that does not exist:

 mysql> DROP TABLE no_such_table; ERROR 1051 (42S02): Unknown table 'no_such_table' 

To prevent an error from occurring if a table does not exist when you attempt to drop it, add an IF EXISTS clause to the statement. In this case, a warning occurs if the table does not exist, which can be displayed with SHOW WARNINGS:

 mysql> DROP TABLE IF EXISTS no_such_table; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> SHOW WARNINGS; +-------+------+-------------------------------+ | Level | Code | Message                       | +-------+------+-------------------------------+ | Note  | 1051 | Unknown table 'no_such_table' | +-------+------+-------------------------------+ 1 row in set (0.00 sec) 

If you drop a table by mistake, you must recover it from backups, so be careful.



MySQL 5 Certification Study Guide
MySQL 5.0 Certification Study Guide
ISBN: 0672328127
EAN: 2147483647
Year: 2006
Pages: 312

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net