您好,登錄后才能下訂單哦!
本篇內容主要講解“MySQL怎么創建多個表的更新與刪除”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“MySQL怎么創建多個表的更新與刪除”吧!
1.涉及多個表的更新與刪除
創建測試用表:
mysql> create table users1
-> (
-> uid tinyint unsigned,
-> uname varchar(255),
-> gid tinyint unsigned
-> );
Query OK, 0 rows affected (0.06 sec)
mysql> create table groups1
-> (
-> gid tinyint unsigned,
-> gname varchar(255)
-> );
Query OK, 0 rows affected (0.02 sec)
[@more@]mysql> insert into users1 values (0, 'root', 0);
Query OK, 1 row affected (0.00 sec)
mysql> insert into users1 values (201, 'ggyy', 101);
Query OK, 1 row affected (0.00 sec)
mysql> insert into users1 values (202, 'ssff', 101);
Query OK, 1 row affected (0.00 sec)
mysql> insert into groups1 values (0, 'root');
Query OK, 1 row affected (0.00 sec)
mysql> insert into groups1 values (101, 'guest');
Query OK, 1 row affected (0.00 sec)
mysql> select * from users1;
+------+-------+------+
| uid | uname | gid |
+------+-------+------+
| 0 | root | 0 |
| 201 | ggyy | 101 |
| 202 | ssff | 101 |
+------+-------+------+
3 rows in set (0.00 sec)
mysql> select * from groups1;
+------+-------+
| gid | gname |
+------+-------+
| 0 | root |
| 101 | guest |
+------+-------+
2 rows in set (0.00 sec)
下面的語句將users1表中屬于guest組的用戶的uid加10:
mysql> update users1, groups1 set users1.uid = users1.uid + 10 where users1.gid = groups1.gid and gr
oups1.gname = 'guest';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from users1;
+------+-------+------+
| uid | uname | gid |
+------+-------+------+
| 0 | root | 0 |
| 211 | ggyy | 101 |
| 212 | ssff | 101 |
+------+-------+------+
3 rows in set (0.00 sec)
下面的語句將兩個表中guest組的gid變為102:
mysql> update users1, groups1 set users1.gid = 102, groups1.gid = 102 where users1.gid = groups1.gid
and groups1.gid = 101;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from users1;
+------+-------+------+
| uid | uname | gid |
+------+-------+------+
| 0 | root | 0 |
| 211 | ggyy | 102 |
| 212 | ssff | 102 |
+------+-------+------+
3 rows in set (0.00 sec)
mysql> select * from groups1;
+------+-------+
| gid | gname |
+------+-------+
| 0 | root |
| 102 | guest |
+------+-------+
2 rows in set (0.00 sec)
但是,這樣的語句就會產生錯誤的結果:
mysql> update users1, groups1 set users1.gid = 102, groups1.gid = 102 where users1.gid = groups1.gid
and groups1.gname = 'guest';
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from users1;
+------+-------+------+
| uid | uname | gid |
+------+-------+------+
| 0 | root | 0 |
| 211 | ggyy | 102 |
| 212 | ssff | 101 |
+------+-------+------+
3 rows in set (0.00 sec)
mysql> select * from groups1;
+------+-------+
| gid | gname |
+------+-------+
| 0 | root |
| 102 | guest |
+------+-------+
2 rows in set (0.00 sec)
ssff用戶的gid沒有更新,想不太明白原因。
下面的語句刪除users1表中屬于root組的用戶的記錄:
mysql> delete from users1 using users1, groups1 where users1.gid = groups1.gid and groups1.gname = '
root';
Query OK, 1 row affected (0.00 sec)
mysql> select * from users1;
+------+-------+------+
| uid | uname | gid |
+------+-------+------+
| 211 | ggyy | 102 |
| 212 | ssff | 102 |
+------+-------+------+
2 rows in set (0.02 sec)
mysql> select * from groups1;
+------+-------+
| gid | gname |
+------+-------+
| 0 | root |
| 102 | guest |
+------+-------+
2 rows in set (0.00 sec)
該刪除語句可以寫成這樣的形式:“delete users1 from users1, groups1 where users1.gid = groups1.gid and groups1.gname = 'root';”。注意,from前面的是要刪除記錄的表,后面的是刪除操作涉及的幾個表(本例中是內連接,可以為其它連接類型)。
下面的語句刪除users1表中屬于guest組的用戶的記錄以及groups1表中guest組的記錄。
mysql> delete from users1, groups1 using users1, groups1 where users1.gid = groups1.gid and groups1.
gname = 'guest';
Query OK, 3 rows affected (0.00 sec)
mysql> select * from users1;
Empty set (0.02 sec)
mysql> select * from groups1;
+------+-------+
| gid | gname |
+------+-------+
| 0 | root |
+------+-------+
1 row in set (0.00 sec)
同樣,該刪除語句可以寫成這樣的形式:“delete users1, groups1 from users1, groups1 where users1.gid = groups1.gid and groups1.gname = 'guest';”。
2.隨機選擇記錄
將ORDER BY子句和RAND()函數結合使用,可以達到隨機選擇表中記錄的效果:
mysql> select * from oraleng;
+--------------------------+--------------------------+
| ask | answer |
+--------------------------+--------------------------+
| How do you do? | How do you do? |
| How are you? | Fine.Thank you. |
| What's your name? | My name is Jack Sparrow. |
| Where are you from? | I'm from maldives. |
| What's the weather like? | It's fine. |
| What time is it now? | It's seven o'clock. |
| What day is it today? | It's Wednesday. |
+--------------------------+--------------------------+
7 rows in set (0.00 sec)
mysql> select * from oraleng order by rand() limit 1;
+--------------+-----------------+
| ask | answer |
+--------------+-----------------+
| How are you? | Fine.Thank you. |
+--------------+-----------------+
1 row in set (0.02 sec)
mysql> select * from oraleng order by rand() limit 1;
+-----------------------+-----------------+
| ask | answer |
+-----------------------+-----------------+
| What day is it today? | It's Wednesday. |
+-----------------------+-----------------+
1 row in set (0.02 sec)
mysql> select * from oraleng order by rand() limit 1;
+-------------------+--------------------------+
| ask | answer |
+-------------------+--------------------------+
| What's your name? | My name is Jack Sparrow. |
+-------------------+--------------------------+
1 row in set (0.02 sec)
mysql> select * from oraleng order by rand() limit 2;
+----------------------+---------------------+
| ask | answer |
+----------------------+---------------------+
| What time is it now? | It's seven o'clock. |
| Where are you from? | I'm from maldives. |
+----------------------+---------------------+
2 rows in set (0.02 sec)
3.控制SELECT行為
下面是一些能夠改變SELECT語句行為的關鍵字:
DISTINCT:刪除結果集中的包含重復值記錄。
SQL_CALC_FOUND_ROWS:計算符合查詢的總行數。不受LIMIT影響,通過調用FOUND_ROWS函數可以得到結果。
SQL_CACHE和SQL_NO_CACHE:指定查詢結果是否需要高速緩存。
SQL_BUFFER_RESULT:強制將查詢結果存儲到一個臨時表。這種緩沖消除了對查詢的表的鎖定。
SQL_BIG_RESULT和SQL_SMALL_RESULT:指定結果集的期望大小。這樣可幫助找到對返回的記錄進行排序和存儲的最佳方法(基于磁盤或者內存中的臨時表)。
SQL_HIGH_PRIORITY:提升與UPDATE, INSERT和DELETE語句相競爭的查詢的優先級。可以在繁忙的數據庫服務器上快速地執行查詢。
4.從文件導入和向文件導出
可以使用LOAD DATA INFILE語句將文件中的數據導入到表中,也可以使用SELECT...INTO OUTFILE語句將表中的記錄導出到文件中。
1)分隔符
在上述語句中,使用一些子句和關鍵字指定文件中的數據格式。
LINES TERMINATED BY子句:指定記錄的結束符。(默認情況下,n表示新的一行。)
FIELDS子句:指定字段的分割符。FIELDS后面跟著TERMINATED BY, ESCAPED BY, ENCLOSED BY等關鍵字中的一個或多個。
TERMINATED BY指定字段的結束符(默認為t);ESCAPED BY用于跳過特殊的字符(默認為反斜線);ENCLOSED BY指定包圍字段的符號(默認無)。
2)從文件中導入數據
E:downloadcontact.txt是一個包含著一組聯系人信息的文本文件,其內容如下:
河北聯通石家莊分公司,張少蘭,0311-87052200
河北聯通滄州分公司,王建榮,0317-3520079
河北聯通保定分公司,孫鳳睿,0312-3075574
河北聯通廊坊分公司,龐海靜,0316-2684535
河北聯通秦皇島分公司,代艷麗,0335-3050172
......
現在創建一個用于存儲這些聯系人信息的表:
mysql> create table contact
-> (
-> name varchar(20),
-> sex enum('男','女'),
-> tel bigint,
-> email varchar(50),
-> company varchar(50)
-> );
Query OK, 0 rows affected (0.13 sec)
使用Load DATA INFILE語句向其中導入數據:
mysql> load data infile 'E:downloadcontact.txt' into table contact
-> fields terminated by ',' escaped by '-' lines terminated by 'rn'
-> (company, name, tel);
Query OK, 46 rows affected (0.02 sec)
Records: 46 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from contact limit 7;
+--------+------+-------------+-------+----------------------+
| name | sex | tel | email | company |
+--------+------+-------------+-------+----------------------+
| 張少蘭 | NULL | 31187052200 | NULL | 河北聯通石家莊分公司 |
| 王建榮 | NULL | 3173520079 | NULL | 河北聯通滄州分公司 |
| 孫鳳睿 | NULL | 3123075574 | NULL | 河北聯通保定分公司 |
| 龐海靜 | NULL | 3162684535 | NULL | 河北聯通廊坊分公司 |
| 代艷麗 | NULL | 3353050172 | NULL | 河北聯通秦皇島分公司 |
| 齊衛花 | NULL | 3132018225 | NULL | 河北聯通張家口分公司 |
| 劉守政 | NULL | 3182698169 | NULL | 河北聯通衡水分公司 |
+--------+------+-------------+-------+----------------------+
7 rows in set (0.00 sec)
幾點說明:
a.進行導入的用戶必須具有FILE權限。
b.文件路徑中的“”符號要用“”來代替。
c.當文件中各部分內容與表中的字段數量或順序不符時,可以在LOAD DATA INFILE語句的最后指定一個字段名的列表,來將文件中的各部分內容映射到正確的字段中。
介紹LOAD DATA INFILE語句中的一些關鍵字:
LOCAL:指定INFILE是在客戶機的文件系統上。默認情況下,認為在服務器上。
LOW_PRIORITY:延遲LOAD DATA語句的執行,直到沒有其它的客戶端從表中讀取為止。
IGNORE, REPLACE:當插入的新記錄的一個鍵與已存在的記錄的重復時,跳過該條新記錄或用新記錄替換已存在的記錄。
3)向文件中導出記錄
使用SELECT INTO...OUTFILE語句向文本文件contact2.txt中導出記錄:
mysql> select name, tel, company from contact where name like '張%'
-> into outfile 'E:downloadcontact2.txt'
-> fields enclosed by '"' lines terminated by 'rn';
Query OK, 4 rows affected (0.06 sec)
查看一下該文件的內容:
"張少蘭" "31187052200" "河北聯通石家莊分公司"
"張雷" "3125902030" "河北電信保定分公司"
"張東旺" "3155960019" "遷安市星宇商貿有限公司"
"張蕾" "3123100913" "保定企盟信息網絡有限公司"
幾點說明:
a.進行導出的用戶必須具有FILE權限。
b.導出文件事先不能存在。否則會發生錯誤:
ERROR 1086 (HY000): File 'E:downloadcontact2.txt' already exists
c.對于二進制數據,如BLOB類型,可以使用INTO DUMPFILE子句代替INTO OUTFILE子句。這樣MySQL將以一個單獨行的格式向文件寫入數據(沒有字段或記錄結束符),從而避免破壞二進制數據。
到此,相信大家對“MySQL怎么創建多個表的更新與刪除”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。