mysql介绍及应用

mysql基础

1. 关系型数据库介绍

1.1 数据结构模型

数据结构模型主要有:

层次模型
网状结构
关系模型

关系模型:

二维关系(表格类型):row,column

数据库管理系统:DBMS

关系型数据库管理系统:Relational,RDBMS

1.2 RDBMS专业名词

常见的关系型数据库管理系统:

MySQL:MySQL,MariaDB,Percona-Server
PostgreSQL:简称为pgsql
Oracle
MSSQL
SQL:Structure Query Language,结构化查询语言

约束:constraint,向数据表提供的数据要遵守的限制

主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL)。
一个表只能存在一个
惟一键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。允许为空(NULL)
一个表可以存在多个
外键约束:一个表中的某字段可填入数据取决于另一个表的主键已有的数据
检查性约束
索引:将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储

1.3 关系型数据库的常见组件:

数据库:database
表:table,由行(row)和列(column)组成
索引:index
视图:view
用户:user
权限:privilege
存储过程:procedure
存储函数:function
触发器:trigger
事件调度器:event scheduler

1.4 SQL语句

SQL语句有三种类型:
  1. DDL:Data Defination Language,数据定义语言
  2. DML:Data Manipulation Language,数据操纵语言
  3. DCL:Data Control Language,数据控制语言
SQL语句类型对应操作
DDL(作用于组件)CREATE:创建
DROP:删除
ALTER:修改
DML(作用于组件内部的数据)INSERT:向表中插入数据
DELETE:删除表中数据
UPDATE:更新表中数据
SELECT:查询表中数据
DCL(作用于权限)GRANT:授权
REVOKE:移除授权

2. mysql安装与配置

2.1 mysql安装

mysql安装方式有三种:

源代码:编译安装(源码安装;数据库几乎不用)
二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用(绿色包;公司常用)
程序包管理器管理的程序包:(yum方式安装)
 rpm:有两种
    OS Vendor:操作系统发行商提供的(MariaDB)
    项目官方提供的
 deb
//配置mysql的yum源
wget -O /usr/src/mysql57-community-release-el7-10.noarch.rpm \
http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
rpm -Uvh /usr/src/mysql57-community-release-el7-10.noarch.rpm
//安装mysql5.7(本地没有)
yum -y install mysql-community-server mysql-community-client  \
mysql-community-common mysql-community-devel
//安装MariaDB

yum -y install MariaDB*

2.2 mysql配置

//启动mysql并设置开机自动启动

systemctl enable --now mysqld

systemctl status mysqld

//确保3306端口已经监听起来

ss -antl

//在日志文件中找出临时密码

grep “password” /var/log/mysqld.log

//使用获取到的临时密码登录mysql
mysql -uroot -p
Enter password:     //此处输入密码,可以直接复制你的密码粘贴至此处,也可手动输入
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23

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登录密码
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.01 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'wangqing123!';
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
//为避免mysql自动升级,这里需要卸载最开始安装的yum源

rpm -e mysql57-community-release

2.3 MariaDB配置(默认一开始没有密码;默认为root登录,但和客户机的root无关)

//设置MariaDB登录密码

MariaDB [(none)]> set password = password(‘123465’);

//MariaDB登录
[root@yeqixian1 ~]# mysql -uroot -p123465
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit
Bye
[root@yeqixian1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit

3. mysql的程序组成

客户端
 mysql:CLI交互式客户端程序
 mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
 mysqldump:mysql备份工具
  mysqladmin
服务器端
 mysqld

3.1 mysql工具使用

//语法:mysql [OPTIONS] [database]
//常用的OPTIONS:
    -u+用户名      //指定用户名,默认为root
    -h+主机           //指定服务器主机,默认为localhost,推荐使用ip地址
    -p+密码(密码可以让单引号括起来)      //指定用户的密码
    -P+端口            //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
    -V              //查看当前使用的mysql版本
    -e+'命令;(可以有多个)'          //不登录mysql执行sql语句后退出,常用于脚本
    
[root@yeqixian1 ~]# mysql -V
mysql  Ver 15.1 Distrib 5.5.64-MariaDB, for Linux (x86_64) using readline 5.1

[root@yeqixian1 ~]# mysql -uroot -p123465 -h127.0.0.1(localhost和127.0.0.1都是指本机)
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit
Bye
[root@yeqixian1 ~]# mysql -uroot -p123465 -hlocalhost
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit
Bye
//注意,不推荐直接在命令行里直接用-pPASSWORD的方式登录,而是使用-p选项,然后交互式输入密码

[root@yeqixian1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit

[root@yeqixian1 ~]# mysql -uroot -p123465 -hlocalhost -e 'show databases;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| SCHOOL             |
| Syslog             |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
[root@yeqixian1 ~]# mysql -uroot -p123465 -hlocalhost -e'use mysql;show tables;'
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
//让别的ip也可以登录
[root@yeqixian1 ~]# mysql -uroot -p123465 -hlocalhost
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all on *.* to 'root'@192.168.116.128 identified by '123465';
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> flush privileges;(刷新权限)
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> quit
Bye
[root@yeqixian ~]# mysql -uroot -p123465 -h192.168.116.145
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit
Bye

3.2 服务器监听的两种socket地址

socket类型说明
ip socket(只要能够连接此端口的都可以使用)默认监听在tcp的3306端口,支持远程通信
unix sock(只能本地使用)监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock)
仅支持本地通信
server地址只能是:localhost,127.0.0.1
[root@yeqixian1 ~]# mysql -uroot -p123465 -S /var/lib/mysql/mysql.sock
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> quit
Bye

4. mysql数据库操作

4.1 DDL操作

4.1.1 数据库操作
//创建数据库
//语法:CREATE DATABASE [IF NOT EXISTS] (中括号里的可有可无) 'DB_NAME';
//创建数据库school(前面可大可小写;但是数据库的名字要大小写区分开来)

MariaDB [(none)]> create database if not exists school;
Query OK, 1 row affected, 1 warning (0.00 sec)
//查看当前实例有哪些数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| SCHOOL             |
| Syslog             |
| mysql              |
| performance_schema |
| school             |
| test               |
+--------------------+
7 rows in set (0.00 sec)
//删除数据库
//语法:DROP DATABASE [IF EXISTS] (中括号里的可有可无) 'DB_NAME';
//删除数据库school
MariaDB [(none)]> drop database if exists school;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| SCHOOL             |
| Syslog             |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)
4.1.2 表操作
//创建表

