1. 程式人生 > >MYSQL資料庫(十二)- 新增表關係join、insert...select、create ...select多表更新

MYSQL資料庫(十二)- 新增表關係join、insert...select、create ...select多表更新

目錄

資料準備

一、單表寫入,insert…select

案例:獲取goods的分類,寫入到新的資料表

二、多表更新、表關係join

案例一:我們使用內連結,連結goods資料表和goods_two資料表,然後修改goods_cate

三、案例:多表更新之一步到位(create …select)

資料準備

//建立一個新的資料庫

mysql> create table if not exists goods_two(
    -> cate_id smallint unsigned primary key auto_increment,
    -> cate_
name varchar(20) not null); Query OK, 0 rows affected (0.35 sec) //檢視資料表結構: mysql> desc goods_two; +-----------+----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+----------------------+------+-----+---------+----------------+
| cate_id | smallint(5) unsigned | NO | PRI | NULL | auto_increment | | cate_name | varchar(20) | NO | | NULL | | +-----------+----------------------+------+-----+---------+----------------+ 2 rows in set (0.05 sec)

一、單表寫入,insert…select

案例:獲取goods的分類,寫入到新的資料表

//第一步,檢視我們goods資料發現我們資料表分為了7類。注意這裡還是用到的上篇部落格的goods資料表

mysql> select goods_cate from goods group by goods_cate;
+---------------+
| goods_cate    |
+---------------+
| 桌上型電腦            |
| 平板電腦             |
| 伺服器/工作站            |
| 遊戲本             |
| 筆記本            |
| 筆記本配件            |
| 超級本              |
+---------------+
7 rows in set (0.00 sec)


//第二步,把我們這7類寫入我們建立好的新資料表,這裡用到的就是我們今天學的知識,insert...select

語法:insert [into] tbl_name[(col_name,...)] select...



//第三部:把goods資料庫的所有分類,寫入寫的資料表中。

mysql> insert goods_two(cate_name) select goods_cate from goods group by goods_c
ate;
Query OK, 7 rows affected (0.06 sec)
Records: 7  Duplicates: 0  Warnings: 0


//檢視插入到新資料表中的記錄
mysql> select * from goods_two;
+---------+---------------+
| cate_id | cate_name     |
+---------+---------------+
|       1 | 桌上型電腦            |
|       2 | 平板電腦             |
|       3 | 伺服器/工作站            |
|       4 | 遊戲本             |
|       5 | 筆記本            |
|       6 | 筆記本配件            |
|       7 | 超級本              |
+---------+---------------+
7 rows in set (0.00 sec)

二、多表更新、表關係join

注意:我們需要多表更新,就需要給這些表新增關係

  • 語法結構

這裡寫圖片描述

  • 連結型別有三種 :

這裡寫圖片描述

案例一:我們使用內連結,連結goods資料表和goods_two資料表,然後修改goods_cate


//第一步,檢視插入到新資料表中的記錄,新增內連線到goods,用cate_id來代替總表中的goods_cate引數。
mysql> select * from goods_two;
+---------+---------------+
| cate_id | cate_name     |
+---------+---------------+
|       1 | 桌上型電腦            |
|       2 | 平板電腦             |
|       3 | 伺服器/工作站            |
|       4 | 遊戲本             |
|       5 | 筆記本            |
|       6 | 筆記本配件            |
|       7 | 超級本              |
+---------+---------------+



//第二步、我們給這兩張資料表新增內連線,這樣我們更新資料表goods_two中的資料,就會改變goods中的資料,這樣我們就不用操作總表去改變資料,只需要通過子表來改變資料即可,方便我們操作。
mysql> update goods inner join goods_two on goods_cate=cate_name
    -> set goods_cate = cate_id;
Query OK, 22 rows affected (0.07 sec)
Rows matched: 22  Changed: 22  Warnings: 0

//建立關係前.總表中的goods_cate,還是筆記本配件

*************************** 22. row ***************************
   goods_id: 22
 goods_name: 商務雙肩揹包
 goods_cate: 筆記本配件
 brand_name: 索尼
