Example 1 - Create Horizontal View. Example 2 - Create Vertical View. Views. Views

Similar documents
Lecture 6 Structured Query Language (SQL)

Lecture 5 Data Definition Language (DDL)

Standard Query Language. SQL: Data Definition Transparencies

3ISY402 DATABASE SYSTEMS

Lecture 07. Spring 2018 Borough of Manhattan Community College

DB Creation with SQL DDL

Transforming ER to Relational Schema

ADVANCED QUERY AND VIEWS

STRUCTURED QUERY LANGUAGE (SQL)

RELATIONAL DATA MODEL

COMP102: Introduction to Databases, 5 & 6

CMP-3440 Database Systems

Lecture 03. Spring 2018 Borough of Manhattan Community College

Relational Data Model ( 관계형데이터모델 )

Chapter 6. SQL Data Manipulation

CMP-3440 Database Systems

Chapter 6. SQL: SubQueries

CS317 File and Database Systems

Chapter 1 SQL and Data

COSC 304 Introduction to Database Systems. Views and Security. Dr. Ramon Lawrence University of British Columbia Okanagan

OBJECTIVES. How to derive a set of relations from a conceptual data model. How to validate these relations using the technique of normalization.

Lecture 03. Fall 2017 Borough of Manhattan Community College

CS317 File and Database Systems

Chapter 4. The Relational Model

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Chapter 3. The Relational database design

Relational Data Structure and Concepts. Structured Query Language (Part 1) The Entity Integrity Rules. Relational Data Structure and Concepts

Physical Database Design

Views. COSC 304 Introduction to Database Systems. Views and Security. Creating Views. Views Example. Removing Views.

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Objective. The goal is to review material covered in Chapters 1-5. Do the following questions from the book.

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Conceptual Database Design

Slides by: Ms. Shree Jaswal

Lecture 08. Spring 2018 Borough of Manhattan Community College

Relational Model. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Solved MCQ on fundamental of DBMS. Set-1

Relational Algebra and Relational Calculus. Pearson Education Limited 1995,

Concepts of Database Management Seventh Edition. Chapter 4 The Relational Model 3: Advanced Topics

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

8) A top-to-bottom relationship among the items in a database is established by a

Database Technologies. Madalina CROITORU IUT Montpellier

CMP-3440 Database Systems

Database Management System 9

CSC 261/461 Database Systems Lecture 6. Fall 2017

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Logical Database Design. ICT285 Databases: Topic 06

Lab # 4. Data Definition Language (DDL)

Indexes (continued) Customer table with record numbers. Source: Concepts of Database Management

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

Cheltenham Courseware Microsoft Access 2003 Manual - Advanced Level SAMPLE

Database Technologies. Madalina CROITORU IUT Montpellier

Data Definition Language (DDL)

CS317 File and Database Systems

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

SQL STRUCTURED QUERY LANGUAGE

ECE 650 Systems Programming & Engineering. Spring 2018

Basic SQL. Basic SQL. Basic SQL

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

Database design process

Oracle Database 10g: Introduction to SQL

Structured Query Language (SQL)

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Where are we? Week -4: Data definition (Creation of the schema) Week -3: Data definition (Triggers) Week -1: Transactions and concurrency in ORACLE.

3.1. Keys: Super Key, Candidate Key, Primary Key, Alternate Key, Foreign Key

Data about data is database Select correct option: True False Partially True None of the Above

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

CMP-3440 Database Systems

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1


CS317 File and Database Systems

Oracle Syllabus Course code-r10605 SQL

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Top 88 Question Asked in Part I of MIS 150 EXAM #1 (Chapter 1-4, Appendix C) Exams questions gathered from old tests dating back to Fall 2000

SQL Interview Questions

The Relational Model

Introduction to Relational Databases. Introduction to Relational Databases cont: Introduction to Relational Databases cont: Relational Data structure

Basant Group of Institution

Mahathma Gandhi University

Database Systems. A Practical Approach to Design, Implementation, and Management. Database Systems. Thomas Connolly Carolyn Begg

Introduction to Database Systems CSE 444

CS317 File and Database Systems

Referential Integrity and Other Table Constraints Ray Lockwood

Sql Server Syllabus. Overview

CSE 530A. ER Model to Relational Schema. Washington University Fall 2013

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe


D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

