MySQL的库操作
#前言:MySQL中的库相当于我们Windows系统中的一个文件夹,里面存放着一些文件,这些文件在数据库里面我们称之为表
#首先来查看系统数据库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
#information_schema:虚拟库,主要存储系统中的一些数据库对象信息,如用户信息列表、权限信息等
#mysql:MySQL的核心数据库,主要用于存储系统用户的权限信息
#performance_schema:主要用于收集数据库服务器性能参数
#sys:MySQL5.7安装后多的一个数据库,主要提供一些视图,数据都来源performance_schema
#创建数据库
#语法:create database <数据库名>
#数据库命名规则
1.不能单独使用数字 2.不能使用关键字,如:update,alter 3.唯一性 4.区分大小写
#查看帮助
mysql> help create database Name: 'CREATE DATABASE' Description: Syntax: CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] ... create_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name CREATE DATABASE creates a database with the given name. To use this statement, you need the CREATE privilege for the database. CREATE SCHEMA is a synonym for CREATE DATABASE. URL: https://dev.mysql.com/doc/refman/5.7/en/create-database.html
#例子:
mysql> create database data; #创建库 Query OK, 1 row affected (0.00 sec) mysql> show databases; #查看 +--------------------+ | Database | +--------------------+ | information_schema | | data | | mysql | | performance_schema | | sys | +--------------------+ 7 rows in set (0.00 sec) mysql> show databases like 'data'; #查看data库 +-----------------+ | Database (data) | +-----------------+ | data | +-----------------+ 1 row in set (0.00 sec) mysql> show create database data\G #查看建库语句 *************************** 1. row *************************** Database: data Create Database: CREATE DATABASE `data` /*!40100 DEFAULT CHARACTER SET latin1 */ 1 row in set (0.00 sec)
#创建不同字符集数据库
#1.创建一个gbk字符集数据库 mysql> create database data_gbk default character set gbk collate gbk_chinese_ci; Query OK, 1 row affected (0.00 sec) mysql> show create database data_gbk\G; *************************** 1. row *************************** Database: data_gbk Create Database: CREATE DATABASE `data_gbk` /*!40100 DEFAULT CHARACTER SET gbk */ 1 row in set (0.00 sec) #2.创建一个utf8的字符集数据库 mysql> create database data_utf8 character set utf8 collate utf8_general_ci; Query OK, 1 row affected (0.01 sec) mysql> show create database data_utf8\G; *************************** 1. row *************************** Database: data_utf8 Create Database: CREATE DATABASE `data_utf8` /*!40100 DEFAULT CHARACTER SET utf8 */ 1 row in set (0.00 sec)
#提示:
1.字符集的不一致会导致很多问题
2.如果编译的时候指定了特定的字符集,则以后创建对应字符集的数据库就不需要指定
#企业场景:
企业如何创建数据库?
1.根据开发的程序确定字符集(建议UTF8)
2.编译时候指定字符集
3.编译的时候没有指定字符集或者指定了和程序不同的字符集,怎么解决呢?
答:在创建数据库的时候指定字符集
#查看当前数据库
mysql> use data; Database changed mysql> select database(); +------------+ | database() | +------------+ | data | +------------+ 1 row in set (0.00 sec)
#删除数据库
#命令语法:drop database <数据库名>
#查看帮助
mysql> help drop database Name: 'DROP DATABASE' Description: Syntax: DROP {DATABASE | SCHEMA} [IF EXISTS] db_name DROP DATABASE drops all tables in the database and deletes the database. Be very careful with this statement! To use DROP DATABASE, you need the DROP privilege on the database. DROP SCHEMA is a synonym for DROP DATABASE. *Important*: When a database is dropped, privileges granted specifically for the database are not automatically dropped. They must be dropped manually. See [HELP GRANT]. IF EXISTS is used to prevent an error from occurring if the database does not exist. URL: https://dev.mysql.com/doc/refman/5.7/en/drop-database.html
#例子:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | data | | data_gbk | | data_utf8 | | mysql | | performance_schema | | sys | +--------------------+ 9 rows in set (0.00 sec) mysql> drop database data; #删除data库 Query OK, 0 rows affected (0.00 sec) mysql> show databases like 'data'; #再次查看 Empty set (0.00 sec)