goods_price: 99.000
    is_show: 1
 is_saleoff: 0
22 rows in set (0.00 sec)


//建立關係後、後面的值,就會改變成goods_two中與之對應的cate_id值,而cate_id多代表的意思就是筆記本配件

*************************** 22. row ***************************
   goods_id: 22
 goods_name: 商務雙肩揹包
 goods_cate: 6
 brand_name: 索尼
goods_price: 99.000
    is_show: 1
 is_saleoff: 0
22 rows in set (0.00 sec)

三、案例:多表更新之一步到位(create …select)

  • 回憶上部分,單表更新的時候,我們一共用到了三步,

    1、建立goods_two資料表。
    2、把分類寫入到新資料表中,
    3、新增inner join關聯資料表,然後修改資料表。

  • 那能不能減少和優化這些操作呢,這裡就要用到create …select

語法:create table [if not exists] tbl_name [(create_definition,…)] select_statement

//案例:這裡我們操作我們的品牌 brand_name.

//檢視我們資料表,會發現我們有9組不同型別的品牌;

mysql> select brand_name from goods group by brand_name;
+------------+
| brand_name |
+------------+
| IBM        |
| 華碩          |
| 巨集碁         |
| 惠普           |
| 戴爾           |
| 索尼           |
| 聯想           |
| 蘋果          |
| 雷神           |
+------------+
9 rows in set (0.00 sec)


//第二步:我們把品牌也寫入到一張新的資料表,這裡我們用到create...select,新增成後,系統提示我們有9條記錄被寫入


mysql> create table goods_brand(
    -> brand_id smallint unsigned primary key auto_increment,
    -> brand_name varchar(40) not null
    -> )
    -> select brand_name from goods group by brand_name;
Query OK, 9 rows affected (0.14 sec)
Records: 9  Duplicates: 0  Warnings: 0

//第三步,驗證是否新增成功goods_brand資料表和goods_brand資料表是否存在剛才新增的資料。

//存在goods_brand資料表
mysql> show tables;
+--------------+
| Tables_in_t1 |
+--------------+
| city_1       |
| goods        |
| goods_brand  |
| goods_two    |
| sheng        |
| tdb_goods    |
| two          |
| users        |
+--------------+
8 rows in set (0.00 sec)

//並且goods_brand資料表存在剛才我們的9條資料

mysql> select * from goods_brand;
+----------+------------+
| brand_id | brand_name |
+----------+------------+
|        1 | IBM        |
|        2 | 華碩          |
|        3 | 巨集碁         |
|        4 | 惠普           |
|        5 | 戴爾           |
|        6 | 索尼           |
|        7 | 聯想           |
|        8 | 蘋果          |
|        9 | 雷神           |
+----------+------------+
9 rows in set (0.00 sec)


//第四步,新增關係,根據品牌表goods_brand,來更新我們的goods總表,但是注意,我們兩張資料表中的brand_name是一樣的引數名稱,如果還按照上面那種新增連結,會爆如下錯誤:

//因為,他不知道brand_name到底屬於哪一張表
mysql> update goods inner join goods_brand on brand_name= brand_name
    -> set brand_name=brand_id;
ERROR 1052 (23000): Column 'brand_name' in field list is ambiguous

第五步,這裡我們就要給資料表起別名或者在引數前面加上表名(goods as g )。

mysql> update goods as g inner join goods_brand as b on g.brand_name=
    -> b.brand_name
    -> set g.brand_name = b.brand_id;
Query OK, 22 rows affected (0.06 sec)
Rows matched: 22  Changed: 22  Warnings: 0

具體案例步驟

  • 具體案例
//第一步建立資料表tdb_goods
  CREATE TABLE IF NOT EXISTS tdb_goods(
    goods_id    SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    goods_name  VARCHAR(150) NOT NULL,
    goods_cate  VARCHAR(40)  NOT NULL,
    brand_name  VARCHAR(40)  NOT NULL,
    goods_price DECIMAL(15,3) UNSIGNED NOT NULL DEFAULT 0,
    is_show     BOOLEAN NOT NULL DEFAULT 1,
    is_saleoff  BOOLEAN NOT NULL DEFAULT 0
  );


//INSERTtdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)VAL//UES('R510VC 15.6英寸筆記本','筆記本','華碩','3399',DEFAULT,DEFAULT);

//INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
//VALUES('Y400N 14.0英寸膝上型電腦','筆記本','聯想','4899',DEFAULT,DEFAULT);

//INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
//VALUES('G150TH 15.6英寸遊戲本','遊戲本','雷神','8499',DEFAULT,DEFAULT);

//INSERT tdb_goods(goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
//VALUES('X550CC 15.6英寸筆記本','筆記本','華碩','2799',DEFAULT,DEFAULT);


//第三步:插入成功後,檢視資料庫
mysql> select * from tdb_goods\G;
*************************** 1. row ***************************
   goods_id: 1
 goods_name: R510VC 15.6英寸筆記本
 goods_cate: 筆記本
 brand_name: 華碩
goods_price: 3399.000
    is_show: 1
 is_saleoff: 0
*************************** 2. row ***************************
   goods_id: 2
 goods_name: Y400N 14.0英寸膝上型電腦
 goods_cate: 筆記本
 brand_name: 聯想
goods_price: 4899.000
    is_show: 1
 is_saleoff: 0
*************************** 3. row ***************************
   goods_id: 3
 goods_name: G150TH 15.6英寸遊戲本
 goods_cate: 遊戲本
 brand_name: 雷神
goods_price: 8499.000
    is_show: 1
 is_saleoff: 0
*************************** 4. row ***************************
   goods_id: 4
 goods_name: X550CC 15.6英寸筆記本
 goods_cate: 筆記本
 brand_name: 華碩
goods_price: 2799.000
    is_show: 1
 is_saleoff: 0
4 rows in set (0.00 sec)






//第四步、在建立兩個資料表,分別儲存goods_cate和brand_name;

//建立tdb_goods_cates資料表
mysql>   CREATE TABLE IF NOT EXISTS tdb_goods_cates(
    ->     cate_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    ->     cate_name VARCHAR(40)
    ->   );
Query OK, 0 rows affected (0.10 sec)


//寫入資料
mysql> INSERT tdb_goods_cates (cate_name) SELECT goods_cate FROM tdb_goods GROUP BY
    -> goods_cate;

Query OK, 2 rows affected (0.05 sec)


mysql> select * from tdb_goods_cates;
+---------+-----------+
| cate_id | cate_name |
+---------+-----------+
|       1 | 遊戲本         |
|       2 | 筆記本        |
+---------+-----------+
2 rows in set (0.00 sec)


//建立tdb_goods_brands 資料表,並寫入資料

mysql>   CREATE TABLE tdb_goods_brands (
    ->
    ->     brand_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    ->
    ->     brand_name VARCHAR(40) NOT NULL
    ->
    ->   ) SELECT brand_name FROM tdb_goods GROUP BY brand_name;
Query OK, 3 rows affected (0.25 sec)
Records: 3  Duplicates: 0  Warnings: 0




//第五步:通過tdb_goods_cates資料表來更新tdb_goods表


//更新goods_cate的值

mysql>   UPDATE tdb_goods INNER JOIN tdb_goods_cates ON goods_cate = cate_name
    ->   SET goods_cate = cate_id ;
Query OK, 4 rows affected (0.17 sec)
Rows matched: 4  Changed: 4  Warnings: 0

//更新g.brand_name 的值

mysql>   UPDATE tdb_goods AS  g  INNER JOIN tdb_goods_brands AS b ON g.brand_name = 

b.brand_name
    ->   SET g.brand_name = b.brand_id;
Query OK, 4 rows affected (0.03 sec)
Rows matched: 4  Changed: 4  Warnings: 0


//第六步:更新後

mysql> select * from tdb_goods\G;
*************************** 1. row ***************************
   goods_id: 1
 goods_name: R510VC 15.6英寸筆記本
 goods_cate: 2
 brand_name: 1
