mysql基础二

目录

mysql基础二

DML操作

INSERT语句

SELECT语句

update语句

DELETE语句

truncate语句

DCL操作

创建授权GRANT

查看授权

取消授权REVOKE

实战案例

搭建mysql服务

创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age),表结构如下:

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

往新建的student表中插入数据(用insert语句),结果应如下所示:

修改lisi的年龄为50

以age字段降序排序

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

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

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

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

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

修改wangwu的年龄为100

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


DML操作

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

INSERT语句

 

mysql> use zhaozihao

Database changed

mysql> insert into student (id,name,age) value (1,'wrq',18); //插入一条数据

Query OK, 1 row affected (0.03 sec)

mysql> select * from student; //查看插入的数据

+----+------+------+

| id | name | age |

+----+------+------+

| 1 | wrq | 18 |

+----+------+------+

1 row in set (0.00 sec)

mysql> insert into student (id,name,age) values (2,'zzh',20),(3,'www',25),(4,'rrr',30),(5,'qqq',35),(6,'zzz',40),(7,'hhh',null); //插入多条数据

Query OK, 6 rows affected (0.00 sec)

Records: 6 Duplicates: 0 Warnings: 0

mysql> select * from student;

+----+------+------+

| id | name | age |

+----+------+------+

| 1 | wrq | 18 |

| 2 | zzh | 20 |

| 3 | www | 25 |

| 4 | rrr | 30 |

| 5 | qqq | 35 |

| 6 | zzz | 40 |

| 7 | hhh | NULL |

+----+------+------+

7 rows in set (0.00 sec)

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 student; //查看表中所有内容

+----+------+------+

| id | name | age |

+----+------+------+

| 1 | wrq | 18 |

| 2 | zzh | 20 |

| 3 | www | 25 |

| 4 | rrr | 30 |

| 5 | qqq | 35 |

| 6 | zzz | 40 |

| 7 | hhh | NULL |

+----+------+------+

7 rows in set (0.00 sec)

mysql> select name from student; //查看表中name列内容

+------+

| name |

+------+

| wrq |

| zzh |

| www |

| rrr |

| qqq |

| zzz |

| hhh |

+------+

7 rows in set (0.00 sec)

mysql> select * from student order by age; //将表根据年龄进行升序排序查看

+----+------+------+

| id | name | age |

+----+------+------+

| 7 | hhh | NULL |

| 1 | wrq | 18 |

| 2 | zzh | 20 |

| 3 | www | 25 |

| 4 | rrr | 30 |

| 5 | qqq | 35 |

| 6 | zzz | 40 |

+----+------+------+

7 rows in set (0.00 sec)

mysql> select * from student order by age desc; //根据年龄进行降序排列

+----+------+------+

| id | name | age |

+----+------+------+

| 6 | zzz | 40 |

| 5 | qqq | 35 |

| 4 | rrr | 30 |

| 3 | www | 25 |

| 2 | zzh | 20 |

| 1 | wrq | 18 |

| 7 | hhh | NULL |

+----+------+------+

7 rows in set (0.00 sec)

mysql> select * from student order by age limit 3; //根据年龄进行升序排序并只取前3个结果

+----+------+------+

| id | name | age |

+----+------+------+

| 7 | hhh | NULL |

| 1 | wrq | 18 |

| 2 | zzh | 20 |

+----+------+------+

3 rows in set (0.00 sec)

mysql> select * from student order by age limit 1,2; //根据年龄进行升序排序并且略过第1个结果取后面的2个结果

+----+------+------+

| id | name | age |

+----+------+------+

| 1 | wrq | 18 |

| 2 | zzh | 20 |

+----+------+------+

2 rows in set (0.00 sec)

mysql> select * from student where age >= 35; //查看student表中年龄大于等于35

+----+------+------+

| id | name | age |

+----+------+------+

| 5 | qqq | 35 |

| 6 | zzz | 40 |

+----+------+------+

2 rows in set (0.00 sec)

mysql> select * from student where age >= 40 and name= 'zzz'; //查看表中年龄大于等于35并且名字为zzz

+----+------+------+

| id | name | age |

+----+------+------+

| 6 | zzz | 40 |

+----+------+------+

1 row in set (0.00 sec)

mysql> select * from student where age between 30 and 45; //查看表中年龄在30至45中间的

+----+------+------+

| id | name | age |

+----+------+------+

| 4 | rrr | 30 |

| 5 | qqq | 35 |

| 6 | zzz | 40 |

+----+------+------+

3 rows in set (0.00 sec)

mysql> select * from student where age is not null; //查看表中年龄不为空值的

+----+------+------+

| id | name | age |

+----+------+------+

| 1 | wrq | 18 |

| 2 | zzh | 20 |

| 3 | www | 25 |

| 4 | rrr | 30 |

| 5 | qqq | 35 |

| 6 | zzz | 40 |

+----+------+------+

