一、概述

第一章内容介绍了,MySQL数据库的基本结构组成和SQL语言的组成部分,本章内容将从一些基本操作命令和MySQL数据库自带的库以及新建的库进行一些基本操作的使用说明。


二、MySQL数据库基本操作

2.1 查看当前已经创建的额数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

数据库功能说明:

1、information_schema 这个数据库保存了MySQL服务器所有数据库的细腻。如数据库名,数据库类型,访问权限等。

2、performance_schemaMySQL5.5之后新增性能优化的引擎,明明performance_schema主要用于收集数据库服务器性能参数哦。MySQL用户是无法创建村粗引擎为PERFORMANCE_SCHEMA的表的。

3、mysql:系统库,存储有数据库的账户及权限信息等。


2.2 一行的方式查看当前环境县的数据库  (\G结尾)

mysql> show databases \G
*************************** 1. row ***************************
Database: information_schema
*************************** 2. row ***************************
Database: data
*************************** 3. row ***************************
Database: mysql

2.3 创建数据库

    语法:create database <数据库名>;

注意事项:

    1、在文件系统中,MySQL的数据存储区将以目录的方式表示MySQL数据库,故所创建的数据库名字将与操作系统的约束目录名字一致。例如例如不允许文件和目录名中有\,/,:,*,?,”,<,>,|这些符号

    2、数据库名字不能操作64个字符,包含特殊字符的名字或者是全部由数字或保留字组成的名字必须使用反单引号包括起来。

    3、数据库不能重名

创建数据库xiaoxiong和xiaoxiong-db;

mysql> create database xiaoxiong;
mysql> create database `xiaoxiong-db`;

2.3 查看数据库存放目录:

    可通过配置文件/etc/my.cnf下查看datadir=<Dir>

datadir = /data/mysqldata
[root@node1 ~]# ls /data/mysqldata/

2.4 数据库切换

将数据库切换至xiaoxiong下并查看当前位置,此后将xiaoxiong作为默认数据库。

mysql> use xiaoxiong;
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| xiaoxiong  |
+------------+
1 row in set (0.00 sec)

2.5 删除数据库

删除xiaoxiong数据库

mysql> drop database xiaoxiong;


2.6 IF EXISTS子句的使用,可以避免删除不存现在的数据库是出现的MySQL错误信息。

mysql> drop database xiaoxiong;
ERROR 1008 (HY000): Can't drop database 'xiaoxiong'; database doesn't exist
mysql> drop database if exists xiaoxiong;
Query OK, 0 rows affected, 1 warning (0.00 sec)

  2.7 if not exists子句的使用,可以避免因创建已存在的数据库而出现的报错信息

mysql> create database xiaoxiongdb;
Query OK, 1 row affected (0.00 sec)
mysql> create databases xiaoxiongdb;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databases xiaoxiongdb' at line 1
mysql> create database if not exists xiaoxiongdb;
Query OK, 1 row affected, 1 warning (0.00 sec)

   2.8 查看warning信息

mysql> show warnings;
+-------+------+------------------------------------------------------+
| Level | Code | Message                                              |
+-------+------+------------------------------------------------------+
| Note  | 1007 | Can't create database 'xiaoxiongdb'; database exists |
+-------+------+------------------------------------------------------+
1 row in set (0.00 sec)

    2.9 shell环境下使用mysql内部命令

[root@node1 ~]# mysql -e 'show databases;' -uroot -p123456
[root@node1 ~]# mysql -e 'show databases;' -uroot -p123456 xiaoxiongdb        #指定默认数据库xiaoxiongdb
mysql> select now(),database();
+---------------------+-------------+
| now()               | database()  |
+---------------------+-------------+
| 2017-11-14 14:21:13 | xiaoxiongdb |
+---------------------+-------------+
1 row in set (0.02 sec)

  三、表的基本操作

3.1 创建表

     语法:create table <table_name> (字段名 '字段类型',字段名 '字段类型');

创建xiaoxiongtb表;包含uid,name和age且age不能为空

mysql> create table xiaoxiongtb(uid int(11),name char(20),age ENUM('M','W') not null);
Query OK, 0 rows affected (0.08 sec)

3.2 查看表xiaoxiongtb的组成

mysql> desc xiaoxiongtb;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| uid   | int(11)       | YES  |     | NULL    |       |
| name  | char(20)      | YES  |     | NULL    |       |
| age   | enum('M','W') | NO   |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

3.3 查看创建表xiaoxiongtb的时候使用了哪些命令

mysql> show create table xiaoxiongtb \G;
*************************** 1. row ***************************
       Table: xiaoxiongtb
Create Table: CREATE TABLE `xiaoxiongtb` (
  `uid` int(11) DEFAULT NULL,
  `name` char(20) DEFAULT NULL,
  `age` enum('M','W') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR: 
No query specified
mysql> create table xiaotb(bid int(10),bookname varchar(48),address varchar(100))ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)
mysql> show create table xiaotb\G;
*************************** 1. row ***************************
       Table: xiaotb
