MySQL數(shù)據(jù)中如何實(shí)現(xiàn)插入、更新與刪除

小編給大家分享一下MySQL數(shù)據(jù)中如何實(shí)現(xiàn)插入、更新與刪除,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創(chuàng)新互聯(lián)公司專注于祁東企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),商城網(wǎng)站定制開發(fā)。祁東網(wǎng)站建設(shè)公司,為祁東等地區(qū)提供建站服務(wù)。全流程按需開發(fā)網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

案例:創(chuàng)建表books,對數(shù)據(jù)進(jìn)行插入、更新和刪除操作,掌握數(shù)據(jù)表的基本操作。books表結(jié)構(gòu)以及表中的記錄如下表:
MySQL數(shù)據(jù)中如何實(shí)現(xiàn)插入、更新與刪除
案例操作過程:
(1)創(chuàng)建數(shù)據(jù)表books,并按照表8.1所示的結(jié)構(gòu)定義各個(gè)字段。
(2)將表8.2中的記錄插入books表中。分別使用不同的方法插入記錄。
(3)將小說類型(novel)的書的價(jià)格都增加5。
(4)將名稱為EmmaT的書的價(jià)格改為40,并將note說明改為drama。
(5)刪除庫存為0的記錄。

(免費(fèi)學(xué)習(xí)推薦:mysql視頻教程)


(1)、創(chuàng)建數(shù)據(jù)表books,并按照表8.1所示的結(jié)構(gòu)定義各個(gè)字段。
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)

③同時(shí)插入多條記錄

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)的書的價(jià)格都增加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的書的價(jià)格改為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)

幾個(gè)小問題

1、插入記錄時(shí)可以不指定字段名稱嗎?

  • 不管使用哪種insert語法,都必須給出values的正確數(shù)目。如果不提供字段名,則必須給每個(gè)字段提供一個(gè)值,否則將產(chǎn)生一條錯(cuò)誤信息。

  • 如果要在insert操作中省略某些字段,那么這些字段需要滿足一定條件:該列定義為允許空值;或表定義時(shí)給出默認(rèn)值,若不給出則使用默認(rèn)值。

2、更新或者刪除表時(shí)必須指定where子句嗎?

  • 所有的update和delete語句全都在where子句中指定了條件。如果省略where子句,則update或delete將被應(yīng)用到表中所有的行。因此,除非確實(shí)打算更新或刪除所有記錄,否則要注意使用不帶where子句的update或delete語句。

  • 建議在對表進(jìn)行更新和刪除操作之前,使用select語句確認(rèn)需要?jiǎng)h除的記錄,以免造成無法挽回的結(jié)果。

以上是“MySQL數(shù)據(jù)中如何實(shí)現(xiàn)插入、更新與刪除”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站名稱:MySQL數(shù)據(jù)中如何實(shí)現(xiàn)插入、更新與刪除
轉(zhuǎn)載注明:http://muchs.cn/article6/ijojig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google標(biāo)簽優(yōu)化、建站公司網(wǎng)站制作、軟件開發(fā)小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)