Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Announcements Read 6.7 – 6.10 for Friday Homework 6, due Friday 10/29 Research paper –List of sources - due 10/29 Department Seminar –The Role of Experimentation.

Similar presentations


Presentation on theme: "1 Announcements Read 6.7 – 6.10 for Friday Homework 6, due Friday 10/29 Research paper –List of sources - due 10/29 Department Seminar –The Role of Experimentation."— Presentation transcript:

1 1 Announcements Read 6.7 – 6.10 for Friday Homework 6, due Friday 10/29 Research paper –List of sources - due 10/29 Department Seminar –The Role of Experimentation in Computer Science by Marvin Zelkowitz today 3:00 pm in MH 040

2 2 SQL Database Manipulation Language Lecture 16

3 3 DELETE Operator DELETE FROMtablename WHEREpredicate; Used for deleting existing records from database Can delete zero, one, many, or all records Operation may not work if referential integrity would be lost Can use a sub-query to target records to be deleted If you delete all records from a table, its structure still remains, and you can insert into it later

4 4 SELECT Statement SELECT [DISTINCT] col-name [AS newname], [,col-name..]… FROMtable-name [alias] [,table-name]… [WHEREpredicate] [GROUP BY col-name [,col-name]…[HAVING predicate] or [ORDER BY col-name [,col-name]…]; Powerful command – equivalent to relational algebra’s SELECT, PROJECT, JOIN and more… Can be applied to one or more tables or views Can display one or more columns (renaming if desired) Predicate is optional, and may include usual operators and connectives Can put results in order by one or more columns Can also group together records with the same value for column(s) Can also use predefined functions

5 5 Example – Simple Retrieval with Condition Get names, IDs, and number of credits of all Math majors

6 6 Example – Use of Asterisk Notation for “all columns” Get all information about CSC Faculty

7 7 Example – Retrieval without Condition Get the course number of courses in which students are enrolled Eliminate duplicates

8 8 Example – Use of “ORDERED BY” and “AS” Get names and IDs of all Faculty members, arranged in alphabetical order by name. Call the resulting columns FacultyName and FacultyNumber

9 9 Example – Use of Multiple Conditions Get names of all math majors who have more than 30 credits standard comparison operators: =, <>,, >= standard logical operators: AND, OR, and NOT

10 10 Example – Natural Join Find IDS and names of all students taking ART103A

11 11 Example – Natural Join with Ordering Find stuId and grade of all students taking any course taught by the Faculty member whose facId is F110. Arrange in order by stuId.

12 12 Example – Use of Aliases Get a list of all courses that meet in the same room, with their schedules and room numbers

13 13 Example – Natural Join of Three Tables Find course numbers and the names and majors of all students enrolled in the courses taught by Faculty member F110

14 14 Example – Join without Equality Condition Find all combinations of students and Faculty where the student’s major is different from the Faculty member’s department

15 15 Examples – Using a subquery with Equality Find the numbers of all the courses taught by Byrne of the math department

16 16 Example – Subquery Using ‘IN’ Find the names and IDS of all Faculty members who teach a class in Room H221

17 17 Example – Nest Subqueries Get an alphabetical list of names and IDs of all students in any class taught by F110

18 18 Example – Using EXISTS Find the names of all students enrolled in CSC201A

19 19 Example – Query Using NOT EXIST Find the names of all students who are not enrolled in CSC201A

20 20 Example – Query Using UNION Get IDs of all Faculty who are assigned to the history department or who teach in Room H221

21 21 Example – Using Functions Find the total number of students enrolled in ART103A COUNTreturns the number of values in the column SUMreturns the sum of the values in the column AVGreturns the mean of the values in the column MAXreturns the largest value in the column MINreturns the smallest value in the column

22 22 Example – Using Functions Find the number of departments that have Faculty in them. Find the average number of credits student have.

23 23 Examples – Using Functions Find the student with the largest number of credits.

24 24 Examples – Using Functions Find the ID of the student(s) with the highest grade in any course Find names and IDs of students who have less than the average number of credits

25 25 Example – Using an Expression and a String Constant Assuming each course is three credits list, for each student, the number of courses he or she has completed

26 26 Example – Use of GROUP BY For each course, show the number of students enrolled GROUP BY allows us to put together all records with a single value in the specified field

27 27 Example – Use of HAVING Find all courses in which fewer than three students are enrolled HAVING is used to determine which groups have a quality, just as WHERE is used with tuples to determine which records have some quality.

28 28 Example – Use of LIKE Get details of all MTH courses % The percent character stands for any sequence of characters of any length >= 0 _ The underscore character stands for any single character.

29 29 Example – Use of NULL Find the stuId and classNumber of all students whose grades in that course are missing

30 30 Example – Inserting multiple records Create and fill a new table that shows each course and the number of students enrolled in it

31 31 Example – Updating with a Query Change the room to B220 for all courses taught by Tanaka

32 32 Example – Delete with a subquery Erase all enrollment records for Owen McCarthy

33 33 Active Databases-Constraints DBMS monitors database to prevent illegal states, using constraints and triggers Constraints –can be specified when table is created, or later –IMMEDIATE MODE: constraint checked when each INSERT, DELETE, UPDATE is performed –DEFERRED MODE: postpones constraint checking to end of transaction – write SET CONSTRAINT name DEFERRED –Can use DISABLE CONSTRAINT name, and later ENABLE CONSTRAINT name

34 34 Triggers More flexible than constraints Must have three parts: –event, some change made to the database –condition, a logical predicate (can be empty) –action, a procedure done when the event occurs and the condition is true, also called firing the trigger Can be fired before or after insert, update, delete Trigger can access values it needs as :OLD. and :NEW. –prefix :OLD refers to values in a tuple deleted or to the values replaced in an update –prefix :NEW refers to the values in a tuple just inserted or to the new values in an update. Can specify whether trigger fires just once for each triggering statement, or for each row that is changed by the statement

35 35 Trigger Syntax CREATE OR REPLACE TRIGGER trigger_name [BEFORE/AFTER] [INSERT/UPDATE/DELETE] ON table_name [FOR EACH ROW] [WHEN condition] BEGIN trigger body END; Can disable triggers using ALTER TRIGGER name DISABLE; Later write ALTER TRIGGER name ENABLE; Can drop triggers using DROP TRIGGER name;

36 36 Trigger for Student Enrolling in a Class CREATE TRIGGER ADDENROLL AFTER INSERT ON RevEnroll FOR EACH ROW BEGIN UPDATE RevClass SET currentEnroll = currentEnroll + 1 WHERE RevClass.classNumber = :NEW.classNumber; END;

37 37 Trigger for Student Dropping a Class CREATE TRIGGER DROPENROLL AFTER DELETE ON RevEnroll FOR EACH ROW BEGIN UPDATE RevClass SET currentEnroll = currentEnroll – 1 WHERE RevClass.classNumber = :OLD.classNumber; END;

38 38 Trigger for Student Changing Classes CREATE TRIGGER SWITCHENROLL AFTER UPDATE OF classNumber ON RevEnroll FOR EACH ROW BEGIN UPDATE RevClass SET currentEnroll = currentEnroll + 1 WHERE RevClass.classNumber = :NEW.classNumber; UPDATE RevClass SET currentEnroll = currentEnroll – 1 WHERE RevClass.classNumber = :OLD.classNumber; END;

39 39 Trigger for Checking for Over- enrollment Before Enrolling Student CREATE TRIGGER ENROLL_REQUEST BEFORE INSERT OR UPDATE OF classNumber ON RevEnroll FOR EACH ROW DECLARE numStu number; maxStu number; BEGIN setmaxEnroll into maxStu fromRevClass whereRevClass.classNumber = :NEW.classNumber; setcurrentEnroll + 1 into numStu fromRevClass whereRevClass.classNumber = :NEW.classNumber; if numStu > maxStu RequestClosedCoursePermission(:NEW.stuId, :NEW.classNumber, RevClass.currentEnroll, RevClass.maxEnroll); end if; END;

40 40 Example Trigger Prevent students from enrolling in two classes that meet at the same time

41 41 Ending Transactions COMMIT makes permanent changes in the current transaction ROLLBACK undoes changes made by the current transaction


Download ppt "1 Announcements Read 6.7 – 6.10 for Friday Homework 6, due Friday 10/29 Research paper –List of sources - due 10/29 Department Seminar –The Role of Experimentation."

Similar presentations


Ads by Google