announcements read 6.7 – 6.10 for wednesday homework 6, due friday 10/29 project step 4, due today...

49
Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper List of sources - due 10/29

Upload: harvey-chapman

Post on 13-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Announcements

• Read 6.7 – 6.10 for Wednesday

• Homework 6, due Friday 10/29

• Project Step 4, due today

• Research paper– List of sources - due 10/29

Page 2: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

ALTER TABLE Command

• To add a new columnALTER TABLE basetablename ADD COLUMN columnname

datatype;Ex. ALTER TABLE Student ADD COLUMN birthdate

DATETYPE;– Cannot specify NOT NULL, since existing records have

no value for this field

• To drop a columnALTER TABLE basetablename DROP COLUMN columnname;Ex. ALTER TABLE Student DROP COLUMN major;

Change database to allow double majors

Page 3: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

ALTER TABLE Command

• To add a constraintALTER TABLE basetablename ADD CONSTRAINT

constraint_defn;

• To drop a constraintALTER TABLE basetablename DROP CONSTRAINT

constraint_name;

Add a constraint that does not allow two classes to be scheduled in the same room

Page 4: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Other Changes to Tables• Renaming a table:RENAME TABLE old-table-name TO new-table-name;Ex: RENAME TABLE FACULTY TO TEACHERS;

• Dropping a table:– CASCADE; //remove table and all references to it (ex. in a

REFERENCES) – RESTRICT; //remove the table only if there are no references

to it elsewhere DROP TABLE basetablename;Ex. DROP TABLE CLASS;

• Dropping an index:DROP INDEX indexname;Ex. DROP INDEX Student_lastName_firstName_ndx;

Page 5: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

SQL Database Design Language

Lecture 16

Page 6: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

SQL DML

• Non-procedural, declarative language• Can be interactive, can be embedded in

host language, or can be stand-alone programming language (SQL/PSMs)

• Basic commandsSELECT

UPDATE

INSERT

DELETE

Page 7: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

INSERT OperatorINSERTINTO tablename [(colname [,colname]...)]VALUES (constant [,constant]...);

• Used for inserting new records into database, one at a time

• Not necessary to name columns if values are supplied for all columns, in proper order

• To insert null value for a column, specify only the other columns or write null as the value

• Can specify values for some columns, in any order, as long as values match order

Page 8: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

UPDATE OperatorUPDATE tablenameSET columnname = expression [columnname = expression]... [WHEREpredicate];

• Used for changing values in existing records• Can update, zero, one, many, or all records in a table• For null value, use SET columnname = NULL• can use a sub-query to identify records to be updated

Page 9: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Examples

• Change number of credits

• Change credits and major

• Set major to null

Page 10: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Examples

• Change the grade of all students enrolled in CSC201A to ‘A’

• Increase the number of credits for all students by 3

Page 11: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

DELETE OperatorDELETEFROM tablenameWHERE predicate;

• 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

Page 12: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

