標籤: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建立視圖