MySQL建立視圖

來源:互聯網
上載者:User

標籤:sel   int   char   other   一個   nbsp   incr   arch   配置   

(1).第一類:

create view v as select * from table;

(2).第二類:

create view v as select id,name,age from table;

(3).第三類:

create view v[vid,vname,vage] as select id,name,age from table;

 

如果涉及到資料量過大,商務邏輯需要跨表查詢等等也可建立一個跨表的視圖,有三種情況:

一、基於同一資料庫

這種情況較為簡單,只需簡單的一行sql命令,如下:

create view v as (select * from table1) union all (select * from table2);

二、基於不同資料庫

這種情況只比上面的sql語句多一個資料庫的名字,如下:

create view 資料庫1.v as (select * from 資料庫1.table1) union all (select * from 資料庫2.table2);

create view 資料庫2.v as (select * from 資料庫1.table1) union all (select * from 資料庫2.table2);

如果執行第一個sql將在資料庫1下建立視圖,反之亦然;

三、基於不同伺服器

這種情況稍微麻煩一點,需要先建立一個遠端資料表來訪問遠程伺服器的資料表,然後再對這個剛建立的遠端資料表和本地表進行視圖,步驟如下:

1、查看MySql是否支援federated引擎

    (1).登入Mysql;    

    (2).mysql>show engines;

    (3).如果顯示為no,在設定檔中添加:federated (在my.ini),重新啟動mysql服務。

2、建立遠端資料表

     mysql>CREATE TABLE federated_table (                   id  INT(20)  NOT NULL AUTO_INCREMENT,                    name  VARCHAR(32) NOT NULL DEFAULT ‘‘,                    other  INT(20) NOT NULL DEFAULT ‘0‘,                     PRIMARY KEY  (id)                 )                 ENGINE=FEDERATEDDEFAULT                 CHARSET=utf8                 CONNECTION=‘mysql://[email protected]_host:3306/federated/test_table‘;

CONNECTION可以按如下方式進行配置:

  (1).CONNECTION=‘mysql://username:[email protected]:port/database/tablename‘  (2).CONNECTION=‘mysql://[email protected]/database/tablename‘  (3).CONNECTION=‘mysql://username:[email protected]/database/tablename‘

3、建立視圖

create view 本機資料庫.v as (select * from 本機資料庫.table1) union all (select * from 遠端資料庫.test_table);

MySQL建立視圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.