MySQL数据库 之 插入、更新与删除数据
2017-12-20 22:39
513 查看
一、插入数据
MySQL 中使用 insert 语句来向数据库表中插入新的数据记录。☆ 为表的所有字段插入数据
insert into tb_name (col_list) values (value_list)
创建一个数据表 person 如下:
>>> create table person( id int unsigned not null primary key auto_increment, name char(40) not null default "", age int not null default 0, info char(50) null ); >>> show tables; +-----------------+ | Tables_in_learn | +-----------------+ | person | +-----------------+ 1 row in set (0.00 sec) >>> select * from person: Empty set (0.03 sec)
我们查看当前数据表显示已经创建成功,查看数据表中的数据,显示当前数据表为空。
接下来我们向表中插入数据:
>>> insert into person (id, name, age, info) values (1, 'Green', 21, 'Lawyer'); Query OK, 1 row affected (0.00 sec)
插入数据时,不需要按照表定义的顺序插入,只要保证值的顺序与列字段的顺序相同就可以。这里不再演示。
在插入数据时,允许列名称列表 col_list 为空,值列表中需要为列的每一个字段指定值,并且值的顺序必须和数据表中字段定义时的顺序相同。如:
>>> insert into person values (2, 'Lily', 18, 'teacher'); Query OK, 1 row affected (0.00 sec) >>> select * from person; +----+-------+-----+---------+ | id | name | age | info | +----+-------+-----+---------+ | 1 | Green | 21 | Lawyer | | 2 | Lily | 18 | teacher | +----+-------+-----+---------+ 2 rows in set (0.00 sec)
☆ 为表的指定字段插入数据
>>> insert into person (name, age, info) values ('Lilei', 20, 'sports man'); Query OK, 1 row affected (0.00 sec) >>> select * from person; +----+-------+-----+------------+ | id | name | age | info | +----+-------+-----+------------+ | 1 | Green | 21 | Lawyer | | 2 | Lily | 18 | teacher | | 3 | Lilei | 20 | sports man | +----+-------+-----+------------+ 3 rows in set (0.00 sec)
在 person 表中,id 字段为主键,不能为空,所以系统会自动为该字段插入自增的序列值。在插入数据时,如果某些字段没有指定插入值,MySQL 将插入该字段在 定义时的默认值。
如:
>>> insert into person (name, age) values ('Xiaohua', 18); Query OK, 1 row affected (0.01 sec) >>> mysql> select * from person; +----+---------+-----+------------+ | id | name | age | info | +----+---------+-----+------------+ | 1 | Green | 21 | Lawyer | | 2 | Lily | 18 | teacher | | 3 | Lilei | 20 | sports man | | 4 | Xiaohua | 18 | NULL | +----+---------+-----+------------+ 4 rows in set (0.00 sec)
可以看到,插入语句中没有指定 info 的值,所以 MySQL 自动为该字段插入了定义表时定义的默认值——空值。
☆ 同时插入多条数据
insert 语句可以同时向数据表中插入多条记录,插入时指定多个值列表,每个值列表之间用逗号隔开:insert into tb_name (col_list) values (values_list1), (values_list2), ...
eg: >>> insert into person (name, age, info) values ('Evans', 22, 'student'), ('Jack', 27, 'singer'), ('Dawei', 33, 'cook'); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 >>> select * from person; +----+---------+-----+------------+ | id | name | age | info | +----+---------+-----+------------+ | 1 | Green | 21 | Lawyer | | 2 | Lily | 18 | teacher | | 3 | Lilei | 20 | sports man | | 4 | Xiaohua | 18 | NULL | | 5 | Evans | 22 | student | | 6 | Jack | 27 | singer | | 7 | Dawei | 33 | cook | +----+---------+-----+------------+ 7 rows in set (0.00 sec)
我们同样可以不指定插入字段列表,直接插入数据记录,但此时,我们插入的值列表的顺序及数据类型必须和表定义时的字段类型及数据类型匹配。
【注意】由于 MySQL 执行单条 insert 语句插入多行数据,比用多条 insert 语句要快,所以在插入多条记录时,最好选择使用单条 insert 语句。
☆ 将查询结果插入表中
如果我们想要把另外e519
一个表合并个人信息到 person 表中,不需要把每条记录的值逐一输入,我们只需要一个 insert 语句和一个 select 语句组合在一起即可:
insert into tb_name1 (col_list1) select (col_list2) from tb_name2 where (condition)
即:我们根据 condition 条件从 tb_name2 的表中 select 出 col_list2 中指定的字段的值,然后 insert 到 tb_name1 的 col_list1 中相应的字段中。
首先,我们先新建一个 people 表,它具有和 person 表相同的字段名和数据类型,并向其内插入两条数据:
>>> create table people( id int unsigned not null primary key auto_increment, name char(40) not null default '', age int not null default 0, info char(50) null ); Query OK, 0 rows affected (0.01 sec) >>> insert into people values (8, 'Xiaogang', 45, 'Doctor'), (9, 'Eva', 35, 'police'); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 >>> select * from people; +----+----------+-----+--------+ | id | name | age | info | +----+----------+-----+--------+ | 8 | Xiaogang | 45 | Doctor | | 9 | Eva | 35 | police | +----+----------+-----+--------+ 2 rows in set (0.00 sec)
现在,我们将 people 表中的数据插入到 person 表中:
>>> insert into person (id, name, age, info) select id, name, age, info from people; Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 >>> select * from person; +----+----------+-----+------------+ | id | name | age | info | +----+----------+-----+------------+ | 1 | Green | 21 | Lawyer | | 2 | Lily | 18 | teacher | | 3 | Lilei | 20 | sports man | | 4 | Xiaohua | 18 | NULL | | 5 | Evans | 22 | student | | 6 | Jack | 27 | singer | | 7 | Dawei | 33 | cook | | 8 | Xiaogang | 45 | Doctor | | 9 | Eva | 35 | police | +----+----------+-----+------------+ 9 rows in set (0.01 sec)
成功!
id 字段为主键字段,在插入的时候要保证该字段值的唯一性,如果不能确定,可以忽略该字段,系统会自动为我们加上正确的值。
【注意】MySQL 并不关心 select 返回的列名,它其实是根据列的位置进行插入操作,第一列对应插入第一列,第二列对应插入第二列,以此类推,所以我们只要保证 select 出来的数据的 字段数量 与 insert 到的表的 col_list1 字段数量 相同即可,同时别忘了具有相同的数据类型。
二、更新数据
MySQL 使用 update 语句更新表中的记录, 可以更新特定的行或者同时更新所有的行。update tb_name set col_name1 = value1, cul_name2 = value2, ... where condition
如:我们将 id 为 8 的记录中的 name 更新为 Harry,age 更新为 22:
>>> select * from person where id = 8; +----+----------+-----+--------+ | id | name | age | info | +----+----------+-----+--------+ | 8 | Xiaogang | 45 | Doctor | +----+----------+-----+--------+ 1 row in set (0.00 sec) >>> update person set name='Harry', age=22 where id = 8; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 >>> select * from person where id = 8; +----+-------+-----+--------+ | id | name | age | info | +----+-------+-----+--------+ | 8 | Harry | 22 | Doctor | +----+-------+-----+--------+ 1 row in set (0.00 sec)
现在我们要更新年龄在 18 ~ 22 之间的所有人的 info 为 student:
>>> select * from person where age between 18 and 22; +----+---------+-----+------------+ | id | name | age | info | +----+---------+-----+------------+ | 1 | Green | 21 | Lawyer | | 2 | Lily | 18 | teacher | | 3 | Lilei | 20 | sports man | | 4 | Xiaohua | 18 | NULL | | 5 | Evans | 22 | student | | 8 | Harry | 22 | Doctor | +----+---------+-----+------------+ 6 rows in set (0.00 sec) >>> update person set info = 'student' where age between 18 and 22; Query OK, 5 rows affected (0.01 sec) Rows matched: 6 Changed: 5 Warnings: 0 >>> select * from person where age between 18 and 22; +----+---------+-----+---------+ | id | name | age | info | +----+---------+-----+---------+ | 1 | Green | 21 | student | | 2 | Lily | 18 | student | | 3 | Lilei | 20 | student | | 4 | Xiaohua | 18 | student | | 5 | Evans | 22 | student | | 8 | Harry | 22 | student | +----+---------+-----+---------+ 6 rows in set (0.00 sec)
成功!
三、删除数据
MySQL 使用 delete 语句进行数据的删除,同时允许 where 子句指定删除条件:delete from tb_name [where condition]
其中,where condition 为可选的删除条件,如果没有这条子句,将删除整个表。
我们现在将 person 表中的 id 为 8 的数据删除:
>>> select * from person where id = 8; +----+-------+-----+---------+ | id | name | age | info | +----+-------+-----+---------+ | 8 | Harry | 22 | student | +----+-------+-----+---------+ 1 row in set (0.00 sec) >>> delete from person where id = 8; Query OK, 1 row affected (0.00 sec) >>> select * from person where id = 8; Empty set (0.00 sec)
再次查询显示记录为空,说明我们已经成功将 id 为 8 的数据记录删除。
我们现在删除 age 在 18 ~ 22 之间的数据记录:
>>> select * from person where age between 18 and 22; +----+---------+-----+---------+ | id | name | age | info | +----+---------+-----+---------+ | 1 | Green | 21 | student | | 2 | Lily | 18 | student | | 3 | Lilei | 20 | student | | 4 | Xiaohua | 18 | student | | 5 | Evans | 22 | student | +----+---------+-----+---------+ 5 rows in set (0.00 sec) >>> delete from person where age between 18 and 22; Query OK, 5 rows affected (0.00 sec) >>> select * from person where age between 18 and 22; Empty set (0.00 sec)
再次查询显示记录为空,说明已经成功删除响应数据记录。
假如我们现在要删除整个数据表的所有记录,那我们可以这样操作:
>>> select * from person; +----+-------+-----+--------+ | id | name | age | info | +----+-------+-----+--------+ | 6 | Jack | 27 | singer | | 7 | Dawei | 33 | cook | | 9 | Eva | 35 | police | +----+-------+-----+--------+ 3 rows in set (0.00 sec) >>> delete from person ; Query OK, 3 rows affected (0.00 sec) >>> select * from person; Empty set (0.00 sec)
现在只是将 person 整个表的数据清空了,但是表还存在:
>>> show tables; +-----------------+ | Tables_in_learn | +-----------------+ | people | | person | +-----------------+ 2 rows in set (0.00 sec)
关于数据的删除,如果我们想删除一个数据表中的所有数据记录,我们还可以使用
truncate table tb_name语句。它的原理是直接将表删除,然后重新建一张跟原表一模一样的表,但是此时表中没有数据,相当于清空表中的所有数据:
>>> select * from people;
+----+----------+-----+--------+
| id | name | age | info |
+----+----------+-----+--------+
| 8 | Xiaogang | 45 | Doctor |
| 9 | Eva | 35 | police |
+----+----------+-----+--------+
2 rows in set (0.00 sec)
>>> truncate table people;
Query OK, 0 rows affected (0.01 sec)
>>> select * from people;
Empty set (0.00 sec)
>>> show tables; +-----------------+ | Tables_in_learn | +-----------------+ | people | | person | +-----------------+ 2 rows in set (0.00 sec)
相关文章推荐
- python对MySQL数据库的操作表单的创建,数据的插入,读取,更新,删除操作
- mysql数据库中创建、删除、更新、插入数据、等语句
- MySQL数据库中插入,更新与删除数据
- 使用java连接mysql数据库进行数据的查询插入更新及删除
- MYSQL必知必会读书笔记 第十九至二十一章 数据的插入、更新、删除
- hbase中创建表、插入数据,更新数据,删除数据
- MongoDB数据插入、删除、更新
- Day2(11.1):(3)插入、更新和删除数据
- 插入、更新、删除数据
- Java在MySQL数据库中删除、更新、循环插入的例子
- 对象标识符和对象引用+对象标识符+对象引用+为对象引用插入数据+查询对象引用OID对应的实际值+更新对象引用+删除对象引用
- GIS数据的查找,插入,删除,更新(ArcEngine)
- mysql学习总结一当数据不存在的时候插入,存在的时候进行更新,删除重复数据
- 插入数据 更新数据 删除数据
- android中常用的查询、插入、更新、删除等SQL语句以及SQLite数据类型
- mySQL教程 第5章 插入 更新与删除数据
- 通过loadrunner 11常规通用的C语言API类型的Vuser 方式,测试验证MySQL数据库插入、查询、修改、删除数据性能脚本实例
- 《MySQL必知必会》第十九二十章-插入数据与更新和删除数据
- mysql 插入,更新与删除数据
- MYSQL----对表中数据进行插入,更新,删除