2.2.2.Relational Database concept

Fundamentals, Design, and Implementation, 9/e Copyright 2004 Database Processing: Fundamentals, Design, and Implementation, 9/e by David M.

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

King Fahd University of Petroleum and Minerals

Transcription:

Views Views RECALLS: View Dynamic result of one or more relational operations operating on the base relations to produce another relation. o Virtual relation that does not actually exist in the database but is produced upon request, at time of request. o Contents of a view are defined as a query on one or more base relations. o Any operations on view are automatically translated into operations on relations from which it is derived. CREATE VIEW view_name [ (column_name [,...]) ] AS subselect.. Advanced SQL - 1 Advanced SQL - 2 Example 1 - Create Horizontal View CREATE VIEW manager3_staff AS SELECT * FROM staff WHERE bno = 'B3'; Example 2 - Create Vertical View CREATE VIEW staff3 AS SELECT sno, fname, lname, address, tel_no, position, sex FROM staff WHERE bno = 'B3'; Or: CREATE VIEW staff3 AS SELECT sno, fname, lname,address, tel_no, position, sex FROM manager3_staff; Advanced SQL - 3 Advanced SQL - 4

Example 2 - Create Vertical View SQL - DROP VIEW DROP VIEW view_name [RESTRICT CASCADE] o Causes definition of view to be deleted from the database. o For example: DROP VIEW manager3_staff; o With CASCADE, all related dependent objects are deleted; i.e. any views defined on view being dropped. o With RESTRICT (default), if any other objects depend for their existence on continued existence of view being dropped, command is rejected. Advanced SQL - 5 Advanced SQL - 6 Restrictions on Views Restrictions on Views o SQL-92 imposes several restrictions on creation and use of views. (a) If column in view is based on an aggregate function: Column may appear only in SELECT and ORDER BY clauses of queries that access view. Column may not be used in WHERE nor be an argument to an aggregate function in any query based on view. o For example, following query would fail: SELECT COUNT(cnt) FROM staff_prop_cnt; o Similarly, following query would also fail: SELECT * FROM staff_prop_cnt WHERE cnt > 2; Advanced SQL - 7 Advanced SQL - 8

Restrictions on Views View Updatability (b) Grouped view may never be joined with a base table or a view. o For example, Staff_Prop_Cnt view is a grouped view, so any attempt to join this view with another table or view fails. o Consider view Staff_Prop_Cnt: CREATE VIEW staff_prop_cnt (branch_no, staff_no, cnt) AS SELECT s.bno, s.sno, COUNT(*) FROM staff s, property_for_rent p WHERE s.sno = p.sno GROUP BY s.bno, s.sno; Advanced SQL - 9 Advanced SQL - 10 View Updatability o If change definition of view and replace count with actual property numbers: CREATE VIEW staff_prop_list (branch_no, staff_no, property_no) AS SELECT s.bno, s.sno, p.pno FROM staff s, property_for_rent p WHERE s.sno = p.sno; View Updatability o SQL-92 specifies the views that must be updatable in system that conforms to standard. o Definition given is that a view is updatable iff: DISTINCT is not specified. Every element in SELECT list of defining query is a column name and no column appears more than once. FROM clause specifies only one table, excluding any views based on a join, union, intersection or difference. Advanced SQL - 11 Advanced SQL - 12

View Updatability WHERE clause does not include any nested SELECTs that reference the table in FROM clause. There is no GROUP BY or HAVING clause in the defining query. o Also, every row added through view must not violate integrity constraints of base table. Integrity Enhancement Feature (IEF) We consider 5 types of integrity constraints: o Required data o Domain constraints o Entity integrity o Referential integrity o Enterprise constraints o For view to be updatable, DBMS must be able to trace any row or column back to its row or column in the source table. Advanced SQL - 13 Advanced SQL - 14 Integrity Enhancement Feature (IEF) 1. Required Data position VARCHAR(10) NOT NULL 2. Domain Constraints (a) CHECK sex CHARNOT NULL CHECK (sex IN ('M', 'F')) b) Using DOMAIN CREATE DOMAIN sex_type AS CHAR CHECK (VALUE IN ('M', 'F')); sex SEX_TYPE NOT NULL IEF - Entity Integrity 3. Entity Integrity o Primary key of a table must contain a unique, nonnull value for each row. o PRIMARY KEY clause can be specified only once per table. Can still ensure uniqueness for alternate keys using UNIQUE. 4. Referential Integrity o If foreign key contains a value, that value must refer to existing row in parent table. Advanced SQL - 15 Advanced SQL - 16