//语法:CREATE TABLE table_name (col1 datatype 修饰符,col2 datatype 修饰符) ENGINE=‘存储引擎类型’;

MariaDB [yeqixian]> CREATE TABLE student1 (id int(11) NOT NULL primary key AUTO_INCREMENT,name varchar(100) NOT NULL,age tinyint(4) DEFAULT NULL) ;
primary key(主键约束) AUTO_INCREMENT(自动增加)
//在数据库SCHOOL里创建表class

MariaDB [SCHOOL]> create table class

//进入SCHOOL数据库

MariaDB [(none)]> use SCHOOL;

Database changed

//创建class表
MariaDB [SCHOOL]> create table class (id int not null,name varchar(50) not null,age tinyint not null);
Query OK, 0 rows affected (0.01 sec)

MariaDB [SCHOOL]> SHOW TABLES;
+------------------+
| Tables_in_SCHOOL |
+------------------+
| class            |
+------------------+
1 row in set (0.00 sec)
MariaDB [yeqixian]> CREATE TABLE student1 (id int(11) NOT NULL primary key AUTO_INCREMENT,name varchar(100) NOT NULL,age tinyint(4) DEFAULT NULL) default charset=utf8;
primary key(主键约束) AUTO_INCREMENT(自动增加)
//查看当前数据库有哪些表
MariaDB [SCHOOL]> SHOW TABLES;
+----------------------+
| Tables_in_SCHOOL     |
+----------------------+
| class                |
+----------------------+
1 row in set (0.00 sec)
//查看表内情况
MariaDB [SCHOOL]> desc class;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(50) | NO   |     | NULL    |       |
| age   | tinyint(4)  | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
MariaDB [SCHOOL]> select * from class;
Empty set (0.00 sec)
//删除表

//语法:DROP TABLE [ IF EXISTS ] ‘table_name’;

//删除表class
MariaDB [SCHOOL]> SHOW TABLES;
+------------------+
| Tables_in_SCHOOL |
+------------------+
| class            |
+------------------+
1 row in set (0.00 sec)

MariaDB [SCHOOL]> drop table class;
Query OK, 0 rows affected (0.00 sec)

MariaDB [SCHOOL]> SHOW TABLES;
Empty set (0.00 sec)
4.1.3 用户操作

mysql用户帐号由两部分组成,如’USERNAME’@‘HOST’,表示此USERNAME只能从此HOST上远程登录(如果host不是本机,就要对它进行授权)

这里(‘USERNAME’@‘HOST’)的HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:

IP地址,如:172.16.12.129
通配符
%:匹配任意长度的任意字符,常用于设置允许从任何主机登录(比如172.16.12.%:172.16.12.0这个网段)
 _ :匹配任意单个字符(比如172.16.12.%:比如172.16.12._  :172.16.12.1~9 )
//数据库用户创建

//语法:CREATE USER ‘username’@‘host’ [IDENTIFIED BY ‘password’];

//创建数据库用户yqx(自己创建的用户有一部分database看不到)
MariaDB [yeqixian]> create user yqx@'127.0.0.1' IDENTIFIED BY 'wangqing123!';
//使用新创建的用户和密码登录
[root@yeqixian1 ~]# mysql -uyqx -pwangqing123! -h127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>
//删除数据库用户

//语法:DROP USER ‘username’@‘host’;
MariaDB [yeqixian]> drop user yqx@192.168.116.128;
Query OK, 0 rows affected (0.00 sec)

4.1.4 查看命令SHOW(不能用于查看用户创建的过程;详细信息:把";"改为\G)
//mysql> SHOW CHARACTER SET; //查看支持的所有字符集
pMariaDB [yeqixian]> SHOW CHARACTER SET; 
+----------+-----------------------------+---------------------+--------+
| Charset  | Description                 | Default collation   | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |
| dec8     | DEC West European           | dec8_swedish_ci     |      1 |
| cp850    | DOS West European           | cp850_general_ci    |      1 |
| hp8      | HP West European            | hp8_english_ci      |      1 |
| koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    |      1 |
| latin1   | cp1252 West European        | latin1_swedish_ci   |      1 |
| latin2   | ISO 8859-2 Central European | latin2_general_ci   |      1 |
| swe7     | 7bit Swedish                | swe7_swedish_ci     |      1 |
| ascii    | US ASCII                    | ascii_general_ci    |      1 |
| ujis     | EUC-JP Japanese             | ujis_japanese_ci    |      3 |
| sjis     | Shift-JIS Japanese          | sjis_japanese_ci    |      2 |
| hebrew   | ISO 8859-8 Hebrew           | hebrew_general_ci   |      1 |
| tis620   | TIS620 Thai                 | tis620_thai_ci      |      1 |
| euckr    | EUC-KR Korean               | euckr_korean_ci     |      2 |
| koi8u    | KOI8-U Ukrainian            | koi8u_general_ci    |      1 |
| gb2312   | GB2312 Simplified Chinese   | gb2312_chinese_ci   |      2 |
| greek    | ISO 8859-7 Greek            | greek_general_ci    |      1 |
| cp1250   | Windows Central European    | cp1250_general_ci   |      1 |
| gbk      | GBK Simplified Chinese      | gbk_chinese_ci      |      2 |
| latin5   | ISO 8859-9 Turkish          | latin5_turkish_ci   |      1 |
| armscii8 | ARMSCII-8 Armenian          | armscii8_general_ci |      1 |
| utf8     | UTF-8 Unicode               | utf8_general_ci     |      3 |
| ucs2     | UCS-2 Unicode               | ucs2_general_ci     |      2 |
| cp866    | DOS Russian                 | cp866_general_ci    |      1 |
| keybcs2  | DOS Kamenicky Czech-Slovak  | keybcs2_general_ci  |      1 |
| macce    | Mac Central European        | macce_general_ci    |      1 |
| macroman | Mac West European           | macroman_general_ci |      1 |
| cp852    | DOS Central European        | cp852_general_ci    |      1 |
| latin7   | ISO 8859-13 Baltic          | latin7_general_ci   |      1 |
| utf8mb4  | UTF-8 Unicode               | utf8mb4_general_ci  |      4 |
| cp1251   | Windows Cyrillic            | cp1251_general_ci   |      1 |
| utf16    | UTF-16 Unicode              | utf16_general_ci    |      4 |
| cp1256   | Windows Arabic              | cp1256_general_ci   |      1 |
| cp1257   | Windows Baltic              | cp1257_general_ci   |      1 |
| utf32    | UTF-32 Unicode              | utf32_general_ci    |      4 |
| binary   | Binary pseudo charset       | binary              |      1 |
| geostd8  | GEOSTD8 Georgian            | geostd8_general_ci  |      1 |
| cp932    | SJIS for Windows Japanese   | cp932_japanese_ci   |      2 |
| eucjpms  | UJIS for Windows Japanese   | eucjpms_japanese_ci |      3 |
+----------+-----------------------------+----------------

