mysql創建視圖


什么是視圖

視圖是數據庫中的一個對象,它是數據庫管理系統提供給用戶的以多種角度觀察數據庫中數據的一種重要機制。 視圖不是數據庫中真實的表,而是一張虛擬表,其自身並不存儲數據。

使用視圖的優點

1.集中分散數據

2.簡化查詢語句

3.重用SQL語句

4.保護數據安全

5.共享所需數據

6.更改數據格式

 

CREATE TABLE customers(
cust_id int  NOT NULL AUTO_INCREMENT,
cust_name CHAR(50) NOT NULL ,
cust_sex CHAR(1) NOT NULL,
cust_address CHAR(50) NULL,
cust_contact CHAR(50) NULL,
PRIMARY KEY(cust_id)
);

insert INTO mysql_test.customers
VALUES(902,'李四','M','武漢','江夏');

insert INTO mysql_test.customers
VALUES(903,'王五','F','杭州','西湖');

select * from mysql_test.customers

use mysql_test
/**
創建視圖
*/
create view mysql_test.customers_view
as 
select * from mysql_test.customers
where cust_sex = 'm' with check option;

/**
顯示視圖
*/
show create view mysql_test.customers_view;


/**
修改視圖
*/

alter view mysql_test.customers_view
as 
select * from mysql_test.customers
where cust_sex = 'f' with check option;

/**
向視圖中插入數據
*/
insert into mysql_test.customers_view
values (909,'周明','f','武漢','洪山區')

update mysql_test.customers_view
set cust_address = '上海市'

delete  from mysql_test.customers_view where cust_name = '周明'

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM