1.insert
数据库于表创建成功后,需要向数据库的表中插入数据。在MySQL中可以使用insert语句向数据库已有的表中插入一行或者多行元组数据
基本语法:
insert 语句有两种语法形式,分别是insert…values语句和insert…set语句
insert into<表名> [列名.....] values(值...)
insert into<表名> set<列名1>=<值1>,........
案例1:使用insert…values
在 test 数据库中创建一个tb_stu 表,包含学生编号st_id、学生姓名st_name,输入的 SQL 语句和执行结果如下所示。
mysql> use test;
Database changed
mysql> create table tb_stu(st_id int ,st_name varchar(18));
Query OK, 0 rows affected (0.03 sec)mysql> desc tb_stu;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| st_id | int | YES | | NULL | |
| st_name | varchar(18) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)mysql> select * from tb_stu;
Empty set (0.00 sec)##添加两条数据
mysql> insert into tb_stu(st_id,st_name) values(1,'zhangsan');
Query OK, 1 row affected (0.01 sec)mysql> insert into tb_stu(st_id,st_name) values(2,'lisi');
Query OK, 1 row affected (0.00 sec)mysql> select * from tb_stu;
+-------+----------+
| st_id | st_name |
+-------+----------+
| 1 | zhangsan |
| 2 | lisi |
+-------+----------+
2 rows in set (0.00 sec)
insert 语句后面的列名称顺序可以不是表定义是的顺序,即插入数据时,不需要按照表定义的顺序插入,只要保证值的顺序于列字段的顺序相同就可以。
mysql> insert into tb_stu(st_name,st_id) values('wangwu',3);
Query OK, 1 row affected (0.00 sec)mysql> select * from tb_stu;
+-------+----------+
| st_id | st_name |
+-------+----------+
| 1 | zhangsan |
| 2 | lisi |
| 3 | wangwu |
+-------+----------+
3 rows in set (0.00 sec)
使用 INSERT 插入数据时,允许列名称列表 column_list 为空,此时值列表中需要为表的每一个字段指定值,并且值的顺序必须和数据表中字段定义时的顺序相同。
mysql> insert into tb_stu values(4,'lao6');
Query OK, 1 row affected (0.01 sec)mysql> select * from tb_stu;
+-------+----------+
| st_id | st_name |
+-------+----------+
| 1 | zhangsan |
| 2 | lisi |
| 3 | wangwu |
| 4 | lao6 |
+-------+----------+
4 rows in set (0.00 sec)
指定字段插入数据
mysql> insert into tb_stu(st_id) values(5);
Query OK, 1 row affected (0.01 sec)mysql> select * from tb_stu;
+-------+----------+
| st_id | st_name |
+-------+----------+
| 1 | zhangsan |
| 2 | lisi |
| 3 | wangwu |
| 4 | lao6 |
| 5 | NULL |
+-------+----------+
5 rows in set (0.00 sec)
案列2:使用insert…set
mysql> insert into tb_stu set st_id=6,st_name='yy';
Query OK, 1 row affected (0.01 sec)mysql> select * from tb_stu;
+-------+----------+
| st_id | st_name |
+-------+----------+
| 1 | zhangsan |
| 2 | lisi |
| 3 | wangwu |
| 4 | lao6 |
| 5 | NULL |
| 6 | yy |
+-------+----------+
6 rows in set (0.00 sec)
使用 INSERT INTO…FROM 语句复制表数据
INSERT INTO…SELECT…FROM 语句用于快速地从一个或多个表中取出数据,并将这些数据作为行数据
插入另一个表中。
SELECT 子句返回的是一个查询到的结果集,INSERT 语句将这个结果集插入指定表中,结果集中的每行
数据的字段数、字段的数据类型都必须与被操作的表完全一致。
案例3:使用insert into…from语句复制表数据
##创建一个类似的表mysql> create table like_tb_stu(st_id int,st_name varchar(18),st_sex varchar(10));
Query OK, 0 rows affected (0.03 sec)mysql> desc like_tb_stu;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| st_id | int | YES | | NULL | |
| st_name | varchar(18) | YES | | NULL | |
| st_sex | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)mysql> select * from like_tb_stu;
Empty set (0.00 sec)#插入数据
mysql> insert into like_tb_stu(st_id,st_name) select st_id,st_name from tb_stu;
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 0 Warnings: 0mysql> select * from like_tb_stu;
+-------+----------+--------+
| st_id | st_name | st_sex |
+-------+----------+--------+
| 1 | zhangsan | NULL |
| 2 | lisi | NULL |
| 3 | wangwu | NULL |
| 4 | lao6 | NULL |
| 5 | NULL | NULL |
| 6 | yy | NULL |
+-------+----------+--------+
6 rows in set (0.00 sec)
2.update
在MySQL中,可以使用update语句来修改,更新一个或者多个表数据
UPDATE SET 字段1=值1... where ...
案例1:
保证update 以where字句结束,通过where子句被更新的记录所需要满足的条件,如果忽略where子句,MySQL将更新表中所有的行。
mysql> update like_tb_stu set st_sex='male';
Query OK, 6 rows affected (0.01 sec)
Rows matched: 6 Changed: 6 Warnings: 0mysql> select * from like_tb_stu;
+-------+----------+--------+
| st_id | st_name | st_sex |
+-------+----------+--------+
| 1 | zhangsan | male |
| 2 | lisi | male |
| 3 | wangwu | male |
| 4 | lao6 | male |
| 5 | NULL | male |
| 6 | yy | male |
+-------+----------+--------+
6 rows in set (0.00 sec)
案例2:
mysql> update like_tb_stu set st_sex='female' where st_id=6;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from like_tb_stu;
+-------+----------+--------+
| st_id | st_name | st_sex |
+-------+----------+--------+
| 1 | zhangsan | male |
| 2 | lisi | male |
| 3 | wangwu | male |
| 4 | lao6 | male |
| 5 | NULL | male |
| 6 | yy | female |
+-------+----------+--------+
6 rows in set (0.00 sec)
3.delete
在MySQL中,可以使用delete语句来删除表的一行或者多行数据。
删除单个表中的数据
使用delete语句从单个表中删除数据:
delete from <表名> [where ...]
注意
在不使用where条件是,将删除所有数据。
案例1:指定where子句
mysql> delete from like_tb_stu where st_name is null;
Query OK, 1 row affected (0.03 sec)mysql> select * from like_tb_stu;
+-------+----------+--------+
| st_id | st_name | st_sex |
+-------+----------+--------+
| 1 | zhangsan | male |
| 2 | lisi | male |
| 3 | wangwu | male |
| 4 | lao6 | male |
| 6 | yy | female |
+-------+----------+--------+
5 rows in set (0.00 sec)mysql> delete from like_tb_stu where st_name='yy';
Query OK, 1 row affected (0.01 sec)mysql> select * from like_tb_stu;
+-------+----------+--------+
| st_id | st_name | st_sex |
+-------+----------+--------+
| 1 | zhangsan | male |
| 2 | lisi | male |
| 3 | wangwu | male |
| 4 | lao6 | male |
+-------+----------+--------+
4 rows in set (0.00 sec)
案例2.不指定where子句
mysql> delete from like_tb_stu;
Query OK, 4 rows affected (0.01 sec)mysql> select * from like_tb_stu;
Empty set (0.00 sec)
4.truncate
MySQL提供了delete和truncate关键字来删除表中的数据。
truncate关键字用于完全清空一个表。
案例
mysql> select * from tb_stu;
+-------+----------+
| st_id | st_name |
+-------+----------+
| 1 | zhangsan |
| 2 | lisi |
| 3 | wangwu |
| 4 | lao6 |
| 5 | NULL |
| 6 | yy |
+-------+----------+
6 rows in set (0.00 sec)mysql> truncate tb_stu;
Query OK, 0 rows affected (0.04 sec)mysql> select * from tb_stu;
Empty set (0.00 sec)
truncate 和delete的区别
从逻辑上说,两语句作用相同,但是在某些情况下,两者在使用上有所区别。
-
delete是dml类型的语句;truncate的ddl类型的语句。都用来清空表中的数据
-
DELETE 是逐行一条一条删除记录的;TRUNCATE 则是直接删除原来的表,再重新创建一个一模一样的新表,而不是逐行删除表中的数据,执行数据比 DELETE 快。因此需要删除表中全部的数据行时,尽量使用 TRUNCATE 语句, 可以缩短执行时间。
-
DELETE 删除数据后,配合事件回滚可以找回数据;TRUNCATE 不支持事务的回滚,数据删除后无法找回。
-
DELETE 删除数据后,系统不会重新设置自增字段的计数器;TRUNCATE 清空表记录后,系统会重新设置自增字段的计数器。
-
DELETE 的使用范围更广,因为它可以通过 WHERE 子句指定条件来删除部分数据;而 TRUNCATE不支持 WHERE 子句,只能删除整体。
-
DELETE 会返回删除数据的行数,但是 TRUNCATE 只会返回 0,没有任何意义。
总结
当不需要该表时,用 DROP;当仍要保留该表,但要删除所有记录时,用 TRUNCATE;当要删除部分记
录时,用 DELETE。