关系型数据库大王Mysql——DML语句操作示例

DML操作

有关数据的操作:

  1. 增加 insert
  2. 删除 delete
  3. 修改 update
  4. 查询 select(重点)
  5. 条件查询
  6. 排序
  7. 函数
  8. 分组查询(group by)

在对数据进行操作时要注意表的结构------约束条件

insert语句

复制代码
格式:
insert into 表名 values(value1,value2...);
  • value后的内容:与表字段匹配的数据,如果字段为主键,主键自带非空约束,可以与auto_increment结合设置为null,让其自增
  • 主键使用insert时是唯一的,不能重复插入
  • 字符串可以int转化,eg: "111" = 111
  • 自增长策略中,如果手动设置值,则下一个新增数据按最大索引值为准

示例1

mysql 复制代码
mysql> create database dml_test;
Query OK, 1 row affected (0.01 sec)

mysql> use dml_test;
Database changed
mysql> create table user(
    -> user_id int(10) primary key auto_increment,
    -> username varchar(255),
    -> password varchar(255));
Query OK, 0 rows affected, 1 warning (0.05 sec)

mysql> desc user;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| user_id  | int          | NO   | PRI | NULL    | auto_increment |
| username | varchar(255) | YES  |     | NULL    |                |
| password | varchar(255) | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> insert into user values(null,'cj','123');
Query OK, 1 row affected (0.02 sec)

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | cj       | 123      |
+---------+----------+----------+
1 row in set (0.00 sec)

#设置主键约束的字段不能插入相同的数据
mysql> insert into user values(1,'cj','123');
ERROR 1062 (23000): Duplicate entry '1' for key 'user.PRIMARY'

#主键字段为null时会自增
mysql> insert into user values(null,'cj',123);
Query OK, 1 row affected (0.01 sec)

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | cj       | 123      |
|       2 | cj       | 123      |
+---------+----------+----------+
2 rows in set (0.00 sec)

#主键字段可以插入别的值
mysql> insert into user values(111,'ljx','111');
Query OK, 1 row affected (0.01 sec)

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | cj       | 123      |
|       2 | cj       | 123      |
|     111 | ljx      | 111      |
+---------+----------+----------+
3 rows in set (0.00 sec)

#会根据上个数据自增
mysql> insert into user values(null,'cj','123');
Query OK, 1 row affected (0.01 sec)

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | cj       | 123      |
|       2 | cj       | 123      |
|     111 | ljx      | 111      |
|     112 | cj       | 123      |
+---------+----------+----------+
4 rows in set (0.00 sec)

#'113' = 113
mysql> insert into user values('113','cj','123');
Query OK, 1 row affected (0.01 sec)

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | cj       | 123      |
|       2 | cj       | 123      |
|     111 | ljx      | 111      |
|     112 | cj       | 123      |
|     113 | cj       | 123      |
+---------+----------+----------+
5 rows in set (0.00 sec)

#不能插入多的列,要按照表的结构插入对应的值
mysql> insert into user values(null,'ljx','111',111);
ERROR 1136 (21S01): Column count doesn't match value count at row 1

示例2

  • 插入指定字段及数据
  • 插入多行数据
  • 插入多行数据并指定字段
mysql 复制代码
mysql> insert into user(username) values('zhangsan');	#插入指定单个字段及数据
Query OK, 1 row affected (0.01 sec)

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | cj       | 123      |
|       2 | cj       | 123      |
|     111 | ljx      | 111      |
|     112 | cj       | 123      |
|     113 | cj       | 123      |
|     114 | zhangsan | NULL     |	#插入的数据
+---------+----------+----------+
6 rows in set (0.00 sec)

mysql> insert into user(username,password) values('lisi',666);	#插入指定多个字段及数据
Query OK, 1 row affected (0.01 sec)

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | cj       | 123      |
|       2 | cj       | 123      |
|     111 | ljx      | 111      |
|     112 | cj       | 123      |
|     113 | cj       | 123      |
|     114 | zhangsan | NULL     |	
|     115 | lisi     | 666      |	#插入的数据
+---------+----------+----------+
7 rows in set (0.00 sec)

