Saturday 7 January 2012

IF EXISTS

If you drop a table and it does not exist, you will get an error message but, if you include the if exists clause, you will only get a warning:
  
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| andrews_table  |
+----------------+
1 row in set (0.00 sec)

mysql> drop table andrews_table;
Query OK, 0 rows affected (0.06 sec)

mysql> show tables;
Empty set (0.00 sec)

mysql> drop table andrews_table;
ERROR 1051 (42S02): Unknown table 'andrews_table'
mysql> drop table if exists
    -> andrews_table;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+-------------------------------+
| Level | Code | Message                       |
+-------+------+-------------------------------+
| Note  | 1051 | Unknown table 'andrews_table' |
+-------+------+-------------------------------+
1 row in set (0.00 sec)

mysql>

No comments: