More on Views (3.6—3.7) Author: Dr. Adam Anthony For text: Database Management Systems (Ramakrishnan and Gherke)

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

Review Session ER and Relational –ER  Relational –Constraints, Weak Entities, Aggregation, ISA Relational Algebra  Relational Calculus –Selections/Projections/Joins/Division.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke. Edited by Keith Shomper, The Relational Model Chapter 3.
CSC 411/511: DBMS Design 1 1 Dr. Nan WangCSC411_L3_Relational Model 1 The Relational Model (Chapter 3)
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Mirsad Hadzikadic.
Database Security by Muhammad Waheed Aslam SIS Project Leader ITC/KFUPM.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
1 Query Languages: How to build or interrogate a relational database Structured Query Language (SQL)
The Relational Model 198:541 Rutgers University. Why Study the Relational Model?  Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle,
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 The Relational Model Chapter 3. 2 Objectives  Representing data using the relational model.  Expressing integrity constraints on data.  Creating,
The Relational Model These slides are based on the slides of your text book.
The Relational Model Chapter 3
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3 Modified by Donghui Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
 Relational database: a set of relations.  Relation: made up of 2 parts: › Instance : a table, with rows and columns. #rows = cardinality, #fields =
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
1 Database Systems ( 資料庫系統 ) October 4, 2005 Lecture #3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
The Relational Model1 ER-to-Relational Mapping and Views.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 Database Systems ( 資料庫系統 ) October 4/5, 2006 Lecture #3.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Lecture 3 Book Chapter 3 (part 2 ) From ER to Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 3 The Relational Model.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
CMPT 258 Database Systems Final Exam Review.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Jianping Fan.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #5 (E-R  Relational, Cont.) Instructor: Chen Li.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Xin Zhang.
1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
COP Introduction to Database Structures
These slides are based on the slides of your text book.
Translation of ER-diagram into Relational Schema
Database Applications (15-415) The Relational Model Lecture 3, August 28, 2016 Mohammad Hammoud.
Database Applications (15-415) The Relational Model Lecture 4, January 16, 2018 Mohammad Hammoud.
Database Systems (資料庫系統)
The Relational Model Chapter 3
The Relational Model Relational Data Model
CS 405G: Introduction to Database Systems
The Relational Model Chapter 3
Database Systems (資料庫系統)
The Relational Model Chapter 3
Views 1.
The Relational Model Chapter 3
Presentation transcript:

More on Views (3.6—3.7) Author: Dr. Adam Anthony For text: Database Management Systems (Ramakrishnan and Gherke)

Understanding Queries SELECT * FROM Students S WHERE S.age=18 What is the difference?

View Basics Views, once defined, may be treated just like a table No explicit values Data comes from other tables Defined by queries Indirect consequence: Changing the queried table(s) changes the view! CREATE VIEW YoungActiveStudents (name, grade) AS SELECT S.name, E.grade FROM Students S, Enrolled E WHERE S.sid = E.sid and S.age<21

Why Views? Hiding Complexity Simplifying the programming interface Security (Possibly) Convenient updates, insertions, deletions Domain-restricted insertions

Hiding Complexity CREATE VIEW red_green_sailors (sailor_id) AS SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ AND S.sid IN ( SELECT S2.sid FROM Sailors S2, Boats B2, Reserves R2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=‘green’) SELECT * FROM red_green_sailors When is this useful? Frequently used queries Simplifying even BIGGER queries Secret Queries When is this useful? Frequently used queries Simplifying even BIGGER queries Secret Queries

Views and Application Programming Conceptual Schema: the actual database tables and how they interact External Schema: What an application programmer expects to see from the DB Naïve external schema: provide application programmer with details of conceptual schema – Disallows changing the schema at a later date! Better choice: implement external schema as views, and maintain them as the conceptual schema changes

Views and Security Most DBMS’s provide some means of access control – Given a username, privileges to data can be restricted General approach: restrict tables, allow views Student table—only available to administrators Student_Accounts: For helpdesk employees

Updatable views A view is updatable if*: – The query that builds it only selects columns, and does not create any calculated columns (not always true) – The query uses data from a table (usually true) – The update/insert/delete only affects a single base table *Rules taken from MSDN online documentation:

Updating View Rows 1.View is created, and shown below 2.View uses two base tables 3.The update must only SET fields from a single base table 4.The WHERE portion of the update may condition on other fields CREATE VIEW StudentGrades (name, cid, grade) AS SELECT S.name, E.cid, E.grade FROM Students S, Enrolled E WHERE S.sid = E.sid UPDATE StudentGrades SET grade = “I” WHERE SG.name = “Jones”

Inserting and Deleting View Rows Deleting: view may only be defined over one table Insertion: Will work, sort of: – Still may only affect one of the base tables Insertion will result in null for view fields from other tables – Any base table fields not in view will be set to NULL If primary key of base not in view, this will fail!

Restricting Domains With Views Views depend on underlying data, but are loaded dynamically When inserting: – If inserted values don’t match query, they won’t be visible in the view! WITH CHECK OPTION: – Reject any insertions that won’t be visible in the view Can be used for limited domain restriction on fields CREATE VIEW ProbationaryStudents (name, grade) AS SELECT S.name, E.grade FROM Students S, Enrolled E WHERE S.sid = E.sid and E.grade<2.0 WITH CHECK OPTION

Other Odds and Ends

More on Defaults With foreign keys, we have the SET DEFAULT option: FOREIGN KEY sid REFERENCES students ON DELETE SET DEFAULT What is the default value? – We set it on table creation: CREATE TABLE students( sid INTEGER DEFAULT , …

Translating Aggregates create table projects(…) create table departments (…) create table employees (… ) CREATE TABLE SPONSORS( since : datetime, pid : char(10), did: char(10), primary key (pid,did) foreign key pid references projects foreign key did references departments ); CREATE TABLE MONITORS( until : datetime, pid: char(10), did: char(10), ssn: chare(11), primary key (pid,did,ssn), foreign key (pid,did) references sponsors foreigh key ssn references employee); budget did pid started_on pbudget dname until Departments Projects Sponsors Employees Monitors lot name ssn since