MySQL 数据操纵语言 DML


数据操纵语言 DML

在这里插入图片描述

数据操纵语言(Data Manipulation Language,DML)是 SQL 语言的核心部分之一。在添加、更新或者删除表中的数据时,需要执行 DML 语句。很多时候我们提到数据库的基本操作,都会说增、删、改、查,为什么 DML 里面没有查询?因为 SELECT 查询语句属于数据查询语言 DQL,不属于数据操纵语言 DML,只是在日常工作中,多数研发人员、数据库管理员都习惯性的将 SELECT 语句归入数据操纵语言中,这一点大家需要弄清楚。接下来,我们一起来学习 DML。

INSERT 语句

INSERT 语句用于向表中插入新的记录。其基本语法形式如下:

INSERT INTO table[(column1, column2…)]
VALUES (value1,value2…)

其中 table 是表名,column1,column2…是表中的字段名列表,用“,”隔开;value1,value2…是字段对应的值列表。

INSERT 语句可以有两种书写形式。

第一种形式无需指定要插入数据的列名,只需提供被插入的值即可。比如往 city 表中再插入一条阿富汗(代码 AFG)这个国家的城市信息。先查看插入前 city 表中现存的阿富汗的城市,其 SQL 语句如下:

select * from city where countrycode ='AFG';

输出结果:

MariaDB [world]> select * from city where countrycode ='AFG';
+----+----------------+-------------+----------+------------+
| ID | Name           | CountryCode | District | Population |
+----+----------------+-------------+----------+------------+
|  1 | Kabul          | AFG         | Kabol    |    1780000 |
|  2 | Qandahar       | AFG         | Qandahar |     237500 |
|  3 | Herat          | AFG         | Herat    |     186800 |
|  4 | Mazar-e-Sharif | AFG         | Balkh    |     127800 |
+----+----------------+-------------+----------+------------+
4 rows in set (0.001 sec)

接下来,我们往 city 表中插入的城市信息如下表所示:

ID Name CountryCode District Population
4080 Farah AFG Farah 500000

因为 city 中已经存在 4079 条记录,此时我们的 ID 为 4080。实现插入的 SQL 语句如下:

insert into city values(4080,'Farah','AFG','Farah',500000);

输出结果:

MariaDB [world]> insert into city values(4080,'Farah','AFG','Farah',500000);
Query OK, 1 row affected (0.002 sec)

该方式因为没有在表名后面跟字段名,所以插入的信息必须和表中字段的顺序保持一致。检查插入信息是否完成。结果如下:

select * from city where countrycode ='AFG';

输出结果:

MariaDB [world]> select * from city where countrycode ='AFG';
+------+----------------+-------------+----------+------------+
| ID   | Name           | CountryCode | District | Population |
+------+----------------+-------------+----------+------------+
|    1 | Kabul          | AFG         | Kabol    |    1780000 |
|    2 | Qandahar       | AFG         | Qandahar |     237500 |
|    3 | Herat          | AFG         | Herat    |     186800 |
|    4 | Mazar-e-Sharif | AFG         | Balkh    |     127800 |
| 4080 | Farah          | AFG         | Farah    |     500000 |
+------+----------------+-------------+----------+------------+
5 rows in set (0.001 sec)

第二种形式就是表名后面跟字段名,此时字段名的顺序可以随意指定,不需要按照表中字段的顺序列出。待插入的城市信息如下表所示:

ID Name CountryCode District Population
4081 Balkh AFG Balkh 400000

实现插入的 SQL 语句如下:

insert into city(Name,District,ID,CountryCode,Population) values('Balkh','Balkh',4081,'AFG',400000);

表名后的字段顺序已经被打乱了,此时 values 后面的值就必须和前面的字段顺序保持一致。检查插入后的信息,结果如下:

select * from city where countrycode ='AFG';

输出结果:

MariaDB [world]> select * from city where countrycode ='AFG';
+------+----------------+-------------+----------+------------+
| ID   | Name           | CountryCode | District | Population |
+------+----------------+-------------+----------+------------+
|    1 | Kabul          | AFG         | Kabol    |    1780000 |
|    2 | Qandahar       | AFG         | Qandahar |     237500 |
|    3 | Herat          | AFG         | Herat    |     186800 |
|    4 | Mazar-e-Sharif | AFG         | Balkh    |     127800 |
| 4080 | Farah          | AFG         | Farah    |     500000 |
| 4081 | Balkh          | AFG         | Balkh    |     400000 |
+------+----------------+-------------+----------+------------+
6 rows in set (0.001 sec)

插入的信息仍然会按原表的字段顺序进行显示。

在表中我们可以设置整数类型的字段进行自动增长(后面会细讲),city 表中的 ID 拥有自动增长属性。设置了该属性的字段,在进行插入操作的时候,其值使用 null 来代替。插入一个阿富汗城市,待插入的城市信息如下表所示:

ID Name CountryCode District Population
null Ghor AFG Ghor 300000

其 SQL 语句如下:

