create view sales_by_client as
select c.client_id,
c.name,
sum(invoice_total) as total_sales
from clients c
join invoices i using (client_id)
group by client_id, name;
select *
from sales_by_client
order by total_sales desc
create view see_balance as
select client_id,
name,
sum(invoice_total) - sum(payment_total) as balance
from invoices
join clients using (client_id)
group by client_id, name
drop view sales_by_client
create or replace view sales_by_client as
select c.client_id,
c.name,
sum(invoice_total) as total_sales
from clients c
join invoices i using (client_id)
group by client_id, name;
create or replace view invoices_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 - invoices.payment_total) > 0
delete from invoices_with_balance
where invoice_id = 1
update invoices_with_balance
set due_date = date_add(due_date, interval 2 day)
where invoice_id = 2
update invoices_with_balance
set payment_total = invoice_total
where invoice_id = 2
create or replace view invoices_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 - invoices.payment_total) > 0
with check option
update invoices_with_balance
set payment_total = invoice_total
where invoice_id = 3;
视图为数据库提供了一种抽象化,这种抽象化减少了变动带来的影响。
如果所有的查询都是基于视图,不会受基础表改动的影响