查询
查询所有数据库
SHOW DATABASES;
查询当前数据库
SELECT DATABASE();创建
CREATE DATABASE [IF NOT EXIST] 数据库名 [DEFAULT CHARSET 字符集] [COLLATE 排序规则];删除
DROP DATABASE [IF EXISTS] 数据库名;使用
USE 数据库名;代码示例:
``` mysql> show databases; +——————————+ | Database | +——————————+ | information_schema | | mysql | | performance_schema | | sakila | | sys | | world | +——————————+ 6 rows in set (0.01 sec)
mysql> create database itcast; Query OK, 1 row affected (0.01 sec)
mysql> show databases; +——————————+ | Database | +——————————+ | information_schema | | itcast | | mysql | | performance_schema | | sakila | | sys | | world | +——————————+ 7 rows in set (0.00 sec)
mysql> create database itcast; ERROR 1007 (HY000): Can’t create database ‘itcast’; database exists mysql> create database if not exists itcast; Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> show databases; +——————————+ | Database | +——————————+ | information_schema | | itcast | | mysql | | performance_schema | | sakila | | sys | | world | +——————————+ 7 rows in set (0.00 sec)
mysql> create database defult charset utf8mb4; Query OK, 1 row affected (0.01 sec)
mysql> show databases; +——————————+ | Database | +——————————+ | defult | | information_schema | | itcast | | mysql | | performance_schema | | sakila | | sys | | world | +——————————+ 8 rows in set (0.01 sec)
mysql> drop database itcast; Query OK, 0 rows affected (0.02 sec)
mysql> drop database itcast; ERROR 1008 (HY000): Can’t drop database ‘itcast’; database doesn’t exist mysql> drop database if exists itcast; Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> use defult; Database changed mysql> mysql> mysql> mysql> select database(); +——————+ | database() | +——————+ | defult | +——————+ 1 row in set (0.00 sec) ```
