语法:
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,
create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
说明:
db1
的数据库 create database db1;
说明:当我们创建数据库没有指定字符集和校验规则时,系统使用默认字符集:
utf8
,校验规则是:utf8_general_ci
utf8
字符集的db2
数据库create database db2 charset=utf8;
utf8
字符集,并带校对规则的db3
数据库create database db3 charset=utf8 collate utf8_general_ci;
show variables like 'character_set_database';
show variables like 'collation_database';
show charset;
字符集主要是控制用什么语言。比如utf8
就可以使用中文。
show collation;
sql
语句后面都会讲解]创建一个数据库,校验规则使用utf8_general_ci
[不区分大小写]
create database test1 collate utf8_general_ci;
use test1;
create table person(name varchar(20));
insert into person values('a');
insert into person values('A');
insert into person values('b');
insert into person values('B');
创建一个数据库,校验规则使用utf8_bin
[区分大小写]
create database test2 collate utf8_bin;
use test2;
create table person(name varchar(20));
insert into person values('a');
insert into person values('A');
insert into person values('b');
insert into person values('B');
不区分大小写的查询以及结果
mysql> select * from person;
+------+
| name |
+------+
| a |
| A |
| b |
| B |
+------+
4 rows in set (0.00 sec)
mysql> select * from person where name = 'a';
+------+
| name |
+------+
| a |
| A |
+------+
2 rows in set (0.00 sec)
区分大小写的查询以及结果
mysql> select * from person;
+------+
| name |
+------+
| a |
| A |
| b |
| B |
+------+
4 rows in set (0.00 sec)
mysql> select * from person where name = 'a';
+------+
| name |
+------+
| a |
+------+
1 row in set (0.00 sec)
不区分大小写排序以及结果
mysql> select * from person order by name asc;
+------+
| name |
+------+
| a |
| A |
| b |
| B |
+------+
4 rows in set (0.00 sec)
mysql> select * from person order by name desc;
+------+
| name |
+------+
| b |
| B |
| a |
| A |
+------+
4 rows in set (0.00 sec)
区分大小写排序以及结果
mysql> select * from person order by name asc;
+------+
| name |
+------+
| A |
| B |
| a |
| b |
+------+
4 rows in set (0.00 sec)
mysql> select * from person order by name desc;
+------+
| name |
+------+
| b |
| a |
| B |
| A |
+------+
4 rows in set (0.00 sec)
show databases;
演示
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| 104_db |
| mysql |
| performance_schema |
| sys |
| test1 |
| test2 |
+--------------------+
7 rows in set (0.00 sec)
drop database [IF EXISTS] [数据库名];
执行删除之后的结果:
注意:不要随意删除数据库
演示
mysql> drop database test1;
Query OK, 1 row affected (0.09 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| 104_db |
| mysql |
| performance_schema |
| sys |
| test2 |
+--------------------+
6 rows in set (0.00 sec)
show create database [数据库名];
演示
mysql> show create database test2;
+----------+---------------------------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------------------------+
| test2 | CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */ |
+----------+---------------------------------------------------------------------------------+
1 row in set (0.00 sec)
说明:
MySQL
建议我们关键字使用大写,但是不是必须的。/*!40100 DEFAULT .....*/
这个不是注释,表示当前MySQL
的版本大于4.01版本,就执行这句话。语法:
ALTER DATABASES [数据库名]
[alter_spacification [,alter_spacification]...]
alter_spacification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
说明:
实例:将test2
数据库字符集改成gdk
mysql> alter database test2 charset=gbk;
Query OK, 1 row affected (0.00 sec)
mysql> show create database test2;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| test2 | CREATE DATABASE `test2` /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
语法:
#mysqldump -P3306 -u root -p 密码 -B 数据库名 > 数据库备份存储的文件路径
示例:将test2
库备份到文件(退出连接,在Linux指令行操作)
#mysqldump -P3306 -u root -p123456 -B test2 > D:/test2.sql
这时,可以打开看看test2.sql
文件里面的内容,其实可以把我们整个创建数据库,建表,导入数据的语句都装载在这个文件中。
[Lxy@VM-20-12-centos lesson1]$ mysqldump -B test2 > /home/Lxy/mysql/mysql/lesson1/test2.sql
[Lxy@VM-20-12-centos lesson1]$ ll
total 12
-rw-rw-r-- 1 Lxy Lxy 8082 Apr 9 18:11 mysql.sql
-rw-rw-r-- 1 Lxy Lxy 1989 Apr 10 13:11 test2.sql
我们可以查看test2.sql
文件
-- MySQL dump 10.13 Distrib 5.7.41, for Linux (x86_64)
--
-- Host: localhost Database: test2
-- ------------------------------------------------------
-- Server version 5.7.41
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: `test2`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test2` /*!40100 DEFAULT CHARACTER SET gbk */;
USE `test2`;
--
-- Table structure for table `person`
--
DROP TABLE IF EXISTS `person`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `person` (
`name` varchar(20) COLLATE utf8_bin DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `person`
--
LOCK TABLES `person` WRITE;
/*!40000 ALTER TABLE `person` DISABLE KEYS */;
INSERT INTO `person` VALUES ('a'),('A'),('b'),('B');
/*!40000 ALTER TABLE `person` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2023-04-10 13:11:08
mysql> source 路径;
演示:我们先删除test2,再还原
mysql> drop database test2;
Query OK, 1 row affected (0.17 sec)
mysql> source /home/Lxy/mysql/mysql/lesson1/test2.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.01 sec)
Database changed
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.21 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.02 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 4 rows affected (0.02 sec)
Records: 4 Duplicates: 0 Warnings: 0
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected, 1 warning (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.02 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.02 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| 104_db |
| mysql |
| performance_schema |
| sys |
| test2 |
+--------------------+
6 rows in set (0.01 sec)
#mysqldump -u root -p 数据库名 表名1 表名2 > 路径
#mysqldump -u root -p -B 数据库名1 数据库名2 .... > 路径
语法
show processlist;
mysql> show processlist;
+----+------+-----------+-------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+-------+---------+------+----------+------------------+
| 11 | root | localhost | test2 | Query | 0 | starting | show processlist |
+----+------+-----------+-------+---------+------+----------+------------------+
1 row in set (0.00 sec)
show processlist;
mysql> show processlist;
+----+------+-----------+-------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+-------+---------+------+----------+------------------+
| 11 | root | localhost | test2 | Query | 0 | starting | show processlist |
+----+------+-----------+-------+---------+------+----------+------------------+
1 row in set (0.00 sec)
可以告诉我们当前有哪些用户连接到我们的MySQL
,如果查出某个用户不是你正常登录的,很有可能你的数据库被人入侵了。以后大家发现自己数据库比较慢时,可以用这个指令来查看数据库连接情况。