zoukankan      html  css  js  c++  java
  • MariaDB VIEW

    MariaDB VIEW

    VIEW支持UPDATE,INSERT,DELETE ,但虚拟表只上将操作转移到了真正的表上
    示例一:替换复杂的JOIN

    (jlive)[crashcourse]>SELECT cust_name, cust_contact, prod_id FROM customers, orders, orderitems WHERE customers.cust_id = orders.cust_id AND orderitems.order_num = orders.order_num;

    +----------------+--------------+---------+

    | cust_name      | cust_contact | prod_id |

    +----------------+--------------+---------+

    | Coyote Inc.    | Y Lee        | ANV01   |

    | Coyote Inc.    | Y Lee        | ANV02   |

    | Coyote Inc.    | Y Lee        | TNT2    |

    | Coyote Inc.    | Y Lee        | FB      |

    | Wascals        | Jim Jones    | JP2000  |

    | Yosemite Place | Y Sam        | TNT2    |

    | The Fudds      | E Fudd       | FC      |

    | Coyote Inc.    | Y Lee        | FB      |

    | Coyote Inc.    | Y Lee        | OL1     |

    | Coyote Inc.    | Y Lee        | SLING   |

    | Coyote Inc.    | Y Lee        | ANV03   |

    +----------------+--------------+---------+

    11 rows in set (0.00 sec)


    (jlive)[crashcourse]>CREATE VIEW productcustomers AS SELECT cust_name, cust_contact, prod_id FROM customers, orders, orderitems WHERE customers.cust_id = orders.cust_id AND orderitems.order_num = orders.order_num;

    Query OK, 0 rows affected (0.00 sec)


    (jlive)[crashcourse]>DESC productcustomers;

    +--------------+----------+------+-----+---------+-------+

    | Field        | Type     | Null | Key | Default | Extra |

    +--------------+----------+------+-----+---------+-------+

    | cust_name    | char(50) | NO   |     | NULL    |       |

    | cust_contact | char(50) | YES  |     | NULL    |       |

    | prod_id      | char(10) | NO   |     | NULL    |       |

    +--------------+----------+------+-----+---------+-------+

    3 rows in set (0.00 sec)


    (jlive)[crashcourse]>SELECT cust_name, cust_contact FROM productcustomers WHERE prod_id = 'TNT2';

    +----------------+--------------+

    | cust_name      | cust_contact |

    +----------------+--------------+

    | Coyote Inc.    | Y Lee        |

    | Yosemite Place | Y Sam        |

    +----------------+--------------+

    2 rows in set (0.00 sec)


    示例二:重新格式化查询到的数据

    (jlive)[crashcourse]>SELECT Concat(RTrim(vend_name), ' (', RTrim(vend_country), ')') AS vend_title FROM vendors ORDER BY vend_name;

    +-------------------------+

    | vend_title              |

    +-------------------------+

    | ACME (USA)              |

    | Anvils R Us (USA)       |

    | Furball Inc. (USA)      |

    | Jet Set (England)       |

    | Jouets Et Ours (France) |

    | LT Supplies (USA)       |

    +-------------------------+

    6 rows in set (0.00 sec)


    (jlive)[crashcourse]>CREATE VIEW vendorlocations AS SELECT Concat(RTrim(vend_name), ' (', RTrim(vend_country), ')') AS vend_title FROM vendors ORDER BY vend_name;

    Query OK, 0 rows affected (0.00 sec)


    (jlive)[crashcourse]>DESC vendorlocations;

    +------------+--------------+------+-----+---------+-------+

    | Field      | Type         | Null | Key | Default | Extra |

    +------------+--------------+------+-----+---------+-------+

    | vend_title | varchar(103) | YES  |     | NULL    |       |

    +------------+--------------+------+-----+---------+-------+

    1 row in set (0.00 sec)


    (jlive)[crashcourse]>SELECT * FROM vendorlocations;

    +-------------------------+

    | vend_title              |

    +-------------------------+

    | ACME (USA)              |

    | Anvils R Us (USA)       |

    | Furball Inc. (USA)      |

    | Jet Set (England)       |

    | Jouets Et Ours (France) |

    | LT Supplies (USA)       |

    +-------------------------+

     

    6 rows in set (0.00 sec)




    示例三:过滤不需要的数据

    (jlive)[crashcourse]>SELECT cust_id, cust_name, cust_email FROM customers WHERE cust_email IS NOT NULL;

    +---------+----------------+---------------------+

    | cust_id | cust_name      | cust_email          |

    +---------+----------------+---------------------+

    |   10001 | Coyote Inc.    | ylee@coyote.com     |

    |   10003 | Wascals        | rabbit@wascally.com |

    |   10004 | Yosemite Place | sam@yosemite.com    |

    +---------+----------------+---------------------+

    3 rows in set (0.00 sec)


    (jlive)[crashcourse]>CREATE VIEW customeremaillist AS SELECT cust_id, cust_name, cust_email FROM customers WHERE cust_email IS NOT NULL;

    Query OK, 0 rows affected (0.00 sec)


    (jlive)[crashcourse]>DESC customeremaillist;

    +------------+-----------+------+-----+---------+-------+

    | Field      | Type      | Null | Key | Default | Extra |

    +------------+-----------+------+-----+---------+-------+

    | cust_id    | int(11)   | NO   |     | 0       |       |

    | cust_name  | char(50)  | NO   |     | NULL    |       |

    | cust_email | char(255) | YES  |     | NULL    |       |

    +------------+-----------+------+-----+---------+-------+

    3 rows in set (0.00 sec)


    (jlive)[crashcourse]>SELECT * FROM customeremaillist;

    +---------+----------------+---------------------+

    | cust_id | cust_name      | cust_email          |

    +---------+----------------+---------------------+

    |   10001 | Coyote Inc.    | ylee@coyote.com     |

    |   10003 | Wascals        | rabbit@wascally.com |

    |   10004 | Yosemite Place | sam@yosemite.com    |

    +---------+----------------+---------------------+

     

    3 rows in set (0.00 sec)





    示例四:混杂计算

    (jlive)[crashcourse]>SELECT order_num, prod_id, quantity, item_price, quantity*item_price AS expanded_price FROM orderitems;

    +-----------+---------+----------+------------+----------------+

    | order_num | prod_id | quantity | item_price | expanded_price |

    +-----------+---------+----------+------------+----------------+

    |     20005 | ANV01   |       10 |       5.99 |          59.90 |

    |     20005 | ANV02         3 |       9.99 |          29.97 |

    |     20005 | TNT2          5 |      10.00 |          50.00 |

    |     20005 | FB            1 |      10.00 |          10.00 |

    |     20006 | JP2000        1 |      55.00 |          55.00 |

    |     20007 | TNT2        100 |      10.00 |        1000.00 |

    |     20008 | FC      |       50 |       2.50 |         125.00 |

    |     20009 | FB            1 |      10.00 |          10.00 |

    |     20009 | OL1           1 |       8.99 |           8.99 |

    |     20009 | SLING         1 |       4.49 |           4.49 |

    |     20009 | ANV03         1 |      14.99 |          14.99 |

    +-----------+---------+----------+------------+----------------+

     

    11 rows in set (0.00 sec)

    (jlive)[crashcourse]>CREATE OR REPLACE VIEW orderitemsexpanded AS SELECT order_num, prod_id, quantity, item_price, quantity*item_price AS expanded_price FROM orderitems;

    Query OK, 0 rows affected (0.00 sec)


    (jlive)[crashcourse]>DESC orderitemsexpanded;

    +----------------+---------------+------+-----+---------+-------+

    | Field          | Type          | Null | Key | Default | Extra |

    +----------------+---------------+------+-----+---------+-------+

    | order_num      | int(11)       | NO   |     | NULL    |       |

    | prod_id        | char(10)      | NO   |     | NULL    |       |

    | quantity       | int(11)       | NO   |     | NULL    |       |

    | item_price     | decimal(8,2)  | NO   |     | NULL    |       |

    | expanded_price | decimal(18,2) | NO   |     | 0.00    |       |

    +----------------+---------------+------+-----+---------+-------+

    5 rows in set (0.01 sec)


    (jlive)[crashcourse]>SELECT * FROM orderitemsexpanded WHERE order_num = 20005;

    +-----------+---------+----------+------------+----------------+

    | order_num | prod_id | quantity | item_price | expanded_price |

    +-----------+---------+----------+------------+----------------+

    |     20005 | ANV01   |       10 |       5.99 |          59.90 |

    |     20005 | ANV02         3 |       9.99 |          29.97 |

    |     20005 | TNT2          5 |      10.00 |          50.00 |

    |     20005 | FB            1 |      10.00 |          10.00 |

    +-----------+---------+----------+------------+----------------+

     

    4 rows in set (0.00 sec)


    删除VIEW

    (jlive)[crashcourse]>DROP VIEW vendorlocations;

     

    Query OK, 0 rows affected (0.16 sec)


  • 相关阅读:
    Java实现 LeetCode 382 链表随机节点
    Java实现 LeetCode 382 链表随机节点
    Java实现 LeetCode 381 O(1) 时间插入、删除和获取随机元素
    Java实现 LeetCode 381 O(1) 时间插入、删除和获取随机元素
    Java实现 LeetCode 381 O(1) 时间插入、删除和获取随机元素
    Java实现 LeetCode 380 常数时间插入、删除和获取随机元素
    Java实现 LeetCode 380 常数时间插入、删除和获取随机元素
    Linux下的iwpriv(iwlist、iwconfig)的简单应用
    OCX控件的注册卸载,以及判断是否注册
    .OCX、.dll文件注册命令Regsvr32的使用
  • 原文地址:https://www.cnblogs.com/lixuebin/p/10814173.html
Copyright © 2011-2022 走看看