Unit I-2.

Slides:



Advertisements
Similar presentations
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity (and Security) Domain Constraints Referential Integrity Assertions Triggers.
Advertisements

Introduction to Structured Query Language (SQL)
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Authorization Forms of authorization on parts of the database: Read - allows.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all tuples from the loan relation. delete.
A Free sample background from © 2006 By Default! R.SARAVANA KUMAR S.NAVEEN Relational Database Model.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Constraints, Triggers and Index James Wang.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Advanced SQL: Cursors & Stored Procedures
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Triggers. Why Triggers ? Suppose a warehouse wishes to maintain a minimum inventory of each item. Number of items kept in items table Items(name, number,...)
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Integrity Constraints. 421B: Database Systems - Integrity Constraints 2 Integrity Constraints (Review) q An IC describes conditions that every legal.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Oracle 11g: SQL Chapter 4 Constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for.
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
Chapter 5-2: Advanced SQL. 2 Join Expressions Views Integrity Constraints SQL Data Types and Schemas Authorization Accessing SQL From a Programming Language.
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Triggers Chapter 5.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Database System Concepts, 5th Ed. ©Sang Ho Lee Chapter 8: Application Design and Development.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module 7: Advanced SQL.
1. Advanced SQL Functions Procedural Constructs Triggers.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
1st Semester, 2017 Sanghyun Park
Fundamental of Database Systems
Introduction to SQL Programming Techniques
Chapter 6: Integrity (and Security)
Creating Database Triggers
Chapter 6 - Database Implementation and Use
Chapter 5: Advanced SQL.
Introduction to Relational Model
Active Database Concepts
Chapter 2: Intro to Relational Model
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Database Construction (and Usage)
Chapter 3: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 5: Advanced SQL.
Chapter 2: Intro to Relational Model
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Chapter 4 The Relational Model Pearson Education © 2009.
Triggers A trigger is a statement that is executed automatically by the system as a side effect of a modification to the database. To design a trigger.
Databases and Information Management
Chapter 2: Intro to Relational Model
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Chapter 2: Intro to Relational Model
Design tools and techniques for a relational database system
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Prof. Arfaoui. COM390 Chapter 9
-Transactions in SQL -Constraints and Triggers
Presentation transcript:

Unit I-2

Contents Referential Integrity Views Keys Embedded SQL Index

Referential Integrity Rules [ Follows Which Codd’s Rule ?? ] A referential integrity is a rule defined on a key (a column or set of columns) in one table that guarantees that the values in that key match the values in a key in a related table (the referenced value). Referential integrity constraints can specify particular actions to be performed on the dependent rows in a child table if a referenced parent key value is modified. The referential actions supported by the FOREIGN KEY integrity constraints of Oracle are UPDATE and DELETE NO ACTION, and DELETE CASCADE.

Issued Against Parent Table Issued Against Child Table Referential integrity also includes the rules that dictate what types of data manipulation are allowed on referenced values and how these actions affect dependent values. The rules associated with referential integrity are: DML Statement Issued Against Parent Table Issued Against Child Table INSERT Always OK if the parent key value is unique. OK only if the foreign key value exists in the parent key or is partially or all null. UPDATE No Action Allowed if the statement does not leave any rows in the child table without a referenced parent key value. Allowed if the new foreign key value still references a referenced key value. DELETE No Action Allowed if no rows in the child table reference the parent key value. Always OK. DELETE Cascade DELETE Set Null

Views [ Which Codd’s Rule ]

View

Keys Let K  R K is a superkey of R if values for K are sufficient to identify a unique tuple of each possible relation r(R) Example: {ID} and {ID,name} are both superkeys of instructor. Superkey K is a candidate key if K is minimal Example: {ID} is a candidate key for Instructor One of the candidate keys is selected to be the primary key. which one? Foreign key constraint: Value in one relation must appear in another Referencing relation Referenced relation Example – dept_name in instructor is a foreign key from instructor referencing department

Embedded SQL The SQL standard defines embeddings of SQL in a variety of programming languages such as C, C++, Java, Fortran, and PL/1, A language to which SQL queries are embedded is referred to as a host language, and the SQL structures permitted in the host language comprise embedded SQL. The basic form of these languages follows that of the System R embedding of SQL into PL/1. EXEC SQL statement is used to identify embedded SQL request to the preprocessor EXEC SQL <embedded SQL statement >; Note: this varies by language: In some languages, like COBOL, the semicolon is replaced with END-EXEC In Java embedding uses # SQL { …. };

Embedded SQL (Cont.) Before executing any SQL statements, the program must first connect to the database. This is done using: EXEC-SQL connect to server user user-name using password; Here, server identifies the server to which a connection is to be established. Variables of the host language can be used within embedded SQL statements. They are preceded by a colon (:) to distinguish from SQL variables (e.g., :credit_amount ) Variables used as above must be declared within DECLARE section, as illustrated below. The syntax for declaring the variables, however, follows the usual host language syntax. EXEC-SQL BEGIN DECLARE SECTION} int credit-amount ; EXEC-SQL END DECLARE SECTION;