-----+--------+
39 rows in set (0.00 sec)
//mysql> SHOW ENGINES; //查看当前数据库支持的所有存储引擎
MariaDB [yeqixian]> SHOW ENGINES;  
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                                          | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys       | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                                            | NO           | NO   | NO         |
| MyISAM             | YES     | Non-transactional engine with good performance and small data footprint          | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears)                   | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                                               | NO           | NO   | NO         |
| CSV                | YES     | Stores tables as CSV files                                                       | NO           | NO   | NO         |
| ARCHIVE            | YES     | gzip-compresses tables for a low storage footprint                               | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables                        | NO           | NO   | NO         |
| FEDERATED          | YES     | Allows to access tables on other MariaDB servers, supports transactions and more | YES          | NO   | YES        |
| Aria               | YES     | Crash-safe tables with MyISAM heritage                                           | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
10 rows in set (0.00 sec)
MariaDB [yeqixian]> SHOW ENGINES\G
*************************** 1. row ***************************
      Engine: InnoDB
     Support: DEFAULT
     Comment: Percona-XtraDB, Supports transactions, row-level locking, and foreign keys
Transactions: YES
          XA: YES
  Savepoints: YES
*************************** 2. row ***************************
      Engine: MRG_MYISAM
     Support: YES
     Comment: Collection of identical MyISAM tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 3. row ***************************
      Engine: MyISAM
     Support: YES
     Comment: Non-transactional engine with good performance and small data footprint
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 4. row ***************************
      Engine: BLACKHOLE
     Support: YES
     Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 5. row ***************************
      Engine: PERFORMANCE_SCHEMA
     Support: YES
     Comment: Performance Schema
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 6. row ***************************
      Engine: CSV
     Support: YES
     Comment: Stores tables as CSV files
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 7. row ***************************
      Engine: ARCHIVE
     Support: YES
     Comment: gzip-compresses tables for a low storage footprint
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 8. row ***************************
      Engine: MEMORY
     Support: YES
     Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 9. row ***************************
      Engine: FEDERATED
     Support: YES
     Comment: Allows to access tables on other MariaDB servers, supports transactions and more
Transactions: YES
          XA: NO
  Savepoints: YES
*************************** 10. row ***************************
      Engine: Aria
     Support: YES
     Comment: Crash-safe tables with MyISAM heritage
Transactions: NO
          XA: NO
  Savepoints: NO
10 rows in set (0.00 sec)
//mysql> SHOW DATABASES; //查看数据库信息
MariaDB [yeqixian]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| SCHOOL             |
| Syslog             |
| mysql              |
| performance_schema |
| test               |
| yeqixian           |
+--------------------+
7 rows in set (0.00 sec)
//mysql> SHOW TABLES FROM wangqingge; //不进入某数据库而列出其包含的所有表
MariaDB [(none)]> SHOW TABLES FROM yeqixian; 
+--------------------+
| Tables_in_yeqixian |
+--------------------+
| student            |
+--------------------+
1 row in set (0.00 sec)
//查看表结构

//语法:DESC [db_name.]table_name;

MariaDB [(none)]> desc yeqixian.student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(100) | NO   |     | NULL    |                |
| age   | tinyint(4)   | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
//查看某数据库的创建命令
MariaDB [(none)]> show create database yeqixian;
+----------+---------------------------------------------------------------------+
| Database | Create Database                                                     |
+----------+---------------------------------------------------------------------+
| yeqixian | CREATE DATABASE `yeqixian` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+---------------------------------------------------------------------+
1 row in set (0.01 sec)
//查看某表的创建命令

//语法:SHOW CREATE TABLE table_name;

MariaDB [(none)]> use yeqixian;
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
MariaDB [yeqixian]> show tables;
+--------------------+
| Tables_in_yeqixian |
+--------------------+
| student            |
+--------------------+
1 row in set (0.00 sec)

