一.数据表本身的基本操作
1.1新建表
1.2查看表
1.3查看表结构
1.4删除表
二.数据表的增删查改(CRUD)
2.1插入
2.2查询
2.2.1查询表中所有元素(查询整表)
2.2.2指定列查询
2.2.3指定字段为表达式查询
2.2.4指定别名字段查询
2.2.5去重查询
2.2.6排序查询
2.2.7条件查询
2.2.8分页查询
2.3修改
2.4删除
在选定数据库后就可以进行数据表的创建了,语法:
create table 表名(定义列1, 定义列2, .......);--具体如下
create table 表名(变量名1 数据类型1, 变量名2 数据类型2, .......);
例如创建一个学生表:
MySQL> create table student(id int, name varchar(50),score double(3,1));
Query OK, 0 rows affected (0.03 sec)
查看某数据库中有哪些数据表,语法:
show tables;
栗子:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| student |
| test_1 |
+----------------+
2 rows in set (0.00 sec)
查看一个数据表中有哪一些列(字段),语法:
desc 表名;
查看学生表结构:
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| score | double(3,1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
(慎重操作),对数据表整体进行删除,语法:
drop table 表名;
栗子:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| student |
| test_1 |
+----------------+
2 rows in set (0.00 sec)
mysql> drop table test_1;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| student |
+----------------+
1 row in set (0.00 sec)
往表里插入一行数据,语法:
insert into 表名 values (对应列的实参列表);
其中values后面()里面的值列表要与创建表时的列一一对应。
如在学生表中插入一行:
mysql> insert into student values (1, "喜羊羊", 98);
Query OK, 1 row affected (0.01 sec)
当然也可以指定列插入,语法:
insert into 表名 (需要插入的列) values (对应列的实参列表);
没有指定的列为默认值。
栗子:
mysql> insert into student (id, name) values (2, "懒羊羊");
Query OK, 1 row affected (0.00 sec)
当然也可以多行插入,格式:
insert into 表名 values (对应列的实参列表1), (对应列的实参列表2), ...;
栗子:
mysql> insert into student values (3,"美羊羊", 92), (4, "沸羊羊", 70), (5, "暖羊羊", 88);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
有关日期数据的插入,可以使用datetime数据类型,其中now()能够获取当前日期。
该类型日期的格式为:yyyy-mm-dd hh:mm:ss
栗子:
mysql> create table time(id int, time datetime);
Query OK, 0 rows affected (0.02 sec)
mysql> desc time;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| time | datetime | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into time values (1, '2022-03-12');
Query OK, 1 row affected (0.00 sec)
mysql> insert into time values (2, '2022-03-12 20:22:22');
Query OK, 1 row affected (0.00 sec)
mysql> insert into time values (3, now());
Query OK, 1 row affected (0.01 sec)
上面我们已经介绍数据库中表的查询和表中元素的插入等操作,上面所演示的代码中只提示插入成功,但是我们并不能看见它是否真的插入进去了,所以需要对表进行查询,数据表的查询是数据表增删查改中最复杂的一项操作。
该操作要慎重使用,因为MySQL数据都储存在服务器的硬盘上,查询操作是将服务器中的数据返回给客户端,由于在企业级的数据库中,数据量非常的大,是TB级别的,一旦使用整表查询,会把服务器吃满,此时其他客户端访问服务器时,服务器无法及时作出响应。
语法:
select * from 表名;
其中上面的*表示通配符,表示一个表所有的列。
栗子,查询上面新建的学生表和日期表。
mysql> insert into time values (3, now());
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+------+-----------+-------+
| id | name | score |
+------+-----------+-------+
| 1 | 喜羊羊 | 98.0 |
| 2 | 懒羊羊 | NULL |
| 3 | 美羊羊 | 92.0 |
| 4 | 沸羊羊 | 70.0 |
| 5 | 暖羊羊 | 88.0 |
+------+-----------+-------+
5 rows in set (0.00 sec)
mysql> select * from time;
+------+---------------------+
| id | time |
+------+---------------------+
| 1 | 2022-03-12 00:00:00 |
| 2 | 2022-03-12 20:22:22 |
| 3 | 2022-03-12 20:13:39 |
+------+---------------------+
3 rows in set (0.00 sec)
这个输出显示的表是服务器端数据库返回给客户端的一个临时表,使用查询操作不会对服务器中的数据造成影响。且该临时表在客户端打印后也随之销毁了。
指定列查询后,服务器返回的数据量会大大减少,因此查询一般指定列来进行查询。
语法格式:
select 列名, 列名, ... from 表名;
比如我们只查询学生表中的姓名和成绩:
mysql> select name, score from student;
+-----------+-------+
| name | score |
+-----------+-------+
| 喜羊羊 | 98.0 |
| 懒羊羊 | NULL |
| 美羊羊 | 92.0 |
| 沸羊羊 | 70.0 |
| 暖羊羊 | 88.0 |
+-----------+-------+
5 rows in set (0.00 sec)
如果我们期望对数据表中的某列统一加上一个权重进行查询,我们可以指定列的表达式来查询,比如对学生表的成绩统一减少10分进行查询:
mysql> select name, score-10 from student;
+-----------+----------+
| name | score-10 |
+-----------+----------+
| 喜羊羊 | 88.0 |
| 懒羊羊 | NULL |
| 美羊羊 | 82.0 |
| 沸羊羊 | 60.0 |
| 暖羊羊 | 78.0 |
+-----------+----------+
5 rows in set (0.00 sec)
我们发现null不受影响。
我们重新再建一个表,该表里有学号,姓名,语文成绩,数学成绩,英语成绩,计算机综合成绩,满分都为100分。
我们现在需要统计表中所有同学的总成绩和每个学生的平均分。
-- 建表
mysql> create table exam_score(id int, name varchar(50), chinese double(4,1), math double(4,1),english double(4,1), computer double(4,1));
Query OK, 0 rows affected (0.02 sec)
-- 插入数据
mysql> insert into exam_score values (1,'美羊羊',99.5,90.5,98,82),
-> (2, '懒羊羊', 58.5,32.5,24,66.5),
-> (3, '喜羊羊', 92,98,88,100),
-> (4, '沸羊羊', 78,72,74.5,81),
-> (5, '暖羊羊', 90,91,98,76),
-> (6, '灰太狼', 33,91,12,98.5),
-> (7, '小灰灰', 81, 82,78,88),
-> (8, '黑大帅', 10,11,9,20);
Query OK, 8 rows affected (0.01 sec)
Records: 8 Duplicates: 0 Warnings: 0
-- 整表查询
mysql> select * from exam_score;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 2 | 懒羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 88.0 | 100.0 |
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
| 6 | 灰太狼 | 33.0 | 91.0 | 12.0 | 98.5 |
| 7 | 小灰灰 | 81.0 | 82.0 | 78.0 | 88.0 |
| 8 | 黑大帅 | 10.0 | 11.0 | 9.0 | 20.0 |
+------+-----------+---------+------+---------+----------+
8 rows in set (0.00 sec)
-- 统计每一位同学的总分与平均分
mysql> select name, chinese+math+english+computer,(chinese+math+english+computer)/4 from exam_score;
+-----------+-------------------------------+-----------------------------------+
| name | chinese+math+english+computer | (chinese+math+english+computer)/4 |
+-----------+-------------------------------+-----------------------------------+
| 美羊羊 | 370.0 | 92.50000 |
| 懒羊羊 | 181.5 | 45.37500 |
| 喜羊羊 | 378.0 | 94.50000 |
| 沸羊羊 | 305.5 | 76.37500 |
| 暖羊羊 | 355.0 | 88.75000 |
| 灰太狼 | 234.5 | 58.62500 |
| 小灰灰 | 329.0 | 82.25000 |
| 黑大帅 | 50.0 | 12.50000 |
+-----------+-------------------------------+-----------------------------------+
8 rows in set (0.00 sec)
上面查询总成绩与平均成绩时,输出的表头是一个表达式,不是非常合适作为表头标题,那能不能修改别名呢?答案是肯定的。
语法格式:
select 列名或表达式 as 别名, ... from 表名;
其中as可以省略,但是不建议这样做,因为可读性会变差。
以上面的考试成绩表为例,
mysql> select name, chinese+math+english+computer as total,(chinese+math+english+computer)/4 as average from exam_score;
+-----------+-------+----------+
| name | total | average |
+-----------+-------+----------+
| 美羊羊 | 370.0 | 92.50000 |
| 懒羊羊 | 181.5 | 45.37500 |
| 喜羊羊 | 378.0 | 94.50000 |
| 沸羊羊 | 305.5 | 76.37500 |
| 暖羊羊 | 355.0 | 88.75000 |
| 灰太狼 | 234.5 | 58.62500 |
| 小灰灰 | 329.0 | 82.25000 |
| 黑大帅 | 50.0 | 12.50000 |
+-----------+-------+----------+
8 rows in set (0.00 sec)
要注意一件事,前面指定的列的别名,不能用于其他指定列或表达式中,比如像这样就会报错。
mysql> select name, chinese+math+english+computer as total,total/4 as average from exam_score;
ERROR 1054 (42S22): Unknown column 'total' in 'field list'
MySQL中支持对列的查询,比如上面的英语成绩,美羊羊和暖羊羊都是98分,只对英语这一列成绩查询时可以通过关键字distinct进行去重查询。
mysql> select distinct english from exam_score;
+---------+
| english |
+---------+
| 98.0 |
| 24.0 |
| 88.0 |
| 74.5 |
| 12.0 |
| 78.0 |
| 9.0 |
+---------+
7 rows in set (0.00 sec)
看,结果少了一个98,达到了去重的效果。
可以针对查询结果对数据进行排序,得到临时表输出,语法格式:
select 列名,... from 表名 order by 列名(需排序)asc(或者desc);
最后的asc表示升序,也是默认排序方式,desc是降序排序。
例如,将上面的成绩按照总分进行升序和降序排名。
-- 升序
mysql> select name, chinese+math+english+computer as total,(chinese+math+english+computer)/4 as average from exam_score order by total asc;
+-----------+-------+----------+
| name | total | average |
+-----------+-------+----------+
| 黑大帅 | 50.0 | 12.50000 |
| 懒羊羊 | 181.5 | 45.37500 |
| 灰太狼 | 234.5 | 58.62500 |
| 沸羊羊 | 305.5 | 76.37500 |
| 小灰灰 | 329.0 | 82.25000 |
| 暖羊羊 | 355.0 | 88.75000 |
| 美羊羊 | 370.0 | 92.50000 |
| 喜羊羊 | 378.0 | 94.50000 |
+-----------+-------+----------+
8 rows in set (0.00 sec)
-- 降序
mysql> select name, chinese+math+english+computer as total,(chinese+math+english+computer)/4 as average from exam_score order by total desc;
+-----------+-------+----------+
| name | total | average |
+-----------+-------+----------+
| 喜羊羊 | 378.0 | 94.50000 |
| 美羊羊 | 370.0 | 92.50000 |
| 暖羊羊 | 355.0 | 88.75000 |
| 小灰灰 | 329.0 | 82.25000 |
| 沸羊羊 | 305.5 | 76.37500 |
| 灰太狼 | 234.5 | 58.62500 |
| 懒羊羊 | 181.5 | 45.37500 |
| 黑大帅 | 50.0 | 12.50000 |
+-----------+-------+----------+
8 rows in set (0.00 sec)
如果asc或者desc省略,则按照升序排列。
mysql> select name, chinese+math+english+computer as total,(chinese+math+english+computer)/4 as average from exam_score order by total;
+-----------+-------+----------+
| name | total | average |
+-----------+-------+----------+
| 黑大帅 | 50.0 | 12.50000 |
| 懒羊羊 | 181.5 | 45.37500 |
| 灰太狼 | 234.5 | 58.62500 |
| 沸羊羊 | 305.5 | 76.37500 |
| 小灰灰 | 329.0 | 82.25000 |
| 暖羊羊 | 355.0 | 88.75000 |
| 美羊羊 | 370.0 | 92.50000 |
| 喜羊羊 | 378.0 | 94.50000 |
+-----------+-------+----------+
8 rows in set (0.00 sec)
如果出现null,则默认是最小的。
排序时,可以针对多列排序,这样排序是有优先级的,优先级按照从左至右,比如先针对英语成绩排序,再针对数学成绩排序,意思是先比较英语,当英语成绩相同的情况下再比较数学。
mysql> select * from exam_score order by english desc, math desc;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 88.0 | 100.0 |
| 7 | 小灰灰 | 81.0 | 82.0 | 78.0 | 88.0 |
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 2 | 懒羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 6 | 灰太狼 | 33.0 | 91.0 | 12.0 | 98.5 |
| 8 | 黑大帅 | 10.0 | 11.0 | 9.0 | 20.0 |
+------+-----------+---------+------+---------+----------+
8 rows in set (0.00 sec)
所谓条件查询,就是给定一个条件,如果满足就那个查询出来,在MySQL中使用关键字where+条件来设定条件,条件查询语法格式如下:
select 列名, ... from 表名 where 条件;
这个所谓的条件就相当于JAVA中的boolean表达式,首先来认识一下MySQL里面的运算符。
比较大小:>, <, <=, >=,=(判断是否相等,但是不能比较NULL), <=>(判断是否相等,能比较NULL)。
划定区间:between a and b;表示闭区间[a, b]。
判断是否在指定的几个值之中存在:in(a,b,c,...);判定结果是否在a,b,c,…之中。
判断是否为NULL:is null。
判断是否不为NULL:is not null。
模糊匹配:like,需要配合通配符%或者-来使用。
通配符:%表示任意个数量的字符,_表示任意的一个字符。
逻辑运算符:and(&&), or(||), not(!)。
例如查询数学不及格同学名单:
mysql> select * from exam_score where math < 60;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 2 | 懒羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 8 | 黑大帅 | 10.0 | 11.0 | 9.0 | 20.0 |
+------+-----------+---------+------+---------+----------+
2 rows in set (0.00 sec)
查询英语成绩大于计算机综合的同学:
mysql> select name, english, computer from exam_score where english > computer;
+-----------+---------+----------+
| name | english | computer |
+-----------+---------+----------+
| 美羊羊 | 98.0 | 82.0 |
| 暖羊羊 | 98.0 | 76.0 |
+-----------+---------+----------+
2 rows in set (0.00 sec)
查询总分在300分以下的同学:
mysql> select name, chinese+math+english+computer as total from exam_score where chinese+math+english+computer < 300;
+-----------+-------+
| name | total |
+-----------+-------+
| 懒羊羊 | 181.5 |
| 灰太狼 | 234.5 |
| 黑大帅 | 50.0 |
+-----------+-------+
3 rows in set (0.00 sec)
注意使用别名时,条件表达式不能使用别名判断,否则会报错:
mysql> select name, chinese+math+english+computer as total from exam_score where total < 300;
ERROR 1054 (42S22): Unknown column 'total' in 'where clause'
查询语文数学均大于等于90分的同学:
mysql> select name, chinese, math from exam_score where chinese >= 90 and math >= 90;
+-----------+---------+------+
| name | chinese | math |
+-----------+---------+------+
| 美羊羊 | 99.5 | 90.5 |
| 喜羊羊 | 92.0 | 98.0 |
| 暖羊羊 | 90.0 | 91.0 |
+-----------+---------+------+
3 rows in set (0.00 sec)
查询语数英中有大于等于95分的同学:
mysql> select name, chinese, math, english from exam_score where chinese >= 95 or math >= 95 or english >= 95;
+-----------+---------+------+---------+
| name | chinese | math | english |
+-----------+---------+------+---------+
| 美羊羊 | 99.5 | 90.5 | 98.0 |
| 喜羊羊 | 92.0 | 98.0 | 88.0 |
| 暖羊羊 | 90.0 | 91.0 | 98.0 |
+-----------+---------+------+---------+
3 rows in set (0.00 sec)
查询语文数学总分大于等于180分或者计算机综合与英语大于85分的同学:
mysql> select name, chinese+math as cmtotal, english, computer from exam_score where chinese+math >= 180 or computer > 85 and english > 85;
+-----------+---------+---------+----------+
| name | cmtotal | english | computer |
+-----------+---------+---------+----------+
| 美羊羊 | 190.0 | 98.0 | 82.0 |
| 喜羊羊 | 190.0 | 88.0 | 100.0 |
| 暖羊羊 | 181.0 | 98.0 | 76.0 |
+-----------+---------+---------+----------+
3 rows in set (0.00 sec)
从上述代码我们能够看出来and的优先级大于or,如果需要打破这个优先级需要使用()。
查询计算机综合在[80, 90]之间的同学:
mysql> select name, computer from exam_score where computer between 80 and 90;
+-----------+----------+
| name | computer |
+-----------+----------+
| 美羊羊 | 82.0 |
| 沸羊羊 | 81.0 |
| 小灰灰 | 88.0 |
+-----------+----------+
-- 等价于
mysql> select name, computer from exam_score where computer >= 80 and computer <= 90;
+-----------+----------+
| name | computer |
+-----------+----------+
| 美羊羊 | 82.0 |
| 沸羊羊 | 81.0 |
| 小灰灰 | 88.0 |
+-----------+----------+
3 rows in set (0.00 sec)
查询计算机综合成绩为82,88,92,98的同学:
mysql> select name, computer from exam_score where computer in(82,88,92,98);
+-----------+----------+
| name | computer |
+-----------+----------+
| 美羊羊 | 82.0 |
| 小灰灰 | 88.0 |
+-----------+----------+
2 rows in set (0.00 sec)
-- 等价于
mysql> select name, computer from exam_score where computer=82 or computer=88 or computer=92 or computer=98;
+-----------+----------+
| name | computer |
+-----------+----------+
| 美羊羊 | 82.0 |
| 小灰灰 | 88.0 |
+-----------+----------+
2 rows in set (0.00 sec)
查找名字含有羊的同学:
mysql> select * from exam_score where name like '%羊';
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 2 | 懒羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 88.0 | 100.0 |
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
+------+-----------+---------+------+---------+----------+
5 rows in set (0.00 sec)
这里如果使用需要这样写,因为只能替代一个任意字符而%能替代任意个任意字符(包括0个)。
mysql> select * from exam_score where name like '__羊';
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 2 | 懒羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 88.0 | 100.0 |
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
+------+-----------+---------+------+---------+----------+
5 rows in set (0.00 sec)
我们在使用浏览器搜索时会搜索出大量的数据,一般搜出几十几百万条数据是很常见的,我们所浏览的电脑页面是不可能将所有的内容都呈现在一个页面,而我们拉到搜索页面的最下面会发现一个页码:
搜索引擎将所有的数据结果按页进行呈现,而且我们点进下一页时需要重新加载,说明搜索引擎不是一次性把所有的结果全部加载好,而是一页一页地将结果呈现出来,像这种模式的搜索或者查询就是分页查询,SQL也支持分页查询,使用关键字limit和offset实现。
语法格式:
select 列,列,列,... from 表名 limit 结果个数;
比如以每页最多3个元素查询考试成绩表:
mysql> select * from exam_score limit 3;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 2 | 懒羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 88.0 | 100.0 |
+------+-----------+---------+------+---------+----------+
3 rows in set (0.00 sec)
这个表后面的元素,可以指定一个下标(从0开始),以该下标对应的搜索结果为该页的首个结果依次输出。
语法格式:
select 列,列,列,... from 表名 limit 结果个数 offset 起始下标;
考试成绩表第二页结果:
mysql> select * from exam_score limit 3 offset 3;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
| 6 | 灰太狼 | 33.0 | 91.0 | 12.0 | 98.5 |
+------+-----------+---------+------+---------+----------+
3 rows in set (0.00 sec)
剩下的结果:
mysql> select * from exam_score limit 3 offset 6;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 7 | 小灰灰 | 81.0 | 82.0 | 78.0 | 88.0 |
| 8 | 黑大帅 | 10.0 | 11.0 | 9.0 | 20.0 |
+------+-----------+---------+------+---------+----------+
2 rows in set (0.00 sec)
分页查询本质上就是限制查询结果的条数,你可以设置最多输出几条结果,同时也可以指定从哪一条结果开始输出。
此外分页查询也支持嵌套排序查找以及搭配条件等其他语句一起使用。
输出成绩表语文成绩前三名:
mysql> select * from exam_score order by chinese desc limit 3;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 88.0 | 100.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
+------+-----------+---------+------+---------+----------+
3 rows in set (0.00 sec)
查询总结:
整表查询是一个非常危险的操作,要慎重使用,为了降低查询的危险性,一般需要搭配where语句使用,或者直接使用limit限制。
数据表的修改操作格式语法:
update 表名 set 列名 = 值, 列名 = 值 where 需要修改元素的条件;
该操作除了where,order by 和 limit也可以使用。
比如,对于上述成绩表,喜羊羊的英语成绩登记错了,需要将喜羊羊的英语成绩修改为92。
mysql> update exam_score set english = 92 where name = "喜羊羊";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from exam_score where name = "喜羊羊";
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 3 | 喜羊羊 | 92.0 | 98.0 | 92.0 | 100.0 |
+------+-----------+---------+------+---------+----------+
1 row in set (0.00 sec)
由于修改操作不可逆,而且如果修改条件不严格,可能会造成大量数据被修改,因此该操作也是比较危险的操作,要慎重。
另外NULL值进行运算结果仍为NULL。
再来练习一下修改操作,分别将总分成绩倒数第一,倒数第二,倒数第三的同学英语成绩加上15分。
-- 查询倒数前三的名单
mysql> select * from exam_score order by chinese+math+english+computer limit 3;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 8 | 黑大帅 | 10.0 | 11.0 | 9.0 | 20.0 |
| 2 | 懒羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 6 | 灰太狼 | 33.0 | 91.0 | 12.0 | 98.5 |
+------+-----------+---------+------+---------+----------+
3 rows in set (0.00 sec)
-- 修改
mysql> select * from exam_score order by chinese+math+english+computer limit 3;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 8 | 黑大帅 | 10.0 | 11.0 | 9.0 | 20.0 |
| 2 | 懒羊羊 | 58.5 | 32.5 | 24.0 | 66.5 |
| 6 | 灰太狼 | 33.0 | 91.0 | 12.0 | 98.5 |
+------+-----------+---------+------+---------+----------+
3 rows in set (0.00 sec)
mysql> update exam_score set english=english+20 order by chinese+math+english+computer limit 3;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
-- 查看新结果
mysql> select * from exam_score where name="黑大帅" or name="懒羊羊" or name="灰太狼";
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 2 | 懒羊羊 | 58.5 | 32.5 | 44.0 | 66.5 |
| 6 | 灰太狼 | 33.0 | 91.0 | 32.0 | 98.5 |
| 8 | 黑大帅 | 10.0 | 11.0 | 29.0 | 20.0 |
+------+-----------+---------+------+---------+----------+
3 rows in set (0.00 sec)
格式语法:
delete from 表名 where 需要删除的条件;
也可以嵌套order by和limit语句。
例如,删除黑大帅的成绩。
-- 删除前
mysql> select * from exam_score;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 2 | 懒羊羊 | 58.5 | 32.5 | 44.0 | 66.5 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 92.0 | 100.0 |
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
| 6 | 灰太狼 | 33.0 | 91.0 | 32.0 | 98.5 |
| 7 | 小灰灰 | 81.0 | 82.0 | 78.0 | 88.0 |
| 8 | 黑大帅 | 10.0 | 11.0 | 29.0 | 20.0 |
+------+-----------+---------+------+---------+----------+
8 rows in set (0.00 sec)
-- 删除中
mysql> delete from exam_score where name="黑大帅";
Query OK, 1 row affected (0.00 sec)
-- 删除后
mysql> select * from exam_score;
+------+-----------+---------+------+---------+----------+
| id | name | chinese | math | english | computer |
+------+-----------+---------+------+---------+----------+
| 1 | 美羊羊 | 99.5 | 90.5 | 98.0 | 82.0 |
| 2 | 懒羊羊 | 58.5 | 32.5 | 44.0 | 66.5 |
| 3 | 喜羊羊 | 92.0 | 98.0 | 92.0 | 100.0 |
| 4 | 沸羊羊 | 78.0 | 72.0 | 74.5 | 81.0 |
| 5 | 暖羊羊 | 90.0 | 91.0 | 98.0 | 76.0 |
| 6 | 灰太狼 | 33.0 | 91.0 | 32.0 | 98.5 |
| 7 | 小灰灰 | 81.0 | 82.0 | 78.0 | 88.0 |
+------+-----------+---------+------+---------+----------+
7 rows in set (0.00 sec)
该操作危险,要慎重使用。
MySQL有关数据表的基本增删查改的内容就全部介绍完毕了,还有一些更高级的CRUD操作博主后续也会安排上的
原文链接:
https://blog.csdn.NET/m0_59139260/article/details/123281542