Sei sulla pagina 1di 31

ORACLE TEACHING NOTES

Part I

SQL Topic 1. Basic SQL


Data Definition Language (DDL)
Creating a Table
create table t (fd_name1 fd_type, fd_name2 fd_type, ...);
create table customer (ssn char(9), cname char(30), caddress char(30),
amount number(10,2));

* field type: char(n)


varchar(n)
date
number(x,n)

Deleting a table
drop table t;
drop table customer;
truncate table t;
truncate table customer;

/* delete all records in the table but not the field definitions */

Add A Column
alter table t add (fx fd_type, fy fd_type);
alter table customer add (zip char(5), phone char(10));

Modify Column Definition


alter table table_name modify (f1 new_type, f2 new_type, ...);
Alter table customer modify (cname char(50));

/* Certain restrictions apply. See p.388 */

Display Table Structure


describe t;
describe customer;

Data Manipulation Language (DML)


Insert Records (you can only insert one record at a time)
insert into t values (f1value, f2value,...);
insert into t (f1, f2,...) values (f1value, f2value,...);
2

Delete Records
delete from t where [conditions];

Update Records
update t set f1=newvalue, f2=newvalue where [conditions];

/* Notice the difference between Insert and Update */


Display Records
select
select
select
select

* from t;
* from t where [conditions];
f1, f2, ...from t;
f1, f2, ...from t where [conditions];

Conditions
Mathematical and Logical operators
=, <>, >, <, >=, <=, and, or, not

ex:
ex:

... where price > 10;


... where price > 10 and code <> 1234;

Wildcard
like

Compares to similar character string


%
represents any group of characters
represents one character
_
ex:

... where name like 'S%';


... where name like '_llen';

Order by
default is ascending.

... order by f1;

add DESC for descending

... order by f1 desc;

Views

A definition of a restricted portion of a table


create view v1 as
select f1, f2,...
from t1 where [conditions];

ex:

create view invent_2 as select p_code, price from Inventory where

price>50;
select * from invent_2;
3

in
select sname from student where major in (IS,Finance);

Join Mulitple Tables


select t1.f1, t2.f2, ... from t1, t2, ... where [conditions];
ex.

select sname from student, advisor where student.anum=advisor.anum;

Transaction Control Statements


Save and Undo records
commit;
rollback;

/*Undo, save to the last "commit" status */

Create a new table from an existing table


create table newtable as select * from oldtable;
create table newtable as select column1, column2, ... from oldtable;

Copy contents of one table to another table


insert into to_table select * from from_table;
insert into to_table (column1, column2, ...) select column1, column2, ...
from from_table;

Exercise 1.1
Write the SQL code to ...
1. Create a table named S with the following columns
S#
char(2)
SNAME
char(10)
STATUS
number(3,0)

2. Insert a supplier record into the S table. The suppliers s# is S1, SNAME is Smith, and
Status is 20.
3. Save the record to the S table.
4. Display all records of the S table.
5. Display the sname and status columns of the S table.
6. Insert another supplier record into the S table. The suppliers s# is S2, SNAME is
Jones, and Status is 10.
7. Display all records of the S table.
8. Undo the action of problem 6. (That is, un-insert S2.)
9. Display all records of the S table. What should you see?
10. Add a new column CITY to the S table. Its column definition is char(10).
11. Update supplier S1s city to be London and S2s city to be Paris. (Can you do it
in one SQL statement, or you need to write 2?)
12. Delete the record of supplier s2.
13. Undo the action of problem 12.
(Assume now that all three tables, S, P, and SP are in place, and they have records as
above.)

S
S
#
S
1
S2
S3
S4
S5

SNA
ME
Smit
h
Jones
Blake
Clark
Adam
s

STAT
US
20
10
30
20
30

CITY
Londo
n
Paris
Paris
London
Athens

P
P
#
P
1
P2
P3
P4
P5
P6

PNA
ME
Nut
Bolt
Scre
w
Scre
w
Cam
Cog

COL
OR
Red
Gree
n
Blue
Red
Blue
Red

WEIG
HT
12
17
17
14
12
19

CITY
Lond
on
Paris
Rome
Londo
n
Paris
Londo
n

SP
S# P#

QTY

S1
S1
S1
S1
S1
S1
S2
S2
S3
S4
S4
S4

300
200
400
200
100
100
300
400
200
200
300
400

P1
P2
P3
P4
P5
P6
P1
P2
P2
P2
P4
P5

14. Display the name of supplier who is from Paris.


15. Display the name of supplier who is from Paris and whose status is higher or
equal to 20.
16. Delete suppliers whose name starts with a D.
17. Display all records of the S in descending order of STATUS and ascending order of
SNAME (that is, the supplier of the highest STATUS will be displayed at first, the
second highest at the second, etc. Among the supplier of the same status, their names
will be sorted alphabetically.)
18. In one SQL command, create a new table named S_LONDON. The S_LONDON
table has the same structure as the S table, and it contains supplier records of suppliers
who are from London.
19. Can you undo the action of problem 18?
20. Delete the S_LONDON table.
21. Display the names of the suppliers who supply product number P1.
22. Display the name and weight of parts which are supplied by supplier S3.
23. Display the quantity of part "Blue Screw" supplied by supplier S1.
24. Display the quantity of part Green Bolt supplied by supplier Smith.
25. Display the name of suppliers who supply Red Nut.
26. Display names of parts which are supplied by Blake.
27. Display names of all parts which are supplied by suppliers whose status is higher
than 25.
28. Display s#, p# pairs of which the supplier and parts are from the same city and the
supplier has supplied that part. (You need to use the SP table).

SQL Topic 2. Data Integrity


Commands
1. Primary Key Commands
When the pk contains a single column
ex: create table t1 (
f1 char(2) primary key, ....

When the pk contains multiple columns


ex: create table t2 (
f1 char(2), f2 char(2), f3 char(2),...,
primary key (f1, f2));

ADD new constraints:

alter table t2 add primary key (f1,f2);

DROP constraints:
alter table t2 drop primary key (f1,f2);

2. Foreign Key Commands


1. When the fk contains a single column:
create table child (
f1 char(2),
f2 char(2) constraint c1 references parent(f1),
....);
create table child (
f1 char(2), f2 char(2),...,
constraint c2 foreign key (f1,f2) references parent(f1,f2)
... );
alter table child add constraint c1
foreign key (f1,f2) references parent(f1,f2));
alter table child drop constraint c1;

Exercise 2.1 Primary key and Foreigh Key declaration


COURSE
dept char(4),
code char(3)
title char(10)
credits
number(1)

INSTRUCTOR
ssn char(9)
name
char(10)
primary key:
ssn

SECTIONS
call# char(5)
dept char(4)
code char(3)
sec# char(2)
i_ssn char(9)
7

primary key: dept


+ code

primary key: call#


foreign key: (1) (dept,code) references
COURSE
(2) i_ssn references
INSTRUCTOR

(1) Write SQL statement to create the above 3 tables in sequence with proper
primary key and foreign key declarations.
(2) Write SQL statements to delete the above 3 tables in sequence.

SQL Topic 3. Advanced SQL


Commands
Group Functions
select group_function(f2) ... from table;
select
select
select
select
select
select

...
...
...
...
...
...

count(field)
sum(field)
avg (field)
distinct(field)
max(field)
min(field)

Example:

SQL> select count(pnum) from sp where snum='S1';


COUNT(PNUM)
----------6
SQL> select avg(qty) from sp where snum='S1';
AVG(QTY)
--------216.66667

... Group by and ... Having


select f1, group_function(f2), group_function(f3),... from table
group by f1;
select f1, group_function(f2), group_function(f3),... from table
group by f1
having [group_function related conditions];
select f1, group_function(f2), group_function(f3),... from table
group by f1
having [group_function related conditions]
order by group_function(..);

Examples

SQL> select snum, count(qty) from sp


2 group by snum;
9

SN COUNT(QTY)
-- ---------S1
6
S2
2
S3
1
S4
3
SQL> select snum, count(qty) from sp
2 group by snum
3 order by count(qty);
SN COUNT(QTY)
-- ---------S3
1
S2
2
S4
3
S1
6
SQL> select snum, sum(qty) from sp
2 group by snum;
SN SUM(QTY)
-- --------S1
1300
S2
700
S3
200
S4
900
SQL> select snum, sum(qty) from sp
2 group by snum
3 having count(qty)>=2;
SN SUM(QTY)
-- --------S1
1300
S2
700
S4
900

Exercise 3.1
1. Display the number of times each supplier supplies parts, sorted by the highest
number to the lowest.
2. Display the number of times each supplier supplies parts, sorted by the
supplier's number.
3. Display the average quantity each supplier supplies parts.
4. Display the total quantity each supplier supplies parts.
10

