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语句有三种类型:
- DDL:Data Defination Language,数据定义语言
- CREATE:创建
- DROP:删除
- ALTER:修改
- DML:Data Manipulation Language,数据操纵语言
- INSERT:向表中插入数据
- DELETE:删除表中数据
- UPDATE:更新表中数据
- SELECT:查询表中数据
- DCL:Data Control Language,数据控制语言
- GRANT:授权
- REVOKE:移除授权
2. mysql安装与配置
2.1 mysql安装
mysql安装方式有三种:
- 源代码:编译安装
- 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
- 程序包管理器管理的程序包:
- rpm:有两种
- OS Vendor:操作系统发行商提供的
- 项目官方提供的
- deb
- rpm:有两种
#配置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
#配置mysql的yum源
[root@node01-linux ~]# yum -y install wget
···
[root@node01-linux ~]# wget -O /usr/src/mysql57-community-release-el7-10.noarch.rpm http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@node01-linux ~]# rpm -Uvh /usr/src/mysql57-community-release-el7-10.noarch.rpm
warning: /usr/src/mysql57-community-release-el7-10.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql57-community-release-el7-10 ################################# [100%]
#安装mysql5.7
[root@node01-linux ~]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel
···
Installed:
mysql-community-client.x86_64 0:5.7.30-1.el7
mysql-community-common.x86_64 0:5.7.30-1.el7
mysql-community-devel.x86_64 0:5.7.30-1.el7
mysql-community-libs.x86_64 0:5.7.30-1.el7
mysql-community-libs-compat.x86_64 0:5.7.30-1.el7
mysql-community-server.x86_64 0:5.7.30-1.el7
Dependency Installed:
net-tools.x86_64 0:2.0-0.25.20131004git.el7
Dependency Updated:
postfix.x86_64 2:2.10.1-9.el7
Replaced:
mariadb-libs.x86_64 1:5.5.56-2.el7
Complete!
2.2 mysql配置
#启动mysql并设置开机自动启动
[root@node01-linux ~]# systemctl enable --now mysqld
[root@node01-linux ~]# systemctl status mysqld
#确保3306端口已经监听起来
[root@node01-linux ~]# ss -antl
#在日志文件中找出临时密码
[root@node01-linux ~]# grep "password" /var/log/mysqld.log
#使用获取到的临时密码登录mysql
[root@node01-linux ~]# mysql -uroot -p'ykp<ibcSF5ra'
或者
[root@node01-linux ~]# 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.30
Copyright (c) 2000, 2020, 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> //看到有这样的标识符则表示成功登录了
//57版本
#查看密码策略
show variables like '%validate_password_policy%';
show variables like '%validate_password_length%';
#修改密码策略
mysql> set global validate_password_policy=0; #设置为弱口令
Query OK, 0 rows affected (0.01 sec)
mysql> set global validate_password_length=1; #密码最小长度为1
Query OK, 0 rows affected (0.00 sec)
//永久修改密码策略解决办法:
1、查看 mysql 初始的密码策略,
输入语句 “ SHOW VARIABLES LIKE 'validate_password%'; ” 进行查看,
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.01 sec)
2、首先需要设置密码的验证强度等级,设置 validate_password_policy 的全局参数为 LOW 即可,
输入设值语句 “ set global validate_password_policy=LOW; ” 进行设值,
mysql> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 1 |
+--------------------------------------+-------+
7 rows in set (0.00 sec)
//修改mysql登录密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql123456';
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
//为避免mysql自动升级,这里需要卸载最开始安装的yum源
[root@node01-linux ~]# rpm -e mysql57-community-release
2.3 mariadb安装与配置
安装mariadb相关包
[root@node02-linux ~]# yum -y install mariadb*
2.4 mariadb安装与配置
#启动mariadb并设置开机自动启动
[root@node02-linux ~]# systemctl enable --now mariadb
[root@node02-linux ~]# systemctl status mariadb
#确保3306端口已经监听起来
#默认root账户登陆,不需要密码
[root@node02-linux ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-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
//修改mariadb登录密码,用password函数加密
MariaDB [(none)]> set password = password('mysql123456');
//登陆
[root@node02-linux ~]# mysql -uroot -p'mysql123456'
MariaDB [(none)]> quit
Bye
3. mysql的程序组成
客户端
- mysql:CLI交互式客户端程序( 图形化
navicat
收费workbench
免费开源 ) - mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
- mysqldump:mysql备份工具
- mysqladmin
服务器端
- mysqld
3.1 mysql工具使用
//语法:mysql [OPTIONS] [database]
//常用的OPTIONS:
-uUSERNAME //指定用户名,默认为root
-hHOST //指定服务器主机,默认为localhost,推荐使用ip地址
-pPASSWORD //指定用户的密码
-P# //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
-V //查看当前使用的mysql版本
-e //不登录mysql执行sql语句后退出,常用于脚本
[root@node01-linux ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using EditLine wrapper
//mysql数据库设置远程连接权限
mysql> grant all on *.* to 'root'@192.168.67.131 identified by 'mysql123456';
[root@node01-linux ~]# mysql -uroot -p'mysql123456' -h192.168.67.131
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 15
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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>
//注意,不推荐直接在命令行里直接用-pPASSWORD的方式登录,而是使用-p选项,然后交互式输入密码
[root@node01-linux ~]# mysql -uroot -p -h192.168.67.131
Enter password: mysql123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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执行sql语句后退出,常用于脚本
[root@node01-linux ~]# mysql -uroot -p -h192.168.67.131 -e 'show databases;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@node01-linux ~]# mysql -uroot -p'mysql123456' -h192.168.67.131 -e 'use mysql;show tables'
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
[root@node01-linux ~]# mysql -uroot -p'mysql123456' -h192.168.67.131 -e 'use mysql;show tables'|grep plugin
mysql: [Warning] Using a password on the command line interface can be insecure.
plugin
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@node01-linux ~]# mysql -uroot -p'mysql123456' -S /var/lib/mysql/mysql.sock
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 25
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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> quit
Bye
4. mysql数据库操作
4.1 DDL操作
4.1.1 数据库操作
//创建数据库
//语法:CREATE DATABASE [IF NOT EXISTS] 'DB_NAME';
//创建数据库school
mysql> CREATE DATABASE IF NOT EXISTS school;
Query OK, 1 row affected (0.00 sec)
//查看当前实例有哪些数据库
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
5 rows in set (0.01 sec)
//删除数据库
//语法:DROP DATABASE [IF EXISTS] 'DB_NAME';
//删除数据库shcool
mysql> DROP DATABASE IF EXISTS school;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
4.1.2 表操作
//创建表
//语法:CREATE TABLE table_name (col1 datatype 修饰符,col2 datatype 修饰符) ENGINE='存储引擎类型';
//在数据库wangqingge里创建表school
mysql> CREATE DATABASE school;
Query OK, 1 row affected (0.00 sec)
mysql> USE school
Database changed
mysql> CREATE TABLE linux (id int NOT NULL,name VARCHAR(50) NOT NULL,age tinyint NOT NULL);
Query OK, 0 rows affected (0.01 sec)
//查看当前数据库有哪些表
mysql> SHOW TABLES;
+------------------+
| Tables_in_school |
+------------------+
| linux |
+------------------+
1 row in set (0.00 sec)
//查看具体表的结构
mysql> DESC linux;
+-------+-------------+------+-----+---------+-------+
| 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.04 sec)
//查找数据
mysql> SELECT * FROM linux;
Empty set (0.00 sec)
//删除表
mysql> DROP TABLE linux;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW TABLES;
Empty set (0.00 sec)
//创建主键约束表
mysql> create table zhangchaoqun(id int primary key auto_increment not null,name varchar(100) not null,age tinyint);
Query OK, 0 rows affected (0.01 sec)
mysql> desc zhangchaoqun;
+-------+--------------+------+-----+---------+----------------+
| 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)
4.1.3 用户操作
mysql用户帐号由两部分组成,如’USERNAME’@‘HOST’,表示此USERNAME只能从此HOST上远程登录
这里(‘USERNAME’@‘HOST’)的HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:
IP地址,如:172.16.12.129
通配符
- %:匹配任意长度的任意字符,常用于设置允许从任何主机登录
- _:匹配任意单个字符
//数据库用户创建
//语法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
//创建数据库用户linux
mysql> CREATE USER 'linux'@'127.0.0.1' IDENTIFIED BY 'mysql123456';
Query OK, 0 rows affected (0.01 sec)
//使用新创建的用户和密码登陆
[root@node01-linux ~]# mysql -ulinux -p'mysql123456' -h127.0.0.1
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 3
Server version: 5.7.30 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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>
//删除数据库用户
//语法:DROP USER 'username'@'host';
mysql> DROP USER 'linux'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)
4.1.4 查看命令SHOW
mysql> SHOW CHARACTER SET; //查看支持的所有字符集
mysql> SHOW ENGINES; //查看当前数据库支持的所有存储引擎
mysql> SHOW ENGINES\G //以记录的形式显示数据库支持的所有存储引擎
mysql> SHOW DATABASES; //查看数据库信息
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> SHOW TABLES FROM school; //不进入某数据库而列出其包含的所有表
+------------------+
| Tables_in_school |
+------------------+
| linux |
+------------------+
1 row in set (0.00 sec)
//查看表结构
//语法:DESC [db_name.]table_name;
mysql> DESC school.linux;
+-------+-------------+------+-----+---------+-------+
| 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.03 sec)
//查看某数据库的创建命令
//语法:SHOW CREATE DATABASE table_name;
mysql> SHOW CREATE DATABASE school;
+----------+-------------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------------+
| school | CREATE DATABASE `school` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)
//查看某表的创建命令
//语法:SHOW CREATE TABLE table_name;
mysql> SHOW CREATE TABLE school.linux;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| linux | CREATE TABLE `linux` (
`id` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`age` tinyint(4) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
//查看某表的状态
//语法:SHOW TABLE STATUS LIKE 'table_name'\G
mysql> USE school; //进入数据库school
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
mysql> SHOW TABLE STATUS LIKE 'linux'\G //查看wangqing表的状态
*************************** 1. row ***************************
Name: linux
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2020-05-21 16:03:24
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
4.1.5 获取帮助
//获取命令使用帮助
//语法:HELP keyword;
mysql> HELP CREATE TABLE; //获取创建表的帮助
Name: 'CREATE TABLE'
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[partition_options]
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options]
[partition_options]
[IGNORE | REPLACE]
[AS] query_expression
···
···
···
4.2 DML操作
**DML操作包括增(INSERT)、删(DELETE)、改(UPDATE)、查(SELECT),均属针对表的操作。**
4.2.1 INSERT语句
//DML操作之增操作insert
//语法:INSERT [INTO] table_name [(column_name,...)] {VALUES | VALUE} (value1,...),(...),...
mysql> use school;
Database changed
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| linux |
+------------------+
1 row in set (0.00 sec)
mysql> INSERT INTO linux (id,name,age) VALUE(1,'tom',19);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO linux VALUES(2,'jerry',20),(3,'zhang',21),(4,'li',22),(5,'wang',23); //一次插入多条记录
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
4.2.2 SELECT语句
字段column表示法
表示符 | 代表什么? |
---|---|
* | 所有字段 |
as | 字段别名,如col1 AS alias1 当表名很长时用别名代替 |
条件判断语句WHERE
操作类型 | 常用操作符 |
---|---|
操作符 | >,<,>=,<=,=,!= BETWEEN column# AND column# LIKE:模糊匹 RLIKE:基于正则表达式进行模式匹配 IS NOT NULL:非空 IS NULL:空 |
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’ LIMIT 1,2 | 根据column_name进行升序排序 并且略过第1个结果取后面的2个结果 |
GROUP BY 语句 | 意义 |
---|---|
SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name; | GROUP BY 语句用于结合聚合函数,根据一个或多个列对结果集进行分组。 |
//DML操作之查操作select
//语法:SELECT column1,column2,... FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
mysql> use school;
Database changed
mysql> SELECT * FROM linux;
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 1 | tom | 19 |
| 2 | jerry | 20 |
| 3 | zhang | 21 |
| 4 | li | 22 |
| 5 | wang | 23 |
+----+-------+-----+
5 rows in set (0.00 sec)
mysql> select id as 学号,age from linux; //字段别名,临时生成表
+--------+-----+
| 学号 | age |
+--------+-----+
| 1 | 19 |
| 2 | 20 |
| 3 | 21 |
| 4 | 22 |
| 5 | 23 |
+--------+-----+
5 rows in set (0.00 sec)
mysql> SELECT name FROM linux; //查看name字段
+-------+
| name |
+-------+
| tom |
| jerry |
| zhang |
| li |
| wang |
+-------+
5 rows in set (0.00 sec)
mysql> select * from linux where age = 20; //查看age为20记录
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 2 | jerry | 20 |
+----+-------+-----+
1 row in set (0.00 sec)
mysql> select name from linux where age = 20; //查看age为20,只看name名字
+-------+
| name |
+-------+
| jerry |
+-------+
1 row in set (0.00 sec)
[root@node01-linux ~]# mysql -uroot -p'mysql123456' -e 'select name from school.linux where age = 20;'|awk 'NR==2' //在外面查询age为20的name为jerry名称
mysql: [Warning] Using a password on the command line interface can be insecure.
jerry
mysql> select * from linux where age = 20 and name = 'jerry'; //查看age为20,并且name为jerry
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 2 | jerry | 20 |
+----+-------+-----+
1 row in set (0.00 sec)
mysql> select * from linux where age = 20 or name = 'zhang'; //查看age为20或name为zhang
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 2 | jerry | 20 |
| 3 | zhang | 21 |
+----+-------+-----+
2 rows in set (0.01 sec)
mysql> select * from linux where age between 20 and 22; //age在20和22之间的
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 2 | jerry | 20 |
| 3 | zhang | 21 |
| 4 | li | 22 |
+----+-------+-----+
3 rows in set (0.00 sec)
mysql> select * from linux where age >= 20 and age <= 22; //age在大于等于20,小于等于22之间
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 2 | jerry | 20 |
| 3 | zhang | 21 |
| 4 | li | 22 |
+----+-------+-----+
3 rows in set (0.00 sec)
mysql> select * from linux where age like '2_'; //age像2的,_ 表示匹配任意单个字符
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 2 | jerry | 20 |
| 3 | zhang | 21 |
| 4 | li | 22 |
| 5 | wang | 23 |
+----+-------+-----+
4 rows in set (0.00 sec)
mysql> select * from linux where age like '2%'; //age像2的,% 表示匹配任意长度的任意字符
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 2 | jerry | 20 |
| 3 | zhang | 21 |
| 4 | li | 22 |
| 5 | wang | 23 |
+----+-------+-----+
4 rows in set (0.00 sec)
//RLIKE:基于正则表达式进行模式匹配, ^[a-z]{3}$表示三个字母
mysql> select * from linux where name rlike '^[a-z]{3}$';
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 19 |
+----+------+-----+
1 row in set (0.00 sec)
mysql> select * from linux where name rlike '^...$';
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 19 |
+----+------+-----+
1 row in set (0.00 sec)
mysql> select * from linux where name rlike '^t';
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 19 |
+----+------+-----+
1 row in set (0.00 sec)
//IS NOT NULL:非空
mysql> select * from zhangchaoqun;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | jerry | 21 |
| 2 | tom | 22 |
| 3 | sun | NULL |
+----+-------+------+
3 rows in set (0.00 sec)
mysql> select * from zhangchaoqun where age is not null;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | jerry | 21 |
| 2 | tom | 22 |
+----+-------+------+
2 rows in set (0.00 sec)
//IS NULL:空
mysql> select * from zhangchaoqun where age is null;
+----+------+------+
| id | name | age |
+----+------+------+
| 3 | sun | NULL |
+----+------+------+
1 row in set (0.00 sec)
mysql> select * from zhangchaoqun where age is not null order by age; //按照年龄排序,age不为空
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | jerry | 21 |
| 2 | tom | 22 |
+----+-------+------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM linux ORDER BY age; //按照年龄排序,默认升序
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 1 | tom | 19 |
| 2 | jerry | 20 |
| 3 | zhang | 21 |
| 4 | li | 22 |
| 5 | wang | 23 |
+----+-------+-----+
5 rows in set (0.00 sec)
mysql> SELECT * FROM linux ORDER BY age limit 2; //取出年龄最小的2个;排序后的前两名
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 1 | tom | 19 |
| 2 | jerry | 20 |
+----+-------+-----+
2 rows in set (0.00 sec)
//排序后,跳过第一个,只取一个
mysql> select * from zhangchaoqun;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | jerry | 21 |
| 2 | tom | 22 |
| 3 | sun | NULL |
+----+-------+------+
3 rows in set (0.00 sec)
mysql> select * from zhangchaoqun order by age;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 3 | sun | NULL |
| 1 | jerry | 21 |
| 2 | tom | 22 |
+----+-------+------+
3 rows in set (0.00 sec)
mysql> select * from zhangchaoqun order by age limit 1,1;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | jerry | 21 |
+----+-------+------+
1 row in set (0.00 sec)
mysql> SELECT * FROM linux ORDER BY age DESC; //按照年龄降序
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 5 | wang | 23 |
| 4 | li | 22 |
| 3 | zhang | 21 |
| 2 | jerry | 20 |
| 1 | tom | 19 |
+----+-------+-----+
5 rows in set (0.00 sec)
mysql> select * from linux order by age desc limit 1; //龄最大的
+----+------+-----+
| id | name | age |
+----+------+-----+
| 5 | wang | 23 |
+----+------+-----+
1 row in set (0.00 sec)
演示GROUP BY 语句:
mysql> create database if not exists Internet; 创建数据库Internet
Query OK, 1 row affected (0.01 sec)
mysql> show databases; //查看当前有哪些数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| Internet |
| Store_Information |
| mysql |
| performance_schema |
| school |
| sys |
| zcq |
+--------------------+
8 rows in set (0.00 sec)
mysql> use Internet; //进入Internet数据库
Database changed
mysql> create table Websites //创建表Websites
-> (id int primary key auto_increment not null,
-> name varchar(100) not null,
-> url varchar(100) not null,
-> alexa int not null,
-> country varchar(10) not null);
Query OK, 0 rows affected (0.01 sec)
mysql> desc Websites; //查看表结构
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| url | varchar(100) | NO | | NULL | |
| alexa | int(11) | NO | | NULL | |
| country | varchar(10) | NO | | NULL | |
+---------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> insert into Websites (name,url,alexa,country) values //一次插入多条记录
-> ('google','https://www.google.cogle.cm/',1,'USA'),
-> ('taobao','https://www.taobao.com/',13,'CN'),
-> ('runoob','http://www.runow.runoob.com/',4689,'CN'),
-> ('weibo','http://weibo.com/',20,'CN'),
-> ('Facebook','https:/ www./www.facebook.com/',3,'USA'),
-> ('stackoverflow','http://stackoverflow.com/',0,'IND');
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from Websites; //查询financial_report表
+----+---------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+---------------+---------------------------+-------+---------+
| 7 | google | https://www.google.cm/ | 1 | USA |
| 8 | taobao | https://www.taobao.com/ | 13 | CN |
| 9 | runoob | http://www.runoob.com/ | 4689 | CN |
| 10 | weibo | http://weibo.com/ | 20 | CN |
| 11 | Facebook | https://www.facebook.com/ | 3 | USA |
| 12 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
6 rows in set (0.00 sec)
mysql> create table access_log //创建表access_log
-> (aid int primary key auto_increment not null,
-> site_id int not null,
-> count int not null,
-> date date not null);
Query OK, 0 rows affected (0.00 sec)
mysql> desc access_log; //查看表结构
+---------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+----------------+
| aid | int(11) | NO | PRI | NULL | auto_increment |
| site_id | int(11) | NO | | NULL | |
| count | int(11) | NO | | NULL | |
| date | date | NO | | NULL | |
+---------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> insert into access_log (site_id,count,date) values //一次插入多条记录
-> (7,50,'2020-05-20'),
-> (8,100,''2020-05-20'),
-> (10,300,'2020-05-21'),
-> (7,150,'2020-05-23'),
-> (8,200,'2020-05-23'),
-> (9,150,,'2020-05-23'),
-> (10,150,'2020-05-23'),
-> (8,400,'2020-05-24'),
-> (11,150,'2020-05-24');
Query OK, 9 rows affected (0.00 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql> select * from access_log; //查询access_log表
+-----+---------+-------+------------+
| aid | site_id | count | date |
+-----+---------+-------+------------+
| 1 | 7 | 50 | 2020-05-20 |
| 2 | 8 | 100 | 2020-05-20 |
| 3 | 10 | 300 | 2020-05-21 |
| 4 | 7 | 150 | 2020-05-23 |
| 5 | 8 | 200 | 2020-05-23 |
| 6 | 9 | 150 | 2020-05-23 |
| 7 | 10 | 150 | 2020-05-23 |
| 8 | 8 | 400 | 2020-05-24 |
| 9 | 11 | 150 | 2020-05-24 |
+-----+---------+-------+------------+
9 rows in set (0.00 sec)
//GROUP BY 简单应用
统计 access_log 各个 site_id 的访问量:
mysql> select site_id,SUM(access_log.count) AS nums
-> from access_log GROUP BY site_id;
+---------+------+
| site_id | nums |
+---------+------+
| 7 | 200 |
| 8 | 700 |
| 9 | 150 |
| 10 | 450 |
| 11 | 150 |
+---------+------+
5 rows in set (0.01 sec)
//SQL GROUP BY 多表连接
下面的 SQL 语句统计有记录的网站的记录数量:
mysql> SELECT Websites.name,COUNT(access_log.aid) AS nums FROM access_log
-> LEFT JOIN Websites
-> ON access_log.site_id=Websites.id
-> GROUP BY Websites.name;
+----------+------+
| name | nums |
+----------+------+
| Facebook | 1 |
| google | 2 |
| runoob | 1 |
| taobao | 3 |
| weibo | 2 |
+----------+------+
5 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];
mysql> select * from zhangchaoqun;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | jerry | 21 |
| 2 | tom | 22 |
| 3 | sun | NULL |
+----+-------+------+
3 rows in set (0.00 sec)
mysql> update zhangchaoqun set age = 23 where name = 'sun';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from zhangchaoqun where name ='sun';
+----+------+------+
| id | name | age |
+----+------+------+
| 3 | sun | 23 |
+----+------+------+
1 row in set (0.00 sec)
4.2.4 delete语句
mysql> delete from zhangchaoqun where id = 2; //删除某条记录
Query OK, 1 row affected (0.00 sec)
mysql> select * from zhangchaoqun;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | jerry | 21 |
| 3 | sun | 23 |
+----+-------+------+
2 rows in set (0.00 sec)
mysql> select * from linux; //排序后删除
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 1 | tom | 19 |
| 2 | jerry | 20 |
| 3 | zhang | 21 |
| 4 | li | 22 |
| 5 | wang | 23 |
+----+-------+-----+
5 rows in set (0.00 sec)
mysql> select * from linux order by age desc limit 1;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 5 | wang | 23 |
+----+------+-----+
1 row in set (0.00 sec)
mysql> delete from linux order by age desc limit 1;
Query OK, 1 row affected (0.00 sec)
mysql> select * from linux;
+----+-------+-----+
| id | name | age |
+----+-------+-----+
| 1 | tom | 19 |
| 2 | jerry | 20 |
| 3 | zhang | 21 |
| 4 | li | 22 |
+----+-------+-----+
4 rows in set (0.00 sec)
mysql> delete from linux; //删除整张表的内容
Query OK, 4 rows affected (0.00 sec)
mysql> select * from linux;
Empty set (0.00 sec)
mysql> desc linux;
+-------+-------------+------+-----+---------+-------+
| 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.2.5 truncate语句
truncate与delete的区别:
语句类型 | 特点 |
---|---|
delete | DELETE删除表内容时仅删除内容,但会保留表结构 DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项 可以通过回滚事务日志恢复数据 非常占用空间 |
truncate | 删除表中所有数据,且无法恢复 表结构、约束和索引等保持不变,新添加的行计数值重置为初始值 执行速度比DELETE快,且使用的系统和事务日志资源少 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放 对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据 不能用于加入了索引视图的表 |
mysql> select * from zhangchaoqun;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | jerry | 21 |
| 3 | sun | 23 |
+----+-------+------+
2 rows in set (0.00 sec)
mysql> truncate zhangchaoqun;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from zhangchaoqun;
Empty set (0.00 sec)
mysql> desc zhangchaoqun;
+-------+--------------+------+-----+---------+----------------+
| 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)
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];
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| Internet |
| Store_Information |
| mysql |
| performance_schema |
| school |
| sys |
| zcq |
+--------------------+
8 rows in set (0.00 sec)
//授权zhangsan用户在数据库本机上登录访问所有数据库
mysql> GRANT ALL ON *.* TO 'zhangsan'@'localhost' IDENTIFIED BY 'zhangsan123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> GRANT ALL ON *.* TO 'zhangsan'@'127.0.0.1' IDENTIFIED BY 'zhangsan123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
//授权zhangsan用户在172.16.12.129上远程登录访问school数据库
mysql> GRANT ALL ON school.* TO 'zhangsan'@'192.168.67.131' IDENTIFIED BY 'zhangsan123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
//授权zhangsan用户在所有位置上远程登录访问school数据库
mysql> GRANT ALL ON *.* TO 'zhangsan'@'%' IDENTIFIED BY 'zhangsan123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
//刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
4.3.2 查看授权
//查看当前登录用户的授权信息
mysql> SHOW GRANTS;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
//查看指定用户zhangsan的授权信息
mysql> SHOW GRANTS FOR 'zhangsan';
+-----------------------------------------------+
| Grants for zhangsan@% |
+-----------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'zhangsan'@'%' |
+-----------------------------------------------+
1 row in set (0.00 sec)
mysql> SHOW GRANTS FOR 'zhangsan'@'localhost';
+-------------------------------------------------------+
| Grants for zhangsan@localhost |
+-------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'zhangsan'@'localhost' |
+-------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SHOW GRANTS FOR 'zhangsan'@'127.0.0.1';
+-------------------------------------------------------+
| Grants for zhangsan@127.0.0.1 |
+-------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'zhangsan'@'127.0.0.1' |
+-------------------------------------------------------+
1 row in set (0.00 sec)
4.3.3 取消授权REVOKE
//语法:REVOKE priv_type,... ON db_name.table_name FROM 'username'@'host';
mysql> REVOKE ALL ON *.* FROM 'zhangsan'@'192.168.67.131';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
注意:mysql服务进程启动时会读取mysql库中的所有授权表至内存中:
- GRANT或REVOKE等执行权限操作会保存于表中,mysql的服务进程会自动重读授权表,并更新至内存中
- 对于不能够或不能及时重读授权表的命令,可手动让mysql的服务进程重读授权表
mysql> FLUSH PRIVILEGES;
//查看授权表
mysql> use mysql;
Database changed
mysql> show tables; //user表存放用户
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
实例
1.搭建mysql服务
2.创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age),表结构如下:
mysql> 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.01 sec)
mysql> CREATE DATABASE zcq; //创建数据库名为"zcq"
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES; //查看数据库信息
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| zcq |
+--------------------+
6 rows in set (0.00 sec)
mysql> USE zcq; //进入zcq数据库
Database changed
mysql> create table student
-> (id int primary key auto_increment not null,
-> name varchar(100) not null,
-> age tinyint);
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+---------------+
| Tables_in_zcq |
+---------------+
| student |
+---------------+
1 row in set (0.00 sec)
mysql> 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语句)
mysql> select * from student;
Empty set (0.00 sec)
4.往新建的student表中插入数据(用insert语句),结果应如下所示:
+----+-------------+------+
| 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 |
+----+-------------+------+
mysql> INSERT INTO student (name,age) values('tom',20), //一次插入多条记录
-> ('jerry',23),('zcq',24),('sean',28),('zhangsan',26),('zhangsan',20),
-> ('lisi',NULL),('chenshuo',10),('wangwu',3),('qiuyi',15),('qiiuxiaotian',20);
Query OK, 11 rows affected (0.00 sec)
Records: 11 Duplicates: 0 Warnings: 0
mysql> select * from student; //查看student表
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | zcq | 24 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 6 | zhangsan | 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
mysql> update student set age = 50 where name = 'lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student where name = 'lisi';
+----+------+------+
| id | name | age |
+----+------+------+
| 7 | lisi | 50 |
+----+------+------+
1 row in set (0.00 sec)
6.以age字段降序排序
mysql> select * from student order by age desc;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 7 | lisi | 50 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 3 | zcq | 24 |
| 2 | jerry | 23 |
| 1 | tom | 20 |
| 6 | zhangsan | 20 |
| 11 | qiuxiaotian | 20 |
| 10 | qiuyi | 15 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
+----+-------------+------+
11 rows in set (0.00 sec)
7.查询student表中年龄最小的3位同学跳过前2位
mysql> select * from student order by age limit 2,3;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 10 | qiuyi | 15 |
| 1 | tom | 20 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
3 rows in set (0.00 sec)
8.查询student表中年龄最大的4位同学
mysql> select * from student order by age desc limit 4;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 7 | lisi | 50 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 3 | zcq | 24 |
+----+----------+------+
4 rows in set (0.00 sec)
9.查询student表中名字叫zhangsan的记录
mysql> select * from student where name = 'zhangsan';
+----+----------+------+
| id | name | age |
+----+----------+------+
| 5 | zhangsan | 26 |
| 6 | zhangsan | 20 |
+----+----------+------+
2 rows in set (0.00 sec)
10.查询student表中名字叫zhangshan且年龄大于20岁的记录
mysql> select * from student where name = 'zhangsan' and age > 20;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 5 | zhangsan | 26 |
+----+----------+------+
1 row in set (0.00 sec)
11.查询student表中年龄在23到30之间的记录
mysql> select * from student where age between 23 and 30;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 2 | jerry | 23 |
| 3 | zcq | 24 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
+----+----------+------+
4 rows in set (0.00 sec)
mysql> select * from student where age >= 23 and age <= 30;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 2 | jerry | 23 |
| 3 | zcq | 24 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
+----+----------+------+
4 rows in set (0.00 sec)
12.修改wangwu的年龄为100
mysql> update student set age = 100 where name = 'wangwu';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student where name = 'wangwu';
+----+--------+------+
| id | name | age |
+----+--------+------+
| 9 | wangwu | 100 |
+----+--------+------+
1 row in set (0.00 sec)
13.删除student中名字叫zhangsan且年龄小于等于20的记录
mysql> delete from student where age <= 20 and name = 'zhangsan';
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | zcq | 24 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
10 rows in set (0.00 sec)