Embedded SQL (Cont.) To write an embedded SQL query, we use the declare c cursor for <SQL query> statement. The variable c is used to identify the query Example: From within a host language, find the ID and name of students who have completed more than the number of credits stored in variable credit_amount in the host langue Specify the query in SQL as follows: EXEC SQL declare c cursor for select ID, name from student where tot_cred > :credit_amount END_EXEC

Embedded SQL (Cont.) Example: Specify the query in SQL as follows: From within a host language, find the ID and name of students who have completed more than the number of credits stored in variable credit_amount in the host langue Specify the query in SQL as follows: EXEC SQL declare c cursor for select ID, name from student where tot_cred > :credit_amount END_EXEC The variable c (used in the cursor declaration) is used to identify the query

Embedded SQL (Cont.) The open statement for our example is as follows: EXEC SQL open c ; This statement causes the database system to execute the query and to save the results within a temporary relation. The query uses the value of the host-language variable credit-amount at the time the open statement is executed. The fetch statement causes the values of one tuple in the query result to be placed on host language variables. EXEC SQL fetch c into :si, :sn END_EXEC Repeated calls to fetch get successive tuples in the query result

Embedded SQL (Cont.) A variable called SQLSTATE in the SQL communication area (SQLCA) gets set to ‘02000’ to indicate no more data is available The close statement causes the database system to delete the temporary relation that holds the result of the query. EXEC SQL close c ; Note: above details vary with language. For example, the Java embedding defines Java iterators to step through result tuples.

Updates Through Embedded SQL Embedded SQL expressions for database modification (update, insert, and delete) Can update tuples fetched by cursor by declaring that the cursor is for update EXEC SQL declare c cursor for select * from instructor where dept_name = ‘Music’ for update We then iterate through the tuples by performing fetch operations on the cursor (as illustrated earlier), and after fetching each tuple we execute the following code: update instructor set salary = salary + 1000 where current of c

Triggers

Triggers A trigger is a statement that is executed automatically by the system as a side effect of a modification to the database. To design a trigger mechanism, we must: Specify the conditions under which the trigger is to be executed. Specify the actions to be taken when the trigger executes. Triggers introduced to SQL standard in SQL:1999, but supported even earlier using non-standard syntax by most databases. Syntax illustrated here may not work exactly on your database system; check the system manuals

Triggering Events and Actions in SQL Triggering event can be insert, delete or update Triggers on update can be restricted to specific attributes For example, after update of takes on grade Values of attributes before and after an update can be referenced referencing old row as : for deletes and updates referencing new row as : for inserts and updates Triggers can be activated before an event, which can serve as extra constraints. For example, convert blank grades to null. create trigger setnull_trigger before update of takes referencing new row as nrow for each row when (nrow.grade = ‘ ‘) begin atomic set nrow.grade = null; end;

Trigger to Maintain credits_earned value create trigger credits_earned after update of takes on (grade) referencing new row as nrow referencing old row as orow for each row when nrow.grade <> ’F’ and nrow.grade is not null and (orow.grade = ’F’ or orow.grade is null) begin atomic update student set tot_cred= tot_cred + (select credits from course where course.course_id= nrow.course_id) where student.id = nrow.id; end;

Statement Level Triggers Instead of executing a separate action for each affected row, a single action can be executed for all rows affected by a transaction Use for each statement instead of for each row Use referencing old table or referencing new table to refer to temporary tables (called transition tables) containing the affected rows Can be more efficient when dealing with SQL statements that update a large number of rows

When Not To Use Triggers Triggers were used earlier for tasks such as Maintaining summary data (e.g., total salary of each department) Replicating databases by recording changes to special relations (called change or delta relations) and having a separate process that applies the changes over to a replica There are better ways of doing these now: Databases today provide built in materialized view facilities to maintain summary data Databases provide built-in support for replication Encapsulation facilities can be used instead of triggers in many cases Define methods to update fields Carry out actions as part of the update methods instead of through a trigger

When Not To Use Triggers (Cont.) Risk of unintended execution of triggers, for example, when Loading data from a backup copy Replicating updates at a remote site Trigger execution can be disabled before such actions. Other risks with triggers: Error leading to failure of critical transactions that set off the trigger Cascading execution

Index • A database index is a data structure that improves the speed of operations on a table. Indexes can be created using one or more columns, providing the basis for both rapid random scan on table for data retrieval and efficient ordering of access to records. • Queries based on Indexed colums gets executed faster. • Practically, indexes are also type of tables, which keep primary key or index field and a pointer to each record into the actual table. • Index are created for columns with primary keys, unique keys. • You can use one or more columns to create an index. For example, we can create an index on tutorials_tbl using tutorial_author. • Example of Explicit Unique index creation – CREATE UNIQUE INDEX index_name ON table_name ( column1, column2,...);