【MySQL】表的增删查改(初阶)

MySQL中表的增删查改,即为CRUD,是增加(Create)查询(Retrieve)更新(Update)删除(Delete)四个单词首字母的缩写。


目录

1.新增

1.1单行数据+全列插入

1.2多行数据+指定列插入

1.3一次插入多个数据

2.查询

2.1全列查询

2.2指定列查询

2.3查询字段为表达式

2.4别名 AS

2.5去重查询 DISTINCT

2.6排序查询 ORDER BY

2.7条件查询 WHERE

//时间的插入与查询

2.8模糊查询

2.9NULL的查询

2.10分页查询LIMIT

3.修改

4.删除


1.新增

1.1单行数据+全列插入

格式:insert into 表名 values(列名 类型,列名 类型 ……);

注意:1> into 可以省略;

           2> 数量必须和定义表的列的数量及顺序一致;

           3> SQL中引用字符串使用 ' ' 或 " " 都可以;

           4> 若想插入中文,必须保证在创建数据库的时候指定charset utf8,忘记指定可以删库重建并指定。


mysql> use javasql;
Database changed

mysql> create table student (
    -> id int,
    -> name varchar(20)
    -> );
Query OK, 0 rows affected (0.05 sec)

mysql> show tables;
+-------------------+
| Tables_in_javasql |
+-------------------+
| student           |
+-------------------+
1 row in set (0.00 sec)

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into student values(1,'zhangsan');
Query OK, 1 row affected (0.00 sec)

mysql> insert student values(2,"lisi");
Query OK, 1 row affected (0.00 sec)

mysql> insert into student values(3,'王五');
Query OK, 1 row affected (0.01 sec)

1.2多行数据+指定列插入

 格式:insert into 表名(想要插入的列)values(列名 ……);

mysql> insert into student (name) values ('zhaoliu');
Query OK, 1 row affected (0.00 sec)

1.3一次插入多个数据

格式:insert into 表名 values(列名 类型,列名 类型 ……),(列名 类型,列名 类型 ……),(列名 类型,列名 类型 ……)……;

注意:一次插入三条数据只需一次服务器和客户端的交互,分三次每次插入一条数据则需要三次交互,后者的交互成本比前者高上不少,类似于一次寄三件和分三次寄出所需的运费。所以最好使用一次插入多个数据

mysql> insert into student values(10,'aaa'),(11,'bbb'),(12,'ccc');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

2.查询

2.1全列查询

格式:select * from 表名;

注意:1> 其中 * 为通配符;

           2> 这个操作很危险!!若这个表中存了上亿个数据,服务器解析SQL并执行就会读取student表的每一条数据,把所有的记录都通过网络返回给客户端,导致服务器一瞬间硬盘的带宽和网卡就都被吃满了,后续的操作速度就会特别慢,就像“卡死”了一样。