6 rows in set (0.00 sec)

mysql> select * from student where age is null; //查看表中年龄为空值的

+----+------+------+

| id | name | age |

+----+------+------+

| 7 | hhh | NULL |

+----+------+------+

1 row in set (0.00 sec)

update语句

 

mysql> select * from student; //查看所有内容

+----+------+------+

| id | name | age |

+----+------+------+

| 1 | wrq | 18 |

| 2 | zzh | 20 |

| 3 | www | 25 |

| 4 | rrr | 30 |

| 5 | qqq | 35 |

| 6 | zzz | 40 |

| 7 | hhh | NULL |

+----+------+------+

7 rows in set (0.00 sec)

mysql> update student set age = 45 where name = 'hhh'; //用update语句将hhh年龄从null改为45

Query OK, 1 row affected (0.01 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from student where name = 'hhh'; //查看hhh

+----+------+------+

| id | name | age |

+----+------+------+

| 7 | hhh | 45 |

+----+------+------+

1 row in set (0.00 sec)

DELETE语句

DML操作之删操作delete

 

mysql> select * from student; //查看所有内容

+----+------+------+

| id | name | age |

+----+------+------+

| 1 | wrq | 18 |

| 2 | zzh | 20 |

| 3 | www | 25 |

| 4 | rrr | 30 |

| 5 | qqq | 35 |

| 6 | zzz | 40 |

| 7 | hhh | 45 |

+----+------+------+

7 rows in set (0.00 sec)

mysql> delete from student where id = 1; //删除id为1表记录

Query OK, 1 row affected (0.00 sec)

mysql> select * from student; //查看已被删除

+----+------+------+

| id | name | age |

+----+------+------+

| 2 | zzh | 20 |

| 3 | www | 25 |

| 4 | rrr | 30 |

| 5 | qqq | 35 |

| 6 | zzz | 40 |

| 7 | hhh | 45 |

+----+------+------+

6 rows in set (0.00 sec)

mysql> select * from student; //删除整张表的内容

Empty set (0.00 sec)

mysql> desc student;

+-------+-------------+------+-----+---------+-------+

| 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)

truncate语句

truncate与delete的区别:

语句类型           特点 
delete                DELETE删除表内容时仅删除内容,但会保留表结构 
                         DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项 
                         可以通过回滚事务日志恢复数据 
                         非常占用空间 
truncate           删除表中所有数据,且无法恢复 
                         表结构、约束和索引等保持不变,新添加的行计数值重置为初始值 
                         执行速度比DELETE快,且使用的系统和事务日志资源少 
                         通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放 
                          对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据 
                         不能用于加入了索引视图的表

 

mysql> select * from student;

+----+------+------+

| id | name | age |

+----+------+------+

| 2 | zzh | 20 |

| 3 | www | 25 |

| 4 | rrr | 30 |

| 5 | qqq | 35 |

| 6 | zzz | 40 |

| 7 | hhh | NULL |

| 1 | wrq | 18 |

+----+------+------+

7 rows in set (0.00 sec)

mysql> truncate student; //删除student表

Query OK, 0 rows affected (0.01 sec)

mysql> select * from student;

Empty set (0.00 sec)

mysql> desc student;

+-------+-------------+------+-----+---------+-------+

| 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指定库的所有表

vdb_name.table_name |指定库的指定表| 
WITH GRANT OPTION:被授权的用户可将自己的权限副本转赠给其他用户,说白点就是将自己的权限完全复制给另一个用户。不建议使用。

 

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| zhaozihao |

+--------------------+

5 rows in set (0.00 sec)

mysql> grant all on *.* to 'zhaozihao'@'localhost' identified by 'zhaozihao'; //授权zhaozihao用户在数据库本机上登录访问所有数据库

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all on *.* to 'zhaozihao'@'127.0.0.1' identified by 'zhaozihao';

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all on shuai.* to 'zhaozihao'@'192.168.183.133' identified by 'zhaozihao'; //授权zhaozihao用户在192.168.183.133上远程登录访问shuai数据库

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all on *.* to 'zhaozihao'@'%' identified by 'zhaozihao'; //授权zhaozihao用户在所有位置上远程登录访问zhaozihao数据库

Query OK, 0 rows affected, 1 warning (0.00 sec)

查看授权

 

//查看当前登录用户的授权信息

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)

//查看指定用户shuai的授权信息

mysql> show grants for zhaozihao;

+------------------------------------------------+

| Grants for shuai@% |

+------------------------------------------------+

| GRANT ALL PRIVILEGES ON *.* TO 'zhaozihao'@'%' |

+------------------------------------------------+

1 row in set (0.01 sec)

mysql> show grants for 'zhaozihao'@'localhost';

+--------------------------------------------------------+

| Grants for shuai@localhost |

+--------------------------------------------------------+

| GRANT ALL PRIVILEGES ON *.* TO 'zhaozihao'@'localhost' |