goods_price: 3399.000
    is_show: 1
 is_saleoff: 0
*************************** 2. row ***************************
   goods_id: 2
 goods_name: Y400N 14.0英寸膝上型電腦
 goods_cate: 2
 brand_name: 2
goods_price: 4899.000
    is_show: 1
 is_saleoff: 0
*************************** 3. row ***************************
   goods_id: 3
 goods_name: G150TH 15.6英寸遊戲本
 goods_cate: 1
 brand_name: 3
goods_price: 8499.000
    is_show: 1
 is_saleoff: 0
*************************** 4. row ***************************
   goods_id: 4
 goods_name: X550CC 15.6英寸筆記本
 goods_cate: 2
 brand_name: 1
goods_price: 2799.000
    is_show: 1
 is_saleoff: 0
4 rows in set (0.00 sec)




第七步:、修改goods_cate、brand_name的名稱和型別;

mysql>   ALTER TABLE tdb_goods
    ->  CHANGE goods_cate cate_id SMALLINT UNSIGNED NOT NULL,
    ->  CHANGE brand_name brand_id SMALLINT UNSIGNED NOT NULL;
Query OK, 4 rows affected (0.44 sec)
Records: 4  Duplicates: 0  Warnings: 0



//修改成功後
mysql> select * from tdb_goods\G;
*************************** 1. row ***************************
   goods_id: 1
 goods_name: R510VC 15.6英寸筆記本
    cate_id: 2
   brand_id: 1
goods_price: 3399.000
    is_show: 1
 is_saleoff: 0
*************************** 2. row ***************************
   goods_id: 2
 goods_name: Y400N 14.0英寸膝上型電腦
    cate_id: 2
   brand_id: 2
goods_price: 4899.000
    is_show: 1
 is_saleoff: 0
*************************** 3. row ***************************
   goods_id: 3
 goods_name: G150TH 15.6英寸遊戲本
    cate_id: 1
   brand_id: 3
goods_price: 8499.000
    is_show: 1
 is_saleoff: 0
*************************** 4. row ***************************
   goods_id: 4
 goods_name: X550CC 15.6英寸筆記本
    cate_id: 2
   brand_id: 1
goods_price: 2799.000
    is_show: 1
 is_saleoff: 0
4 rows in set (0.00 sec)



//分別為這三個資料表,插入資料,為了之後區分三種連線的含義做準備。

//插入成功
//mysql> INSERT tdb_goods_cates(cate_name) VALUES('路由器'),('交換機'),('網絡卡');
//Query OK, 3 rows affected (0.11 sec)


//插入成功
//mysql>    INSERT tdb_goods_brands(brand_name) VALUES('海爾'),('清華同方'),('神舟');
//Query OK, 3 rows affected (0.04 sec)


//插入成功
mysql> INSERT tdb_goods(goods_name,cate_id,brand_id,goods_price) VALUES(' LaserJet Pro'>'> P1606dn 黑白鐳射印表機','12','4','1849');
Query OK, 1 row affected (0.07 sec)


//第八步,檢視三個資料表。
//資料表tdb_goods;

mysql> select * from tdb_goods\G;
*************************** 1. row ***************************
   goods_id: 1
 goods_name: R510VC 15.6英寸筆記本
    cate_id: 2
   brand_id: 1
goods_price: 3399.000
    is_show: 1
 is_saleoff: 0
*************************** 2. row ***************************
   goods_id: 2
 goods_name: Y400N 14.0英寸膝上型電腦
    cate_id: 2
   brand_id: 2
goods_price: 4899.000
    is_show: 1
 is_saleoff: 0
*************************** 3. row ***************************
   goods_id: 3
 goods_name: G150TH 15.6英寸遊戲本
    cate_id: 1
   brand_id: 3
goods_price: 8499.000
    is_show: 1
 is_saleoff: 0
*************************** 4. row ***************************
   goods_id: 4
 goods_name: X550CC 15.6英寸筆記本
    cate_id: 2
   brand_id: 1
