Professional Documents
Culture Documents
Session 5 Agenda
Standards
Components of SQL
Enroll Table
Class Table
ADD (who_updated date,
when_updated varchar2(8));
UNIQUE INDEXES
CREATE USER
GRANT ROLE: Instead of granting individual grants to user you can grant
to the role and then just grant the role to the user.
Chapter 5: Relational Database Management Systems and SQL
CREATE VIEW
SELECT Clause
INSERT Clause
Note: The order of the values should follow the order of the columns on the table.
Chapter 5: Relational Database Management Systems and SQL
DELETE/TRUNCATE Clause
UPDATE Clause
WHERE Clause
Find the name of all students that are majoring in Math and have taken
more than 30 credits:
SQL> SELECT firstname, lastname
FROM student
WHERE major = ‘Math’
AND credits > 30;
Chapter 5: Relational Database Management Systems and SQL
Find the name of all students EXCEPT the ones majoring in Math:
SQL> SELECT firstname, lastname
FROM student
WHERE major <> ‘Math’;
Find the name of all students with the last name beginning with ‘S’
SQL> SELECT firstname, lastname
FROM student
WHERE lastName like ‘S%’;
Chapter 5: Relational Database Management Systems and SQL
Find the names of all faculty with salary between 90k and 100k:
SQL> SELECT *
FROM faculty
WHERE salary BETWEEN 90000 AND 100000;
ORDER Clause
OUTER JOIN will tell you all the students whether they are enrolled in a class or not:
SQL> SELECT s.stuId, s.firstname, s.lastname, e.classNumber
FROM student s,
enroll e
WHERE s.stuID = e.stuID(+)
ORDER BY s.lastname;
Chapter 5: Relational Database Management Systems and SQL
Find the names and average salaries of all departments whose average salary is
greater than 90000:
SQL> SELECT department, avg(salary)
FROM faculty
GROUP BY department
HAVING avg(salary) > 90000;
Chapter 5: Relational Database Management Systems and SQL
NOT IN
NOT IN Example
Chapter 5: Relational Database Management Systems and SQL
INTERSECT /MINUS
MINUS– returns rows from one table MINUS the rows from the
second table:
SQL> SELECT stuid
FROM student
MINUS
SELECT stuid
FROM enroll;
Chapter 5: Relational Database Management Systems and SQL
TRIGGERS
TRIGGERS (Con’t)
TRIGGERS (Con’t)
Chapter 5: Relational Database Management Systems and SQL
TRIGGERS (Con’t)
Step 5.1. Highlight and copy the CREATE TABLE commands (one at a
time) and paste them into SQL*Plus at the SQL> prompt.
You should see the message: Table Created after each statement is executed.
Show your work – screenshots
Step 5.2. Now insert the records into your tables. To do this, copy each
INSERT command (one at a time) and paste them into SQL*Plus at
SQL> prompt.
You should see the message: 1 row created after each statement is executed.
Show your work – screenshots
Chapter 5: Relational Database Management Systems and SQL
Step 5.3b. Find the class number, schedule, and room for all classes
that Smith of the History department teaches.
Step 5.3c. Find the names of all students who have fewer than average
number of credits.
Step 5.3d. Find the names of all the teachers that Ann Chin has, along
with all her classes and midterm grades from each
Step 5.3e. For each student, find the number of classes he or she is
enrolled in. Limit the results to only students who are enrolled.
Chapter 5: Relational Database Management Systems and SQL
5.4. Execute the five queries in the database and show the results.
Show your work – screenshots
Chapter 5: Relational Database Management Systems and SQL
For the relational schema you developed at the end of Chapter 4 for
the team project, carry out the following steps to implement the
design using Oracle:
Example:
TABLE: Student
Attribute Data Type Length Nullity Constraint
stuId VARCHAR2 6 NOT NULL Primary
Key
lastName VARCHAR2 20 NOT NULL
firstName VARCHAR2 20 NOT NULL
major VARCHAR2 10
credits NUMBER 3 DEFAULT 0
>=0 <150
Chapter 5: Relational Database Management Systems and SQL
Step 5.2 - Write and execute SQL statements to create all tables needed to
implement the design.
Create Oracle DDL statements from Step 5.1 and create your tables and
primary keys in the Oracle database.
Show your work - screenshots.
Step 5.3 – Write and execute SQL statements to create indexes for foreign
keys and for any other columns as needed
Create Oracle DDL statements from Step 5.1 and issue your foreign key
constraint statements in the Oracle database.
Show your work - screenshots.
Note: you may combine Step 5.2 and 5.3 and issue your foreign
constraints as part of the create table statement.
Chapter 5: Relational Database Management Systems and SQL
Step 5.4 - Write and execute SQL statements to insert at least five records
in each table, preserving all constraints.
Create five INSERT statements for each table and execute each insert
statement in the Oracle database.
Show your work - screenshots.
Step 5.5 - Write and execute SQL statements that will process five non-
routine requests for information from the database just created.
Come up with 5 questions and create SQL Statements to satisfy each question.
Show your work - screenshots.
Note: Make sure to ask 5 different questions so you have 5 different sql
statements.
Chapter 5: Relational Database Management Systems and SQL
Step 5.6 - Write and execute SQL statements to create at least one trigger.
Show your work - screenshots.
Step 5.7 - Write and execute SQL statements to demonstrate that the
trigger is working as expected.
To demonstrate that the trigger is working as expected, provide a screenshot of
the data before and after the trigger is executed.