5. Display the total quantity each supplier supplies parts for suppliers who have
placed 3 or more times of orders
6. Display the number of times each part has been supplied.
Display the number of times each part has been supplied, sorted by the highest
number to the lowest.
7. Display the average quantity that each part has been supplied.
8. Display the total quantity each part has been supplied, sorted by part number.
9. Display the average quantity that each part has been supplied for parts which
average quantity order is less than 300.
10. Display the total quantity supplied by S1.
11. Display the overall average quantity supplied.
12. Display the highest and lowest quantity supplied by each supplier.
13. Display suppliers whose average quantity is less or equal to 250.
14. Display suppliers who have the highest average quantity.

11

SQL Topic 4. Simple Data Dictionary


Commands
The USER_TABLES Table
SQL> select table_name from user_tables;
TABLE_NAME
-----------------------------C
COUNTER
ENG
G
ORD
P
PRODUCT
......

The USER_TAB_COLUMNS
SQL> SELECT table_name, column_name, data_type from
user_tab_columns;
Table
Name
COLUMN_NAM DATA_TYPE
---------- ---------- --------C
CALL#
CHAR
C
DEPT
CHAR
C
CODE
CHAR
C
TITLE
CHAR
COUNTER TYPE
CHAR
COUNTER MAXNUM
NUMBER
ENG
SNUM
CHAR
ENG
SNAME
CHAR
G
S#
CHAR
G
CALL#
CHAR
G
GRADE
CHAR
G
GRADEPT NUMBER
.....

The USER_CONSTRAINTS Table


SQL>
SQL>
SQL>
SQL>
SQL>
SQL>

column
column
column
column
column
column

owner format a10


constraint_name heading 'Cons|Name' format a15
constraint_type heading 'Cons|Type' format a15
table_name heading 'Table|Name' format a10
r_owner format a10
r_constraint_name heading 'R|Cons|Name' format a15
12

SQL>
SQL> select owner, constraint_name, constraint_type, table_name,
r_owner,
2
r_constraint_name from user_constraints;
R
Cons
Cons
Table
Cons
OWNER
Name
Type
Name
R_OWNER Name
---------- --------------- --------------- ---------- ---------- --------------SOPHIE
SYS_C00403
P
ORD
SOPHIE
ORDER_PS
R
ORD
SOPHIE
SYS_C00400
SOPHIE
SYS_C00374
P
P
SOPHIE
SYS_C00398
P
PRODUCT
SOPHIE
SYS_C00400
P
SP
SOPHIE
SP_P
R
SP
SOPHIE
SYS_C00398
SOPHIE
SP_S
R
SP
SOPHIE
SYS_C00399
SOPHIE
SYS_C00399
P
SUPPLIER
8 rows selected.
Note: P=Primary Key; R=Foreign Key

The USER_CONS_COLUMNS Table


SQL> select owner, constraint_name, table_name, column_name,
position
2
from user_cons_columns
3
order by table_name;
OWNER
CONSTRAINT_NAME TABLE_NAME COLUMN_NAM POSITION
---------- --------------- ---------- ---------- --------SOPHIE
ORDER_PS
ORD
PNUM
1
SOPHIE
ORDER_PS
ORD
SNUM
2
SOPHIE
SYS_C00403
ORD
ONUM
1
SOPHIE
SYS_C00374
P
PNUM
1
SOPHIE
SYS_C00398
PRODUCT PNUM
1
SOPHIE
SP_P
SP
PNUM
1
SOPHIE
SYS_C00400
SP
SNUM
2
SOPHIE
SP_S
SP
SNUM
1
SOPHIE
SYS_C00400
SP
PNUM
1
SOPHIE
SYS_C00399
SUPPLIER SNUM
1
10 rows selected.

13

SQL Topic 5. Introduction to


ORACLE
** Bring a floppy disk to Lab**

Step 1. Log in to ORACLE


Open SQL: Start - Citrix..., in the Citrix window, double-click the Oracle 8.0.5...
icon.
Log in: A log in screen appears. Log in to ORACLE by (your username and
password will be announced in class)
username
password
host

press [tab]
press [tab]
press [enter]

You should see the SQL> prompt now.


Log off: To log-off from Oracle, type exit at the prompt. Another way to exit is
to close the window. Make sure you exit Oracle properly.

Step 2. Write SQL commands in a text file


Open Notepad: Choose Start - Programs Accessories Notepad
At the Notepad file, enter the following commands:

14

now choose File - Save. Specify your filename as a:myprog1.SQL.


