Lab 6

advertisement
IST 318
3/4/2003
Lab 6
Other Database Objects
Initialize a iSQL*Plus session using the user ID and password provided by the instructor.
Write SQL statements to accomplish the following tasks. You may write your code with
your favorite editor (e.g., Notepad), and then copy and paste into the iSQL*Plus window
to execute. Cut and paste the results right after your code. Print out the file (with your
code and results) after you are done with this lab.
Part I – Using a Sequence to Generate Keys
1. Run the following CREATE SEQUENCE statement to create a sequence which will
be used next to generate auto-numbers for keys in tables used in this lab.
CREATE SEQUENCE oid_seq
INCREMENT BY 1
START WITH 1000
MAXVALUE 9999
NOCACHE
NOCYCLE
The NOCYCLE option means that this sequence can only generate integers from 1000
to 9999, incremented by one each time the NEXTVAL is called. Each time the
number will be undated to the underlying table, not just stored in memory (i.e.,
NOCACHE).
2. Use the following query to verify your sequence in the USER_SEQUENCES data
dictionary table.
SELECT
FROM
sequence_name, min_value, max_value,
increment_by, last_number
user_sequences;
Although it is called last_number, the corresponding value actually tells you the
next available sequence number. To verify this, run the following query which
retrieves the next number by using a dummy table DUAL.
SELECT
FROM
oid_seq.nextval
dual;
3. Since each time oid_seq.nextval is referenced, a unique number will be
generated. If you want to reference the value just generated, you may use oid_
seq.currval.
Create tables CUSTOMER and BRANCH, by running script labeled #3a in the
lab6.sql file. Complete the INSERT statements under #3b to add one record to
each table, which use currval and nextvar for keys. Verify the records you just
inserted.
4. Roll back the two records you just inserted and then run the script to insert them
again. This time use nextval in both cases. See the difference in PK values.
Although you may roll back the records you entered, you may not force the sequence
generate the numbers you used last time.
-1-
IST 318
3/4/2003
Part II – Using Constraints
5. Create table ACCOUNT with four constraints specified each in a different way:
a) a PRIMARY KEY constraint and two NOT NULL constraints specified in line;
b) an anonymous FOREIGN KEY constraint; and
c) a CHECK constraint to safeguard a nonnegative balance value is specified with the
CONSTRAINT keyword and a name.
CREATE TABLE
account_id
account_num
branch_id
balance
FOREIGN KEY
CONSTRAINT
account (
number(4) PRIMARY KEY,
char(5) NOT NULL,
number(4),
number(10,2) NOT NULL,
(branch_id) REFERENCES branch(branch_id),
acct_balance_min CHECK (balance >= 0));
5.1 Use the following two invalid insertion attempts to verify constraints are
correctly set up.
INSERT INTO account
values (oid_seq.nextval, 'A-101', 1004, -100);
and
INSERT INTO account
values (oid_seq.nextval, 'A-101', 1000, 100);
5.2 Be careful when specifying foreign keys. Although the last insertion was
rejected due to invalid foreign key reference, it doesn’t prevent a NULL value to
be inserted for the branch_id column in an ACCOUNT record. Run the
following insert statement and don’t be surprised when it gets through.
INSERT INTO account
values (oid_seq.nextval, 'A-101', NULL, 100);
What do you need to do now? Delete all records from the ACCOUNT table and
add a NOT NULL constraint to the branch_id field.
ALTER TABLE account
MODIFY
(BRANCH_ID number(4) NOT NULL);
6. Create table DEPOSITOR using the following statement, which specifies that
deletions to an ACCOUNT or CUSTOMER record will be cascaded to this table.
CREATE TABLE
customer_id
account_id
FOREIGN KEY
FOREIGN KEY
depositor (
number(4) NOT NULL,
number(4) NOT NULL,
(customer_id) REFERENCES customer(customer_id)
ON DELETE CASCADE,
(account_id) REFERENCES account(account_id)
ON DELETE CASCADE);
7. Modify the DEPOSITOR table to add the column DATE_CREATED as DATE type,
with a default value obtained from the system when a record is added to the table.
ALTER TABLE depositor
ADD
(date_created DATE DEFAULT sysdate);
8. You may view constraints by running the following query
SELECT
FROM
WHERE
constraint_name, constraint_type, search_condition
user_constraints
table_name = 'ACCOUNT';
-2-
IST 318
3/4/2003
9. Use the following four .sql files, in the given order, to load data into the four tables
you’ve created. These files can be generated from the corresponding raw data file
(the .txt files as in Lab6 folder) by using Excel or a high-level programming language
such as Java.
branch.sql
customer.sql
account.sql
depositor.sql
Check with your instructor if you’re interested in formatting the .sql files.
Part III – Create and Use Views
10. Read pp152-4 in the DBA text to see what views can be used for. Run the following
statement to create a view which set column level security on the ACCOUNT table.
The balance column will be hidden from users who can access the ACCT_VW instead
of the ACCOUNT table.
CREATE VIEW cust_branch_vw
AS (
SELECT customer_name, branch_name
FROM
customer c, depositor d, account a, branch b
WHERE
c.customer_id = d.customer_id
AND
a.account_id = d.account_id
AND
b.branch_id = a.branch_id);
This view also hide the complexity from its users: to them, the two ids seem to reside
together. But behind the scene is a four-way join.
11. You can then use the view as if it were a table. You may describe the structure of the
view, or retrieve data from it. Run the following queries and record the results.
DESC cust_branch_vw
and
SELECT *
FROM
cust_branch_vw;
12. A view is also good for displaying calculated values. Create a view as follows to
display the total deposit for each branch.
CREATE VIEW
AS (
SELECT
FROM
WHERE
GROUP BY
deposit_by_branch_vw
branch_name, SUM(balance)
account a, branch b
b.branch_id = a.branch_id
branch_name);
Select all from this view and attach your result.
13. Top-N Analysis. The following query can display the top three balances together
with the associated customer name.
SELECT
FROM
WHERE
ROWNUM as
(SELECT
FROM
WHERE
AND
ORDER BY
ROWNUM <=
RANK, customer_name, balance
customer_name, balance
customer c, depositor d, account a
c.customer_id = d.customer_id
a.account_id = d.account_id
balance DESC)
3;
-3-
IST 318
3/4/2003
The subquery orders the balances in descending order from which the main query can
select and assign a rank to it. The ROWNUM pseudocolumn assigns a sequential value
starting with 1 to each of the rows returned from the subquery.
13.1 Finding the set of smallest values is also considered Top-N analysis. Write a
query that returns the three lowest assets from all branches.
14. A synonym can be used to rename a DB object for your convenience. The following
example renames the EMPLOYEES table owned by the user Scott to EMPL.
CREATE SYNONYM empl
FOR scott.employees;
Deliverables
A hard copy of the code and results.
Due by 3/18/03, at the beginning of class.
Appendix – Sample Data Used In Lab 6
Content of Table CUSTOMER
CUSTOMER_ID CUSTOMER_NAME
1028 Adams
1029 Brooks
1030 Curry
1031 Glenn
1032 Green
1033 Hayes
1034 Johnson
1035 Jones
1036 Lindsay
1037 Smith
1038 Turner
1039 Williams
CUSTOMER_STREET
Spring
Senator
North
Sand Hill
Walnut
Main
Alma
Main
Park
North
Putnam
Nassau
12 rows selected.
-4-
CUSTOMER_CITY
Pittsfield
Brooklyn
Rye
Woodside
Stamford
Harrison
Palo Alto
Harrison
Pittsfield
Rye
Stamford
Princeton
IST 318
3/4/2003
Content of Table BRANCH
BRANCH_ID
1008
1009
1010
1011
1012
1013
1014
1015
BRANCH_NAME
Brighton
Downtown
Mianus
North Town
Perryridge
Pownal
Redwood
Round Hill
BRANCH_CITY
Brooklyn
Brooklyn
Horseneck
Rye
Horseneck
Bennington
Palo Alto
Horseneck
ASSETS
7100000
9000000
400000
3700000
1700000
300000
2100000
8000000
8 rows selected.
Content of Table ACCOUNT
ACCOUNT_ID
BRANCH_ID
1041
1042
1043
1044
1045
1046
1047
BALANCE
1009
1012
1008
1010
1008
1014
1015
ACCOU
500 A-101
400 A-102
900 A-201
700 A-215
750 A-217
700 A-222
350 A-305
7 rows selected.
Content of Table DEPOSITOR
CUSTOMER_ID
ACCOUNT_ID
1033
1034
1034
1035
1036
1037
1038
1042
1041
1043
1045
1046
1044
1047
7 rows selected.
-5-
DATE_CREA
04-MAR-03
04-MAR-03
04-MAR-03
04-MAR-03
04-MAR-03
04-MAR-03
04-MAR-03
Download