mysql8學習筆記6--SQL基礎-insert update delete語句
阿新 • • 發佈:2020-07-22
• Insert語句用於插入資料到表中,其基本語法有以下三種:
其中insert…values和insert…set兩種語句都是將指定的資料插入到現成的表中,而insert…select語句是將另外表中資料查出來並插入到現成的表中
• Partition子句代表可以將資料插入到指定的表分割槽中 • Tbl_name代表將資料插入到的目標表 • Col_name代表要插入指定資料的目標表列,如果是多列則用逗號隔開,如果目標表中的某些列沒有在Insert語句中指定,則這些列會插入預設值,當然可以使用default顯視指定插入預設值 • Values中除了可以指定確定的數值之外,還可以使用表示式expr • INSERT INTO tbl_name (col1,col2) VALUES(15,col1*2); ##正確 • INSERT INTO tbl_name (col1,col2) VALUES(col2*2,15); ##錯誤 • Insert…values語句不光可以插入一條資料,也可以插入多條資料 • INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9); • Insert into students values(7,’abc’),(8,’bcd’); • Insert…values和insert…select語句的執行結果如下 • Records: 100 Duplicates: 0 Warnings: 0 • Records代表此語句操作了多少行資料,但不一定是多少行被插入的資料,因為如果存在相同的行資料且違反了某個唯一性,則duplicates會顯示非0數值,warning代表語句執行過程中的一些警告資訊 • low_priority關鍵詞代表如果有其他連結正在讀取目標表資料,則此insert語句需要等待讀取完成 • low_priority和high_priority關鍵詞僅在MyISAM, MEMORY, and MERGE三種儲存引擎下才生效 • Ignore關鍵詞代表insert語句如果違反主鍵和唯一鍵的約束條件,則不報錯而只產生警告資訊,違反的行被丟棄,而不是整個語句回退;在資料型別轉換有問題時如果有ignore則只產生警告資訊,而不是語句mysql> create table orders3 as select* from orders2; Query OK, 0 rows affected (0.16 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> insert into orders3(order_num,cust_id,order_date) values(20010,10007,'2005-09-01 00:00:00');#insert第一種方式 Query OK, 1 row affected (0.00 sec) mysql> select * from orders3; +-----------+---------------------+---------+ | order_num | order_date | cust_id | +-----------+---------------------+---------+ | 20010| 2005-09-01 00:00:00 | 10007 | +-----------+---------------------+---------+ 1 row in set (0.00 sec) mysql> insert into orders3 values(20010,10007,'2005-09-01 00:00:00');#insert時可以不指定列,但是需要按照排列順序傳入values; ERROR 1292 (22007): Incorrect datetime value: '10007' for column 'order_date' at row 1 mysql> insert into orders3 values(20010,'2005-09-01 00:00:00',1007); Query OK, 1 row affected (0.01 sec) mysql> select * from orders3; +-----------+---------------------+---------+ | order_num | order_date | cust_id | +-----------+---------------------+---------+ | 20010 | 2005-09-01 00:00:00 | 10007 | | 20010 | 2005-09-01 00:00:00 | 1007 | +-----------+---------------------+---------+ 2 rows in set (0.00 sec) mysql> insert into orders3(order_num,cust_id,order_date) values(20010,order_num*2,'2005-09-01 00:00:00'); #insert時,列和值一一對應就行。value可以放入表示式。 Query OK, 1 row affected (0.03 sec) mysql> select * from orders3; +-----------+---------------------+---------+ | order_num | order_date | cust_id | +-----------+---------------------+---------+ | 20010 | 2005-09-01 00:00:00 | 10007 | | 20010 | 2005-09-01 00:00:00 | 1007 | | 20010 | 2005-09-01 00:00:00 | 40020 | +-----------+---------------------+---------+ 3 rows in set (0.00 sec) mysql> insert into orders3(order_num,cust_id,order_date) values(20010,order_num*2,'2005-09-01 00:00:00'),(20011,order_num*2,'2020-09-01 00:00:00');#可一次插入多條記錄。 Query OK, 2 rows affected (0.08 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from orders3; +-----------+---------------------+---------+ | order_num | order_date | cust_id | +-----------+---------------------+---------+ | 20010 | 2005-09-01 00:00:00 | 10007 | | 20010 | 2005-09-01 00:00:00 | 1007 | | 20010 | 2005-09-01 00:00:00 | 40020 | | 20010 | 2005-09-01 00:00:00 | 40020 | | 20011 | 2020-09-01 00:00:00 | 40022 | +-----------+---------------------+---------+ 5 rows in set (0.00 sec) mysql> insert into orders3 set order_num=21210,cust_id=order_num*2,order_date='2122-09-01 00:00:00';#insert第二種方式。 Query OK, 1 row affected (0.08 sec) mysql> select * from orders3; +-----------+---------------------+---------+ | order_num | order_date | cust_id | +-----------+---------------------+---------+ | 20010 | 2005-09-01 00:00:00 | 10007 | | 20010 | 2005-09-01 00:00:00 | 1007 | | 20010 | 2005-09-01 00:00:00 | 40020 | | 20010 | 2005-09-01 00:00:00 | 40020 | | 20011 | 2020-09-01 00:00:00 | 40022 | | 21210 | 2122-09-01 00:00:00 | 42420 | +-----------+---------------------+---------+ 6 rows in set (0.00 sec) mysql> insert into orders3 select * from orders;#insert第三種方式 Query OK, 5 rows affected (0.09 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> select * from orders3; +-----------+---------------------+---------+ | order_num | order_date | cust_id | +-----------+---------------------+---------+ | 20010 | 2005-09-01 00:00:00 | 10007 | | 20010 | 2005-09-01 00:00:00 | 1007 | | 20010 | 2005-09-01 00:00:00 | 40020 | | 20010 | 2005-09-01 00:00:00 | 40020 | | 20011 | 2020-09-01 00:00:00 | 40022 | | 21210 | 2122-09-01 00:00:00 | 42420 | | 20005 | 2005-09-01 00:00:00 | 10001 | | 20006 | 2005-09-12 00:00:00 | 10003 | | 20007 | 2005-09-30 00:00:00 | 10004 | | 20008 | 2005-10-03 00:00:00 | 10005 | | 20009 | 2005-10-08 00:00:00 | 10001 | +-----------+---------------------+---------+ 11 rows in set (0.00 sec) mysql>
• 當insert語句中使用on duplicate key update子句時,如果碰到當前插入的資料違反主鍵或唯一鍵的唯一性約束,則Insert會轉變成update語句修改對應的已經存在表中的這條資料。比如如果a欄位有唯一性約束且已經含有1這條記錄,則以下兩條語句的執行結果相同。
• INSERT INTO table (a,b,c) VALUES (1,2,3)ON DUPLICATE KEY UPDATE c=c+1; • UPDATE table SET c=c+1 WHERE a=1; • On duplicate key update子句後面可以跟多個修改,用逗號隔開 • 上述例子中如果b欄位也有唯一性約束,則與此語句的執行結果相同,但一般應該避免出現對應多條的情況 • UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;mysql> select * from customers5; +---------+-----------+-----------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+-----------------+-----------+------------+----------+--------------+--------------+------------+ | 110 | 小明 | 中國廣東省 | 深圳 | 南山區 | NULL | NULL | NULL | NULL | +---------+-----------+-----------------+-----------+------------+----------+--------------+--------------+------------+ 1 row in set (0.00 sec) mysql> insert into customers5(cust_id,cust_address) values(190,"China"); Query OK, 1 row affected (0.01 sec) mysql> select * from customers5; +---------+-----------+-----------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+-----------------+-----------+------------+----------+--------------+--------------+------------+ | 110 | 小明 | 中國廣東省 | 深圳 | 南山區 | NULL | NULL | NULL | NULL | | 190 | NULL | China | 深圳 | 南山區 | NULL | NULL | NULL | NULL | +---------+-----------+-----------------+-----------+------------+----------+--------------+--------------+------------+ 2 rows in set (0.01 sec) mysql> insert into customers5(cust_id,cust_address) values(190,"China") on duplicate key update cust_id=200,cust_address='China'; Query OK, 2 rows affected (0.04 sec) mysql> select * from customers5; +---------+-----------+-----------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+-----------------+-----------+------------+----------+--------------+--------------+------------+ | 110 | 小明 | 中國廣東省 | 深圳 | 南山區 | NULL | NULL | NULL | NULL | | 200 | NULL | China | 深圳 | 南山區 | NULL | NULL | NULL | NULL | +---------+-----------+-----------------+-----------+------------+----------+--------------+--------------+------------+ 2 rows in set (0.00 sec) mysql>• update語句用於修改表中已經存在的資料 • 單表修改語句結構 • 多表修改語句結構
update語句用於修改表中已經存在的資料
• 單表修改語句結構mysql> update customers7 set cust_name='大班' where cust_id=110; Query OK, 1 row affected (0.17 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> update customers7 set cust_address='中國臺灣' -> ,cust_city='臺灣' where cust_id=110; Query OK, 1 row affected (0.18 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from customers7 where cust_id=110; +---------+-----------+--------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+--------------+-----------+------------+----------+--------------+--------------+------------+ | 110 | 大班 | 中國臺灣 | 臺灣 | 南山區 | NULL | NULL | NULL | NULL | +---------+-----------+--------------+-----------+------------+----------+--------------+--------------+------------+ 1 row in set (0.00 sec) mysql> select * from customers5 where cust_id=110; +---------+-----------+-----------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+-----------------+-----------+------------+----------+--------------+--------------+------------+ | 110 | 小明 | 中國廣東省 | 深圳 | 南山區 | NULL | NULL | NULL | NULL | +---------+-----------+-----------------+-----------+------------+----------+--------------+--------------+------------+ 1 row in set (0.00 sec) mysql> update customers5,customers7 set customers5.cust_country='中國',customers7.cust_country='中國' where customers5.cust_id = customers7.cust_id; Query OK, 16 rows affected (0.14 sec) Rows matched: 16 Changed: 16 Warnings: 0 mysql> select * from customers5; +---------+----------------+-----------------------+-----------+------------+----------+--------------+--------------+---------------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+----------------+-----------------------+-----------+------------+----------+--------------+--------------+---------------------+ | 110 | 小明 | 中國廣東省 | 深圳 | 南山區 | NULL | 中國 | NULL | NULL | | 123 | | 中國深圳南山區 | NULL | NULL | NULL | 中國 | NULL | NULL | | 200 | 大同 | China | 深圳 | 南山區 | NULL | 中國 | NULL | NULL | | 10001 | Coyote Inc. | 中國深圳南山區 | Detroit | MI | 44444 | 中國 | Y Lee | [email protected] | | 10002 | Mouse House | 中國深圳南山區 | Columbus | OH | 43333 | 中國 | Jerry Mouse | NULL | | 10003 | Wascals | 中國深圳南山區 | Muncie | IN | 42222 | 中國 | Jim Jones | [email protected] | | 10004 | Yosemite Place | 北京西城區 | Phoenix | AZ | 88888 | 中國 | Y Sam | [email protected] | | 10005 | E Fudd | 北京西城區 | Chicago | IL | 54545 | 中國 | E Fudd | NULL | +---------+----------------+-----------------------+-----------+------------+----------+--------------+--------------+---------------------+ 8 rows in set (0.00 sec) mysql> select * from customers7; +---------+----------------+-----------------------+-----------+------------+----------+--------------+--------------+---------------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+----------------+-----------------------+-----------+------------+----------+--------------+--------------+---------------------+ | 110 | 大班 | 中國臺灣 | 臺灣 | 南山區 | NULL | 中國 | NULL | NULL | | 123 | | 中國深圳南山區 | NULL | NULL | NULL | 中國 | NULL | NULL | | 200 | 大同 | China | 深圳 | 南山區 | NULL | 中國 | NULL | NULL | | 10001 | Coyote Inc. | 中國深圳南山區 | Detroit | MI | 44444 | 中國 | Y Lee | [email protected] | | 10002 | Mouse House | 中國深圳南山區 | Columbus | OH | 43333 | 中國 | Jerry Mouse | NULL | | 10003 | Wascals | 中國深圳南山區 | Muncie | IN | 42222 | 中國 | Jim Jones | [email protected] | | 10004 | Yosemite Place | 北京西城區 | Phoenix | AZ | 88888 | 中國 | Y Sam | [email protected] | | 10005 | E Fudd | 北京西城區 | Chicago | IL | 54545 | 中國 | E Fudd | NULL | +---------+----------------+-----------------------+-----------+------------+----------+--------------+--------------+---------------------+ 8 rows in set (0.00 sec) mysql>
mysql> select * from customers7; +---------+----------------+-----------------------+-----------+------------+----------+--------------+--------------+---------------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+----------------+-----------------------+-----------+------------+----------+--------------+--------------+---------------------+ | 110 | 大班 | 中國臺灣 | 臺灣 | 南山區 | NULL | 中國 | NULL | NULL | | 123 | | 中國深圳南山區 | NULL | NULL | NULL | 中國 | NULL | NULL | | 200 | 大同 | China | 深圳 | 南山區 | NULL | 中國 | NULL | NULL | | 10001 | Coyote Inc. | 中國深圳南山區 | Detroit | MI | 44444 | 中國 | Y Lee | [email protected] | | 10002 | Mouse House | 中國深圳南山區 | Columbus | OH | 43333 | 中國 | Jerry Mouse | NULL | | 10003 | Wascals | 中國深圳南山區 | Muncie | IN | 42222 | 中國 | Jim Jones | [email protected] | | 10004 | Yosemite Place | 北京西城區 | Phoenix | AZ | 88888 | 中國 | Y Sam | [email protected] | | 10005 | E Fudd | 北京西城區 | Chicago | IL | 54545 | 中國 | E Fudd | NULL | +---------+----------------+-----------------------+-----------+------------+----------+--------------+--------------+---------------------+ 8 rows in set (0.00 sec) mysql> select * from customers7 order by cust_id desc limit 2; +---------+----------------+-----------------+-----------+------------+----------+--------------+--------------+------------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+----------------+-----------------+-----------+------------+----------+--------------+--------------+------------------+ | 10005 | E Fudd | 北京西城區 | Chicago | IL | 54545 | 中國 | E Fudd | NULL | | 10004 | Yosemite Place | 北京西城區 | Phoenix | AZ | 88888 | 中國 | Y Sam | [email protected] | +---------+----------------+-----------------+-----------+------------+----------+--------------+--------------+------------------+ 2 rows in set (0.00 sec) mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> delete from customers7 order by cust_id desc limit 2; Query OK, 2 rows affected (0.00 sec) mysql> select * from customers7; +---------+-------------+-----------------------+-----------+------------+----------+--------------+--------------+---------------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-------------+-----------------------+-----------+------------+----------+--------------+--------------+---------------------+ | 110 | 大班 | 中國臺灣 | 臺灣 | 南山區 | NULL | 中國 | NULL | NULL | | 123 | | 中國深圳南山區 | NULL | NULL | NULL | 中國 | NULL | NULL | | 200 | 大同 | China | 深圳 | 南山區 | NULL | 中國 | NULL | NULL | | 10001 | Coyote Inc. | 中國深圳南山區 | Detroit | MI | 44444 | 中國 | Y Lee | [email protected] | | 10002 | Mouse House | 中國深圳南山區 | Columbus | OH | 43333 | 中國 | Jerry Mouse | NULL | | 10003 | Wascals | 中國深圳南山區 | Muncie | IN | 42222 | 中國 | Jim Jones | [email protected] | +---------+-------------+-----------------------+-----------+------------+----------+--------------+--------------+---------------------+ 6 rows in set (0.00 sec) mysql>
13.2.2 DELETE語句
DELETE
是DML語句,用於從表中刪除行。
一條DELETE
語句可以從一個WITH
子句開始,以定義可在內訪問的公用表表達式DELETE
。請參見第13.2.15節“ WITH(公用表表達式)”。
單表語法
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [[AS] tbl_alias]
[PARTITION (partition_name [, partition_name] ...)]
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
主要子句
可選WHERE
子句中的條件標識要刪除的行。沒有noWHERE
子句,將刪除所有行。
where_condition
是一個表示式,對於要刪除的每一行,其值為true。如第13.2.10節“ SELECT語句”中所述指定它。
如果ORDER BY
指定了子句,則按指定的順序刪除行。該LIMIT
子句限制了可以刪除的行數。這些子句適用於單表刪除,但不適用於多表刪除。
mysql> select * from customers7 where cust_id=110 or cust_id=123; +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ | 110 | 大班 | 中國臺灣 | 臺灣 | 南山區 | NULL | 中國 | NULL | NULL | | 123 | | 中國深圳南山區 | NULL | NULL | NULL | 中國 | NULL | NULL | +---------+-----------+-----------------------+-----------+------------+----------+--------------+--------------+------------+ 2 rows in set (0.00 sec) mysql> start transaction; Query OK, 0 rows affected (0.00 sec) mysql> delete from customers7 where cust_id=110 or cust_id=123; Query OK, 2 rows affected (0.00 sec) mysql> select * from customers7 where cust_id=110 or cust_id=123; Empty set (0.00 sec)
多表語法
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] tbl_name[.*] [, tbl_name[.*]] ... FROM table_references [WHERE where_condition] DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name[.*] [, tbl_name[.*]] ... USING table_references [WHERE where_condition]
Privileges
You need theDELETE
privilege on a table to delete rows from it. You need only theSELECT
privilege for any columns that are only read, such as those named in theWHERE
clause.