mysql数据库操作
2.创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age),表结构如下:
4.往新建的student表中插入数据(用insert语句),结果应如下所示:
10.查询student表中名字叫zhangshan且年龄大于20岁的记录
13.删除student中名字叫zhangshan且年龄小于等于20的记录
DML操作
DML操作包括增(INSERT)、删(DELETE)、改(UPDATE)、查(SELECT),均属针对表的操作。
INSERT语句
mysql> use wq; Database changed mysql> create table wangquan(id int not null,name varchar(10) not null,age tinyint); Query OK, 0 rows affected (0.01 sec) mysql> show tables; +--------------+ | Tables_in_wq | +--------------+ | wangquan | +--------------+ 1 row in set (0.00 sec) mysql> desc wangquan; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | | NULL | | | name | varchar(10) | NO | | NULL | | | age | tinyint(4) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec) mysql> insert into wangquan(id,name,age) values(1,'w',20),(2,'q',21),(3,'wang',22),(4,'quan',23); 一次插入多条记录 Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select * from wangquan; +----+------+------+ | id | name | age | +----+------+------+ | 1 | w | 20 | | 2 | q | 21 | | 3 | wang | 22 | | 4 | quan | 23 | +----+------+------+ 4 rows in set (0.00 sec) mysql>
SELECT语句
字段column表示法
表示符 | 代表什么? |
---|---|
* | 所有字段 |
as | 字段别名,如col1 AS alias1 当表名很长时用别名代替 |
条件判断语句WHERE
操作类型 | 常用操作符 |
---|---|
操作符 | >,<,>=,<=,=,!= BETWEEN column# AND column# LIKE:模糊匹配 RLIKE:基于正则表达式进行模式匹配 IS NOT NULL:非空 IS NULL:空 |
条件逻辑操作 | AND OR NOT |
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个结果 |
mysql> select * from wangquan; +----+------+------+ | id | name | age | +----+------+------+ | 1 | w | 20 | | 2 | q | 21 | | 3 | wang | 22 | | 4 | quan | 23 | +----+------+------+ 4 rows in set (0.00 sec) mysql> select name from wangquan; +------+ | name | +------+ | w | | q | | wang | | quan | +------+ 4 rows in set (0.00 sec) mysql> select * from wangquan order by age; +----+------+------+ | id | name | age | +----+------+------+ | 1 | w | 20 | | 2 | q | 21 | | 3 | wang | 22 | | 4 | quan | 23 | +----+------+------+ 4 rows in set (0.00 sec) mysql> select * from wangquan order by age desc; +----+------+------+ | id | name | age | +----+------+------+ | 4 | quan | 23 | | 3 | wang | 22 | | 2 | q | 21 | | 1 | w | 20 | +----+------+------+ 4 rows in set (0.00 sec) mysql> select * from wangquan order by age limit 2; +----+------+------+ | id | name | age | +----+------+------+ | 1 | w | 20 | | 2 | q | 21 | +----+------+------+ 2 rows in set (0.00 sec) mysql> select * from wangquan order by age limit 1,2; +----+------+------+ | id | name | age | +----+------+------+ | 2 | q | 21 | | 3 | wang | 22 | +----+------+------+ 2 rows in set (0.00 sec) mysql> select * from wangquan where age >= 21; +----+------+------+ | id | name | age | +----+------+------+ | 2 | q | 21 | | 3 | wang | 22 | | 4 | quan | 23 | +----+------+------+ 3 rows in set (0.00 sec) mysql> select * from wangquan where age >=22 and name = 'quan'; +----+------+------+ | id | name | age | +----+------+------+ | 4 | quan | 23 | +----+------+------+ 1 row in set (0.00 sec) mysql> select * from wangquan where age between 21 and 22; +----+------+------+ | id | name | age | +----+------+------+ | 2 | q | 21 | | 3 | wang | 22 | +----+------+------+ 2 rows in set (0.00 sec) mysql> select * from wangquan where age is not null; +----+------+------+ | id | name | age | +----+------+------+ | 1 | w | 20 | | 2 | q | 21 | | 3 | wang | 22 | | 4 | quan | 23 | +----+------+------+ 4 rows in set (0.00 sec) mysql> select * from wangquan where age is null; Empty set (0.00 sec) mysql>
update语句
mysql> select * from wangquan; +----+------+------+ | id | name | age | +----+------+------+ | 1 | w | 20 | | 2 | q | 21 | | 3 | wang | 22 | | 4 | quan | 23 | +----+------+------+ 4 rows in set (0.00 sec) mysql> update wangquan set age = 25 where name = 'quan'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from wangquan where name = 'quan'; +----+------+------+ | id | name | age | +----+------+------+ | 4 | quan | 25 | +----+------+------+ 1 row in set (0.00 sec) mysql>
delete语句
mysql> select * from wangquan; +----+------+------+ | id | name | age | +----+------+------+ | 1 | w | 20 | | 2 | q | 21 | | 3 | wang | 22 | | 4 | quan | 25 | +----+------+------+ 4 rows in set (0.00 sec) mysql> delete from wangquan where id = 4; Query OK, 1 row affected (0.00 sec) mysql> select * from wangquan; +----+------+------+ | id | name | age | +----+------+------+ | 1 | w | 20 | | 2 | q | 21 | | 3 | wang | 22 | +----+------+------+ 3 rows in set (0.00 sec) mysql> delete from wangquan; Query OK, 4 rows affected (0.00 sec) mysql> select * from wangquan; Empty set (0.00 sec)
truncate语句
truncate与delete的区别:
语句类型 | 特点 |
---|---|
delete | DELETE删除表内容时仅删除内容,但会保留表结构 DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项 可以通过回滚事务日志恢复数据 非常占用空间 |
truncate | 删除表中所有数据,且无法恢复 表结构、约束和索引等保持不变,新添加的行计数值重置为初始值 执行速度比DELETE快,且使用的系统和事务日志资源少 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放 对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据 不能用于加入了索引视图的表 |
mysql> select * from wangquan; +----+------+------+ | id | name | age | +----+------+------+ | 1 | w | 20 | | 2 | q | 21 | | 3 | wang | 22 | | 4 | quan | 25 | +----+------+------+ 4 rows in set (0.00 sec) mysql> truncate jiang; Query OK, 0 rows affected (0.00 sec) mysql> select * from wangquan; Empty set (0.00 sec) mysql> desc wangquan; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | | NULL | | | name | varchar(10) | NO | | NULL | | | age | tinyint(4) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
DCL操作
创建授权grant
权限类型(priv_type)
权限类型 | 代表什么? |
---|---|
ALL | 所有权限 |
SELECT | 读取内容的权限 |
INSERT | 插入内容的权限 |
UPDATE | 更新内容的权限 |
DELETE | 删除内容的权限 |
指定要操作的对象db_name.table_name
表示方式 | 意义 |
---|---|
. | 所有库的所有表 |
db_name | 指定库的所有表 |
db_name.table_name | 指定库的指定表 |
WITH GRANT OPTION:被授权的用户可将自己的权限副本转赠给其他用户,说白点就是将自己的权限完全复制给另一个用户。不建议使用。
查看授权
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)
取消授权REVOKE
注意:mysql服务进程启动时会读取mysql库中的所有授权表至内存中:
GRANT或REVOKE等执行权限操作会保存于表中,mysql的服务进程会自动重读授权表,并更新至内存中
对于不能够或不能及时重读授权表的命令,可手动让mysql的服务进程重读授权表
mysql> revoke all on *.* from 'shuai'@'192.168.183.133'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql>
实战案例
1.搭建mysql服务
[root@localhost ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm --2022-07-26 18:55:38-- http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm Resolving dev.mysql.com (dev.mysql.com)... 23.2.135.207, 2600:140b:2:5ad::2e31, 2600:140b:2:59c::2e31 Connecting to dev.mysql.com (dev.mysql.com)|23.2.135.207|:80... connected. HTTP request sent, awaiting response... 301 Moved Permanently Location: https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm [following] --2022-07-26 18:55:38-- https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm Connecting to dev.mysql.com (dev.mysql.com)|23.2.135.207|:443... connected. HTTP request sent, awaiting response... 302 Moved Temporarily Location: https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm [following] --2022-07-26 18:55:39-- https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm Resolving repo.mysql.com (repo.mysql.com)... 104.123.205.20 Connecting to repo.mysql.com (repo.mysql.com)|104.123.205.20|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 25548 (25K) [application/x-redhat-package-manager] Saving to: 'mysql57-community-release-el7-10.noarch.rpm' mysql57-community-rel 100%[======================>] 24.95K 90.5KB/s in 0.3s 2022-07-26 18:55:40 (90.5 KB/s) - 'mysql57-community-release-el7-10.noarch.rpm' saved [25548/25548] [root@localhost ~]# rpm -Uvh mysql57-community-release-el7-10.noarch.rpm warning: mysql57-community-release-el7-10.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Verifying... ################################# [100%] Preparing... ################################# [100%] Updating / installing... 1:mysql57-community-release-el7-10 ################################# [100%] [root@localhost ~]# yum module disable mysql Failed to set locale, defaulting to C.UTF-8 MySQL Connectors Community 28 kB/s | 51 kB 00:01 MySQL Tools Community 339 kB/s | 711 kB 00:02 MySQL 5.7 Community Server 1.4 MB/s | 2.7 MB 00:01 Dependencies resolved. ===================================================================================== Package Architecture Version Repository Size ===================================================================================== Disabling modules: mysql Transaction Summary ===================================================================================== Is this ok [y/N]: y Complete! [root@localhost ~]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel --nogpgcheck Failed to set locale, defaulting to C.UTF-8 Last metadata expiration check: 0:00:31 ago on Tue Jul 26 18:56:54 2022. Dependencies resolved. ===================================================================================== Package Arch Version Repository Size ===================================================================================== Installing: mysql-community-client x86_64 5.7.39-1.el7 mysql57-community 28 M mysql-community-common x86_64 5.7.39-1.el7 mysql57-community 311 k mysql-community-devel x86_64 5.7.39-1.el7 mysql57-community 4.2 M mysql-community-server x86_64 5.7.39-1.el7 mysql57-community 178 M ....... pkgconf-m4-1.4.2-1.el8.noarch pkgconf-pkg-config-1.4.2-1.el8.x86_64 Complete! [root@localhost ~]# systemctl enable --now mysqld [root@localhost ~]# systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: d> Active: active (running) since Tue 2022-07-26 19:20:16 CST; 2s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 54100 ExecStart=/usr/sbin/mysqld --daemonize --pid- ....... [root@localhost ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 80 *:3306 *:* [root@localhost ~]# grep "password" /var/log/mysqld.log 2022-07-26T11:20:14.556473Z 1 [Note] A temporary password is generated for root@localhost: B/-boX#Vp3Bt [root@localhost ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.39 Copyright (c) 2000, 2022, Oracle and/or its affiliates. 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> 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.00 sec) mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'wangquan'; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye //卸载yum源,避免自动升级 [root@localhost ~]# rpm -e mysql57-community-release
2.创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age),表结构如下:
mysql> use wangquan; Database changed mysql> create table student (id int not null,name varchar(100) not null,age tinyint,primary key (id)); Query OK, 0 rows affected (0.00 sec) mysql> desc student; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(100) | NO | | NULL | | | age | tinyint(4) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql>
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)
3.查看下该新建的表有无内容(用select语句)
mysql> select * from student; Empty set (0.00 sec) mysql>
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 (id,name,age) VALUES (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); Query OK, 11 rows affected (0.00 sec) Records: 11 Duplicates: 0 Warnings: 0 mysql> 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
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; +----+-------------+------+ | 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字段降序排序
mysql> 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位
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 | zhangshan | 26 | | 3 | wangqing | 25 | +----+-----------+------+ 4 rows in set (0.00 sec)
9.查询student表中名字叫zhangshan的记录
mysql> select * from student where name = 'zhangshan'; +----+-----------+------+ | id | name | age | +----+-----------+------+ | 5 | zhangshan | 26 | | 6 | zhangshan | 20 | +----+-----------+------+ 2 rows in set (0.00 sec)
10.查询student表中名字叫zhangshan且年龄大于20岁的记录
mysql> select * from student where name = 'zhangshan' and age > 20; +----+-----------+------+ | id | name | age | +----+-----------+------+ | 5 | zhangshan | 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 | wangqing | 25 | | 4 | sean | 28 | | 5 | zhangshan | 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中名字叫zhangshan且年龄小于等于20的记录
mysql> 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) mysql> delete from student where name = 'zhangshan' and age <= 20; Query OK, 1 row affected (0.00 sec) mysql> select * from student; +----+-------------+------+ | id | name | age | +----+-------------+------+ | 1 | tom | 20 | | 2 | jerry | 23 | | 3 | wangqing | 25 | | 4 | sean | 28 | | 5 | zhangshan | 26 | | 7 | lisi | 50 | | 8 | chenshuo | 10 | | 9 | wangwu | 100 | | 10 | qiuyi | 15 | | 11 | qiuxiaotian | 20 | +----+-------------+------+ 10 rows in set (0.00 sec)