系列文章:MySQL 基础:库表、约束与类型 的第 1 篇。
Mysql介绍
- Mysql数据库是一种
C\S结构,就是客户端和服务端。 - 关键字
- DB :
DataBase数据库 - DBMS:
Database Management System数据库管理系统 - DBS:
Database System = DBMS + DB数据库系统 - DBA:
Database Administrator数据库管理员 - 行\列:二维表中存储的数据。
- 行
row\record(一条记录)。 - 列
column\field(一个字段)。
- 行
- DB :
- SQL
SQL:Structured Query Language,结构化查询语言(数据以查询为主,99% 都是在进行查询操作),SQL是关系型数据库的操作指令,是一种约束,但不强制,类似与W3C,所以不同的关系型数据SQL语句可能会有一定的差异性。
- DDL:
Data Definition Language数据定义语言,用来维护存储数据的结构(数据库、表),代表指令为create、drop和alter等。 - DML:
Data Manipulation Language,数据操作语言,用来对数据进行操作(表中的内容)代表指令为insert、delete和update等,不过在 DML 内部又单独进行了一个分类,即 DQL(Data Query Language),数据查询语言,代表指令为select。 - DCL:
Data Control Language,数据控制语言,主要是负责(用户)权限管理,代表指令为grant和revoke等。 - TPL:``,事物控制语言
Mysql基本操作
交互方式
客户链接认证,即链接服务端,认证
mysql -hPup
mysql -hPup介绍-h主机地址,本地是localhost或者127.0.0.1,远程为IP地址。-P端口号,一般默认是3306,所以不指定就是3306。-u用户名,认证需要用户名。-p认证用户名,认证用户名对应的密码。
- 断开链接
exitquit\q
- 案例
$ mysql -uroot -pxxxxxxxxxx
xxxx是密码若登陆成功则输出,这里的警告不用太在意,是指登陆没有指定对应的数据库实例。
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2029
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> \q;
mysql> exit;
mysql> quit;
这三个指令都是等处操作。
Mysql标识符
当我们进入mysql命令界面的时候,命令前面会出现一个标识符。我们可以自定义。
PROMPT介绍- 使用
PROMPT xxx指定标识符。
- 使用
- 案例
| 参数 | 描述 |
|---|---|
| \D | 完整的日期 |
| \d | 当前数据库 |
| \h | 服务器的名称 |
| \u | 当前用户名 |
我们使用Mysql常用的参数配合PROMPT来自定义提示符信息,默认的刚才我们登陆之后发现默认的提示符是mysql>
- 这里演示两种使用方式
- 连接客户端的时候通过参数指定。
mysql -uroot -pxxxx --prompt xxxxxx分别代表密码和标识符。 - 连接客户端之后,通过
PROMPT指定。 - 通过my.cof配置文件全局设置。
- 连接客户端的时候通过参数指定。
- 案例
$ mysql -uroot -pxxxxxxxxxx --prpmpt @\\h #在连接客户端的时候指定需要转义\h-->\\h不然可能出现BUG。
输出
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2029
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
@localhost
登陆后再次修改
@localhost PROMPT \u@\h \d>
PROMPT set to '\u@\h \d>'
root@localhost (none)>
由于没有进入数据库,所以\d是none。那我们查询下当前的数据库,然后选择一个数据库并进入。
root@localhost (none)> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| Demo |
| mysql |
| performance_schema |
| rbac001 |
| sys |
+--------------------+
6 rows in set (0.06 sec)
进入Demo数据库
root@localhost (none)>USE Demo
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
root@localhost Demo>
那么这里mysql的标识符就显示完整了。
还有一种发发就是在my.cof配置文件中指定全局的。
我用的是linux上的mysql,如果是windows在mysql安装目录中寻找my.cof配置文件并修改相关参数。
root@localhost Demo> \q
登出mysql后编辑my.cof文件。
[root@VM_115_109_centos ~]# vim /etc/my.cnf
修改文件添加prompt = [\\u@\\h][\\d]>\\_这个设置,注意这里\是需要转义的。
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
#
[mysql]
prompt = [\\u@\\h][\\d]>\\_
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#设置端口
port=3306
#设置mysql数据存放目录
datadir=/opt/db/mysql
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# # 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
保存退出并重启mysql。退出VIM ESC键 然后: 在命令行输入wq回车。如果成功保存并退出就重启mysql服务。
[root@VM_115_109_centos ~]# systemctl restart mysqld
这里是Centos7.3的环境演示。再次登陆mysql。
[root@VM_115_109_centos ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
[root@localhost][(none)]>
[root@localhost][(none)]> USE Demo
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
[root@localhost][Demo]>
到这里全局设置PROMPT就OK了。
Mysql常用命令
这里演示一些经常用到的MYSQL常用命令,方便快速了解Mysql。同时接触一些MYSQL语句的规范。
- 演示语句
- 显示当前服务器的版本:
SELECT VERSION(); - 显示当前日期时间:
SELECT NOW(); - 显示当前的用户:
SELECT USER();
- 显示当前服务器的版本:
输出效果
[root@localhost][Demo]> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.22 |
+-----------+
1 row in set (0.00 sec)
[root@localhost][Demo]> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2018-04-29 04:22:31 |
+---------------------+
1 row in set (0.00 sec)
[root@localhost][Demo]> SELECT USER();
+----------------+
| USER() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.01 sec)
[root@localhost][Demo]>
- MYSQL标准的规范
关键字与函数名称全部大写。数据库、表名称、字段名称全部小写。- SQL语句必须
分号结尾。
在不同的系统中大小写是区分的,如果SQL语句不够规范或者数据库、表名、字段名不规范命名,可能会出现严重的BUG。
Mysql数据库的操作
这里做下记录吧,应该是要专门写一篇关于MySQL数据库结构文章。
创建数据库
CREATE { DATABASES | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name。
- 简单解释下上面的语句,{}花括弧必须要填写的,|是或则 选择其中一个即可。[]中括号可有可无。CHARACTER是库的约束项目,指定数据库的编码集。
示例
[root@localhost][Demo]> CREATE DATABASE db1;
Query OK, 1 row affected (0.00 sec)
这里已经提示我们创建成功了一个数据库db1。检查下当前的数据库列表。
[root@localhost][Demo]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| Demo |
| db1 |
| mysql |
| performance_schema |
| rbac001 |
| sys |
+--------------------+
7 rows in set (0.00 sec)
在来看看[IF NOT EXISTS] 这个参数。我们测试下。重复创建db1数据库。
[root@localhost][Demo]> CREATE DATABASE db1;
ERROR 1007 (HY000): Can't create database 'db1'; database exists
这里会警告我们,数据已经存在。在上IF NOT EXISTS在试试。
[root@localhost][Demo]> CREATE DATABASE IF NOT EXISTS db1;
Query OK, 1 row affected, 1 warning (0.00 sec)
这里警告会看不到了,我们能不能查询一下警告信息呢?当然是可以的。
[root@localhost][Demo]> SHOW WARNINGS;
+-------+------+----------------------------------------------+
| Level | Code | Message |
+-------+------+----------------------------------------------+
| Note | 1007 | Can't create database 'db1'; database exists |
+-------+------+----------------------------------------------+
1 row in set (0.00 sec)
其实,使用IF NOT EXISTS 之后警告依然存在,只不过不会立刻显示出来。
查询数据库
SHOW {DATABASES | SCHEMAS} [LIKE 'pattern' | WHERE expr]
- 查询全部:
SHOW DATABASES; - 模糊查询:
SHOW DATABASES LIKE 'pattern'- 这里的pattern是指匹配模式,
%是指匹配多个字符,_是指匹配单个字符。需要注意的是在匹配有带有_下划线的数据库名字时候,需要添加\反斜杠转义。
- 这里的pattern是指匹配模式,
创建db1的时候已经使用全局查询了,这里演示下模糊查询。
[root@localhost][Demo]> SHOW DATABASES LIKE "db%";
+----------------+
| Database (db%) |
+----------------+
| db1 |
+----------------+
1 row in set (0.00 sec)
在检查一下数据库的编码级。
[root@localhost][Demo]> SHOW CREATE DATABASE db1;
+----------+--------------------------------------------------------------+
| Database | Create Database |
+----------+--------------------------------------------------------------+
| db1 | CREATE DATABASE `db1` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)
这里查询出创建数据库的指定编码集。是utf8,这里是应为我在my.cof文件中设置了character-set-server=utf8这个参数,所以即便创建数据库时utf8编码,数据库创建默认就是utf8。
那么我们创建一个非默认编码的数据库试试。
[root@localhost][Demo]> CREATE DATABASE IF NOT EXISTS db2 CHARACTER SET gbk;
Query OK, 1 row affected (0.00 sec)
我们在检查下db2是不是指定的编码集。
[root@localhost][Demo]> SHOW CREATE DATABASE db2 ;
+----------+-------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------+
| db2 | CREATE DATABASE `db2` /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+-------------------------------------------------------------+
1 row in set (0.00 sec)
当然并不打算使用gbk来做编码集。
修改数据库
ALTER {DATABASE | SCHEMA } [db_name] [DEFAULT ] CHARACTER SET [=] charset_name
示例
[root@localhost][Demo]> ALTER DATABASE db2 CHARACTER SET = utf8;
Query OK, 1 row affected (0.00 sec)
[root@localhost][Demo]> SHOW CREATE DATABASE db2;
+----------+--------------------------------------------------------------+
| Database | Create Database |
+----------+--------------------------------------------------------------+
| db2 | CREATE DATABASE `db2` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)
删除数据库
DROP {DATABASE| SCHEMA} [IF EXISTS] db_name
示例
[root@localhost][Demo]> SHOW CREATE DATABASE db2;
+----------+--------------------------------------------------------------+
| Database | Create Database |
+----------+--------------------------------------------------------------+
| db2 | CREATE DATABASE `db2` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)
[root@localhost][Demo]> DROP DATABASE db2;
Query OK, 0 rows affected (0.03 sec)
[root@localhost][Demo]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| Demo |
| db1 |
| mysql |
| performance_schema |
| rbac001 |
| sys |
+--------------------+
7 rows in set (0.01 sec)
注意删除数据库之前记得备份,不然会悲剧。
回顾总结
- 新增:
CREATE { DATABASES | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name - 查询:
SHOW {DATABASES | SCHEMAS} [LIKE 'pattern' | WHERE expr] - 修改:
ALTER {DATABASE | SCHEMA } [db_name][DEFAULT ] CHARACTER SET [=] charset_name - 删除:
DROP {DATABASE| SCHEMA} [IF EXISTS] db_name - SQL语句注意大小写。
- 数据库操作的语句属于
DDL语句。
小结
连接参数、标识符规则与库级增删改查是日常操作的起点;建表时确定字符集与引擎,后续改表尽量用 ALTER 而非重建。