MariaDB [yeqixian]> show create table student;
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                    |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `age` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1 |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> show create table yeqixian.student;
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                    |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `age` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1 |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [yeqixian]> CREATE TABLE student1 (id int(11) NOT NULL primary key AUTO_INCREMENT,name varchar(100) NOT NULL,age tinyint(4) DEFAULT NULL) default charset=utf8;
Query OK, 0 rows affected (0.00 sec)
MariaDB [yeqixian]> show create table student1;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                 |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student1 | CREATE TABLE `student1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `age` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [yeqixian]> show create table student1\G
*************************** 1. row ***************************
       Table: student1
Create Table: CREATE TABLE `student1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `age` tinyint(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
//查看某表的状态

//语法:SHOW TABLE STATUS LIKE ‘table_name’\G

MariaDB [yeqixian]> show table status like 'student'; //查看wangqing表的状态
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
| Name    | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation         | Checksum | Create_options | Comment |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
| student | InnoDB |      10 | Compact    |   11 |           1489 |       16384 |               0 |            0 |   8388608 |             12 | 2020-05-22 14:44:41 | NULL        | NULL       | latin1_swedish_ci |     NULL |                |         |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
1 row in set (0.00 sec)

MariaDB [yeqixian]> show table status like 'student%'\G
*************************** 1. row ***************************
           Name: student
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 11
 Avg_row_length: 1489
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 8388608
 Auto_increment: 12
    Create_time: 2020-05-22 14:44:41
    Update_time: NULL
     Check_time: NULL
      Collation: latin1_swedish_ci
       Checksum: NULL
 Create_options: 
        Comment: 
*************************** 2. row ***************************
           Name: student1
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 0
 Avg_row_length: 0
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 8388608
 Auto_increment: 1
    Create_time: 2020-05-23 16:10:42
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: 
2 rows in set (0.00 sec)
4.1.5 获取帮助
//获取命令使用帮助

//语法:HELP keyword;

MariaDB [(none)]> 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: http://dev.mysql.com/doc/refman/5.5/en/create-database.html

4.2 DML操作

DML操作包括增(INSERT)、删(DELETE)、改(UPDATE)、查(SELECT),均属针对表的操作。

4.2.1 INSERT语句
//DML操作之增操作insert

//语法:INSERT [INTO] table_name [(column_name,…)] {VALUES | VALUE} (value1,…),(…),…

MariaDB [yeqixian]> insert student(name,age)(如果没有()里的东西则默认要填所有选项比如这里的id,name,age) value('xxx',78); //一次插入一条记录
Query OK, 1 row affected (0.00 sec)
MariaDB [yeqixian]> select * from student ;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | xxx         |   78 |
+----+-------------+------+
12 rows in set (0.00 sec)

MariaDB [yeqixian]> insert student(name,age) values('xxx',28),('sdfh',96); //一次插入多条记录
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [yeqixian]> select * from student ;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | xxx         |   78 |
| 13 | xxx         |   28 |
| 14 | sdfh        |   96 |
+----+-------------+------+
14 rows in set (0.00 sec)
4.2.2 SELECT语句
//DML操作之查操作select

//语法:SELECT column1,column2,… FROM table_name [WHERE clause] [ORDER BY ‘column_name’ [DESC]] [LIMIT [m,]n];

字段column表示法
表示符代表什么?
*所有字段
as字段别名,如col1 AS alias1
当表名用别名代替(临时生效)
MariaDB [yeqixian]> select *(可以换成选项,比如这里的id,name,age) from student;
MariaDB [yeqixian]> select id as(修改名字为:) fsd,name from student;
+-----+-------------+
| fsd | name        |
+-----+-------------+
|   1 | tom         |
|   2 | jerry       |
|   3 | wangqing    |
|   4 | sean        |
|   5 | zhangshan   |
|   6 | zhangshan   |
|   7 | lisi        |
|   8 | chenshuo    |
|   9 | wangwu      |
|  10 | qiuyi       |
|  11 | qiuxiaotian |
|  12 | xxx         |
|  13 | xxx         |
|  14 | sdfh        |
+-----+-------------+
14 rows in set (0.00 sec)
条件判断语句WHERE
操作类型常用操作符
操作符>,<,>=,<=,=,!=
BETWEEN column# AND column#
LIKE:模糊匹配
RLIKE:基于正则表达式进行模式匹配
IS NOT NULL:非空
IS NULL:空
条件逻辑操作AND
OR
NOT
操作符
MariaDB [yeqixian]> select id as fsd,name from student where age>(><>=<==!=)20;
MariaDB [yeqixian]> select id as fsd,name from student where age>20;
+-----+-----------+
| fsd | name      |
+-----+-----------+
|   2 | jerry     |
|   3 | wangqing  |
|   4 | sean      |
|   5 | zhangshan |
|  12 | xxx       |
|  13 | xxx       |
|  14 | sdfh      |
+-----+-----------+
7 rows in set (0.00 sec)

MariaDB [yeqixian]> quit
Bye
[root@yeqixian1 ~]# mysql -uroot -p123465 -e 'select id as fsd,name from yeqixian.student where age>20;' |awk 'NR==5'
5	zhangshan
MariaDB [yeqixian]> select * from student where age between 20 and 25;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  6 | zhangshan   |   20 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
5 rows in set (0.00 sec)
MariaDB [yeqixian]> select * from student where age like '2%';
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
| 11 | qiuxiaotian |   20 |
| 13 | xxx         |   28 |
+----+-------------+------+
8 rows in set (0.00 sec)
MariaDB [yeqixian]> select * from student where name rlike '^z';
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  5 | zhangshan |   26 |
|  6 | zhangshan |   20 |
+----+-----------+------+
2 rows in set (0.00 sec)
MariaDB [yeqixian]> select * from student where age is not null;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | xxx         |   78 |
| 13 | xxx         |   28 |
| 14 | sdfh        |   96 |
+----+-------------+------+
13 rows in set (0.00 sec)

MariaDB [yeqixian]> select * from student where age is  null;
+----+------+------+
| id | name | age  |
+----+------+------+
|  7 | lisi | NULL |
| 15 | liu  | NULL |
+----+------+------+
2 rows in set (0.01 sec)
条件逻辑操作
MariaDB [yeqixian]> select id as fsd,name from student where age > 20 and(AND,OR,NOT) name = 'xxx';
+-----+------+
| fsd | name |
+-----+------+
|  12 | xxx  |
|  13 | xxx  |
+-----+------+
2 rows in set (0.01 sec)
ORDER BY:排序,默认为升序(ASC)
ORDER BY语句意义
ORDER BY ‘column_name’根据column_name进行升序排序
ORDER BY ‘column_name’ DESC根据column_name进行降序排序
ORDER BY ’column_name’ LIMIT 2根据column_name进行升序排序
并只取前2个结果
ORDER BY ’column_name’ DESC LIMIT 2根据column_name进行降序排序
并只取前2个结果
ORDER BY ‘column_name’ LIMIT 1,2根据column_name进行升序排序
并且略过第1个结果取后面的2个结果
MariaDB [yeqixian]> select * from student order by age;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
| 18 | ^[a-z]$     | NULL |
| 17 | ^[a-z]{3}   | NULL |
| 16 | ^[a-z]{3}$  | NULL |
| 15 | liu         | NULL |
|  7 | lisi        | NULL |
|  9 | wangwu      |    3 |
|  8 | chenshuo    |   10 |
| 10 | qiuyi       |   15 |
|  6 | zhangshan   |   20 |
|  1 | tom         |   20 |
| 11 | qiuxiaotian |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  5 | zhangshan   |   26 |
| 13 | xxx         |   28 |
|  4 | sean        |   28 |
| 12 | xxx         |   78 |
| 14 | sdfh        |   96 |
+----+-------------+------+
18 rows in set (0.00 sec)

MariaDB [yeqixian]> select * from student order by age  limit 1,2;
+----+------------+------+
| id | name       | age  |
+----+------------+------+
| 17 | ^[a-z]{3}  | NULL |
| 16 | ^[a-z]{3}$ | NULL |
+----+------------+------+
2 rows in set (0.01 sec)

MariaDB [yeqixian]> select * from student order by age desc;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
| 14 | sdfh        |   96 |
| 12 | xxx         |   78 |
|  4 | sean        |   28 |
| 13 | xxx         |   28 |
|  5 | zhangshan   |   26 |
|  3 | wangqing    |   25 |
|  2 | jerry       |   23 |
|  1 | tom         |   20 |
|  6 | zhangshan   |   20 |
| 11 | qiuxiaotian |   20 |
| 10 | qiuyi       |   15 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
|  7 | lisi        | NULL |
| 15 | liu         | NULL |
| 16 | ^[a-z]{3}$  | NULL |
| 17 | ^[a-z]{3}   | NULL |
| 18 | ^[a-z]$     | NULL |
+----+-------------+------+
18 rows in set (0.00 sec)

MariaDB [yeqixian]> select * from student order by age desc limit 2;
+----+------+------+
| id | name | age  |
+----+------+------+
| 14 | sdfh |   96 |
| 12 | xxx  |   78 |
+----+------+------+
2 rows in set (0.00 sec)

MariaDB [yeqixian]> select * from student order by age desc limit 1,2;
+----+------+------+
| id | name | age  |
+----+------+------+
| 12 | xxx  |   78 |
|  4 | sean |   28 |
+----+------+------+
2 rows in set (0.00 sec)
MariaDB [yeqixian]> select * from student where age is not null order by age;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  9 | wangwu      |    3 |
|  8 | chenshuo    |   10 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
|  1 | tom         |   20 |
|  6 | zhangshan   |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  5 | zhangshan   |   26 |
|  4 | sean        |   28 |
| 13 | xxx         |   28 |
| 12 | xxx         |   78 |
| 14 | sdfh        |   96 |
+----+-------------+------+
13 rows in set (0.00 sec)

MariaDB [yeqixian]> select * from student where age is not null order by age  limit 1;
+----+--------+------+
| id | name   | age  |
+----+--------+------+
|  9 | wangwu |    3 |
+----+--------+------+
1 row in set (0.00 sec)
group by:排序,默认为升序(每一种只会显示一次,只有最前面的会显示)

常用聚合函数
count() 计数
sum() 求和
avg() 平均数
max() 最大值
min() 最小值

MariaDB [yeqixian]> select * from student ;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | xxx         |   78 |
| 13 | xxx         |   28 |
| 14 | sdfh        |   96 |
| 15 | liu         | NULL |
| 16 | ^[a-z]{3}$  | NULL |
| 17 | ^[a-z]{3}   | NULL |
| 18 | ^[a-z]$     | NULL |
+----+-------------+------+
18 rows in set (0.00 sec)

MariaDB [yeqixian]> select * from student group by age;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  7 | lisi      | NULL |
|  9 | wangwu    |    3 |
|  8 | chenshuo  |   10 |
| 10 | qiuyi     |   15 |
|  1 | tom       |   20 |
|  2 | jerry     |   23 |
|  3 | wangqing  |   25 |
|  5 | zhangshan |   26 |
|  4 | sean      |   28 |
| 12 | xxx       |   78 |
| 14 | sdfh      |   96 |
+----+-----------+------+
11 rows in set (0.00 sec)
4.2.3 update语句
//DML操作之改操作update

//语法:UPDATE table_name SET column1 = new_value1[,column2 = new_value2,…] [WHERE clause] [ORDER BY ‘column_name’ [DESC]] [LIMIT [m,]n];

MariaDB [yeqixian]> select * from student group by age;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  7 | lisi      | NULL |
|  9 | wangwu    |    3 |
|  8 | chenshuo  |   10 |
| 10 | qiuyi     |   15 |
|  1 | tom       |   20 |
|  2 | jerry     |   23 |
|  3 | wangqing  |   25 |
|  5 | zhangshan |   26 |
|  4 | sean      |   28 |
| 12 | xxx       |   78 |
| 14 | sdfh      |   96 |
+----+-----------+------+
11 rows in set (0.00 sec)
MariaDB [yeqixian]> update  student set age = 30 where id = 7;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [yeqixian]> select * from student group by age;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
| 15 | liu       | NULL |
|  9 | wangwu    |    3 |
|  8 | chenshuo  |   10 |
| 10 | qiuyi     |   15 |
|  1 | tom       |   20 |
|  2 | jerry     |   23 |
|  3 | wangqing  |   25 |
|  5 | zhangshan |   26 |
|  4 | sean      |   28 |
|  7 | lisi      |   30 |
| 12 | xxx       |   78 |
| 14 | sdfh      |   96 |
+----+-----------+------+
12 rows in set (0.00 sec)
4.2.4 delete语句
//DML操作之删操作delete

//语法:DELETE FROM table_name [WHERE clause] [ORDER BY ‘column_name’ [DESC]] [LIMIT [m,]n];

MariaDB [yeqixian]> delete from student where name like '^[a-z]%';
Query OK, 3 rows affected (0.00 sec)

MariaDB [yeqixian]> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        |   30 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | xxx         |   78 |
| 13 | xxx         |   28 |
| 14 | sdfh        |   96 |
| 15 | liu         | NULL |
+----+-------------+------+
15 rows in set (0.00 sec)
MariaDB [yeqixian]> select * from student ;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        |   30 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | xxx         |   78 |
| 13 | xxx         |   28 |
| 14 | sdfh        |   96 |
| 15 | liu         | NULL |
+----+-------------+------+
15 rows in set (0.00 sec)

MariaDB [yeqixian]> select * from student order by age limit 1;
+----+------+------+
| id | name | age  |
+----+------+------+
| 15 | liu  | NULL |
+----+------+------+
1 row in set (0.00 sec)

MariaDB [yeqixian]> delete from student order by age limit 1;
Query OK, 1 row affected (0.00 sec)

MariaDB [yeqixian]> select * from student ;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        |   30 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
| 12 | xxx         |   78 |
| 13 | xxx         |   28 |
| 14 | sdfh        |   96 |
+----+-------------+------+
14 rows in set (0.00 sec)
MariaDB [yeqixian]> delete from student(清空表内内容)
4.2.5 truncate语句

//语法:TRUNCATE table_name;
truncate与delete都只是删除内容,truncate如果没有备份,则无法恢复;delete可以通过日志恢复。
truncate与delete的区别:

语句类型特点
deleteDELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项
可以通过回滚事务日志恢复数据
truncate删除表中所有数据,且无法恢复,新添加的行计数值重置为初始值
对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据,不能用于加入了索引视图的表
MariaDB [SCHOOL]> select * from xxx;
+----+------+-----+
| id | name | age |
+----+------+-----+
|  1 | sdad |  32 |
|  2 | sdad |  22 |
+----+------+-----+
2 rows in set (0.00 sec)

MariaDB [SCHOOL]> truncate xxx;
Query OK, 0 rows affected (0.00 sec)

MariaDB [SCHOOL]> select * from xxx;
Empty set (0.00 sec)
MariaDB [SCHOOL]> desc xxx;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(50) | NO   |     | NULL    |       |
| age   | tinyint(4)  | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

4.3 DCL操作

4.3.1 创建授权grant
权限类型(priv_type)
权限类型代表什么?
ALL所有权限
SELECT读取内容的权限
INSERT插入内容的权限
UPDATE更新内容的权限
DELETE删除内容的权限
指定要操作的对象db_name.table_name
表示方式意义
.所有库的所有表
db_name指定库的所有表
db_name.table_name指定库的指定表

WITH GRANT OPTION:被授权的用户可将自己的权限副本转赠给其他用户,说白点就是将自己的权限完全复制给另一个用户。一般不能去使用。

//语法:GRANT priv_type,… ON [object_type] db_name.table_name TO ‘username’@‘host’ [IDENTIFIED BY ‘password’(如果前面没有设置密码时,在这里设置密码。)] [WITH GRANT OPTION];
MariaDB [SCHOOL]> create user 'yqx'@'192.168.116.145' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [SCHOOL]> grant insert on SCHOOL.xxx to 'yqx'@'192.168.116.145';
Query OK, 0 rows affected (0.00 sec)
MariaDB [SCHOOL]> grant insert on SCHOOL.xxx to 'lll'@'192.168.116.145' identified by '123';(当设置权限的用户不存在时,会自动创建;mysql8.0不能使用此操作。)
Query OK, 0 rows affected (0.00 sec)
4.3.2 查看授权
//语法:show GRANTS [for ‘username’@‘host’];
MariaDB [(none)]> show GRANTS for 'wangqing'@'localhost';
+--------------------------------------------------------------------------------------------------------------------------+
| Grants for wangqing@localhost                                                                                            |
+--------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'wangqing'@'localhost' IDENTIFIED BY PASSWORD '*B975BAE9B0F5C8056FD985B6E685F8B356FAF8BD' |
+--------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> show GRANTS for 'lll'@'192.168.116.145';
+------------------------------------------------------------------------------------------------------------------+
| Grants for lll@192.168.116.145                                                                                   |
+------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'lll'@'192.168.116.145' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
| GRANT INSERT ON `SCHOOL`.`xxx` TO 'lll'@'192.168.116.145'                                                        |
+------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

MariaDB [(none)]> show GRANTS for 'lll';
ERROR 1141 (42000): There is no such grant defined for user 'lll' on host '%'
MariaDB [(none)]> show GRANTS ;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*36614794E82EFEA8F2BD78B4DFAF0DCB7A8C223C' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
4.3.3 取消授权REVOKE
//语法:REVOKE priv_type,… ON db_name.table_name FROM ‘username’@‘host’;
MariaDB [(none)]> GRANT ALL ON *.* TO 'wangqing'@'localhost' IDENTIFIED BY 'wangqing123!';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> REVOKE SELECT ON *.* FROM 'wangqing'@'localhost';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show GRANTS for'wangqing'@'localhost';
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for wangqing@localhost                                                                                                                                                                                                                                                                                                                                                                                                 |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE ON *.* TO 'wangqing'@'localhost' IDENTIFIED BY PASSWORD '*B975BAE9B0F5C8056FD985B6E685F8B356FAF8BD' |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
注意:mysql服务进程启动时会读取mysql库中的所有授权表至内存中:

GRANT或REVOKE等执行权限操作会保存于表中,mysql的服务进程会自动重读授权表,并更新至内存中
对于不能够或不能及时重读授权表的命令,可手动让mysql的服务进程重读授权表

mysql> FLUSH PRIVILEGES;(刷新数据库,重读授权表)

5. 破解密码

5.1 如果忘记密码,先停止mysql服务

[root@yeqixian1 ~]# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@yeqixian1 ~]# systemctl stop mariadb

5.2 打开MySQL的配置文件/etc/my.cnf.在[mysql]下面添加一行代码skip-grant-tables。这行代码意思是跳过密码验证直接登录。

[mysqld]
skip-grant-tables
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
#skip_name_resolve = on
#innodb_file_per_table = on

5.3 重启数据库,进入数据库。

[root@yeqixian1 ~]# systemctl restart mariadb
[root@yeqixian1 ~]# mysql -uroot -p
Enter password: (不需要输密码)
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

5.4 选择MySQL这个库,查看user表中有哪些字段。

MariaDB [(none)]> use mysql
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
MariaDB [mysql]> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)

