DML操作
有关数据的操作:
- 增加 insert
- 删除 delete
- 修改 update
- 查询 select(重点)
- 条件查询
- 排序
- 函数
- 分组查询(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,删除表数据和表结构,什么也没了