Download presentation
Presentation is loading. Please wait.
1
SQL-Views and External Schemas
5/21/2019
2
Objectives To understand the idea of External Schemas and Logical Data Independence. To develop a means of expressing schemas. 5/21/2019
3
Appears to be a base relation (table) to the user.
‘The dynamic result of one or more relational operations operating on the base relations to produce another relation. A View is a ‘virtual relation’ that does not necessarily exist in the database but can be produced upon request by a particular user, at the time of the request.’ Connolly and Begg Appears to be a base relation (table) to the user. 5/21/2019
4
View Specification Nature of the mappings between external schemas and logical schema. External schemas can be derived from more than one table. View (external schema) may have additional constraints to maintain the database integrity. Normal user privileges apply at the level of a table. 5/21/2019
5
VIEWS CREATE VIEW salesman
AS SELECT name, number, salary, commission FROM employees WHERE job_title='salesman' ; 'salesman' can now be used in a similar manner to a base table i.e. queries can be carried out on the view as if it were an ordinary table. GRANT UPDATE ON salesman TO sales_manager; GRANT Privilege(s) ON View TO user_identity; (privileges for read and read/write are SELECT and UPDATE respectively) 5/21/2019
6
Need privilege to create views from a base table.
It is the Database Administrators responsibility to determine who needs access to the data. Determines if access restricted to table/columns/rows grant privileges. Need privilege to create views from a base table. 5/21/2019
7
Can also be used to provide more meaningful names to user.
CREATE VIEW counselling(student_name, student_number, counsellor_name, counsellor_number, region) AS SELECT student.name, student_id, staff.name, counsellor_no, student.region FROM student,staff WHERE counsellor_no=staff.no; Note that the order of the columns can be changed from the base table definition to modify the presentation of the data. 5/21/2019
8
The previous View definition is more powerful than it first appears.
e.g. if the base tables were changed in some way (columns renamed, or tables merged…..) what effect would there be on the users of the View?? Nothing- if the external definition of the View stays the same-users still ‘see’ a ‘table’ called counselling. Base tables are hidden CREATE VIEW counselling(student_name, student_number, counsellor_name, counsellor_number, region) AS SELECT student.name, student_id, staff.name, counsellor_no, region FROM merged_table This is Logical Data Independence in action. Base tables (Logical Schema) change-Views External Schema stays the same 5/21/2019
9
Derived Data CREATE VIEW regioncount(studentnos, region)
AS SELECT count(*), region, FROM student GROUP BY region; Note that derived values can also be provided through views. 5/21/2019
10
Views often used to simplify complex queries by producing an ‘intermediate’ result-
5/21/2019
11
The enrolment table student_id course_code tutor_no
s c s c s c s c s c s c s c s c s c s c s c s c NULL s c NULL s c NULL s c s c s c (17 row(s) affected) The enrolment table 5/21/2019
12
A list of the students that are enrolled with no duplicates?
select distinct student_id from enrolment student_id s01 s05 s07 s09 s10 s46 s57 Now, how many enrolled students are there (count the students rather than list them)? select distinct count(student_id) from enrolment 17 How many students are enrolled? 7 or 17?? 5/21/2019
13
There are 17 enrolments involving 7 students.
select distinct count(student_id) from enrolment Order of processing critically important. From-creates copy of table Count-counts the number of rows in student id column Select-creates a new table with the count result i.e. 17 Distinct-eliminates any duplicate rows in result. Result only has one row and one column containing the value 17. Not much scope for duplicates! 5/21/2019
14
select distinct student_id from enrolment Produce 7 rows?
Why does select distinct student_id from enrolment Produce 7 rows? Order of processing From-creates intermediate copy of enrolment table. Select-picks out the student_id column from the copy and creates an intermediate table with just this column. Distinct-eliminates duplicate values in single column table. Result- one column with 7 rows. So- how to count the number of students enrolled? 5/21/2019
15
(select distinct student_id from enrolment)
create view s as (select distinct student_id from enrolment) select count(*) from s 7 Not the only or necessarily desirable way of doing it – but illustrates how views can exploit ‘intermediate results. Use intermediate results to simplify queries. Possible to have views based on views. And views based on joins betweenv base tables and views. 5/21/2019
16
Resolution vs Materialisation
Resolution- holding the view definition and the query on the view for dynamic execution (i.e. at the time query on the view is made) Materialisation- Produce the result of the view as a temporary table in the database and execute queries on the view against that table. Has implications for currency of data. When data in base tables change (perhaps by a user other than that using the view) data in view should change. Otherwise data in view becomes out-of-date. If Materialisation used, temporary table must be updated automatically every time base tables updated. 5/21/2019
17
Update Rules In general, Views can only be updated
if the SELECT clause contains only column names i.e. no numeric/string expressions and must not use the distinct operator. the FROM clause includes only one table. the WHERE clause contains no sub-query. there is no GROUP BY clause. there is no HAVING clause. There needs to be a one-one correspondence to rows in the base table. 5/21/2019
18
Removing Views Views removed using DROP e.g. Drop View Regioncount;
Can specify CASCADE which removes all objects that depend on the View. This includes any Views that use the View. Views can be defined on Views. If RESTRICT specified the DROP command is rejected if there are any dependent objects. What if base tables on which Views depend are dropped? 5/21/2019
19
DROP TABLE removes a table definition and all data, indexes, triggers, constraints, and permission specifications for that table. Any view or stored procedure that references the dropped table must be explicitly dropped by using the DROP VIEW or DROP PROCEDURE statement. What happens to a view if a base table that it uses is dropped and re-created-perhaps with a different definition (e.g. data types of columns changed)? View re-bound to new base table and works with the new base table definition. 5/21/2019
20
MSACCESS Views MS_ACCESS 2000 uses the notion of Views. Tends to make them synonymous with Queries. Employs a Querybuilder with automated generation of SQL. 5/21/2019
21
SQL viewed through View/ShowPanes/SQL.
Tables to be employed are ‘dragged and dropped’ onto Querybuilder window. SQL viewed through View/ShowPanes/SQL. SQL statements created by the Query Designer can be edited directly. SQL statements can also be entered directly. 5/21/2019
22
The Views/Queries can then be used in same manner as base tables
From help/view……. The data displayed in a view is stored in the database tables. You can open a view and modify the data that appears inside it. When you modify the data you see in a view, you are actually changing data in the underlying database tables. Changes to data in those tables are automatically reflected in the views derived from them. 5/21/2019
23
Summary Views are the SQL mechanism for External Schemas
Produce a customised fragment of the logical schema. May contain derived data Can be used to control access to the database Produce Logical Data Independence 5/21/2019
24
Queries can then be carried out on the intermediate results.
Views can be used to simplify queries by making intermediate results seemingly persistent. Queries can then be carried out on the intermediate results. 5/21/2019
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.