MariaDB [mysql]> desc user;
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field                  | Type                              | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host                   | char(60)                          | NO   | PRI |         |       |
| User                   | char(16)                          | NO   | PRI |         |       |
| Password               | char(41)                          | NO   |     |         |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Update_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Delete_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Create_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Drop_priv              | enum('N','Y')                     | NO   |     | N       |       |
| Reload_priv            | enum('N','Y')                     | NO   |     | N       |       |
| Shutdown_priv          | enum('N','Y')                     | NO   |     | N       |       |
| Process_priv           | enum('N','Y')                     | NO   |     | N       |       |
| File_priv              | enum('N','Y')                     | NO   |     | N       |       |
| Grant_priv             | enum('N','Y')                     | NO   |     | N       |       |
| References_priv        | enum('N','Y')                     | NO   |     | N       |       |
| Index_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Alter_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Show_db_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Super_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Create_tmp_table_priv  | enum('N','Y')                     | NO   |     | N       |       |
| Lock_tables_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Execute_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Repl_slave_priv        | enum('N','Y')                     | NO   |     | N       |       |
| Repl_client_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Create_view_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Show_view_priv         | enum('N','Y')                     | NO   |     | N       |       |
| Create_routine_priv    | enum('N','Y')                     | NO   |     | N       |       |
| Alter_routine_priv     | enum('N','Y')                     | NO   |     | N       |       |
| Create_user_priv       | enum('N','Y')                     | NO   |     | N       |       |
| Event_priv             | enum('N','Y')                     | NO   |     | N       |       |
| Trigger_priv           | enum('N','Y')                     | NO   |     | N       |       |
| Create_tablespace_priv | enum('N','Y')                     | NO   |     | N       |       |
| ssl_type               | enum('','ANY','X509','SPECIFIED') | NO   |     |         |       |
| ssl_cipher             | blob                              | NO   |     | NULL    |       |
| x509_issuer            | blob                              | NO   |     | NULL    |       |
| x509_subject           | blob                              | NO   |     | NULL    |       |
| max_questions          | int(11) unsigned                  | NO   |     | 0       |       |
| max_updates            | int(11) unsigned                  | NO   |     | 0       |       |
| max_connections        | int(11) unsigned                  | NO   |     | 0       |       |
| max_user_connections   | int(11)                           | NO   |     | 0       |       |
| plugin                 | char(64)                          | NO   |     |         |       |
| authentication_string  | text                              | NO   |     | NULL    |       |
+------------------------+-----------------------------------+------+-----+---------+-------+
42 rows in set (0.00 sec)

