Views Flashcards

1
Q

what is the syntax for creating a view in sql?

A

create view

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False
The CREATE VIEW clause does not return a query?

A

True
It creates a what can be described as a common query that gets used often

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

True or False
VIEWS in sql can be treated and queried much like tables themselves

A

True
In the FROM clause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

True or False
VIEW clause store actually data like the table itself

A

False
VIEW don’t store data, if the table data changes the VIEW gets updated. VIEW is a virtual table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

VIEW exercise

A

create view ‘Client 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the SUM clause performing in this query?

SUM(invoice_total - payment_total) as balance

A

summing the balance for each group (group by) then summing the entire table column

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write the syntax to drop a view

A

drop view sales_by_client

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which is better to drop or replace a view?

A

replace view clause can be used to update a view over recreating a complex view from scratch.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the syntax for updating a view?

A

CREATE OR REPLACE VIEW ‘view_name’ AS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

True or False
It’s a good policy to save views for future use in source control much like GitHub

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why can’t you use an alias in the WHERE clause?

A

because the alias is not an actual table, must use the calculation that generated the alias.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

True or False
a VIEW with DISTINCT, Agg Funcs, GROUP BY /
HAVING, UNION are not updatable (in mysql)

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

True or False
You can delete records from view much like a table

DELETE from invoice_with_balance
WHERE invoice_id = 1

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

True or False
Sometimes altering a VIEW will remove rows from that VIEW

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you prevent rows from being removed in a VIEW update?

A

WITH OPTION CHECK clause
prevents row from being removed or deleted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are some benefits of views?

A

simplify queries,
reduce the impact of changes,
restrict access to data (actual sql tables)