Example 3 - CREATE TABLE CREATE TABLE property_for_rent ( pno PROP_NUMBER NOT NULL, rooms PROPERTY_ROOMS NOT NULL DEFAULT 4, rent PROPERTY_RENT NOT NULL, ono OWNER_NUMBER NOT NULL, bno BRANCH_NUMBER NOT NULL, PRIMARY KEY (pno), FOREIGN KEY (ono) REFERENCES owner ON DELETE NO ACTION ON UPDATE CASCADE); IEF - Referential Integrity o Any INSERT/UPDATE that attempts to create FK value in child table without matching candidate key value in parent is rejected. o Action taken that attempts to update/delete a candidate key value in parent table with matching rows in child is dependent on referential action specified using ON UPDATE/ and ON DELETE subclauses: - CASCADE - SET NULL, - SET DEFAULT - NO ACTION. Advanced SQL - 17 Advanced SQL - 18 IEF - Referential Integrity IEF - Referential Integrity CASCADE: Delete row from parent and delete matching rows in child, and so on in cascading manner. SET NULL: Delete row from parent and set FK column(s) in child to NULL. Only valid if FK columns are NOT NULL. FOREIGN KEY (sno) REFERENCES staff ON DELETE SET NULL FOREIGN KEY (ono) REFERENCES owner ON UPDATE CASCADE SET DEFAULT: Delete row from parent and set each component of FK in child to specified default. Only valid if DEFAULT specified for FK columns NO ACTION: Reject delete from parent. Default. Advanced SQL - 19 Advanced SQL - 20

IEF - Enterprise Constraints Enterprise Constraints Example: CREATE TABLE property_for_rent( pno, address, sno VARCHAR(5) CONSTRAINT staff_not_handling_too_much CHECK (NOT EXISTS (SELECT sno FROM property_for_rent GROUP BY sno HAVING COUNT(*) > 10)), bno..); ALTER TABLE o Add a new column to a table. o Drop a column from a table. o Add a new table constraint. o Drop a table constraint. o Set a default for a column. o Drop a default for a column. Advanced SQL - 21 Advanced SQL - 22 Example 4 - ALTER TABLE ALTER TABLE staff ALTER position DROP DEFAULT; ALTER TABLE staff ALTER sex SET DEFAULT 'F'; ALTER TABLE property_for_rent DROP CONSTRAINT staff_not_handling_too_much; ALTER TABLE renter ADD pref_area VARCHAR(15); Advanced SQL - 23 Access Control - Privileges o Usually, every user has an associated password. o Actions user permitted to carry out on given base table or view: SELECT Retrieve data from a table. INSERT Insert new rows into a table. UPDATE Modify rows of data in a table. DELETE Delete rows of data from a table. REFERENCES Reference columns of named table in integrity constraints. USAGE Use domains, collations, character sets, and translations. Advanced SQL - 24

Example 5 - GRANT All Privileges Example 6 - GRANT Specific Privileges Give Manager full privileges to Staff table. GRANT ALL PRIVILEGES ON staff TO manager WITH GRANT OPTION; Give Admin SELECT and UPDATE on column Salary of Staff. GRANT SELECT, UPDATE (salary) ON staff TO admin; Advanced SQL - 25 Advanced SQL - 26 Example 7 - GRANT Specific Privileges to Multiple Users Example 8 - GRANT Specific Privileges to PUBLIC Give users Personnel and Deputy SELECT on Staff table. GRANT SELECT ON staff TO personnel, deputy; Give all users SELECT on Branch table. GRANT SELECT ON branch TO PUBLIC; Advanced SQL - 27 Advanced SQL - 28

Example 9 - REVOKE Specific Privileges from PUBLIC Example 10 - REVOKE Specific Privileges from Named User Revoke privilege SELECT on Branch table from all users. REVOKE SELECT ON branch FROM PUBLIC; Revoke all privileges given to Deputy on Staff table. REVOKE ALL PRIVILEGES ON staff FROM deputy; Advanced SQL - 29 Advanced SQL - 30