MariaDB [mysql]> desc user\G
*************************** 1. row ***************************
  Field: Host
   Type: char(60)
   Null: NO
    Key: PRI
Default: 
  Extra: 
*************************** 2. row ***************************
  Field: User
   Type: char(16)
   Null: NO
    Key: PRI
Default: 
  Extra: 
*************************** 3. row ***************************
  Field: Password
   Type: char(41)
   Null: NO
    Key: 
Default: 
  Extra: 
*************************** 4. row ***************************
  Field: Select_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 5. row ***************************
  Field: Insert_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 6. row ***************************
  Field: Update_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 7. row ***************************
  Field: Delete_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 8. row ***************************
  Field: Create_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 9. row ***************************
  Field: Drop_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 10. row ***************************
  Field: Reload_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 11. row ***************************
  Field: Shutdown_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 12. row ***************************
  Field: Process_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 13. row ***************************
  Field: File_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 14. row ***************************
  Field: Grant_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 15. row ***************************
  Field: References_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 16. row ***************************
  Field: Index_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 17. row ***************************
  Field: Alter_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 18. row ***************************
  Field: Show_db_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 19. row ***************************
  Field: Super_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 20. row ***************************
  Field: Create_tmp_table_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 21. row ***************************
  Field: Lock_tables_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 22. row ***************************
  Field: Execute_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 23. row ***************************
  Field: Repl_slave_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 24. row ***************************
  Field: Repl_client_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 25. row ***************************
  Field: Create_view_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 26. row ***************************
  Field: Show_view_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 27. row ***************************
  Field: Create_routine_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 28. row ***************************
  Field: Alter_routine_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 29. row ***************************
  Field: Create_user_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 30. row ***************************
  Field: Event_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 31. row ***************************
  Field: Trigger_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 32. row ***************************
  Field: Create_tablespace_priv
   Type: enum('N','Y')
   Null: NO
    Key: 
