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