9.1 创建数据库
命令语法:create database<数据库名> 注意库名不能数字开头
在mysql默认字符集情况下建立数据库测试如下:
a. 建立一个名为zbf的数据库
[email protected] 08:3120->create database zbf;
Query OK, 1 row affected (0.00 sec)
[email protected] 08:3810->show databases like ‘z%‘;
+---------------+
| Database (z%) |
+---------------+
| zbf |
+---------------+
1 row in set (0.01 sec)
查看建库语句
[email protected] 08:4634->show create database zbf\G
*************************** 1. row ***************************
Database: zbf
Create Database: CREATE DATABASE `zbf` /*!40100 DEFAULT CHARACTER SET latin1 */ 默认是latin字符集
1 row in set (0.00 sec)
b.建立一个名为zbf_gbk的GBK字符集数据库
create database zbf_gbk DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
c.创建一个名为zbf_utf8的UTF8数据库
[email protected] 09:1523->create database zbf_utf8 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
[email protected] 09:1616->show create database zbf_utf8\G;
*************************** 1. row ***************************
Database: zbf_utf8
Create Database: CREATE DATABASE `zbf_utf8` /*!40100 DEFAULT CHARACTER SET utf8 */
1 row in set (0.00 sec)
d.创建不同字符集格式的数据库命令
create database zbf; 默认数据库配置,相当于创建拉丁字符集数据库。
create database zbf_gbk DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; 创建gbk字符集数据库
create database zbf_utf8 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 创建utf8字符集数据库
提示:字符集的不一致是导致数据库中文内容乱码的罪魁祸首。
提示:如果编译的时候指定了特定的字符集,则以后创建对应字符集的数据库就不需要指定了。如下:
-DDEFAULT_CHARSET=utf8 \ #指定默认字符集
-DDEFAULT_COLLATION=utf8_general_ci \
然后建库的时候就默认创建即可,create database zbf;
e.那么企业里怎么创建数据库呢?
1.根据开发的环境确定字符集(建议UTF8)
2.编译的时候指定字符集。例如:
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci \
然后建库的时候默认创建即可,create database zbf。
3.编译的时候没有指定字符集或者指定了和程序不同的字符集,如何解决?
指定字符集创建数据库即可
create database zbf; 默认数据库配置,相当于创建拉丁字符集数据库。
create database zbf_gbk DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;创建gbk字符集数据库
create database zbf_utf8 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;创建utf8字符集数据库
数据库要支持创建库的字符集,例如
-DEXTRA_CHARSETS=gbk,gb2312,utf8,asci或
-DEXTRA_CHARSETS=all
9.2 显示数据库
命令:show databases;
[email protected] 07:0810->show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| zbf|
| zbf_utf8 |
+--------------------+
4 rows in set (0.00 sec)
[email protected] 07:3122->help show
SHOW DATABASES [like_or_where]
[email protected] 07:3027->show databases like ‘%zb%‘; %为通配符,匹配所有内容
+-----------------+
| Database (%zb%) |
+-----------------+
| zbf |
| zbf_utf8|
+-----------------+
2 rows in set (0.00 sec)
查看当前数据库,为空
[email protected] 07:3537->select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
进入数据库,在查看一次
[email protected] 07:3638->use zbf;
Database changed
[email protected] 07:4001->select database();
+------------+
| database() |
+------------+
| zbf|
+------------+
1 row in set (0.00 sec)
9.3 删除数据库
命令:drop database<数据库名>
例如:删除名为zbf的数据库
[email protected] 07:4005->show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| zbf|
| zbf_utf8 |
+--------------------+
4 rows in set (0.00 sec)
[email protected] 07:4620->drop database zbf;
Query OK, 0 rows affected (0.00 sec)
[email protected] 07:4635->show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| zbf_utf8 |
+--------------------+
2 rows in set (0.00 sec)
不会要经常看帮助
[email protected] 07:5010->help drop database
Name: ‘DROP DATABASE‘
Description:
Syntax:
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
9.4 连接数据库
命令:use <数据库名> 相当于linux下的cd切换目录的命令,use是切换数据库
例如:
[email protected] 08:0746->use zbf;
Database changed
[email protected] 08:0802->select database();
+------------+
| database() |
+------------+
| zbf|
+------------+
1 row in set (0.00 sec)
9.5 查看当前连接的数据库
[email protected] 08:0802->select database();相当于linux下的pwd
+------------+
| database() |
+------------+
| zbf|
+------------+
1 row in set (0.00 sec)
查看版本
[email protected] 08:0938->select version();
+------------+
| version() |
+------------+
| 5.1.72-log |
+------------+
1 row in set (0.00 sec)
查看当前的用户
[email protected] 08:1316->select user();
+------------------+
| user() |
+------------------+
| [email protected] |
+------------------+
1 row in set (0.00 sec)
查看当前的时间
[email protected] 08:1327->select now();
+---------------------+
| now() |
+---------------------+
| 2018-01-18 20:14:43 |
+---------------------+
1 row in set (0.01 sec)
9.6 查看当前数据库包含的表信息
切换到数据库里面去查看
[email protected] 08:1630->show tables;
Empty set (0.00 sec) 空表,新库还没有建表
[email protected] 08:1742->show tables like ‘user‘;
Empty set (0.00 sec)
[email protected] 08:2016->show tables from zbf; 查询指定数据库的表
Empty set (0.00 sec)
[email protected] 08:2030->show tables in zbf;
Empty set (0.00 sec)
9.7 删除mysql系统多余账号
语法:drop user “user”@”主机域”<=注意引号可以是单引号或双引号,但是不能不加。
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | mysql |
| root | mysql |
+------+-----------+
6 rows in set (0.00 sec)
mysql> drop user ‘‘@‘localhost‘; 没有的部分就用两个单引号代替即可
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
| | mysql |
| root | mysql |
+------+-----------+
5 rows in set (0.00 sec)
注意:如果drop删除不了(一般是特殊字符或大写导致的),可以用下面方式删除。
Delete from mysql.user where user=’root’and host=’mysql’;
Flush privileges;
2018/1/26 23:51:40
原文地址:http://blog.51cto.com/10642812/2065684