【2】MySQL数据库管理 —— 3. MySQL数据操纵语言(DML)

数据操纵语言(DML)

  • insert: 向表中插入数据

  • delete: 删除表中的数据,格式:delete from tablname [ where 条件 ]

  • update: 修改表中的数据,格式:update tablname set colName1=value1[colName2=value2] [ where 条件 ]

  • where: 对表中的数据增加条件进行限制,起到过滤的作用

    1. 格式:where colName 关系运算符 value [or|and 条件2]
    2. 关系运算符:> >= < <= 等于: = 不等于: != 或者 <>
  • null值操作: 比较null时,不能使用=或者!=或者<>, 而是使用 is 或者 is not ,在select子句中,使用关系运算符.

操作

创建一个表

### 创建 表
mysql> create table user(
    -> id int,
    -> name varchar(20),
    -> birth date,
    -> age int,
    -> address varchar(50)
    -> );
Query OK, 0 rows affected (0.01 sec)

### 查看 表结构
mysql> desc user;
字段		 类型		    是否允许为空	   不写默认为空
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| age     | int(11)     | YES  |     | NULL    |       |
| address | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

插入数据

### 单个插入
mysql> insert into user values (1,'wangj',0122,22,'wuhan');
Query OK, 1 row affected (0.00 sec)

### 查看
mysql> select * from user;
+------+-------+------------+------+---------+
| id   | name  | birth      | age  | address |
+------+-------+------------+------+---------+
|    1 | wangj | 2000-01-22 |   22 | wuhan   |
+------+-------+------------+------+---------+
1 row in set (0.00 sec)

### 批量插入
mysql> insert into user(id,name,address) values(2,'liuyww','wuhan'), 
    -> (3,'wangx','wenzhou'),
    -> (4,'test','test');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

### 查看
mysql> select * from user;
+------+--------+------------+------+---------+
| id   | name   | birth      | age  | address |
+------+--------+------------+------+---------+
|    1 | wangj  | 2000-01-22 |   22 | wuhan   |
|    2 | liuyww | NULL       | NULL | wuhan   |
|    3 | wangx  | NULL       | NULL | wenzhou |
|    4 | test   | NULL       | NULL | test    |
+------+--------+------------+------+---------+
4 rows in set (0.01 sec)

删除数据

### where 删除
mysql> delete from user where id='4';
Query OK, 1 row affected (0.00 sec)

### 查看
mysql> select * from user;
+------+--------+------------+------+---------+
| id   | name   | birth      | age  | address |
+------+--------+------------+------+---------+
|    1 | wangj  | 2000-01-22 |   22 | wuhan   |
|    2 | liuyww | NULL       | NULL | wuhan   |
|    3 | wangx  | NULL       | NULL | wenzhou |
+------+--------+------------+------+---------+
3 rows in set (0.00 sec)

### where and 删除
mysql> delete from user where id='1' and address='suzhou';		### 满足两项条件的匹配数据会被删除 [ and 等同于 并且 ]
Query OK, 0 rows affected (0.01 sec)

mysql> select * from user;										### 未删除成功 [ 不存在同时满足上述两项条件的数据 ]
+------+--------+------------+------+---------+
| id   | name   | birth      | age  | address |
+------+--------+------------+------+---------+
|    1 | wangj  | 2000-01-22 |   22 | wuhan   |					
|    2 | liuyww | NULL       | NULL | wuhan   |
|    3 | wangx  | NULL       | NULL | wenzhou |
+------+--------+------------+------+---------+
3 rows in set (0.00 sec)

mysql> delete from user where id='1' and address='wuhan';		### 匹配到满足两项条件的数据
Query OK, 1 row affected (0.00 sec)

mysql> select * from user;										### 删除成功
+------+--------+-------+------+---------+
| id   | name   | birth | age  | address |
+------+--------+-------+------+---------+
|    2 | liuyww | NULL  | NULL | wuhan   |
|    3 | wangx  | NULL  | NULL | wenzhou |
+------+--------+-------+------+---------+
2 rows in set (0.00 sec)


### where or 删除
mysql> delete from user where id='4' or address='wuhan';	### 满足其中一项条件的数据会被匹配删除;[ or 等同于 或者 ]
Query OK, 1 row affected (0.01 sec)

mysql> select * from user;									### address等于武汉的数据已被删除
+------+-------+-------+------+---------+
| id   | name  | birth | age  | address |
+------+-------+-------+------+---------+
|    3 | wangx | NULL  | NULL | wenzhou |
+------+-------+-------+------+---------+
1 row in set (0.00 sec)

### 直接删除所有数据 [ 不加where 条件 ]
mysql> delete from user;								   ### 不加 where 条件 刪除所有数据
Query OK, 4 rows affected (0.00 sec)

mysql> select * from user;								   ### user 中所有数据已被删除
Empty set (0.00 sec)

更新数据

### 整体更新
mysql> update user set address='china';					### 将user表中所有数据地址更新为China
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from user;								### 更新成功
+------+--------+-------+------+---------+
| id   | name   | birth | age  | address |
+------+--------+-------+------+---------+
|    2 | liuyww | NULL  | NULL | china   |
|    3 | wangx  | NULL  | NULL | china   |
|    4 | test   | NULL  | NULL | china   |
+------+--------+-------+------+---------+
3 rows in set (0.00 sec)

### 单个更新
mysql> update user set address='wuhan',name='wangj' where id='4';	### 将user表中id为4的数据 地址和名字更新为wuhan wangj
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from user;											### 更新成功
+------+--------+-------+------+---------+
| id   | name   | birth | age  | address |
+------+--------+-------+------+---------+
|    2 | liuyww | NULL  | NULL | china   |
|    3 | wangx  | NULL  | NULL | china   |
|    4 | wangj  | NULL  | NULL | wuhan   |
+------+--------+-------+------+---------+
3 rows in set (0.00 sec)

おすすめ

転載: blog.csdn.net/weixin_45791800/article/details/121335150