mysql教程
CREATE DATABASE [IF NOT EXISTS] databaseName;
IF NOT EXISTS:为可选子句,如果不加该字句,当要创建的数据库存在时会报错;加了该子句则不会报错。
例如:以下创建一个数据库test_db。
mysql> CREATE DATABASE test_db; Query OK, 1 row affected (0.00 sec) mysql> CREATE DATABASE test_db; ERROR 1007 (HY000): Can't create database 'test_db'; database exists mysql> CREATE DATABASE IF NOT EXISTS test_db; Query OK, 1 row affected, 1 warning (0.00 sec)
查看当前有哪些数据库:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | stool | | test_db | | tools | +--------------------+ 6 rows in set (0.00 sec)
mysql> use test_db; Database changed mysql> show create database test_db; +----------+------------------------------------------------------------------+ | Database | Create Database | +----------+------------------------------------------------------------------+ | test_db | CREATE DATABASE `test_db` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+------------------------------------------------------------------+ 1 row in set (0.00 sec)
DROP DATABASE [IF EXISTS] databaseName;
IF EXISTS: 为可选子句,加上该子句后如果要删除的数据库不存在也不会报错,如:
mysql> drop database test_db; ERROR 1008 (HY000): Can't drop database 'test_db'; database doesn't exist mysql> drop database if exists test_db; Query OK, 0 rows affected, 1 warning (0.00 sec)