Default: N
  Extra: 
*************************** 33. row ***************************
  Field: ssl_type
   Type: enum('','ANY','X509','SPECIFIED')
   Null: NO
    Key: 
Default: 
  Extra: 
*************************** 34. row ***************************
  Field: ssl_cipher
   Type: blob
   Null: NO
    Key: 
Default: NULL
  Extra: 
*************************** 35. row ***************************
  Field: x509_issuer
   Type: blob
   Null: NO
    Key: 
Default: NULL
  Extra: 
*************************** 36. row ***************************
  Field: x509_subject
   Type: blob
   Null: NO
    Key: 
Default: NULL
  Extra: 
*************************** 37. row ***************************
  Field: max_questions
   Type: int(11) unsigned
   Null: NO
    Key: 
Default: 0
  Extra: 
*************************** 38. row ***************************
  Field: max_updates
   Type: int(11) unsigned
   Null: NO
    Key: 
Default: 0
  Extra: 
*************************** 39. row ***************************
  Field: max_connections
   Type: int(11) unsigned
   Null: NO
    Key: 
Default: 0
  Extra: 
*************************** 40. row ***************************
  Field: max_user_connections
   Type: int(11)
   Null: NO
    Key: 
Default: 0
  Extra: 
*************************** 41. row ***************************
  Field: plugin
   Type: char(64)
   Null: NO
    Key: 
