MySQL数据中如何实现插入、更新与删除

41次阅读
没有评论

共计 4213 个字符,预计需要花费 11 分钟才能阅读完成。

自动写代码机器人,免费开通

丸趣 TV 小编给大家分享一下 MySQL 数据中如何实现插入、更新与删除,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

案例: 创建表 books,对数据进行插入、更新和删除操作,掌握数据表的基本操作。books 表结构以及表中的记录如下表:
MySQL 数据中如何实现插入、更新与删除
案例操作过程:
(1)创建数据表 books,并按照表 8.1 所示的结构定义各个字段。
(2)将表 8.2 中的记录插入 books 表中。分别使用不同的方法插入记录。
(3)将小说类型 (novel) 的书的价格都增加 5。
(4)将名称为 EmmaT 的书的价格改为 40,并将 note 说明改为 drama。
(5)删除库存为 0 的记录。

(免费学习推荐:mysql 视频教程)

(1)、创建数据表 books,并按照表 8.1 所示的结构定义各个字段。

mysql  create table books -  ( -  id int(11) not null auto_increment primary key,
 -  name varchar(50) not null,
 -  authors varchar(100) not null,
 -  price float not null,
 -  pubdate year not null,
 -  discount float(3,2) not null,
 -  note varchar(255) null,
 -  num int(11) not null default 0
 -  );Query OK, 0 rows affected (0.05 sec)mysql  select * from books;Empty set (0.05 sec)

可以看到表为空,下面向表中插入记录:

(2)、将表 8.2 中的记录插入 books 表中。分别使用不同的方法插入记录。

①指定所有字段名称插入记录,SQL 语句如下;

mysql  insert into books -  (id,name,authors,price,pubdate,discount,note,num)
 -  values(1, Tale of AAA , Dicks ,23, 1995 ,0.85, novel ,11);Query OK, 1 row affected (0.05 sec)

②不指定字段名称插入记录,SQL 语句如下:

mysql  insert into books -  values(2, EmmaT , Jane lura ,35, 1993 ,0.70, joke ,22);Query OK, 1 row affected (0.05 sec)mysql  select * from books;+----+-------------+-----------+-------+---------+----------+-------+-----+| id | name | authors | price | pubdate | discount | note | num |+----+-------------+-----------+-------+---------+----------+-------+-----+| 1 | Tale of AAA | Dicks | 23 | 1995 | 0.85 | novel | 11 || 2 | EmmaT | Jane lura | 35 | 1993 | 0.70 | joke | 22 |+----+-------------+-----------+-------+---------+----------+-------+-----+2 rows in set (0.00 sec)

③同时插入多条记录

mysql  insert into books -  values(3, Story of Jane , Jane Tim ,40, 2001 ,0.81, novel ,0),
 -  (4, Lovey Day , George Byron ,20, 2005 ,0.85, novel ,30),
 -  (5, Old Land , Honore Blade ,30, 2010 ,0.60, law ,0),
 -  (6, The Battle , Upton Sara ,33, 1999 ,0.65, medicine ,40),
 -  (7, Rose Hood , Richard Kale ,28, 2008 ,0.90, cartoon ,28);Query OK, 5 rows affected (0.05 sec)Records: 5 Duplicates: 0 Warnings: 0mysql  select * from books;+----+---------------+--------------+-------+---------+----------+----------+-----+| id | name | authors | price | pubdate | discount | note | num |+----+---------------+--------------+-------+---------+----------+----------+-----+| 1 | Tale of AAA | Dicks | 23 | 1995 | 0.85 | novel | 11 || 2 | EmmaT | Jane lura | 35 | 1993 | 0.70 | joke | 22 || 3 | Story of Jane | Jane Tim | 40 | 2001 | 0.81 | novel | 0 || 4 | Lovey Day | George Byron | 20 | 2005 | 0.85 | novel | 30 || 5 | Old Land | Honore Blade | 30 | 2010 | 0.60 | law | 0 || 6 | The Battle | Upton Sara | 33 | 1999 | 0.65 | medicine | 40 || 7 | Rose Hood | Richard Kale | 28 | 2008 | 0.90 | cartoon | 28 |+----+---------------+--------------+-------+---------+----------+----------+-----+7 rows in set (0.00 sec)

(3)、将小说类型 (novel) 的书的价格都增加 5。

mysql  update books -  set price = price +5
 -  where note =  novel Query OK, 3 rows affected (0.05 sec)Rows matched: 3 Changed: 3 Warnings: 0mysql  select id,name,price,note -  from books -  where note =  novel +----+---------------+-------+-------+| id | name | price | note |+----+---------------+-------+-------+| 1 | Tale of AAA | 28 | novel || 3 | Story of Jane | 45 | novel || 4 | Lovey Day | 25 | novel |+----+---------------+-------+-------+3 rows in set (0.00 sec)

(4)、将名称为 EmmaT 的书的价格改为 40,并将 note 说明改为 drama。

mysql  update books -  set price=40,note= drama 
 -  where name =  EmmaT Query OK, 1 row affected (0.05 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql  select name,price,note -  from books -  where name=  EmmaT +-------+-------+-------+| name | price | note |+-------+-------+-------+| EmmaT | 40 | drama |+-------+-------+-------+1 row in set (0.00 sec)

(5)、删除库存为 0 的记录。

mysql  delete
 -  from books -  where num = 0;Query OK, 2 rows affected (0.05 sec)mysql  select *
 -  from books -  where num = 0;Empty set (0.00 sec)

几个小问题

1、插入记录时可以不指定字段名称吗?

不管使用哪种 insert 语法,都必须给出 values 的正确数目。如果不提供字段名,则必须给每个字段提供一个值,否则将产生一条错误信息。

如果要在 insert 操作中省略某些字段,那么这些字段需要满足一定条件:该列定义为允许空值;或表定义时给出默认值,若不给出则使用默认值。

2、更新或者删除表时必须指定 where 子句吗?

所有的 update 和 delete 语句全都在 where 子句中指定了条件。如果省略 where 子句,则 update 或 delete 将被应用到表中所有的行。因此,除非确实打算更新或删除所有记录,否则要注意使用不带 where 子句的 update 或 delete 语句。

建议在对表进行更新和删除操作之前,使用 select 语句确认需要删除的记录,以免造成无法挽回的结果。

以上是“MySQL 数据中如何实现插入、更新与删除”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注丸趣 TV 行业资讯频道!

向 AI 问一下细节

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-04发表,共计4213字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)