久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

MySQL怎么創建多個表的更新與刪除

129次閱讀
沒有評論

共計 7818 個字符,預計需要花費 20 分鐘才能閱讀完成。

本篇內容主要講解“MySQL 怎么創建多個表的更新與刪除”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓丸趣 TV 小編來帶大家學習“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 怎么創建多個表的更新與刪除”有了更深的了解,不妨來實際操作一番吧!這里是丸趣 TV 網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-07-17發表,共計7818字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 沁源县| 新疆| 庐江县| 大名县| 河源市| 永年县| 葫芦岛市| 舒兰市| 大竹县| 南江县| 泰顺县| 板桥市| 香河县| 彭山县| 定南县| 中卫市| 清远市| 措美县| 开封市| 临夏市| 菏泽市| 钟山县| 沁水县| 邯郸县| 滕州市| 连平县| 涪陵区| 曲松县| 柯坪县| 遵化市| 封开县| 芦山县| 紫金县| 五河县| 南澳县| 鄢陵县| 盐池县| 金湖县| 古交市| 巴马| 张家港市|