Assertions, Views, and Programming. CS157A Chris Pollett Oct. 31, 2005.

Similar documents
Chapter 9. MORE SQL: Assertions, Views, and Programming Techniques. Constraints as Assertions. Assertions: An Example

SQL: Advanced Queries, Assertions, Triggers, and Views. Copyright 2012 Ramez Elmasri and Shamkant B. Navathe

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

ITCS Implementation. Jing Yang 2010 Fall. Class 14: Introduction to SQL Programming Techniques (Ch13) Outline

SQL- Updates, Asser0ons and Views

Chapter 13 Introduction to SQL Programming Techniques

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

UNIT-V SQL Explain insert, delete and update statements in SQL with example.

More SQL: Complex Queries, Triggers, Views, and Schema Modification

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

Simple SQL Queries (contd.)

More SQL: Complex Queries, Triggers, Views, and Schema Modification

SQL. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

Slides by: Ms. Shree Jaswal

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

COSC344 Database Theory and Applications. Lecture 11 Triggers

Course 492 Supplementary Materials. Mutating Tables

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

SQL: Queries, Constraints, Triggers (iii)

More SQL: Complex Queries, Triggers, Views, and Schema Modification

COSC344 Database Theory and Applications. COSC344 Lecture 15 1

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

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

DATABASE DESIGN II - 1DL400

More Database Programming. CS157A Chris Pollett Nov. 2, 2005.

Ders # 7. Veri Bütünlüğü Programlama ve Güvenlik. From Elmasri/Navathe textbook Ch9,26 Sciore textbook, Ch 9-10

CT13 DATABASE MANAGEMENT SYSTEMS DEC 2015

Views, assertions, and triggers

Database Technology. Topic 6: Triggers and Stored Procedures

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

Database Management Systems Triggers

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

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

ECE 650 Systems Programming & Engineering. Spring 2018

Wentworth Institute of Technology COMP570 Database Applications Fall 2014 Derbinsky. SQL Programming. Lecture 8. SQL Programming

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

CS 338 Nested SQL Queries

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

Enhanced Data Models for Advanced Applications. Active Database Concepts

Outline. Note 1. CSIE30600 Database Systems More SQL 2

Data Modeling with the Entity Relationship Model. CS157A Chris Pollett Sept. 7, 2005.

CS430 Final March 14, 2005

Extension to SQL: View, Triggers, Transaction. SS Chung

DATABASDESIGN FÖR INGENJÖRER - 1DL124

Database Application Programs PL/SQL, Java and the Web

L6 Application Programming. Thibault Sellam Fall 2018

SQL-99: Schema Definition, Basic Constraints, and Queries. Create, drop, alter Features Added in SQL2 and SQL-99

Lab # 9. Java to Database Connection

Announcements. SQL: Part IV. Transactions. Summary of SQL features covered so far. Fine prints. SQL transactions. Reading assignments for this week

CS 338 Basic SQL Part II

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

Relational Database Management Systems for Epidemiologists: SQL Part II

Relational Databases

SQL STRUCTURED QUERY LANGUAGE

INF1383 -Bancos de Dados

ECE 650 Systems Programming & Engineering. Spring 2018

SEF DATABASE FOUNDATION ON ORACLE COURSE CURRICULUM

Overview. Database Application Development. SQL in Application Code. SQL in Application Code (cont.)

Database Application Development

Database Application Development

CS W Introduction to Databases Spring Computer Science Department Columbia University

SQL: Data Sub Language

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

1.8 Database and data Data Definition Language (DDL) and Data Manipulation Language (DML)

CSC 261/461 Database Systems Lecture 6. Fall 2017

School of Computing, Engineering and Information Sciences University of Northumbria. Set Operations

Relational Databases

Relational Database Systems Part 01. Karine Reis Ferreira

PHP Queries and HTML Forms Lecture 23

PHP: Cookies, Sessions, Databases. CS174. Chris Pollett. Sep 24, 2008.

CS2300: File Structures and Introduction to Database Systems

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Database Lab#9 Connecting to postgresql database from java programs

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

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

An Introduction to Structured Query Language

Overview Relational data model

Key Points. COSC 122 Computer Fluency. Databases. What is a database? Databases in the Real-World DBMS. Database System Approach

Database design process

An Introduction to Structured Query Language

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

Outline. CS 235: Introduction to Databases. DB Application Programming. Interface Solutions. Basic PSM Form. Persistent Stored Modules

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Basic SQL. Basic SQL. Basic SQL

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

The Relational Model. CS157A Chris Pollett Sept. 19, 2005.