goods_price: 2799.000
    is_show: 1
 is_saleoff: 0
*************************** 5. row ***************************
   goods_id: 5
 goods_name:  LaserJet Pro

P1606dn 黑白鐳射印表機
    cate_id: 12
   brand_id: 4
goods_price: 1849.000
    is_show: 1
 is_saleoff: 0
5 rows in set (0.00 sec)


//資料表tdb_goods_cates

mysql> select * from tdb_goods_cates;
+---------+-----------+
| cate_id | cate_name |
+---------+-----------+
|       1 | 遊戲本         |
|       2 | 筆記本        |
|       4 | 路由器         |
|       5 | 交換機          |
|       6 | 網絡卡          |
+---------+-----------+
5 rows in set (0.00 sec)


//資料表tdb_goods_brands

mysql> select * from tdb_goods_brands;
+----------+------------+
| brand_id | brand_name |
+----------+------------+
|        1 | 華碩          |
|        2 | 聯想           |
|        3 | 雷神           |
|        4 | 海爾           |
|        5 | 清華同方        |
|        6 | 神舟           |
+----------+------------+
6 rows in set (0.00 sec)




//第九步:查詢資料,查詢出來後,是預設的資料,千萬切記,這裡不是更改資料表

mysql>    SELECT goods_id,goods_name,cate_name,brand_name,goods_price FROM tdb_goods AS g
    ->
    ->    INNER JOIN tdb_goods_cates AS c ON g.cate_id = c.cate_id
    ->
    ->    INNER JOIN tdb_goods_brands AS b ON g.brand_id = b.brand_id\G;
*************************** 1. row ***************************
   goods_id: 3
 goods_name: G150TH 15.6英寸遊戲本
  cate_name: 遊戲本
 brand_name: 雷神
goods_price: 8499.000
*************************** 2. row ***************************
   goods_id: 1
 goods_name: R510VC 15.6英寸筆記本
  cate_name: 筆記本
 brand_name: 華碩
goods_price: 3399.000
*************************** 3. row ***************************
   goods_id: 2
 goods_name: Y400N 14.0英寸膝上型電腦
  cate_name: 筆記本
 brand_name: 聯想
goods_price: 4899.000
*************************** 4. row ***************************
   goods_id: 4
 goods_name: X550CC 15.6英寸筆記本
  cate_name: 筆記本
 brand_name: 華碩
goods_price: 2799.000
4 rows in set (0.00 sec)




//第十步:查詢最終資料庫儲存的資料如下:

mysql> select * from tdb_goods\G;
*************************** 1. row ***************************
   goods_id: 1
 goods_name: R510VC 15.6英寸筆記本
    cate_id: 2
   brand_id: 1
goods_price: 3399.000
    is_show: 1
 is_saleoff: 0
*************************** 2. row ***************************
   goods_id: 2
 goods_name: Y400N 14.0英寸膝上型電腦
    cate_id: 2
   brand_id: 2
goods_price: 4899.000
    is_show: 1
 is_saleoff: 0
*************************** 3. row ***************************
   goods_id: 3
 goods_name: G150TH 15.6英寸遊戲本
    cate_id: 1
   brand_id: 3
goods_price: 8499.000
    is_show: 1
 is_saleoff: 0
*************************** 4. row ***************************
   goods_id: 4
 goods_name: X550CC 15.6英寸筆記本
    cate_id: 2
   brand_id: 1
goods_price: 2799.000
    is_show: 1
 is_saleoff: 0
*************************** 5. row ***************************
   goods_id: 5
 goods_name:  LaserJet Pro

P1606dn 黑白鐳射印表機
    cate_id: 12
   brand_id: 4
goods_price: 1849.000
    is_show: 1
 is_saleoff: 0
5 rows in set (0.00 sec)

ERROR:
No query specified

總結,很可能有人看我部落格看不懂,原因是我的部落格是跟上一遍關聯在一起的。所以這裡給出大家視訊的地址,而這裡只是我自己的一個筆記。視訊地址:http://www.imooc.com/video/2404