DML全称Data Manipulation Language,即数据操作语言,用来对表中的数据进行增删改的操作
mysql> desc emp;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| username | varchar(10) | YES | | NULL | |
| age | int | YES | | NULL | |
| gender | varchar(1) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into emp
-> (id,username,age,gender) values
-> (1,'Richie',21,'男');
Query OK, 1 row affected (0.03 sec)
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
+------+----------+------+--------+
1 row in set (0.00 sec)
mysql> desc emp;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| username | varchar(10) | YES | | NULL | |
| age | int | YES | | NULL | |
| gender | varchar(1) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into emp values
-> (2,'Mike',24,'男');
Query OK, 1 row affected (0.03 sec)
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Mike | 24 | 男 |
+------+----------+------+--------+
2 rows in set (0.00 sec)
mysql> desc emp;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| username | varchar(10) | YES | | NULL | |
| age | int | YES | | NULL | |
| gender | varchar(1) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into emp (id,username,age,gender)
-> values
-> (3,'Nancy',23,'女'),
-> (4,'Jack',28,'女');
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Mike | 24 | 男 |
| 3 | Nancy | 23 | 女 |
| 4 | Jack | 28 | 女 |
+------+----------+------+--------+
4 rows in set (0.00 sec)
mysql> insert into emp values
-> (5,'Taylor',23,'女'),
-> (6,'Joker',25,'男');
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Mike | 24 | 男 |
| 3 | Nancy | 23 | 女 |
| 4 | Jack | 28 | 女 |
| 5 | Taylor | 23 | 女 |
| 6 | Joker | 25 | 男 |
+------+----------+------+--------+
6 rows in set (0.00 sec)
# 将id为1的记录的age改为18
update emp set age = 18 where id = 1;
# 将username为Jack的记录的username改为Pig,age改为38
update emp set username = 'Pig',age = 38 where username = 'Jack';
-------------------------------------
# 原来的
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 21 | 男 |
| 2 | Mike | 24 | 男 |
| 3 | Nancy | 23 | 女 |
| 4 | Jack | 28 | 女 |
| 5 | Taylor | 23 | 女 |
| 6 | Joker | 25 | 男 |
+------+----------+------+--------+
# 更改后
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 18 | 男 |
| 2 | Mike | 24 | 男 |
| 3 | Nancy | 23 | 女 |
| 4 | pig | 38 | 女 |
| 5 | Taylor | 23 | 女 |
| 6 | Joker | 25 | 男 |
+------+----------+------+--------+
mysql> update emp set age = 18;
Query OK, 6 rows affected (0.01 sec)
Rows matched: 7 Changed: 6 Warnings: 0
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 1 | Richie | 18 | 男 |
| 2 | Mike | 18 | 男 |
| 3 | Nancy | 18 | 女 |
| 4 | Pig | 18 | 女 |
| 5 | Taylor | 18 | 女 |
| 6 | Joker | 18 | 男 |
| 7 | Dragon | 18 | 男 |
+------+----------+------+--------+
7 rows in set (0.00 sec)
mysql> delete from emp where gender = '男'; # 删除所有gender为男的记录
Query OK, 4 rows affected (0.01 sec)
mysql> select * from emp;
+------+----------+------+--------+
| id | username | age | gender |
+------+----------+------+--------+
| 3 | Nancy | 18 | 女 |
| 4 | Pig | 18 | 女 |
| 5 | Taylor | 18 | 女 |
+------+----------+------+--------+
3 rows in set (0.00 sec)
mysql> delete from emp; # 删除所有记录
Query OK, 3 rows affected (0.03 sec)
mysql> select * from emp;
Empty set (0.03 sec)