插入&更新&删除数据
- 构建练习表结构
- 插入数据
- 为表的所有字段插入数据
- 为表的指定字段插入数据
- 同时插入多条记录
- 将查询结果插入到表中
- 更新数据
- 删除数据
- 综合案例-记录的插入更新和删除
构建练习表结构
创建以下表结构:
MariaDB [lyshark]> create table person -> ( -> id int unsigned not null auto_increment, -> name char(50) not null default '', -> age int not null default 0, -> info char(50) null, -> primary key(id) -> ); Query OK, 0 rows affected (0.00 sec)
语法格式:
insert into 表名 (字段) values(数值);
插入数据
为表的所有字段插入数据
在person表中,插入一条新纪录,id=1,name=LyShark,age=22,info=Lawyer
MariaDB [lyshark]> select * from person; Empty set (0.00 sec) MariaDB [lyshark]> insert into person(id,name,age,info) values(1,'LyShark',22,'Lawyer'); #插入一条记录 Query OK, 1 row affected (0.00 sec) MariaDB [lyshark]> select * from person; +----+---------+-----+--------+ | id | name | age | info | +----+---------+-----+--------+ | 1 | LyShark | 22 | Lawyer | +----+---------+-----+--------+ 1 row in set (0.00 sec) MariaDB [lyshark]>
为表的指定字段插入数据
在person表中,插入一条新纪录name=Willam,age=18,info=sports
MariaDB [lyshark]> desc person; +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | char(50) | NO | | | | | age | int(11) | NO | | 0 | | | info | char(50) | YES | | NULL | | +-------+------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) MariaDB [lyshark]> insert into person(name,age,info) values('Willam',18,'sports man'); #指定插入字段 Query OK, 1 row affected (0.04 sec) MariaDB [lyshark]> select * from person; +----+---------+-----+------------+ | id | name | age | info | +----+---------+-----+------------+ | 1 | LyShark | 22 | Lawyer | | 2 | Willam | 18 | sports man | +----+---------+-----+------------+ 2 rows in set (0.00 sec) MariaDB [lyshark]>
同时插入多条记录
MariaDB [lyshark]> select * from person; +----+---------+-----+------------+ | id | name | age | info | +----+---------+-----+------------+ | 1 | LyShark | 22 | Lawyer | | 2 | Willam | 18 | sports man | +----+---------+-----+------------+ 2 rows in set (0.00 sec) MariaDB [lyshark]> insert into person(name,age,info) values('Evans',27,'secretary'), #同时插入多条数据的写法 -> ('Dale',22,'cook'), -> ('Edison',28,'singer'); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 MariaDB [lyshark]> select * from person; +----+---------+-----+------------+ | id | name | age | info | +----+---------+-----+------------+ | 1 | LyShark | 22 | Lawyer | | 2 | Willam | 18 | sports man | | 3 | Evans | 27 | secretary | | 4 | Dale | 22 | cook | | 5 | Edison | 28 | singer | +----+---------+-----+------------+ 5 rows in set (0.00 sec)
将查询结果插入到表中
为了实现将另一个表中的记录插入到本表中,我们新建一个person_old表,其表结构和person相同,我们将person_old表中的内容全部迁移到person中去
创建一个测试表,并插入测试字段:
MariaDB [lyshark]> create table person_old -> ( -> id int unsigned not null auto_increment, -> name char(50) not null default '', -> age int not null default 0, -> info char(50) null, -> primary key(id) -> ); Query OK, 0 rows affected (0.01 sec) MariaDB [lyshark]> insert into person_old -> values(11,'harry',20,'student'),(12,'Beckham',33,'police'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0
接下来我们将person_old表中的内容迁移到person中去
MariaDB [lyshark]> select * from person_old; +----+---------+-----+---------+ | id | name | age | info | +----+---------+-----+---------+ | 11 | harry | 20 | student | | 12 | Beckham | 33 | police | +----+---------+-----+---------+ 2 rows in set (0.00 sec) MariaDB [lyshark]> select * from person; +----+---------+-----+------------+ | id | name | age | info | +----+---------+-----+------------+ | 1 | LyShark | 22 | Lawyer | | 2 | Willam | 18 | sports man | | 3 | Evans | 27 | secretary | | 4 | Dale | 22 | cook | | 5 | Edison | 28 | singer | +----+---------+-----+------------+ 5 rows in set (0.00 sec) MariaDB [lyshark]> MariaDB [lyshark]> insert into person(id,name,age,info) #利用查询命令完成迁移 -> select id,name,age,info from person_old; Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 MariaDB [lyshark]> select * from person; +----+---------+-----+------------+ | id | name | age | info | +----+---------+-----+------------+ | 1 | LyShark | 22 | Lawyer | | 2 | Willam | 18 | sports man | | 3 | Evans | 27 | secretary | | 4 | Dale | 22 | cook | | 5 | Edison | 28 | singer | | 11 | harry | 20 | student | | 12 | Beckham | 33 | police | +----+---------+-----+------------+ 7 rows in set (0.00 sec)
更新数据
更新表中指定字段
将id=11的name字段的值改为xxxx,age字段改为200
MariaDB [lyshark]> select * from person; +----+---------+-----+------------+ | id | name | age | info | +----+---------+-----+------------+ | 1 | LyShark | 22 | Lawyer | | 2 | Willam | 18 | sports man | | 3 | Evans | 27 | secretary | | 4 | Dale | 22 | cook | | 5 | Edison | 28 | singer | | 11 | harry | 20 | student | | 12 | Beckham | 33 | police | +----+---------+-----+------------+ 7 rows in set (0.00 sec) MariaDB [lyshark]> update person set age=200,name='xxxx' where id=11; #更新单个字段 Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [lyshark]> select * from person; +----+---------+-----+------------+ | id | name | age | info | +----+---------+-----+------------+ | 1 | LyShark | 22 | Lawyer | | 2 | Willam | 18 | sports man | | 3 | Evans | 27 | secretary | | 4 | Dale | 22 | cook | | 5 | Edison | 28 | singer | | 11 | xxxx | 200 | student | | 12 | Beckham | 33 | police | +----+---------+-----+------------+ 7 rows in set (0.00 sec)
更新表中一整列字段
在person表中,将1-12的info字段全部改为lyshark blog
MariaDB [lyshark]> select * from person; +----+---------+-----+------------+ | id | name | age | info | +----+---------+-----+------------+ | 1 | LyShark | 22 | Lawyer | | 2 | Willam | 18 | sports man | | 3 | Evans | 27 | secretary | | 4 | Dale | 22 | cook | | 5 | Edison | 28 | singer | | 11 | xxxx | 200 | student | | 12 | Beckham | 33 | police | +----+---------+-----+------------+ 7 rows in set (0.00 sec) MariaDB [lyshark]> update person set info='lyshark blog' where age between 1 and 200; #指定修改的字段 Query OK, 7 rows affected (0.00 sec) Rows matched: 7 Changed: 7 Warnings: 0 MariaDB [lyshark]> select * from person; +----+---------+-----+--------------+ | id | name | age | info | +----+---------+-----+--------------+ | 1 | LyShark | 22 | lyshark blog | | 2 | Willam | 18 | lyshark blog | | 3 | Evans | 27 | lyshark blog | | 4 | Dale | 22 | lyshark blog | | 5 | Edison | 28 | lyshark blog | | 11 | xxxx | 200 | lyshark blog | | 12 | Beckham | 33 | lyshark blog | +----+---------+-----+--------------+ 7 rows in set (0.00 sec)