Oracle Commands:
set echo on -- this tells Oracle to reprint your commands when running it.
spool a:myprog1.txt -- this tells Oracle to record screen output to the file
myprog1.txt on Drive A. The recording stops when the program comes to the
spool of command. It is common practice to call your SQL program files .sql and
output (spool) file .txt.
drop table stu; -- This is the common practice to delete old, existing tables
before your create table... command. Since your account is brand new, this will
result in a Table or View Not Exist... error message, which is alright.

Step 3. Run the Program


Go back to the Oracle SQL*Plus window (by clicking anywhere on the window).
To run your program, at the SQL prompt, enter
15

SQL> start a:myprog1.sql

press [enter]. The program will be executed.


Your output will look something like this:
SQL> spool a:myprog1.txt
SQL>
SQL> drop table stu;
drop table stu
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL>
SQL> create table stu (
2
ssn char(9),
3
name char(10),
4
major char(10));
Table created.
SQL>
SQL> insert into stu values ('111','Andy','IS');
1 row created.
SQL> insert into stu values ('222','Betty','finance');
1 row created.
SQL>
SQL> select * from stu;
SSN
--------111
222

NAME
---------Andy
Betty

MAJOR
---------IS
finance

SQL>
SQL> Input truncated to 9 characters
spool off

Your execution should look similar to this. Scroll the window up or [PageUp] to
check your execution. If you do not see any problems, thats great! You can go
directly to Step 5. Otherwise, you need to debug your program. Go to Step 4.

16

Step 4. Debug the Program


If there is any unintended error message (which is quite normal), you can edit
your program file and run it again (this process is called debugging). Go to the
Notepad window and check if there is any typos. Repeat Step 3 to run it again.
Do this until there is no error.

Step 5. View and Print your spool file


with the spool a:myprog1.txt command, your screen output is now captured in
the file a:myprog1.txt.
To view it, go to Notepad and choose File Open ..., and open a:myprog1.txt.
You will see the screen output of your a:myprog1.sql program.
If you want to print it, choose File-Print
** This is the way that you will turn in most of the homework.

Step 6. Write and Run another program


Lets write another Oracle program which would query the STU table that you
just created.
Go to Notepad and choose File New.
At the Notepad file, enter the following commands:
/* Program 2. Query the STU table */
set echo on
spool a:myprog2.txt
select * from stu;
17

select name from stu where major='IS';


insert into stu values ('333','Cindy','IS');
commit;
select name from stu where major='IS';
spool off

now choose File - Save. Specify the new file name to be a:myprog2.sql.
Go back to Oracle SQL*Plus.
To run your program, at the SQL prompt, enter
SQL> start a:myprog2.sql

press [enter], and see what happens.


with the spool a:myprog2.txt command, your screen output is now captured in
the file a:myprog2.txt.
To view it, go to Notepad and choose File Open ..., and open a:myprog2.txt.
You will see the screen output of your a:myprog2.sql program.
If you want to print it, choose File-Print

Step 7. Write Commands Directly at the SQL>


Prompt
You can also write commands directly at the SQL> prompt.
At the SQL> prompt, write
SQL> select * from stu;

Press [Enter], and see the command being executed.


18

Note: Usually people write commands in a text file so they can be executed
together. The program is sometimes called a "script". If you have several
commands that you are likely to rerun them in your future, it is better to write
them in a script file so you do not have to type them over and over again.
However, if you just want to check something quickly, you can enter the
command directly at the SQL> prompt.

Exercise 5.1
Make sure you can do the following without any problem.
1. Write the following SQL codes in a myprog3.sql file. Make sure you have set
echo on in the beginning of the file. Also, spool your output to a file named
myprog3.txt.
select * from stu;
select * from stu where ssn=333;
select ssn, name from stu where ssn=333;

2. Run myprog3.sql until there is no error.


3. View the content of myprog3.txt.

Exercise 5.2
Copy the SUPPLIER, PARTS, and SP tables from my account by the following
commands:
SQL> create table supplier as select * from sophie.supplier;
Table created.
SQL> create table parts as select * from sophie.parts;
Table created.
SQL> create table sp as select * from sophie.sp;
Table created.

Answer questions of Exercise 3.1 using the 3 tables.

19

SQL Topic 6. SQL*Plus Input/Output


Commands
prompt prompt

ex.

prompt
prompt Processing.... please wait....
prompt ----------- Please Detach Along This Line ----------------

pause prompt
ex.
pause Please press [Enter] to continue ...
accept variable prompt 'prompt';

ex.

accept amount prompt 'Please enter the amount: ';


accept cnum prompt 'Please enter the Customers Number: ;