An Introduction to Structured Query Language

CS5300 Database Systems

Ref1 for STUDENT RECORD DB: Ref2 for COMPANY DB:

Slides by: Ms. Shree Jaswal

5 Integrity Constraints and Triggers

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Database Programming. Week 9. *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

CSC 742 Database Management Systems

Database Application Development

COSC 304 Introduction to Database Systems. Advanced SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Transcription:

Assertions, Views, and Programming CS157A Chris Pollett Oct. 31, 2005.

Outline Assertions Views Database Programming

Assertions It is useful to be able to specify general constraints in SQL -- i.e., other than domain and key constraint. SQL allows you to create such constraints with CREATE ASSERTION: CREATE ASSERTION SAL_CONSTRAINT CHECK (NOT EXISTS (SELECT * FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D WHERE E.SALARY > M.SALARY AND E.DNO=D.DNUMBER AND D.MGRSSN=M.SSN); If a tuple in the database causes such an assertion to evaluate to false, the assertion is violated. If no violating tuple exists then the assertion is said to be satisfied.

Oracle and Assertions Oracle does not support CREATE ASSERTION. However, the same effect can largely be achieved using triggers: CREATE TRIGGER EMPLOYEE_TRIG BEFORE INSERT OR UPDATE OF SALARY ON EMPLOYEE -- Could also use AFTER rather than BEFORE FOR EACH ROW -- this means for each row to be inserted DECLARE MSAL INTEGER; BEGIN SELECT M.SALARY INTO MSAL FROM EMPLOYEE M, DEPARTMENT D WHERE :new.dno=d.dnumber AND D.MGRSSN=M.SSN IF (:new.salary > MSAL) THEN RAISE_APPLICATION_ERROR(-1234, Salary not allowed to be bigger than manager salary'); END IF; END; Notice :new and :old are special variables which refer to the tuple before or after the update.

Views Recall a view in SQL terminology is a single table that is derived from other tables. It is sometimes also called a virtual table. To create a view we use the syntax CREATE VIEW name AS query: CREATE VIEW WORKS_ON1 AS SELECT FNAME, LNAME, PNAME, HOURS FROM EMPLOYEE, PROJECT, WORKS_ON, WHERE SSN=ESSN AND PNO=PNUMBER; To delete a view we can use: DROP VIEW name;

View Implementation How are views handled by DBMS? Two possible approaches are: query modification -- queries to the view are rewritten in terms of the base tables. Can lead to complex queries which are slow to evaluate view materialization -- involves creating temporary view tables when the view is first queried and keeping this for future queries. If the view is not queried for a certain length of time it is deleted. For this to work need an efficient way to update a view when the base tables change.

View Update What should be done if someone wants to do an insert or an update on a view? If the view is on a single table, contains a primary key of that table, has all other attributes which are not null constraints, and does not involve aggregates, one can map the update on the view to the base table. Views on multiple tables or involving aggregates in general cannot be updated One can add the WITH CHECK OPTION to a CREATE VIEW declaration to indicate this view will be updated.

Database Programming We now begin discussing how to write canned transaction, i.e., database applications, which can be used by naïve users with little knowledge of how databases work. Such application are often written so as to be accessible via a web interface.

Approaches to Database Programming There are several different approaches to database application development: Embedding database commands in a general-purpose programming language. A precompiler is then run on the source code to convert this code to code that makes function calls to the DBMS API library. Ex: proc or sqlj Using a library of database functions. Here a library of functions is made available to the host programming language for database calls. This library might have functions calls for performing insert, updates, delete, queries, etc. Also known as using a database Application Programming Interface (API). Ex:Oracle OCI calls. Design a brand new language. A database programming languages might be designed from scratch to handle the the database model and query language. Additional operations like looping, etc are added to make it Turing complete. Ex: PL/SQL

Impedance Mismatch One problem with the first two approached is impedance mismatch. Impedance Mismatch refers to the problems which occur because of differences between the database model and the programming language model. For example, databases deal with attribute, tuple, and tables. These have to be mapped to types in the host language. Not all host languages support the same types. So will need a different mapping for C, JAVA, etc. Also, the output of SQL queries are sets or multisets. So one need to set up a host language specific way to iterate over this kind of output.

Typical Sequence of Interaction in Database Programming Our database application will typically be written as a client program in a client server model. A common sequence of interactions is that: The client program establishes a connection with the database server. Once the connection is established, the program submits queries updates, or other database commands. When the program is done, it should terminate or close the connection.

Embedded SQL, SQLJ, and JDBC Example See CS157b Fall 2003 HW2