SQL-Views and External Schemas

Slides:



Advertisements
Similar presentations
Access Control & Views Reading: C&B, Chap 7. Dept of Computing Science, University of Aberdeen2 In this lecture you will learn the principles of object.
Advertisements

Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Virtual training week 4 structured query language (SQL)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Chapter 3 The Relational Model Transparencies © Pearson Education Limited 1995, 2005.
Chapter 3. 2 Chapter 3 - Objectives Terminology of relational model. Terminology of relational model. How tables are used to represent data. How tables.
Concepts of Database Management Sixth Edition
Database Systems More SQL Database Design -- More SQL1.
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
View and Materialized view. What is a view? Logically represents subset of data from one or more table. In sql, a view is a virtual relation based on.
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
Security and Integrity
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Chapter 4 The Relational Model Pearson Education © 2014.
Views: Limiting Access to Data A view is a named select statement that is stored in a database as an object. It allows you to view a subset of rows or.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
Chapter 6 Database Administration
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
Views In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) In some.
The Relational Model Pertemuan 03 Matakuliah: M0564 /Pengantar Sistem Basis Data Tahun : 2008.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
The Relational Model. 2 Relational Model Terminology u A relation is a table with columns and rows. –Only applies to logical structure of the database,
Chapter 4 Indexes. Indexes Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage; composed.
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
Chapter 4 The Relational Model Pearson Education © 2009.
 CONACT UC:  Magnific training   
SQL Basics Review Reviewing what we’ve learned so far…….
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL Query Getting to the data ……..
“Introduction To Database and SQL”
Implementing Views Advanced Database Dr. AlaaEddin Almabhouh.
國立臺北科技大學 課程:資料庫系統 Chapter 7 SQL Data Definition.
TABLES AND INDEXES Ashima Wadhwa.
Managing Privileges.
Creating Database Triggers
Chapter 6 - Database Implementation and Use
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
The Relational Database Model
“Introduction To Database and SQL”
SQL Views CS542.
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
Advanced SQL: Views & Triggers
Chapter 4 Indexes.
CH 4 Indexes.
Chapter 2 Views.
The Relational Model Transparencies
A Guide to SQL, Eighth Edition
Session #, Speaker Name Views 1/2/2019.
CH 4 Indexes.
SQL .. An overview lecture3.
Chapter 2 Views.
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
Structured Query Language (3)
Chapter 8 Advanced SQL.
Views 1.
Chapter 4 The Relational Model Pearson Education © 2009.
IST 318 Database Administration
Indexes and more Table Creation
So What are Views and Triggers anyway?
Integrity 5/5/2019 See scm-intranet.
SQL Views Presented by: Dr. Samir Tartir
Views Base Relation View
Presentation transcript:

SQL-Views and External Schemas 5/21/2019

Objectives To understand the idea of  External Schemas and  Logical Data Independence. To develop a means of expressing schemas. 5/21/2019

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

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

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

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

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

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

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

Views often used to simplify complex queries by producing an ‘intermediate’ result- 5/21/2019

The enrolment table student_id course_code tutor_no ---------- ----------- -------- s01 c4 3158 s05 c2 5324 s05 c7 5324 s07 c4 3158 s09 c1 3158 s09 c2 8431 s09 c3 8431 s09 c4 5324 s09 c7 5324 s10 c4 5324 s10 c7 5324 s46 c2 NULL s46 c3 NULL s46 c4 NULL s57 c2 5324 s57 c3 8431 s57 c4 5324 (17 row(s) affected) The enrolment table 5/21/2019

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

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

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

(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

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

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

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

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

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

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

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

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

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