'&variable'
&variable
select 'text', f1, f2, ... from t where [conditions];
select text from t where [conditions];

ex. select 'Customer Name: ', cname from customer where cnum='&cnum';

ex.
is not null;

select The Order Has Been Processed! from transaction where tran#

File Structure
SQL> select * from s;
S# SNAME
STATUS
--- ---------- ---------001 John
Senior
002 Mary
Junior
003 Joe
Senior
004 Jim
Sophomore

Run Time Behavior

20

SQL> start 3.5


*--- Welcome to the Student Information Program ---*

Please Enter the Student ID: 001


The Student Name is: John
The Student is a Senior
Press [Enter] to continue...
SQL>

Oracle Code
set
set
set
set

feedback of
echo of
verify of
heading of

prompt
prompt *--- Welcome to the Student Information Program ---*
prompt
prompt
accept xsnum prompt 'Please Enter the Student ID: ';
select 'The Student Name is: ', sname from s
where s#='&xsnum';
select 'The Student is a ', status from s
where s#='&xsnum';
pause Press [Enter] to continue...

21

Exercise 6.1
Write the SQL code to do the following:
1. System prompts
Welcome to the Registration System

2. System prompts
Please enter the Student's ID:

and read the student ID (variable name is sid)

3. System displays the student's name according to the sid entered by the user.

4. System skips 2 empty lines

5. System prompts
Are you sure? ** Press [Enter] to continue or [ctrl][c] twice to abort...

and pause

Exercise 5.2
1. System prompts
New Student ID:

and read the student ID (variable name is newid)

2. System prompts
New Student Name:

and read the student's name (variable name is newname)

22

3. System inserts new ID and new name into the STUDENT table

4. The system prompts


The following record has been inserted:

and displays the new student's ID and name.

23

SQL Topic 7. SQL Built in Functions


Please refer to PL/SQL Programming Chapter 5
select 1+2 from dual;

Character Functions
A||B or concat(A,B)
ex:
select Student Name is: ||sname from STUDENT;
initcap(gOOd morNING) returns
lower(gOOd morNING) returns

upper(gOOd morNING) returns

lpad(0011-12110, 20, X) returns


rpad(0011-12110, 20,X) returns
ltrim(0011-12110) returns

ltrim(0011-12110, 0) returns
rtrim(0011-12110) returns

rtrim(0011-12110, 0) returns

substr(0011-12110,5,3) returns
instr(0011-12110,-) returns
length(0011-12110) returns

Numeric Functions
ceil(3.45) returns

floor(3.45) returns
mod(9/4) returns

round(123.45) returns
round(123.65) returns

round(123.45,1) returns

round (123.45,-1) returns


trunc(123.45) returns
trunc(123.65) returns

Date Functions
sysdate returns
24

sysdate - 1 returns

sysdate + 1 returns

sysdate - My_Date_of_Birth returns


round(sysdate) returns
trunc(sysdate) returns

to_char(sysdate, mm/dd/yyyy) returns


to_char(sysdate, dd/mm/yyyy) returns

to_date(10/01/2000,mm/dd/yyyy) returns

to_date(10/01/2000,dd/mm/yyyy) returns

Other Functions
decode(grade,A,4,B,3,C,2,D,1,F,1,Not a Valid Grade)

If grade = C, the above statement will return


greatest(3, 20, 18, 4) returns
least(3, 20, 18, 4) returns
NVL
nvl(grade, No Grade Assigned)
nvl(price, 0)
select units * price from transactions where....
select nvl(units, 0) * nvl(price, 0) from transaction where...
USER
insert into Trans_Audit (user, sysdate, ....);

Others
rownum
select * from transaction
where rownum < 10
order by amount desc;
in
select sname from student where major in (IS,Finance);

25

Create Sequence
create sequence transaction_number start with 10000;
insert into trans values (transaction_number.nextval, ......);
select Transaction_Number.currval from dual;

Exercise 7.1
1. What does the following code return?
SELECT decode (Core_Num, 00000000, NULL, Core_Num) from Product;

2. What does the following code return?


select substr(Part_Number, 1, instr(Part_Number,-)-1) from Product;

3. What does the following command return?


SELECT DECODE (NVL(Core_Num,'00000000'),'00000000',0,Unit_Price)
FROM product;

