Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for.

Similar presentations


Presentation on theme: "Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for."— Presentation transcript:

1 Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for maintaining a DB. To consider non-scalar attribute values.

2 The Need to Sort Relations l A relation is a set. l The elements of a set have no order.  The tuples of a relation have no order. PROBLEM !! Sometimes we need sorted data. Example : a telephone directory is just a set of entries, but it would be useles if not sorted into alphabetic order. PROBLEM !! Sometimes we need the same data sorted into different orders. Example : at different times, we require employee details sorted by name, by company ID, by salary, and by department.

3 Solution Sort a relation when it is retrieved. This also leaves the DBMS free to physically store the relation’s data in any desirable way, and most efficiently serve a variety of different uses and users. Note : l Typically retrieved relations are not whole DB relations but the result of a query expression.  the data may well need to be physically sorted anyway in order to return tuples in the desired order. l Once a sorted copy of the relation is outside the DB, we can treat it how we like.

4 Sorting l Sort relation on the values of 1 or more attribute(s). l Attribute(s) sorted on must have an orderable type for sorting to be possible. Example : numbers and text can be sorted, photos cannot. l Sorting can be ascending or descending. l If several attributes are used to sort a relation : l sort tuples on the 1 st attribute’s values, l sort tuples with the same 1 st attribute value on the 2 nd attribute’s values, l sort tuples with the same 1 st and 2 nd attribute values on the 3 rd attribute’s values, l etc. l Also expressed as “sort on 3 rd attribute within 2 nd attribute within 1 st attribute”. l Thus a major-to-minor attribute order must be specified.

5 Sorting in SQL Add an ORDER BY phrase to the end of a SELECT statement. The ORDER BY phrase must always be the last phrase in the SELECT statement. Examples : l SELECT * FROMEMP ORDER BY EName ; Orders the rows in alphabetical order of employees’ names. l SELECT * FROM EMP ORDER BY Sal DESC, EName ASC ; Orders rows into ascending/alphabetical order of employees’ names within decreasing salaries. ASC is the default if no order is given..

6 Triggers Definition : Piece of code consisting of procedural & declarative statements that is activated by the DBMS if a specific operation is executed on the DB under specific conditions. In SQL DBMSs, triggers are stored in the ‘SQL catalog’. l Their purpose is to provide a facility that will carry out an appropriate action if an error or some specified event occurs. l The motivation and concepts are similar to the handling of interrupts in Operating Systems and exceptions in programming languages. l A trigger has a name & consists of 3 parts :  an event,  a condition (optional),  an action.

7 Trigger Actions Processing Another process runs to handle the “Special Event”. Normal processing resumes. “Special Event” detected. Processing halts. SQL : Trigger SQL : Triggered Procedure

8 Using Triggers l Handling errors. l Managing changes to the DB for example : l Cascaded Deletes l Cascaded Updates l Setting NULLs l Setting default values l Prohibiting Changes l Summation Updates to ensure that changes can be made without violating any rules execute useful supplementary activities

9 SQL Example CREATE TRIGGER CAR_OWNER_DELETE BEFORE DELETE ON EMP FOR EACH ROW BEGIN UPDATE CAR SET OWNER = NULL WHERE OWNER = :OLD.EMP_NO; END; Name of Trigger Event No condition in this example Action refers to old version of triggering table EMP for each row in the deletion

10 SQL Syntax - Main Points CREATE TRIGGER Trigger_Name ON Table_Name FOR EACH WHEN Condition BEGIN Trigger-Action END; BEFORE AFTER INSTEAD OF INSERT DELETE UPDATE OF colname(s) ROW STATEMENT

11 Example : Summation Updates (1) INSERT INTO TRANS VALUES ('004', 'A002', 30); Record of transactions Summary of transactions

12 Example : Summation Updates (2) Record of transactions Summary of transactions Inserted row Updated value

13 SQL Trigger for Summation Update CREATE TRIGGER ADD_TO_DAILY_TOTAL AFTER INSERT ON TRANS FOR EACH ROW BEGIN UPDATE DAILY_TRANS SET TOTAL = TOTAL + :NEW.AMOUNT; END; refers to new version of triggering table TRANS

14 Handling Referential Integrity (1) CAR EMP Supposing employee ‘E8’ is deleted from EMP.  car ‘JON 1’ breaks referential integrity. Solutions : 1 Delete car. 2. Employee deletion disallowed. 3. Set car owner to default value. 4. Set car owner to NULL. RegNoTypeOwner NA03 RTYCorsa 1.3E3 DH56 LGRPrimera GLiE5 JON 1Jaguar XKE8 NK07 YXKVolvo S80E6

15 Handling Referential Integrity (2) l Could use triggers to carry out desired compensatory action. l SQL allows phrases to be added to the Referential Integrity constraint to carry out the chosen solution if the constraint is broken. Example : Create Table CAR ( RegNoChar(9) Primary Key, TypeVarchar2(24), OwnerChar(2) References EMP( EmpNo ) ) ; On Delete CASCADE On Delete RESTRICT On Delete SET DEFAULT On Delete SET NULL Add one of these.

16 Non-Scalar Attribute Values Attribute values are no longer constrained to be scalar values of traditional type. Attributes can also have the following kinds of type : l ‘Enhanced’ basic types - e.g. multimedia, XML. l Object classes. Typically an object is defined to comprise several values of traditional type, i.e. numbers, text, date. SQL row objects may be considered a special case of this. l Collections. Currently Standard SQL4 (of 2003) only permits arrays and bags. All values in an array/bag must have the same type. l Nested relations. An attribute value consists of a relation. In principle there are 2 possibilities : the contents of the nested relation are / are not visible in the outer/holding relation.

17 Conceptual Example Values of type ‘Picture’ Values each of which is an array of values of type ‘Text’

18 SQL Examples CREATE TYPE Money AS DECIMAL(7,2) ; CREATE TYPE Address AS ( HouseNoINTEGER, StreetVARCHAR2(30), TownVARCHAR2(30), PostcodeCHAR(8) ) ; CREATE TABLE Employee ( Emp_IDCHAR(4), HomeAddress, SalaryMoney ); Create scalar type Create row type Use new types to create a table.


Download ppt "Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for."

Similar presentations


Ads by Google