mysql> insert into user values(		#插入多行数据
    -> null,'ljx','12'),
    -> (null,'cj','12'),
    -> (null,'fjw','12');
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | cj       | 123      |
|       2 | cj       | 123      |
|     111 | ljx      | 111      |
|     112 | cj       | 123      |
|     113 | cj       | 123      |
|     114 | zhangsan | NULL     |
|     115 | lisi     | 666      |
|     116 | ljx      | 12       |		#插入的数据
|     117 | cj       | 12     	|
|     118 | fjw      | 12       |
+---------+----------+----------+
10 rows in set (0.00 sec)

mysql> insert into user(user_id,username) values	#插入多行数据并指定字段
    -> (1000,"ls1"),
    -> (null,"ls2"),
    -> (null,"ls3");
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | cj       | 123      |
|       2 | cj       | 123      |
|     111 | ljx      | 111      |
|     112 | cj       | 123      |
|     113 | cj       | 123      |
|     114 | zhangsan | NULL     |
|     115 | lisi     | 666      |
|     116 | ljx      | 12       |
|     117 | cj       | 12       |
|     118 | fjw      | 12       |
|    1000 | ls1      | NULL     |	#插入的数据
|    1001 | ls2      | NULL     |
|    1002 | ls3      | NULL     |
+---------+----------+----------+
13 rows in set (0.00 sec)

示例3

replace语句:

  • insert用法是一样的,当插入主键和原表数据相同时,会先删除原表数据,后插入,等价于insert和select的结合,等价于一个原子操作
mysql 复制代码
mysql> replace into user values(1000,'wangwu',123456);
Query OK, 2 rows affected (0.01 sec)

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | cj       | 123      |
|       2 | cj       | 123      |
|     111 | ljx      | 111      |
|     112 | cj       | 123      |
|     113 | cj       | 123      |
|     114 | zhangsan | NULL     |
|     115 | lisi     | 666      |
|     116 | ljx      | 12       |
|     117 | cj       | 12       |
|     118 | fjw      | 12       |
|    1000 | wangwu   | 123456   |	#替换的数据
|    1001 | ls2      | NULL     |
|    1002 | ls3      | NULL     |
+---------+----------+----------+
13 rows in set (0.00 sec)

mysql> insert into user values(1000,'wangwu',"1111");;
ERROR 1062 (23000): Duplicate entry '1000' for key 'user.PRIMARY'
复制代码
mysql> insert into user(username) select username from user;
Query OK, 13 rows affected (0.01 sec)
Records: 13  Duplicates: 0  Warnings: 0

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | cj       | 123      |
|       2 | cj       | 123      |
|     111 | ljx      | 111      |
|     112 | cj       | 123      |
|     113 | cj       | 123      |
|     114 | zhangsan | NULL     |
|     115 | lisi     | 666      |
|     116 | ljx      | 12       |
|     117 | cj       | 12       |
|     118 | fjw      | 12       |
|    1000 | wangwu   | 123456   |
|    1001 | ls2      | NULL     |
|    1002 | ls3      | NULL     |
|    1004 | cj       | NULL     |
|    1005 | cj       | NULL     |
|    1006 | ljx      | NULL     |
|    1007 | cj       | NULL     |
|    1008 | cj       | NULL     |
|    1009 | zhangsan | NULL     |
|    1010 | lisi     | NULL     |
|    1011 | ljx      | NULL     |
|    1012 | cj       | NULL     |
|    1013 | fjw      | NULL     |
|    1014 | wangwu   | NULL     |
|    1015 | ls2      | NULL     |
|    1016 | ls3      | NULL     |
+---------+----------+----------+
26 rows in set (0.00 sec)

模糊查询

mysql 复制代码
#使用like 与 通配符%来实现模糊查询
mysql> select username from user where username like 'z%';	#模糊查询以z开头的数据
+----------+
| username |
+----------+
| zhangsan |
| zhangsan |
+----------+
2 rows in set (0.00 sec)