4. Write one SQL statement to display the last character of a students last name.
(LASTNAME is a column of the STUDENT table).
5. The student table has three columns: SSN, LASTNAME, and USERNAME. A
students username starts with the letter Z, followed by the first 2 letters of his
last name, and the last 4 digits of his SSN. Currently the USERNAME column is
null. Write one SQL statement to populate the USERNAME column.
6. (In addition to requirements of the previous question) If a student does not
have a SSN, then leave the USERNAME value NULL. Write one SQL statement to
populate the USERNAME column.
7. Write one SQL statement to display the last 2 digits of Prod_Number; if
Prod_Number is NULL, then display XX.
8. The CUSTOMER table has three columns: ACCOUNT_NUMBER, PREFIX, and
SUFFIX. There is always a - in the account number, but the position differs by
customer. Prefix is the portion of the account number before the - and suffix is
the portion after the -. Write one SQL command to populate the prefix and
suffix columns.
26

9. Write a SQL command so that if UNIT_PRICE is null, display -1; if it is 0, display


-2; for anything else, display -3. (UNIT_PRICE is a column of the PRODUCT table).

27

Topic 8. Subqueries
SELECT...
UPDATE...
DELETE...
where snum = (select snum from .... where....);
SELECT...
UPDATE...
DELETE...
where snum in (select snum from .... where....);

Exercise 8.2

1. Upgrade supplier status to 30 for suppliers who have supplied more than 500
units of parts.
2. Upgrade supplier status by 5 (i.e., if it was 10 before, than upgrade it to 15; if it
was 15 before than upgrade it to 20, etc.) for suppliers who have supplied more
than 500 units of parts.
3. Upgrade supplier status to 5 for suppliers who have supplied parts to 2 or
more cities.
4. Display the total quantity each supplier supplies parts for suppliers who have
placed 3 or more times of orders
5. Display supplier who supplies the most average quantity.

Union, Intersect, Minus


select column, column, column, ..... from table
UNION
select column, column, column, .... from table;
select column, column, column, ..... from table
INTERSECT
select column, column, column, .... from table;
select column, column, column, ..... from table
MINUS
select column, column, column, .... from table;

28

STUDENT
SNU
M
1111
2222
3333

SNAM
E
Andy
Betty
Cinty

ENROLL
SNU
M
....
1111
1111
2222
2222
1111
1111
1111
2222
....

CALLNU
M
....
10110
10125
10110
10130
10135
10140
10155
10160
....

COURSE
GRAD
E
....
F
F
A
A

CALLNU
M
10110
10115
10120
10125
10130
10135
10140
10150
10155
10160

COURS
E
IS 380
IS 380
IS 480
IS 385
IS 385
IS 380
IS 380
IS 380
IS 385
IS 480

SECTIO
N
1
2
1
1
2
1
2
3
1
1

SEMESTE
R
Sp 2000
Sp 2000
Sp 2000
Sp 2000
Sp 2000
Fa 2000
Fa 2000
Fa 2000
Fa 2000
Fa 2000

Exercise 8.3

1. In one command, display the number of students who got A, B, C, D, and Fs


in IS 380 class (callnum 10110).
2. In one command, display the number of students who got A, B, C, D, and Fs
of all IS 380 classes in Spirng 2000.
3. Display enrollment by callnum.
4. Display enrollment by course, semester.
5. Display enrollment by semester.
6. Display how many courses student 1111 has enrolled in Fall 2000.
7. Display number of sections each class has offered in Fall 2000.
8. Display the number of times Andy has enrolled in IS 380.
9. Display the highest grade that Andy has ever received on IS 380.
10. Display number of times that Andy has taken each course.
11. Display courses that Andy has taken for 2 or more times.
12. Display students who have taken any classes 2 or more times.
29

13. Display courses where there is any NULL grade.


14. Displays students who are in both IS 380 and IS 385.
15. Display courses that have 2 or more students enrolled.
16. Display courses where no student has enrolled.
17. Display student who are currently enrolled in multiple sections of the same
course.

30

Homework 1. Advanced SQL


Due Date:

Part I.
1. Identify the Primary Key and Foreign Keys of the STUDENT, ENROLL, and
COURSE tables.
2. Write a program create.sql that creates the three tables STUDENT, ENROLL,
and COURSE in the correct sequence with appropriate primary key and foreign
key declaration.
3. Make sure you set echo on and spool. Run create.sql until there is no error.
Turn in a print out of your spool file.

Part II.
Assume you have entered data in the three tables.
3. Write a program query.sql that answers the questions
Exercise 5.3.

of

4. Make sure you set echo on and spool. Run query.sql until there is no error. Turn
in a print out of your spool file.

31

Potrebbero piacerti anche