Default: 
  Extra: 
*************************** 42. row ***************************
  Field: authentication_string
   Type: text
   Null: NO
    Key: 
Default: NULL
  Extra: 
42 rows in set (0.00 sec)

5.5 更改密码,并刷新数据库,然后退出。

MariaDB [mysql]> update user set password=password('0123456') where user="root";
Query OK, 8 rows affected (0.01 sec)
Rows matched: 8  Changed: 8  Warnings: 0

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> exit
Bye

5.6 去除skip-grant-tables,然后登录。

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
#skip_name_resolve = on
#innodb_file_per_table = on
[root@yeqixian1 ~]# mysql -uroot -p
Enter password: (输入密码)
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

实例运用

1.搭建mysql服务
2.创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age)

MariaDB [(none)]> drop database yeqixian;
Query OK, 2 rows affected (0.02 sec)

MariaDB [(none)]> create database yeqixian;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use yeqixian;
Database changed
MariaDB [yeqixian]> create table student (id int primary key auto_increment not null,name varchar(100) not null,age tinyint);
Query OK, 0 rows affected (0.00 sec)

MariaDB [yeqixian]> desc student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(100) | NO   |     | NULL    |                |
| age   | tinyint(4)   | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

3.查看下该新建的表有无内容(用select语句)

MariaDB [yeqixian]> select * from student;
Empty set (0.00 sec)

4.往新建的student表中插入数据(用insert语句)

MariaDB [yeqixian]> INSERT INTO student (name,age) VALUES ('tom',20),('jerry',23),('wangqing',25),('sean',28),('zhangshan',26),('zhangshan',20),('lisi',null),('chenshuo',10),('wangwu',3),('qiuyi',15),('qiuxiaotian',20);
Query OK, 11 rows affected (0.00 sec)
Records: 11  Duplicates: 0  Warnings: 0

MariaDB [yeqixian]> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)

5.修改lisi的年龄为50

MariaDB [yeqixian]> update student set age = 50 where name = 'lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [yeqixian]> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        |   50 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)

6.以age字段降序排序

MariaDB [yeqixian]> select * from student order by age desc;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  7 | lisi        |   50 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  3 | wangqing    |   25 |
|  2 | jerry       |   23 |
|  1 | tom         |   20 |
|  6 | zhangshan   |   20 |
| 11 | qiuxiaotian |   20 |
| 10 | qiuyi       |   15 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
+----+-------------+------+
11 rows in set (0.00 sec)

7.查询student表中年龄最小的3位同学跳过前2位

MariaDB [yeqixian]> select * from student order by age limit 2;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  9 | wangwu   |    3 |
|  8 | chenshuo |   10 |
+----+----------+------+
2 rows in set (0.00 sec)

MariaDB [yeqixian]> select * from student order by age limit 3;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  9 | wangwu   |    3 |
|  8 | chenshuo |   10 |
| 10 | qiuyi    |   15 |
+----+----------+------+
3 rows in set (0.00 sec)

MariaDB [yeqixian]> select * from student order by age limit 2,1;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
| 10 | qiuyi |   15 |
+----+-------+------+
1 row in set (0.01 sec)

8.查询student表中年龄最大的4位同学

MariaDB [yeqixian]> select * from student order by age desc limit 4;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  7 | lisi      |   50 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
|  3 | wangqing  |   25 |
+----+-----------+------+
4 rows in set (0.00 sec)

9.查询student表中名字叫zhangshan的记录

MariaDB [yeqixian]> select * from student where name rlike '^zhang';
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  5 | zhangshan |   26 |
|  6 | zhangshan |   20 |
+----+-----------+------+
2 rows in set (0.01 sec)

10.查询student表中名字叫zhangshan且年龄大于20岁的记录

MariaDB [yeqixian]> select * from student where name rlike '^zhang' and age > 20;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  5 | zhangshan |   26 |
+----+-----------+------+
1 row in set (0.00 sec)

11.查询student表中年龄在23到30之间的记录

MariaDB [yeqixian]> select * from student where age between 23 and 30;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  2 | jerry     |   23 |
|  3 | wangqing  |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
+----+-----------+------+
4 rows in set (0.00 sec)

12.修改wangwu的年龄为100

MariaDB [yeqixian]> update student set age = 100 where name = 'wangwu';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [yeqixian]> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | wangqing    |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        |   50 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |  100 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)

13.删除student中名字叫zhangshan且年龄小于等于20的记录

MariaDB [yeqixian]> select * from student where name rlike '^zhang' and age <= 20;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  6 | zhangshan |   20 |
+----+-----------+------+
1 row in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值