Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Advanced SQL. Relational Set Operators UNIONINTERSECTMINUS Work properly if relations are union- compatible –Names of relation attributes must.

Similar presentations


Presentation on theme: "Chapter 8 Advanced SQL. Relational Set Operators UNIONINTERSECTMINUS Work properly if relations are union- compatible –Names of relation attributes must."— Presentation transcript:

1 Chapter 8 Advanced SQL

2 Relational Set Operators UNIONINTERSECTMINUS Work properly if relations are union- compatible –Names of relation attributes must be the same and their data types must be identical

3 UNION Example query: –SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE FROM CUSTOMER UNION SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE FROM CUSTOMER_2; The resulting data would be all rows from both tables (with no duplicates)

4 UNION ALL Example query: –SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE FROM CUSTOMER UNION ALL SELECT CUS_LNAME, CUS_FNAME, CUS_INITIAL, CUS_AREACODE, CUS_PHONE FROM CUSTOMER_2; The resulting data would be all rows from both tables (with duplicates)

5 INTERSECT This returns only those rows that are in both tables (the duplicates)

6 MINUS This returns only rows that exist in the first table, but not in the second

7 Natural Joins Old style: SELECT P_PRODUCT, V_VENDOR SELECT P_PRODUCT, V_VENDOR FROM PRODUCT, VENDOR WHERE PRODUCT.V_CODE = VENDOR.V_CODE New style: SELECT P_PRODUCT, V_VENDOR SELECT P_PRODUCT, V_VENDOR FROM PRODUCT NATURAL JOIN VENDOR

8 Updatable Views Views that can be updated which will update their corresponding tables. Views can be updatable if the primary key of the base table is a column in the view that also has unique values

9 Sequences Pg. 318 Sequences are independent objects in the database They are considered “Auto-numbers” They are created and used as primary keys in tables where the key is a sequential number CREATE SEQUENCE Part_Num_Seq START WITH 100

10 Sequences on Insert Refer to the sequence (auto-number) object in place of the value needed for the key when inserting data into a table Can be used with NEXTVAL ( sequence is incremented ) or CURRVAL ( current value of sequence is used ) INSERT INTO PARTS VALUES ( PART_ID_SEQ.NEXTVAL, ‘PART_NAME, ect……..)

11 Procedural SQL Shortcomings of SQL –SQL doesn’t support execution of a stored set of procedures based on some logical condition. –SQL fails to support the looping operations. Solutions –Embedded SQL Embedded SQL can be called from within the procedural programming language –Shared Code Critical code is isolated and shared by all application programs.

12 Procedural SQL –Procedural SQL allows the use of procedural code and SQL statements that are stored within the database. –The procedural code is executed by the DBMS when it is invoked by the end user. –End users can use procedural SQL (PL/SQL) to create: Triggers Stored procedures PL/SQL functions Procedural SQL

13 Triggers page 328 –A trigger is procedural SQL code that is automatically invoked by the RDBMS upon the occurrence of a data manipulation event. A trigger is always invoked before or after a data row is selected, inserted, or updated. A trigger is always associated with a database table. Each database table may have one or more triggers. A trigger is executed as part of the transaction that triggered it. Procedural SQL

14 –Role of triggers Triggers can be used to enforce constraints that cannot be enforced at the design and implementation levels. Triggers add functionality by automating critical actions and providing appropriate warnings and suggestions for remedial action. Triggers can be used to update table values, insert records in tables, and call other stored procedures. Triggers add processing power to the RDBMS and to the database system.

15 Procedural SQL Stored Procedures –A stored procedure is a named collection of procedural and SQL statements. –Stored procedures are stored in the database and invoked by name. –Stored procedures are executed as a unit. –The use of stored procedures reduces network traffic, thus improving performance.

16 Stored Functions – is a named group of procedural and SQL statements that returns a value. –Invoked from within stored procedures or triggers –Cannot be invoked from within SQL statements PL/SQL Stored Functions


Download ppt "Chapter 8 Advanced SQL. Relational Set Operators UNIONINTERSECTMINUS Work properly if relations are union- compatible –Names of relation attributes must."

Similar presentations


Ads by Google