insert into city(ID,Name, CountryCode, District ,Population) values(null,'Ghor','AFG', 'Ghor',300000);

检查插入后的信息,结果如下:

select * from city where countrycode ='AFG';

输出结果:

MariaDB [world]> select * from city where countrycode ='AFG';
+------+----------------+-------------+----------+------------+
| ID   | Name           | CountryCode | District | Population |
+------+----------------+-------------+----------+------------+
|    1 | Kabul          | AFG         | Kabol    |    1780000 |
|    2 | Qandahar       | AFG         | Qandahar |     237500 |
|    3 | Herat          | AFG         | Herat    |     186800 |
|    4 | Mazar-e-Sharif | AFG         | Balkh    |     127800 |
| 4080 | Farah          | AFG         | Farah    |     500000 |
| 4081 | Balkh          | AFG         | Balkh    |     400000 |
| 4082 | Ghor           | AFG         | Ghor     |     300000 |
+------+----------------+-------------+----------+------------+
7 rows in set (0.000 sec)

这里需要强调两点:

  1. 在插入记录的时候,我们一般要求表名后面跟上字段名,这样对于软件开发人员来说,提高了代码的可读性。
  2. 在插入数据的时候需要满足该表的其他约束,city 表中就存在一个外键约束 countrycode(有关约束的内容,本章后面的课程会详细介绍)。

UPDATE 语句

修改数据也是经常用到的数据库管理操作。在数据库中使用 UPDATE 语句对数据进行修改,其基本语法形式如下:

UPDATE table SET column1=value1, column2=value2 [where子句]

UPDATE、SET 是该语法的固定形式。其中 table 表示的是表名,column1、column2 表示的是要修改的字段名,value1、value2 表示的是修改后的值。

值得注意的是,在使用 UPDATE 语句时,通常需要使用 WHERE 子句进行条件限制,用来指定被修改的行。如果没有 WHERE 子句,则表中所有的记录都会被修改。

把 ID 为 4082 的这条记录的人口数量更改为 350000,实现 SQL 语句如下:

update city set population = 350000 where id = 4082;

输出结果:

MariaDB [world]> update city set population = 350000 where id = 4082;
Query OK, 1 row affected (0.004 sec)
Rows matched: 1  Changed: 1  Warnings: 0

查看修改后的结果,如下:

select * from city where id = 4082;

输出结果:

MariaDB [world]> select * from city where id = 4082;
+------+------+-------------+----------+------------+
| ID   | Name | CountryCode | District | Population |
+------+------+-------------+----------+------------+
| 4082 | Ghor | AFG         | Ghor     |     350000 |
+------+------+-------------+----------+------------+
1 row in set (0.001 sec)

UPDATE 也可以同时更新多个字段,如语法介绍的那样,多字段之间用“,”隔开。同时更新 ID 为 4082 的 District 字段和 Population 字段。其 SQL 语句如下:

update city set district='Ghor1',population='355000' where id=4082;

查看修改后的结果,如下:

select * from city where id = 4082;

输出结果:

MariaDB [world]> select * from city where id = 4082;
+------+------+-------------+----------+------------+
| ID   | Name | CountryCode | District | Population |
+------+------+-------------+----------+------------+
| 4082 | Ghor | AFG         | Ghor1    |     355000 |
+------+------+-------------+----------+------------+
1 row in set (0.000 sec)

注意:和 INSERT 语句一样,使用 UPDATE 语句时也要注意字段的约束控制。

DELETE 语句

数据库操作语言 DML 包括 INSERT 语句、UPDATE 语句以及 DELETE 语句。接下来要介绍的就是删除语句。其基本语法形式如下:

DELETE FROM table [WHERE]

其中 table 表示表名,如果 DELETE 子句中不写 WHERE 子句,那么删除的将会是表中的所有数据。 现删除 city 表中 ID 为 4082 的城市记录,其 SQL 语句如下:

delete from city where id = 4082;

输出结果:

MariaDB [world]> delete from city where id = 4082;
Query OK, 1 row affected (0.002 sec)

利用 select 子句查看删除后的结果。

select * from city where id = 4082;

输出结果:

MariaDB [world]> select * from city where id = 4082;
Empty set (0.000 sec)

同时删除多条记录可以使用 in 链接符来完成。比如,现要同时删除 city 表中 ID 为 4080 和 4081 的两条记录,其 SQL 语句如下:

delete from city where id in(4080,4081);

输出结果:

MariaDB [world]> delete from city where id in(4080,4081);
Query OK, 2 rows affected (0.002 sec)

查看结果如下:

select * from city where id in(4080,4081);

输出结果:

MariaDB [world]> select * from city where id in(4080,4081);
Empty set (0.000 sec)

注意:利用 DELETE 子句删除记录的时候,一定要结合着 WHERE 子句使用,进行精确删除。DELETE 子句后不跟 WHERE 子句的另外一个用途,就是进行清空数据操作。

猜你喜欢

转载自blog.csdn.net/m0_62617719/article/details/130941132