mysql> create table examResult(
    -> id int,
    -> name varchar(20),
    -> Chinese decimal(3,1),
    -> Math decimal(3,1),
    -> English decimal(3,1)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into examResult(id,name,Chinese,Math,English) values
    -> (1,'zhangsan',67,98,56),
    -> (2,'lisi',87,58,98),
    -> (3,'wangwu',88,56,90),
    -> (4,'zhaoliu',75,65,40);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from examResult;
+------+----------+---------+------+---------+
| id   | name     | Chinese | Math | English |
+------+----------+---------+------+---------+
|    1 | zhangsan |    67.0 | 98.0 |    56.0 |
|    2 | lisi     |    87.0 | 58.0 |    98.0 |
|    3 | wangwu   |    88.0 | 56.0 |    90.0 |
|    4 | zhaoliu  |    75.0 | 65.0 |    40.0 |
+------+----------+---------+------+---------+
4 rows in set (0.00 sec)

2.2指定列查询

格式:select 列名,列名……from 表名;

mysql> select name,Chinese from examResult;
+----------+---------+
| name     | Chinese |
+----------+---------+
| zhangsan |    67.0 |
| lisi     |    87.0 |
| wangwu   |    88.0 |
| zhaoliu  |    75.0 |
+----------+---------+
4 rows in set (0.00 sec)

mysql> select id,English from examResult;
+------+---------+
| id   | English |
+------+---------+
|    1 |    56.0 |
|    2 |    98.0 |
|    3 |    90.0 |
|    4 |    40.0 |
+------+---------+
4 rows in set (0.00 sec)

2.3查询字段为表达式

格式:select 列名 运算,列名 运算 …… from 表名;

注意:1> 运算只能进行数字间的四则运算,不能使用字符串;
           2> 运算完后客户端中显示的结果只是一个“临时表”,也就是说select操作不管怎么写,都不会影响到数据库服务器硬盘上存储的原始数据

           3> 临时表中的数据类型和原始数据类型不一定一致,比如下面的108.0,按理说类型为decimal(3,1),只能有三位有效数字,但是临时表中是四位有效数字也没报错。

mysql> select name,Math + 10 from examResult;
+----------+-----------+
| name     | Math + 10 |
+----------+-----------+
| zhangsan |     108.0 |
| lisi     |      68.0 |
| wangwu   |      66.0 |
| zhaoliu  |      75.0 |
+----------+-----------+
4 rows in set (0.00 sec)

mysql> select name,Math from examResult;
+----------+------+
| name     | Math |
+----------+------+
| zhangsan | 98.0 |
| lisi     | 58.0 |
| wangwu   | 56.0 |
| zhaoliu  | 65.0 |
+----------+------+
4 rows in set (0.00 sec)

2.4别名 AS

格式:select 列名 as 别名 from 表名;

注意:当查询临时表的列名和当前表达式一致时,若表达式过长可读性会较差,这时就需使用别名来简化表达式;

mysql> select name,Chinese + Math + English from examResult;
+----------+--------------------------+
| name     | Chinese + Math + English |
+----------+--------------------------+
| zhangsan |                    221.0 |
| lisi     |                    243.0 |
| wangwu   |                    234.0 |
| zhaoliu  |                    180.0 |
+----------+--------------------------+
4 rows in set (0.00 sec)

mysql> select name,Chinese + Math +English as total from examResult;
+----------+-------+
| name     | total |
+----------+-------+
| zhangsan | 221.0 |
| lisi     | 243.0 |
| wangwu   | 234.0 |
| zhaoliu  | 180.0 |
+----------+-------+
4 rows in set (0.00 sec)

2.5去重查询 DISTINCT

格式:select distinct 列名 from 表名;

注意:distinct 后面的列名也可以是多个,这时要求所有的列的值都相同才算“重复”。

mysql> select Math from examResult;
+------+
| Math |
+------+
| 98.0 |
| 58.0 |
| 98.0 |
| 65.0 |
+------+
4 rows in set (0.00 sec)

mysql> select distinct Math from examResult;
+------+
| Math |
+------+
| 98.0 |
| 58.0 |
| 65.0 |
+------+
4 rows in set (0.00 sec)

2.6排序查询 ORDER BY

格式:select 列名 from 表名 order by 列名;

注意:1> 同上面说的,这还是只是在临时表上排序数;

           2> 默认排序为升序,可在语句末尾加上asc;

           3> 若想降序,语句末尾加上desc(descend),与之前查看指定表的结构describe不同;

           4> order by 后可接多个列名,表示先拿前面的列排序,出现相同时再拿后面的列排序。

mysql> select * from examResult;
+------+----------+---------+------+---------+
| id   | name     | Chinese | Math | English |
+------+----------+---------+------+---------+
|    1 | zhangsan |    67.0 | 98.0 |    56.0 |
|    2 | lisi     |    87.0 | 58.0 |    98.0 |
|    3 | wangwu   |    88.0 | 98.0 |    90.0 |
|    4 | zhaoliu  |    75.0 | 65.0 |    40.0 |
+------+----------+---------+------+---------+
4 rows in set (0.00 sec)

mysql> select * from examResult order by English;
+------+----------+---------+------+---------+
| id   | name     | Chinese | Math | English |
+------+----------+---------+------+---------+
|    4 | zhaoliu  |    75.0 | 65.0 |    40.0 |
|    1 | zhangsan |    67.0 | 98.0 |    56.0 |
|    3 | wangwu   |    88.0 | 98.0 |    90.0 |
|    2 | lisi     |    87.0 | 58.0 |    98.0 |
+------+----------+---------+------+---------+
4 rows in set (0.04 sec)

mysql> select * from examResult order by English desc;
+------+----------+---------+------+---------+
| id   | name     | Chinese | Math | English |
+------+----------+---------+------+---------+
|    2 | lisi     |    87.0 | 58.0 |    98.0 |
|    3 | wangwu   |    88.0 | 98.0 |    90.0 |
|    1 | zhangsan |    67.0 | 98.0 |    56.0 |
|    4 | zhaoliu  |    75.0 | 65.0 |    40.0 |
+------+----------+---------+------+---------+
4 rows in set (0.00 sec)

mysql> select * from examResult order by Math,English;
+------+----------+---------+------+---------+
| id   | name     | Chinese | Math | English |
+------+----------+---------+------+---------+
|    3 | lisi     |    87.0 | 58.0 |    98.0 |
|    2 | zhaoliu  |    75.0 | 65.0 |    40.0 |
|    4 | zhangsan |    67.0 | 98.0 |    56.0 |
|    1 | wangwu   |    88.0 | 98.0 |    90.0 |
+------+----------+---------+------+---------+
4 rows in set (0.00 sec)

2.7条件查询 WHERE

格式:select 列名,列名 …… from 表名 where 条件;

注意:1> 条件中不能使用列的别名;

           2> and 的优先级大于 or,但是建议使用括号手动明确优先级;

SQL中支持的条件表示: 关系运算符 + 逻辑运算符

运算符

说明

>, >=, <, <= 大于,大于等于,小于,小于等于
= 等于,NULL不安全,例如 NULL = NULL的结果是NULL
<=> 等于,NULL安全,例如NULL <=> NULL的结果是TRUE
!=, <> 不等于
BETWEEN a AND b 范围匹配,[ a, b ] ,满足范围返回TRUE
IN ( option, …… ) 如果是option中的任意一个,返回TRUE
IS NULL 是NULL
IS NOT NULL 不是NULL
LIKE 模糊匹配,如:%表示任意多个 (包括0个) 任意字符,_表示任意一个字符

注意:SQL默认情况下对WHERE XX!=NULL和WHERE XX=NULL的判断会永远返回0行,却不会提示语法错误 .

运算符 说明
AND 多个条件必须都为TRUE,结果才为TRUE
OR 任意一个条件为TRUE,结果为TRUE
NOT 条件为TRUE,结果为FALSE
mysql> select name,English from examResult where English < 60;
+----------+---------+
| name     | English |
+----------+---------+
| zhangsan |    56.0 |
| zhaoliu  |    40.0 |
+----------+---------+
2 rows in set (0.04 sec)

mysql> select name,English from examResult where Chinese > English;
+----------+---------+
| name     | English |
+----------+---------+
| zhangsan |    56.0 |
| zhaoliu  |    40.0 |
+----------+---------+
2 rows in set (0.00 sec)

mysql> select name,Chinese + Math + English as total from examResult where Chinese + Math + English > 200;
+----------+-------+
| name     | total |
+----------+-------+
| zhangsan | 221.0 |
| lisi     | 243.0 |
| wangwu   | 234.0 |
+----------+-------+
3 rows in set (0.00 sec)

mysql> select name,Chinese,English from examResult where Chinese > 80 and English >80;
+--------+---------+---------+
| name   | Chinese | English |
+--------+---------+---------+
| lisi   |    87.0 |    98.0 |
| wangwu |    88.0 |    90.0 |
+--------+---------+---------+
2 rows in set (0.00 sec)

mysql> select name,Chinese,English from examResult where Chinese > 80 or Math > 80;
+----------+---------+---------+
| name     | Chinese | English |
+----------+---------+---------+
| zhangsan |    67.0 |    56.0 |
| lisi     |    87.0 |    98.0 |
| wangwu   |    88.0 |    90.0 |
+----------+---------+---------+
3 rows in set (0.00 sec)

//时间的插入与查询

mysql> use javasql;
Database changed

mysql> insert into article values
    -> ('1','abcd',20190101113031),
    -> ('2','efgh',20190203103032),
    -> ('3','ijkl',20190304103000),
    -> ('4',NULL,20220309113020);
Query OK, 4 rows affected (0.04 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from article where date between '2019-1-1 10:30:00' and '2019-11-10 4:02:00';
+--------+-------+---------------------+
| author | title | date                |
+--------+-------+---------------------+
| 1      | abcd  | 2019-01-01 11:30:31 |
| 2      | efgh  | 2019-02-03 10:30:32 |
| 3      | ijkl  | 2019-03-04 10:30:00 |
+--------+-------+---------------------+
3 rows in set (0.00 sec)

mysql> select * from article where title <=> null or date > '2019-01-01 00:00:00';
+--------+-------+---------------------+
| author | title | date                |
+--------+-------+---------------------+
| 1      | abcd  | 2019-01-01 11:30:31 |
| 2      | efgh  | 2019-02-03 10:30:32 |
| 3      | ijkl  | 2019-03-04 10:30:00 |
| 4      | NULL  | 2022-03-09 11:30:20 |
+--------+-------+---------------------+
4 rows in set (0.00 sec)

2.8模糊查询

格式:select * from 表名 where 条件;

% 匹配0个或任意个 任意字符

匹配1个 任意字符

mysql> select * from examresult where name like '孙%';
+------+--------+---------+------+---------+
| id   | name   | Chinese | Math | English |
+------+--------+---------+------+---------+
|    5 | 孙权   |    67.0 | 89.0 |    72.0 |
|    6 | 孙尚香 |    56.0 | 98.0 |    59.0 |
+------+--------+---------+------+---------+
2 rows in set (0.00 sec)

mysql> select * from examresult where name like '孙_';
+------+------+---------+------+---------+
| id   | name | Chinese | Math | English |
+------+------+---------+------+---------+
|    5 | 孙权 |    67.0 | 89.0 |    72.0 |
+------+------+---------+------+---------+
1 row in set (0.00 sec)

mysql> select * from examresult where name like '孙__';
+------+--------+---------+------+---------+
| id   | name   | Chinese | Math | English |
+------+--------+---------+------+---------+
|    6 | 孙尚香 |    56.0 | 98.0 |    59.0 |
+------+--------+---------+------+---------+
1 row in set (0.00 sec)

mysql> select * from examresult where name like '孙___';
Empty set (0.00 sec)

2.9NULL的查询

格式:select * from 表名 where Chinese is null;

           select * from 表名 where Chinese <=> null;

mysql> select * from examresult where Chinese is null;
+------+--------+---------+------+---------+
| id   | name   | Chinese | Math | English |
+------+--------+---------+------+---------+
|    7 | 孙悟空 |    NULL | NULL |    NULL |
+------+--------+---------+------+---------+
1 row in set (0.04 sec)

mysql> select * from examresult where Chinese <=> null;
+------+--------+---------+------+---------+
| id   | name   | Chinese | Math | English |
+------+--------+---------+------+---------+
|    7 | 孙悟空 |    NULL | NULL |    NULL |
+------+--------+---------+------+---------+
1 row in set (0.00 sec)

2.10分页查询LIMIT

格式:select * from 表名 limit M offset N;

表示一次查询,从第N条数据开始,最多获取到M条记录

mysql> select * from examresult limit 4;
+------+----------+---------+------+---------+
| id   | name     | Chinese | Math | English |
+------+----------+---------+------+---------+
|    1 | zhangsan |    67.0 | 98.0 |    56.0 |
|    2 | lisi     |    87.0 | 58.0 |    98.0 |
|    3 | wangwu   |    88.0 | 56.0 |    90.0 |
|    4 | zhaoliu  |    75.0 | 65.0 |    40.0 |
+------+----------+---------+------+---------+
4 rows in set (0.00 sec)

mysql> select * from examresult limit 4 offset 3;
+------+---------+---------+------+---------+
| id   | name    | Chinese | Math | English |
+------+---------+---------+------+---------+
|    4 | zhaoliu |    75.0 | 65.0 |    40.0 |
|    5 | 孙权    |    67.0 | 89.0 |    72.0 |
|    6 | 孙尚香  |    56.0 | 98.0 |    59.0 |
|    7 | 孙悟空  |    NULL | NULL |    NULL |
+------+---------+---------+------+---------+
4 rows in set (0.00 sec)

3.修改

格式:update 表名 set 列名 = M where 条件;

将满足条件的列的值改为M。后面不加where条件则会对所有数据进行操作,很危险!!

注意:此处的修改是修改MySQL服务器,持久生效。

mysql> select * from examresult;
+------+----------+---------+------+---------+
| id   | name     | Chinese | Math | English |
+------+----------+---------+------+---------+
|    1 | zhangsan |    67.0 | 98.0 |    56.0 |
|    2 | lisi     |    87.0 | 58.0 |    98.0 |
|    3 | wangwu   |    88.0 | 56.0 |    90.0 |
|    4 | zhaoliu  |    75.0 | 65.0 |    40.0 |
|    5 | 孙权     |    67.0 | 89.0 |    72.0 |
|    6 | 孙尚香   |    56.0 | 98.0 |    59.0 |
+------+----------+---------+------+---------+
6 rows in set (0.00 sec)

mysql> update examresult set Math = 66 where name = 'wangwu';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update examresult set Chinese = 80,English = 90 where name = '孙尚香';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from examresult;
+------+----------+---------+------+---------+
| id   | name     | Chinese | Math | English |
+------+----------+---------+------+---------+
|    1 | zhangsan |    67.0 | 98.0 |    56.0 |
|    2 | lisi     |    87.0 | 58.0 |    98.0 |
|    3 | wangwu   |    88.0 | 66.0 |    90.0 |
|    4 | zhaoliu  |    75.0 | 65.0 |    40.0 |
|    5 | 孙权     |    67.0 | 89.0 |    72.0 |
|    6 | 孙尚香   |    80.0 | 98.0 |    90.0 |
+------+----------+---------+------+---------+
6 rows in set (0.00 sec)

4.删除

格式:delete from 表名 where 条件;

后面不加where条件则会删除表内所有数据,很危险!!

mysql> select * from examresult;
+------+----------+---------+------+---------+
| id   | name     | Chinese | Math | English |
+------+----------+---------+------+---------+
|    1 | zhangsan |    67.0 | 98.0 |    56.0 |
|    2 | lisi     |    87.0 | 58.0 |    98.0 |
|    3 | wangwu   |    88.0 | 66.0 |    90.0 |
|    4 | zhaoliu  |    75.0 | 65.0 |    40.0 |
+------+----------+---------+------+---------+
7 rows in set (0.00 sec)

mysql> delete from examresult where name = 'zhaoliu';
Query OK, 1 row affected (0.04 sec)

mysql> select * from examresult;
+------+----------+---------+------+---------+
| id   | name     | Chinese | Math | English |
+------+----------+---------+------+---------+
|    1 | zhangsan |    67.0 | 98.0 |    56.0 |
|    2 | lisi     |    87.0 | 58.0 |    98.0 |
|    3 | wangwu   |    88.0 | 66.0 |    90.0 |
+------+----------+---------+------+---------+
6 rows in set (0.00 sec)

你可能感兴趣的:(MySQL,mysql,数据库,表,Java,where,distinct,order,by)