#插入通过模糊查询的数据
mysql> insert into user(username) select username from user where username like 'c%';		#模糊查询以字母c开头的数据并出入到指定字段的表
Query OK, 10 rows affected (0.01 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | cj       | 123      |
|       2 | cj       | 123      |
|     111 | ljx      | 111      |
|     112 | cj       | 123      |
|     113 | cj       | 123      |
|     114 | zhangsan | NULL     |
|     115 | lisi     | 666      |
|     116 | ljx      | 12       |
|     117 | cj       | 12       |
|     118 | fjw      | 12       |
|    1000 | wangwu   | 123456   |
|    1001 | ls2      | NULL     |
|    1002 | ls3      | NULL     |
|    1004 | cj       | NULL     |
|    1005 | cj       | NULL     |
|    1006 | ljx      | NULL     |
|    1007 | cj       | NULL     |
|    1008 | cj       | NULL     |
|    1009 | zhangsan | NULL     |
|    1010 | lisi     | NULL     |
|    1011 | ljx      | NULL     |
|    1012 | cj       | NULL     |
|    1013 | fjw      | NULL     |
|    1014 | wangwu   | NULL     |
|    1015 | ls2      | NULL     |
|    1016 | ls3      | NULL     |
|    1019 | cj       | NULL     |
|    1020 | cj       | NULL     |
|    1021 | cj       | NULL     |
|    1022 | cj       | NULL     |
|    1023 | cj       | NULL     |
|    1024 | cj       | NULL     |
|    1025 | cj       | NULL     |
|    1026 | cj       | NULL     |
|    1027 | cj       | NULL     |
|    1028 | cj       | NULL     |
+---------+----------+----------+

update语句

复制代码
格式:

	update 表名 set 字段1 = 值1,......字段n [where 筛选]

mysql> update user set username='fjw',password='666' where user_id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | fjw      | 666      |		#更改后的数据
|       2 | cj       | 123      |
|     111 | ljx      | 111      |
|     112 | cj       | 123      |
|     113 | cj       | 123      |
|     114 | zhangsan | NULL     |
|     115 | lisi     | 666      |
|     116 | ljx      | 12       |
|     117 | cj       | 12       |
|     118 | fjw      | 12       |
|    1000 | wangwu   | 123456   |
|    1001 | ls2      | NULL     |
|    1002 | ls3      | NULL     |
|    1004 | cj       | NULL     |
|    1005 | cj       | NULL     |
|    1006 | ljx      | NULL     |
|    1007 | cj       | NULL     |
|    1008 | cj       | NULL     |
|    1009 | zhangsan | NULL     |
|    1010 | lisi     | NULL     |
|    1011 | ljx      | NULL     |
|    1012 | cj       | NULL     |
|    1013 | fjw      | NULL     |
|    1014 | wangwu   | NULL     |
|    1015 | ls2      | NULL     |
|    1016 | ls3      | NULL     |
|    1019 | cj       | NULL     |
|    1020 | cj       | NULL     |
|    1021 | cj       | NULL     |
|    1022 | cj       | NULL     |
|    1023 | cj       | NULL     |
|    1024 | cj       | NULL     |
|    1025 | cj       | NULL     |
|    1026 | cj       | NULL     |
|    1027 | cj       | NULL     |
|    1028 | cj       | NULL     |
+---------+----------+----------+
36 rows in set (0.00 sec)

mysql> update user set password='777' where password is null;
Query OK, 26 rows affected (0.01 sec)
Rows matched: 26  Changed: 26  Warnings: 0

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       2 | cj       | 777      |
|     111 | ljx      | 111      |
|     112 | cj       | 777      |
|     113 | cj       | 777      |
|     114 | zhangsan | 777      |
|     115 | lisi     | 666      |
|     116 | ljx      | 12       |
|     117 | cj       | 12       |
|     118 | fjw      | 12       |
|    1000 | wangwu   | 123456   |
|    1001 | ls2      | 777      |
|    1002 | ls3      | 777      |
|    1004 | cj       | 777      |
|    1005 | cj       | 777      |
|    1006 | ljx      | 777      |
|    1007 | cj       | 777      |
|    1008 | cj       | 777      |
|    1009 | zhangsan | 777      |
|    1010 | lisi     | 777      |
|    1011 | ljx      | 777      |
|    1012 | cj       | 777      |
|    1013 | fjw      | 777      |
|    1014 | wangwu   | 777      |
|    1015 | ls2      | 777      |
|    1016 | ls3      | 777      |
|    1019 | cj       | 777      |
|    1020 | cj       | 777      |
|    1021 | cj       | 777      |
|    1022 | cj       | 777      |
|    1023 | cj       | 777      |
|    1024 | cj       | 777      |
|    1025 | cj       | 777      |
|    1026 | cj       | 777      |
|    1027 | cj       | 777      |
|    1028 | cj       | 777      |
+---------+----------+----------+
35 rows in set (0.00 sec)

delete语句

复制代码
格式:
	delete from 表名 [where条件]
删除数据后,保留自增的最大索引值并放在内存中,如果要重新添加数据,则按照最大索引值去添加

mysql> delete from user where password='777';
Query OK, 29 rows affected (0.01 sec)

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|     111 | ljx      | 111      |
|     115 | lisi     | 666      |
|     116 | ljx      | 12       |
|     117 | cj       | 12       |
|     118 | fjw      | 12       |
|    1000 | wangwu   | 123456   |
+---------+----------+----------+
6 rows in set (0.00 sec)

#删除所有表的数据
mysql> delete from user;
Query OK, 6 rows affected (0.01 sec)

mysql> select * from user;
Empty set (0.00 sec)

mysql> insert into user values(null,'fjw','111');
Query OK, 1 row affected (0.02 sec)

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|    1034 | fjw      | 111      |
+---------+----------+----------+
1 row in set (0.00 sec)

mysql> truncate table user;				#删除表数据,并初始化主键索引
Query OK, 0 rows affected (0.05 sec)

mysql> select * from user;
Empty set (0.00 sec)

mysql> insert into user values(null,'fjw','111');
Query OK, 1 row affected (0.02 sec)

mysql> select * from user;
+---------+----------+----------+
| user_id | username | password |
+---------+----------+----------+
|       1 | fjw      | 111      |
+---------+----------+----------+
1 row in set (0.00 sec)


mysql> drop table user;
Query OK, 0 rows affected (0.02 sec)

mysql> drop table user;
Query OK, 0 rows affected (0.02 sec)

删除表数据有三种方法,区别如下

  • delete from user,删除表数据,保留表结构,可以回滚
  • truncate table user,删除表数据,保留表结构。不能回滚。一次性删除所有数据,效率较高,并初始化索引
  • drop from user,删除表数据和表结构,什么也没了
相关推荐
禁默3 小时前
基于金仓KFS工具,破解多数据并存,浙人医改造实战医疗信创
数据库·人工智能·金仓数据库
pen-ai3 小时前
【数据工程】15. Stream Query Processing
数据库
舰长1154 小时前
ubuntu24安装mysql遇到的坑----解决Mysql报错缺少libaio.so.1
linux·mysql·ubuntu
it码喽4 小时前
Redis存储经纬度信息
数据库
星光一影4 小时前
快递比价寄件系统技术解析:基于PHP+Vue+小程序的高效聚合配送解决方案
vue.js·mysql·小程序·php
小马哥编程4 小时前
【软件架构】数据库系统与缓存设计:五种缓存一致性方案
数据库·缓存
DemonAvenger4 小时前
Redis持久化策略对比:RDB与AOF的最佳实践与场景选择
数据库·redis·性能优化
新手小白*5 小时前
Redis Sentinel哨兵集群
数据库·redis·sentinel
一 乐5 小时前
商城推荐系统|基于SprinBoot+vue的商城推荐系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·后端·商城推荐系统