+--------------------------------------------------------+

1 row in set (0.00 sec)

mysql> show grants for 'zhaozihao'@'127.0.0.1';

+--------------------------------------------------------+

| Grants for shuai@127.0.0.1 |

+--------------------------------------------------------+

| GRANT ALL PRIVILEGES ON *.* TO 'zhaozihao'@'127.0.0.1' |

+--------------------------------------------------------+

1 row in set (0.00 sec)

取消授权REVOKE

注意:mysql服务进程启动时会读取mysql库中的所有授权表至内存中:

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

 

mysql> revoke all on *.* from 'zhaozihao'@'192.168.183.133';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

实战案例

搭建mysql服务

 

[root@zzh ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm //配置mysql的yum源

--2022-07-25 08:35:42-- http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

Resolving dev.mysql.com (dev.mysql.com)... 23.64.178.143, 2600:1406:cc00:289::2e31, 2600:1406:cc00:287::2e31

Connecting to dev.mysql.com (dev.mysql.com)|23.64.178.143|: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-25 08:35:42-- https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

Connecting to dev.mysql.com (dev.mysql.com)|23.64.178.143|: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-25 08:35:43-- https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm

Resolving repo.mysql.com (repo.mysql.com)... 104.85.245.54

Connecting to repo.mysql.com (repo.mysql.com)|104.85.245.54|: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-release-el7-10.noarch.rpm 100%[=======================================================================================================>] 24.95K 142KB/s in 0.2s

2022-07-25 08:35:50 (142 KB/s) - 'mysql57-community-release-el7-10.noarch.rpm' saved [25548/25548]

[root@zzh ~]# yum module disable mysql //禁用mysql

Failed to set locale, defaulting to C.UTF-8

MySQL Connectors Community 16 kB/s | 49 kB 00:03

MySQL Tools Community 105 kB/s | 651 kB 00:06

MySQL 5.7 Community Server 265 kB/s | 2.6 MB 00:10

Last metadata expiration check: 0:00:01 ago on Mon Jul 25 08:38:44 2022.

Dependencies resolved.

=================================================================================================================================================================================================

Package Architecture Version Repository Size

=================================================================================================================================================================================================

Disabling modules:

mysql

Transaction Summary

=================================================================================================================================================================================================

Is this ok [y/N]: y

Complete!

[root@zzh ~]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel --nogpgcheck //使用yum安装

Failed to set locale, defaulting to C.UTF-8

Last metadata expiration check: 0:22:50 ago on Mon Jul 25 08:38:44 2022.

Dependencies resolved.

=================================================================================================================================================================================================

Package Architecture Version Repository Size

=================================================================================================================================================================================================

Installing:

mysql-community-client x86_64 5.7.38-1.el7 mysql57-community 28 M

mysql-community-common x86_64 5.7.38-1.el7 mysql57-community 311 k

mysql-community-devel x86_64 5.7.38-1.el7 mysql57-community 4.2 M

[root@zzh ~]# systemctl enable --now mysqld //启动mysql

[root@zzh ~]# systemctl status mysqld

● mysqld.service - MySQL Server

Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

Active: active (running) since Mon 2022-07-25 09:05:23 EDT; 5s ago

Docs: man:mysqld(8)

http://dev.mysql.com/doc/refman/en/using-systemd.html

Process: 299040 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)

Process: 298915 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

Main PID: 299042 (mysqld)

Tasks: 27 (limit: 54222)

Memory: 302.1M

CGroup: /system.slice/mysqld.service

└─299042 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Jul 25 09:05:21 zzh systemd[1]: Starting MySQL Server...

Jul 25 09:05:23 zzh systemd[1]: Started MySQL Server.

[root@zzh ~]# ss -anltup | grep 3306 //查看端口是否监听

tcp LISTEN 0 80 *:3306 *:* users:(("mysqld",pid=299042,fd=21))

[root@zzh ~]# rm -rf /etc/yum.repos.d/mysql-community.repo mysql57-community-release-el7-10.noarch.rpm //删除源

[root@zzh ~]# grep "password" /var/log/mysqld.log

2022-07-25T13:05:22.199998Z 1 [Note] A temporary password is generated for root@localhost: REqIh6fe(XpR //过滤日志文件找到临时密码

[root@zzh ~]# mysql -uroot -p //登录mysql

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.7.38

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 'zhaozihao'; //修改密码

Query OK, 0 rows affected (0.00 sec)

mysql>

创建一个以你名字为名的数据库,并创建一张表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 zhaozihao;

Query OK, 1 row affected (0.01 sec)

mysql> use zhaozihao;

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.01 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.02 sec)

mysql>

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

 

mysql> select * from student;

Empty set (0.00 sec)

mysql>

往新建的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)

修改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)

以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)

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

 

查询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)

查询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)

查询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)

查询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)

查询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)

修改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)

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

 

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值