These notes provide a brief overview of views. For more details, raed CH.7 Using SQL Views in Kroenke (13th or 14th Edition).
SQL views are virtual tables constructed from other tables or views. A view has no data of its own, but obtains data from the other tables, and it only has a definition.
SELECT statements are used to define views, with nearly all SELECT statement options available. Views can then be used as if they were regular tables in other SELECT statements. Basic Syntax:
CREATE VIEW view_name AS
<SELECT STATEMENT>
Continuing our older Midn and Grades example, the following creates a view of those
students with F's in NL110. Views can be compared to tables created by subqueries.
Midn (Alpha, Last, First, Birthdate, SATM, SATV, HighSchool, HighSchoolCity, HighSchoolState, HighSchoolGraduate)
Grades (Alpha, AcYr, Sem, BlkNbr, Course, Title, Section, 6Week, 12Week, EndOfTerm, Exam, CourseGrade, Repeated)
CREATE VIEW nl110 AS
SELECT * FROM Grades
WHERE Course='NL110' AND CourseGrade='F';
SELECT * FROM nl110;
SQL Views are excellent for security purposes, as they can hide column information and table names to users and scripts using the database. Additionally they can be used to:
One of the greatest advantages to using views is the level of abstraction that they can offer. You can create a simple view such as:
CREATE VIEW GradesApp AS
SELECT * FROM Grades;
and then have all of your scripts and applications use GradesApp. We can now
alter the underlying tables used by an application without needing to change
the application code. Consider this single SQL statement, requiring no changes
to any code referencing our GradesApp view, to point an application to a newly
redesigned Grades table:
ALTER VIEW GradesApp AS
SELECT * FROM newGradesTable;
Depending on how the view was created we can update the underlying information via the view itself. We can directly update those views based on a single table with
Midn (Alpha, Last, First, Birthdate, SATM, SATV, HighSchool, HighSchoolCity, HighSchoolState, HighSchoolGraduate)
Grades (Alpha, AcYr, Sem, BlkNbr, Course, Title, Section, 6Week, 12Week, EndOfTerm, Exam, CourseGrade, Repeated)
Write the SQL needed to create two views
Midn (Alpha, Last, First, Birthdate, SATM, SATV)
GPA (Alpha, GPA)
Midn (Alpha, Last, First, Birthdate, SATM, SATV, HighSchool, HighSchoolCity, HighSchoolState, HighSchoolGraduate)
Grades (Alpha, AcYr, Sem, BlkNbr, Course, Title, Section, 6Week, 12Week, EndOfTerm, Exam, CourseGrade, Repeated)
GPA (Alpha, GPA)
For these problems you will need to refer to the SD321.all_sales table, which already has sales data populated for you (if you want to copy it to your own schema here you go!). Even though this table resides in a different schema than your mAlpha schema, you can create SQL Views in your mAlpha schema referring to it!
The schema for all_sales tables is given below:
all_sales(SaleID, EmployeeID, Sale_cents, Trans_Date)
Here is the code to create a view in your schema (mAlpha) pointing to the SD321.all_sales table and converting cents column (good for accuracy in storage) to dollars (good for humans to read)!
DROP VIEW IF EXISTS all_sales_dollars_view;
CREATE VIEW all_sales_dollars_view AS
SELECT SaleID, EmployeeID, ROUND(Sale_cents/100, 2) AS Sale_Dollars, Trans_Date
FROM SD321.all_sales;