9.1 创建数据库
命令语法:create database <数据库名> <==注意库名不能数字开头,大小写不敏感 在 MySQL 默认字符集情况下建立数据库测试如下: 建立一个名为 oldboy 的数据库 mysql> show databases like 'oldboy'; +-------------------+ | Database (oldboy) | +-------------------+ | oldboy | +-------------------+ 1 row in set (0.04 sec) 查看建库的语句 (注意:如果编译时指定字符集是 utf8,创建数据库时默认指定是 utf8) mysql> show create database oldboy; +----------+-----------------------------------------------------------------+ | Database | Create Database | +----------+-----------------------------------------------------------------+ | oldboy | CREATE DATABASE `oldboy` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+-----------------------------------------------------------------+ 1 row in set (0.00 sec) 创建一个指定字符集的数据库 mysql> create database oldboy_gbk CHARACTER SET gbk COLLATE gbk_chinese_ci; Query OK, 1 row affected (0.00 sec) mysql> show create database oldboy_gbk; +------------+--------------------------------------------------------------------+ | Database | Create Database | +------------+--------------------------------------------------------------------+ | oldboy_gbk | CREATE DATABASE `oldboy_gbk` /*!40100 DEFAULT CHARACTER SET gbk */ | +------------+--------------------------------------------------------------------+ 1 row in set (0.00 sec)
9.2 查询库的字符集及校队规则
mysql> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 | | dec8 | DEC West European | dec8_swedish_ci | 1 | | cp850 | DOS West European | cp850_general_ci | 1 | | hp8 | HP West European | hp8_english_ci | 1 | | koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 | | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 | | swe7 | 7bit Swedish | swe7_swedish_ci | 1 | | ascii | US ASCII | ascii_general_ci | 1 | | ujis | EUC-JP Japanese | ujis_japanese_ci | 3 | | sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 | | hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 | | tis620 | TIS620 Thai | tis620_thai_ci | 1 | | euckr | EUC-KR Korean | euckr_korean_ci | 2 | | koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 | | gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 | | greek | ISO 8859-7 Greek | greek_general_ci | 1 | | cp1250 | Windows Central European | cp1250_general_ci | 1 | | gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 | | latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 | | armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | cp866 | DOS Russian | cp866_general_ci | 1 | | keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 | | macce | Mac Central European | macce_general_ci | 1 | | macroman | Mac West European | macroman_general_ci | 1 | | cp852 | DOS Central European | cp852_general_ci | 1 | | latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 | | utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 | | cp1251 | Windows Cyrillic | cp1251_general_ci | 1 | | utf16 | UTF-16 Unicode | utf16_general_ci | 4 | | cp1256 | Windows Arabic | cp1256_general_ci | 1 | | cp1257 | Windows Baltic | cp1257_general_ci | 1 | | utf32 | UTF-32 Unicode | utf32_general_ci | 4 | | binary | Binary pseudo charset | binary | 1 | | geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 | | cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 | | eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 | +----------+-----------------------------+---------------------+--------+ 39 rows in set (0.00 sec)
9.3 企业场景创建什么字符集的数据库
1、根据开发的程序确定字符集(建议 UTF8) 2、可以编译的时候指定字符集,例如: --DDEFAULT_CHARSET=utf8 --DDEFAULT_COLLATION=utf8_general_ci 然后建库的时候默认创建即可,create database oldboy; 3、编译的时候没有指定字符集或者指定了和程序不同的字符集,如何解决? 指定字符集创建数据库即可 例如: create database oldboy_gbk DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci <== 创建 gbk 字符集数据库
9.4 显示数据库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | oldboy | | oldboy_gbk | | performance_schema | +--------------------+ 5 rows in set (0.05 sec)
9.5 删除数据库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | oldboy | | oldboy_gbk | | performance_schema | +--------------------+ 5 rows in set (0.00 sec) mysql> drop database oldboy; Query OK, 0 rows affected (0.05 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | oldboy_gbk | | performance_schema | +--------------------+ 4 rows in set (0.00 sec)
9.6 进入数据库中指定的库
mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select database(); +------------+ | database() | +------------+ | mysql | +------------+ 1 row in set (0.00 sec)
9.7 查看进入当前数据库的用户
mysql> select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.05 sec)
9.8 删除数据库中多余的账号
语法:drop user ‘user’@’主机域 0 ’ <--注意引号,可以是单或双引号,但是不能不加
mysql>drop user ‘root’@’oldboy’; mysql>drop user ‘ ’@’localhost’; <--没有的部分就用两个单引号代替即可 如果 drop 删除不了(一般是特殊字符或大写),可以用下面方式删除(以 root 用户,oldboy 主机为例) delete from mysql.user where user=’root’ and host=’oldboy’ flush privileges;
9.9 查看数据库的用户
mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | localhost | +------+-----------+ 2 rows in set (0.06 sec)