共計 4213 個字符,預計需要花費 11 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
丸趣 TV 小編給大家分享一下 MySQL 數據中如何實現插入、更新與刪除,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
案例: 創建表 books,對數據進行插入、更新和刪除操作,掌握數據表的基本操作。books 表結構以及表中的記錄如下表:
案例操作過程:
(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 問一下細節