mysql创建全外连接的视图_关系型数据库 MySQL 表索引和视图详解
原創: JiekeXu JiekeXu之路
一、索引
數據庫索引通俗的講就是和書本的目錄一樣,主要就是為了提高查詢數據的效率。由于數據存儲在數據庫表中,所以索引是創建在數據庫表對象上,由表中的一個字段或多個字段生成的鍵組成,這些鍵存儲在數據結構(B-樹或 hash 表)中,通過 MySQL 可以快速有效地查找與鍵值相關聯的字段。根據索引的存儲類型,可以將索引分為B型樹索引(BTREE)和哈希索引(HASH)。 MySQL 5.5.21 版本中支持的索引有 6 種,分別為普通索引、唯一索引、全文索引、單列索引、多列索引和空間索引。
1、 創建和查看索引
1> 創建和查看普通索引
(1)創建表時創建普通索引
eg:create table tab_name(L1 數據類型1,L2 數據類型2,L3 數據類型3,……L4 數據類型4,index|key 索引名(列名 長度 ASC|DESC));為了查看是否創建成功,使用以下語句查看一下;
show create table tab_nameG為了校驗表中索引是否被使用,需執行 explain 語句:
EXPLAIN select * from tab_name where deptno=1G;(2)在已經存在的表上創建普通索引
create index 索引名 on 表名(列名 長度 ASC|DESC);eg:create index index_deptno on t_dept(deptno);(3)通過 alter table 創建普通索引
alter table tab_name add index|key 索引名(列名 長度 ASC|DESC);eg:alter table t_dept add index index_deptno(deptno);2> 創建和查看唯一索引
(1)創建表時創建唯一索引
eg:create table tab_name(L1 數據類型1,L2 數據類型2,L3 數據類型3,……L4 數據類型4,unique index|key 索引名(列名 長度 ASC|DESC));為了查看是否創建成功,使用以下語句查看一下;
show create table tab_nameG為了校驗表中索引是否被使用,需執行 explain 語句:
EXPLAIN select * from tab_name where deptno=10G;(2)在已經存在的表上創建唯一索引
create unique index 索引名 on 表名(列名 長度 ASC|DESC);eg:create unique index index_deptno on t_dept(deptno);(3)通過alter table 創建唯一索引
alter table tab_name add unique index|key 索引名(列名 長度 ASC|DESC);eg:alter table t_dept add unique index index_deptno(deptno);3> 創建和查看全文索引
(1)創建表時創建全文索引
eg:create table tab_name(L1 數據類型1,L2 數據類型2,L3 數據類型3,……L4 數據類型4,fulltext index|key 索引名(列名 長度 ASC|DESC));為了查看是否創建成功,使用以下語句查看一下;
show create table tab_nameG為了校驗表中索引是否被使用,需執行explain語句:
EXPLAIN select * from tab_name where deptno=10G;(2)在已經存在的表上創建全文索引
create fulltext index 索引名 on 表名(列名 長度 ASC|DESC);eg:create fulltext index index_deptno on t_dept(deptno);(3)通過alter table 創建唯一索引
alter table tab_name add fulltext index|key 索引名(列名 長度 ASC|DESC);eg:alter table t_dept add fulltext index index_deptno(deptno);4> 創建和查看多列索引
(1)創建表時創建多列索引
eg:create table tab_name(L1 數據類型1,L2 數據類型2,L3 數據類型3,……L4 數據類型4,index|key 索引名(列名1 長度 ASC|DESC, 列名2 長度 ASC|DESC, …… 列名n 長度 ASC|DESC));eg:create table t_dept( deptno int, dnmae varchar(20), loc varchar(40), key index_dname_loc(dname,loc));為了查看是否創建成功,使用以下語句查看一下;
show create table t_deptG為了校驗表中索引是否被使用,需執行 explain 語句:
EXPLAIN select * from t_dept where deptno=10G;(2)在已經存在的表上創建多列索引
create index 索引名 on 表名(列名1 長度 ASC|DESC, 列名n 長度 ASC|DESC );eg:create index index_dname_loc on t_dept(dname,loc);(3)通過 alter table 創建多列索引
alter table tab_name add index|key 索引名(列名1 長度 ASC|DESC, 列名n 長度 ASC|DESC);eg:alter table t_dept add index index_dname_loc(dname,loc);2、 刪除索引
之所以要刪除索引,是由于有些索引會降低表的更新速度,影響數據庫的性能。
刪除索引語法如下:
drop index index_name on tab_name;二、視圖
視圖:本身就是一種虛擬表,其內容與真實表類似,包含一系列帶有名稱的列和行數據。視圖并不在數據庫中以存儲數據值的形式存在。行和列數據來定義視圖的查詢所引用基本表,并且在具體引用視圖時動態生成。
視圖的特點:
- 視圖的列可以來自于不同的表,是表的抽象在邏輯意義上建立的新關系;
- 視圖是由基本表(實表)產生的表(虛表);
- 視圖的建立和刪除不影響基本表;
- 對視圖內容的更新(添加、刪除、修改)直接影響基本表;
- 當視圖來自多個基本表時,不允許添加和刪除數據。
1、創建視圖
視圖被看成是一種虛擬表,在物理上是不存在的,即數據庫管理系統沒有專門的位置為視圖存儲數據。
1>創建視圖的語法為:
create view as select 列1,列2,列3 from tab_name;eg:create view view_selectproduct as select id,name from t_product;2>查詢視圖:
select * from view_selectproduct;3>創建各種視圖:
(1)封裝實現查詢常量語句的視圖,即常量視圖,語句如下:
create view view_test1 as select 3.1415926;(2)封裝使用聚合函數(sum、min、max、count 等)查詢語句的視圖,語句如下:
create view view_test2 as select count(name) from t_student;(3)封裝了實現排序功能(order by )查詢語句的視圖,語句如下:
create view view_test3 as select name from t_student order by id desc;(4)封裝了實現表內連接查詢語句的視圖,語句如下:
create view view_test4 as select s.name from t_student as s,t_group as g where s.group_id=g_id and g.id=2;(5)封裝了實現外連接( left join 和 right join)查詢語句的視圖,語句如下:
create view view_test5 as select s.name from t_student as s left join t_group as g on s.group_id=g.id where g.id=2;(6)封裝了實現子查詢相關查詢語句的視圖,語句如下:
create view view_test6 as select s.name from t_student as s where s.group_id in (select id from t_group);(7)封裝了實現記錄聯合( union 和 union all )查詢語句的視圖,語句如下:
create view view_test7 as select id,name from t_student union all select id,name from t_group;2、查看視圖
創建完視圖后,需要查看視圖信息,MySQL5.5 提供了 3 種方法:
*show tables、show table status、show create view;*
(1)show tables 查看視圖名;
use view;show tables;(2)show table status 查看視圖詳細信息;
語法:
show table status from db_name [like 'pattern'];eg:查看view數據庫里所有的表和視圖的詳細信息:
show table status from view G(3)show create view 語句查看視圖定義信息:
show create view view_selectproduct G(4) describe|desc 語句查看視圖設計信息:
desc view_name; #和describe view_name效果一樣;(5)通過系統表查看視圖信息:
當 MySQL 安裝成功后,系統會自動創建一個名為 ==information_schema== 的系統數據庫,該庫中包含了視圖信息的表格,可以通過查看表==views==來查看所有視圖的信息。
use infomation_schema;select * from views where table_name='view_selectproduct'G3、刪除視圖
drop view view_name[,view_name2,view_name3];4、修改視圖
(1)Crete or replace view語句來修改視圖:
對于已經建好的視圖,尤其是已經存在大量的數據視圖,可以先刪除在創建視圖:
eg:drop view view_selectproduct; create view view_selectproduct as select name from t_product;不過這樣看起來有點麻煩。所以使用如下語句:
create or replace view view_selectproduct as select name from t_product;查看視圖,已經改過來了:
select * from view_selectproduct;(2)alter 語句修改視圖:
alter view view_name as select name from t_product;5、利用視圖操作基本表
(1)檢索(查詢)語句
select * from view_selectproduct;(2)利用視圖操作基本表數據
視圖是一種虛表,對視圖的操作就是對表的操作,但要注意兩點就是:
- 對視圖數據進行添加、刪除直接影響基本表;
- 視圖來源于多個基本表時,不允許添加或刪除數據;
1、添加數據:
insert into view_product(id,name,price,order_id) values(11,'PEAR4',12.3,2);2、刪除數據
delete from view_product where name='apple1';3、更新數據
update view_product set price=3.5 where name='pear1';最后,我自己是一名從事了多年開發的Java老程序員,辭職目前在做自己的Java私人定制課程,今年年初我花了一個月整理了一份最適合2019年學習的Java學習干貨,可以送給每一位喜歡Java的小伙伴,想要獲取的可以關注我的頭條號并在后臺私信我:01,即可免費獲取。
總結
以上是生活随笔為你收集整理的mysql创建全外连接的视图_关系型数据库 MySQL 表索引和视图详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在ArcMap中直接加载谷歌地球影像的方
- 下一篇: datagrid显示mysql_WPF