SELECT StatementSELECT [DISTINCT] col-name [AS newname], [,col-name..]…FROM table-name [alias] [,table-name]…[WHERE predicate][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

Page 13: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Simple Retrieval with Condition

• Get names, IDs, and number of credits of all Math majors

Page 14: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Use of Asterisk Notation for “all columns”

• Get all information about CSC Faculty

Page 15: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Retrieval without Condition

• Get the course number of courses in which students are enrolled

• Eliminate duplicates

Page 16: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

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

Page 17: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

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

Page 18: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Natural Join

• Find IDS and names of all students taking ART103A

Page 19: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

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.

Page 20: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

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

Page 21: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Use of Aliases

• Get a list of all courses that meet in the same room, with their schedules and room numbers

Page 22: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

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

Page 23: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Examples – Using a subquery with Equality

• Find the numbers of all the courses taught by Byrne of the math department

Page 24: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Subquery Using ‘IN’

• Find the names and IDS of all Faculty members who teach a class in Room H221

Page 25: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Nest Subqueries

• Get an alphabetical list of names and IDs of all students in any class taught by F110

Page 26: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Using EXISTS

• Find the names of all students enrolled in CSC201A

Page 27: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Query Using NOT EXIST

• Find the names of all students who are not enrolled in CSC201A

Page 28: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Query Using UNION

• Get IDs of all Faculty who are assigned to the history department or who teach in Room H221

Page 29: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Using Functions

• Find the total number of students enrolled in ART103A

COUNT returns the number of values in the columnSUM returns the sum of the values in the columnAVG returns the mean of the values in the columnMAX returns the largest value in the columnMIN returns the smallest value in the column

Page 30: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Using Functions

• Find the number of departments that have Faculty in them.

• Find the average number of credits student have.

Page 31: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Examples – Using Functions

• Find the student with the largest number of credits.

Page 32: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

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

Page 33: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

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

Page 34: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

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

Page 35: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

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.

Page 36: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

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.

Page 37: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Use of NULL

• Find the stuId and classNumber of all students whose grades in that course are missing

Page 38: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Inserting multiple records

• Create and fill a new table that shows each course and the number of students enrolled in it

Page 39: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Updating with a Query

• Change the room to B220 for all courses taught by Tanaka

Page 40: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example – Delete with a subquery

• Erase all enrollment records for Owen McCarthy

Page 41: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

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

Page 42: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

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

Page 43: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Trigger SyntaxCREATE OR REPLACE TRIGGER trigger_name[BEFORE/AFTER] [INSERT/UPDATE/DELETE] ON

table_name[FOR EACH ROW] [WHEN condition]BEGIN

trigger bodyEND;• Can disable triggers using ALTER TRIGGER name

DISABLE;• Later write ALTER TRIGGER name ENABLE;• Can drop triggers using DROP TRIGGER name;

Page 44: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Trigger for Student Enrolling in a Class

CREATE TRIGGER ADDENROLLAFTER INSERT ON RevEnrollFOR EACH ROWBEGIN

UPDATE RevClassSET currentEnroll = currentEnroll + 1WHERE RevClass.classNumber = :NEW.classNumber;

END;

Page 45: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Trigger for Student Dropping a Class

CREATE TRIGGER DROPENROLLAFTER DELETE ON RevEnrollFOR EACH ROWBEGIN

UPDATE RevClassSET currentEnroll = currentEnroll – 1WHERE RevClass.classNumber = :OLD.classNumber;

END;

Page 46: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Trigger for Student Changing ClassesCREATE TRIGGER SWITCHENROLLAFTER UPDATE OF classNumber ON RevEnrollFOR EACH ROWBEGIN

UPDATE RevClassSET currentEnroll = currentEnroll + 1WHERE RevClass.classNumber = :NEW.classNumber;UPDATE RevClassSET currentEnroll = currentEnroll – 1WHERE RevClass.classNumber = :OLD.classNumber;

END;

Page 47: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Trigger for Checking for Over-enrollment Before Enrolling StudentCREATE TRIGGER ENROLL_REQUESTBEFORE INSERT OR UPDATE OF classNumber ON RevEnrollFOR EACH ROWDECLARE

numStu number;maxStu number;

BEGINset maxEnroll into maxStufrom RevClasswhere RevClass.classNumber = :NEW.classNumber;

set currentEnroll + 1 into numStufrom RevClasswhere RevClass.classNumber = :NEW.classNumber;

if numStu > maxStu

RequestClosedCoursePermission(:NEW.stuId, :NEW.classNumber, RevClass.currentEnroll, RevClass.maxEnroll);end if;

END;

Page 48: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Example Trigger

• Prevent students from enrolling in two classes that meet at the same time

Page 49: Announcements Read 6.7 – 6.10 for Wednesday Homework 6, due Friday 10/29 Project Step 4, due today Research paper –List of sources - due 10/29

Ending Transactions

• COMMIT makes permanent changes in the current transaction

• ROLLBACK undoes changes made by the current transaction