Create Table: CREATE TABLE `xiaotb` (
  `bid` int(10) DEFAULT NULL,
  `bookname` varchar(48) DEFAULT NULL,
  `address` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR: 
No query specified

3.3 删除表

语法:drop table 

mysql> drop table xiaotb;
Query OK, 0 rows affected (0.02 sec)

3.4 修改表

    3.4.1 修改表名

   语法:alter table <原表名> rename <新表名>

mysql> alter table xiaoxiongtb rename xiaoxiong;
Query OK, 0 rows affected (0.02 sec)

    3.4.2 修改字段类型

    语法:alter table 表名 modify 字段名 <新字段类型>

mysql> alter table xiaoxiong modify uid int(20);
mysql> desc xiaoxiong;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| uid   | int(20)       | YES  |     | NULL    |       |
| name  | char(20)      | YES  |     | NULL    |       |
| age   | enum('M','W') | NO   |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

    3.4.3 修改字段名和类型

    语法:alter table 表名 change 字段名 <新字段名> <新字段类型>

mysql> alter table xiaoxiong change uid uuid int(20);
Query OK, 0 rows affected (0.02 sec)

    注:change和modify的区别

    change对列进行重命名和更改类型,徐给定旧的列名称和新的列名称、当前的类型。列的类型,此时不需要重命名(不需要定新的列名称)

    3.5 新加字段

语法:alter table 表名 add 字段名 字段类型 {first|after};

mysql> alter table xiaoxiong add ID int(11) first;
mysql> alter table xiaoxiong add address varchar(48) after name;

    3.6 删除字段

   语法:alter table 表名 drop <字段名>;

mysql> alter table xiaoxiong drop address;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc xiaoxiong;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| ID    | int(11)       | YES  |     | NULL    |       |
| uuid  | int(20)       | YES  |     | NULL    |       |
| name  | char(20)      | YES  |     | NULL    |       |
| age   | enum('M','W') | NO   |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

四、记录操作

4.1 插入

    语法:insert into <表名> values('值1',"值2");or insert into <tb_name>(字段1,字段2) vlaues(“值1”,“值2”);

mysql> insert into xiaoxiong values(0001,1001,'zhangfei','M');
mysql> insert into xiaoxiong(ID,Name,age) values(0002,'zhaoyun','M'),(0003,'liubei','M');
mysql> select * from xiaoxiong;
+------+------+----------+-----+
| ID   | uuid | name     | age |
+------+------+----------+-----+
|    1 | 1001 | zhangfei | M   |
|    2 | NULL | zhaoyun  | M   |
|    3 | NULL | liubei   | M   |
+------+------+----------+-----+
3 rows in set (0.00 sec)

4.2 更新

语法:update <tb_name> set name='new-name' where ID=1;

mysql> update xiaoxiong set name='new-name' where ID=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from xiaoxiong;
+------+------+----------+-----+
| ID   | uuid | name     | age |
+------+------+----------+-----+
|    1 | 1001 | new-name | M   |
|    2 | NULL | zhaoyun  | M   |
|    3 | NULL | liubei   | M   |
+------+------+----------+-----+
3 rows in set (0.00 sec)
mysql> update xiaoxiong set ID=2;
Query OK, 3 rows affected (0.00 sec)
如果没有限制的话默认改变全部ID号均为2;更新多个字段的时候可以使用“,”号隔开

注意:这里添加了where 限制ID为1的行对应的name进行更新,其他行不变,否则表中的所有name对应的值都会更新;

4.3 查看

语法:select <字段1> |* from tb_name;

mysql> select * from xiaoxiong;
+------+------+----------+-----+
| ID   | uuid | name     | age |
+------+------+----------+-----+
|    1 | 1001 | new-name | M   |
|    2 | NULL | zhaoyun  | M   |
|    3 | NULL | liubei   | M   |
+------+------+----------+-----+
3 rows in set (0.00 sec)
mysql> select ID,uuid,name from xiaoxiong;
+------+------+----------+
| ID   | uuid | name     |
+------+------+----------+
|    1 | 1001 | new-name |
|    2 | NULL | zhaoyun  |
|    3 | NULL | liubei   |
+------+------+----------+
3 rows in set (0.00 sec)

4.4 删除

语法:delete from tb_name where id=2;

mysql> delete from xiaoxiong where id=2;
Query OK, 1 row affected (0.00 sec)
mysql> select * from xiaoxiong;
+------+------+----------+-----+
| ID   | uuid | name     | age |
+------+------+----------+-----+
|    1 | 1001 | new-name | M   |
|    3 | NULL | liubei   | M   |
+------+------+----------+-----+
2 rows in set (0.00 sec)
删除uuid为空的行
mysql> delete from xiaoxiong where uuid is null;
Query OK, 1 row affected (0.00 sec)

mysql> select * from xiaoxiong;
+------+------+----------+-----+
| ID   | uuid | name     | age |
+------+------+----------+-----+
|    1 | 1001 | new-name | M   |
+------+------+----------+-----+
1 row in set (0.00 sec)

五、SQL基础条件语句查询

语法:select 字段1,字段2 from tb_name [where 条件];

创建学生表;fengshenbang;还有ID,name,age,sex,address;等字段字段设置为默认类型;

mysql> create table fengshenbang(id int(10),name varchar(18),age int(3),sex ENUM('M','W'),address varchar(50));
mysql> select * from fengshenbang;
+------+-------------+------+------+---------+
| id   | name        | age  | sex  | address |
+------+-------------+------+------+---------+
|    1 | jiangziya   |   85 | M    | xuchang |
|    4 | Zhaodi      |   75 | W    | xuchang |
|    2 | zhouwenwang |   45 | M    | xiqi    |
|    3 | daji        |   20 | W    | zhaoge  |
+------+-------------+------+------+---------+
5.1 去重查询 distinct
mysql> select distinct address from fengshenbang;
+---------+
| address |
+---------+
| xuchang |
| xiqi    |
| zhaoge  |
+---------+
3 rows in set (0.00 sec)
观察去重查询和一般查询的区别

mysql> select * from fengshenbang;
+------+-------------+------+------+---------+
| id   | name        | age  | sex  | address |
+------+-------------+------+------+---------+
|    1 | jiangziya   |   85 | M    | xuchang |
|    4 | Zhaodi      |   75 | W    | xuchang |
|    2 | zhouwenwang |   45 | M    | xiqi    |
|    3 | daji        |   20 | W    | zhaoge  |
|    4 | Zhaodi      |   75 | W    | xuchang |
|    2 | zhouwenwang |   45 | M    | xiqi    |
|    3 | daji        |   20 | W    | zhaoge  |
+------+-------------+------+------+---------+
7 rows in set (0.00 sec)

mysql> select distinct * from fengshenbang;
+------+-------------+------+---------+
| id   | name        | sex  | address |
+------+-------------+------+---------+
|    1 | jiangziya   | M    | xuchang |
|    4 | Zhaodi      | W    | xuchang |
|    2 | zhouwenwang | M    | xiqi    |
|    3 | daji        | W    | zhaoge  |
+------+-------------+------+---------+
4 rows in set (0.00 sec)

5.2 使用and和or进行多条件查询

and和or的执行顺序为,当and和or同时存在时,先算and两边的额值,逻辑与先于执行

mysql> select * from fengshenbang where id >2 and age < 75;
+------+------+------+------+---------+
| id   | name | age  | sex  | address |
+------+------+------+------+---------+
|    3 | daji |   20 | W    | zhaoge  |
|    3 | daji |   20 | W    | zhaoge  |
+------+------+------+------+---------+
2 rows in set (0.00 sec)
mysql> select * from fengshenbang where id >3 or age > 20;
+------+-------------+------+------+--------------+
| id   | name        | age  | sex  | address      |
+------+-------------+------+------+--------------+
|    1 | jiangziya   |   85 | M    | xuchang      |
|    4 | Zhaodi      |   75 | W    | xuchang      |
|    2 | zhouwenwang |   45 | M    | xiqi         |
|    4 | Zhaodi      |   75 | W    | xuchang      |
|    2 | zhouwenwang |   45 | M    | xiqi         |
|    5 | litianwang  |   46 | M    | qiantangguan |
|    6 | nezha       |   10 | M    | qiantangguan |
+------+-------------+------+------+--------------+
and和or组合使用
mysql> select * from fengshenbang where name='nezha' and (age > 25 or id = 6);
+------+-------+------+------+--------------+
| id   | name  | age  | sex  | address      |
+------+-------+------+------+--------------+
|    6 | nezha |   10 | M    | qiantangguan |
+------+-------+------+------+--------------+

5.3 MySQL区分大小写查询:

    MySQL查询是默认不区分大小写的

mysql> select name from fengshenbang where name='litianwang' or name = 'NEZHA';
+------------+
| name       |
+------------+
| litianwang |
| nezha      |
| NEZHA      |
| LITIANWANG |
| NEZHA      |
+------------+
5 rows in set (0.00 sec)

如果使用过程中需要区分大小写进行查询则可使用binary进行约束如下:
mysql> select name from fengshenbang where name='litianwang' or binary name = 'NEZHA';
+------------+
| name       |
+------------+
| litianwang |
| NEZHA      |
| LITIANWANG |
| NEZHA      |
+------------+
4 rows in set (0.00 sec)
由于where条件剧中定义了binary则此时小写的nezha已经不见踪影
说明:BINARY是类型转换运算符。它用来强制它后面的字符串为一个二进制字符串,可以理解为在比较的时候区分大小写。

5.4 排序查询

语法:select <字段> from tb_name order by <待排序字段>; 默认为升序asc

mysql> select distinct id from fengshenbang order by id;    
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
|    6 |
+------+
6 rows in set (0.00 sec)
倒序查询
mysql> select distinct id from fengshenbang order by id desc;
+------+
| id   |
+------+
|    6 |
|    5 |
|    4 |
|    3 |
|    2 |
|    1 |
+------+
6 rows in set (0.00 sec)