MySql教程

mysql day7 第八章 创建视图

本文主要是介绍mysql day7 第八章 创建视图,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
-- 第八章 1-创建视图
-- 视图不存储数据,数据存在表中
-- use sql_invoicing;
-- create view sales_by_client as
-- select
-- 		c.client_id,
-- 		c.name,
-- 		sum(invoice_total) as total_sale
-- from clients c
-- join invoices i using (client_id)
-- group by client_id,name

-- 习题
-- create view clients_balance as
-- select
-- 		c.client_id,
-- 		c.name,
-- 		sum(invoice_total - payment_total) as balance
-- from clients c
-- join invoices i using (client_id)
-- group by client_id,name

-- 更改或删除视图
-- drop view clients_balance
-- create or replace view clients_balance as ...不需要删除视图

-- 如果视图中没有distinct,aggregate function(min,max,sub...),group by,having,union则说明视图是可更新的
-- create or replace view invoice_with_balance as
-- select 
-- 		invoice_id,
-- 		number,
-- 		client_id,
-- 		invoice_total,
-- 		payment_total,
-- 		invoice_total - payment_total as balance,
-- 		invoice_date,
-- 		due_date,
-- 		payment_date
-- from invoices
-- where (invoice_total - payment_total) > 0

-- delete from invoice_with_balance
-- where invoice_id = 1

-- update invoice_with_balance
-- set due_date = date_add(due_date,interval 2 day)
-- where invoice_id = 2 -- 更新2号发票的到期日

-- with option check -- 产生错误提示
-- 在创建视图的末尾加上会防止update和delete语句将行从视图中删除,

这篇关于mysql day7 第八章 创建视图的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!