Você está na página 1de 2

Assignment #6

DML, DDL, and Views


1) Create an empty table called SECTION2 with the same structure as the SECTION table. Create a
view on the SECTION2 table that will be used for updating the table and restricts updates to sections
with capacities that are less than 25. Write two INSERT statements one that succeeds and one
that fails and that uses the view to insert into the SECTION2 table.
CREATE
SELECT
FROM
WHERE

TABLE section2 AS
*
section
1=2

CREATE VIEW v_section2 AS


SELECT *
FROM section2
WHERE
capacity < 25
WITH CHECK OPTION
-- should succeed
INSERT INTO v_section2
VALUES (1,1,1,sysdate,NULL,102,15,user,sysdate,user,sysdate)
--should fail
INSERT INTO v_section2
VALUES (1,1,1,sysdate,NULL,102,30,user,sysdate,user,sysdate)
2) Create a SQL script file that will
a)
b)

Create a view that will display the names of courses taught by Anita Morris
Display that data from the view.

You should be able to run the script from the SQL*Plus command line with the @ symbol or the "run"
command.
CREATE
SELECT
FROM
WHERE

OR REPLACE VIEW v_morris AS


course_no, description
course c
EXISTS (SELECT null
FROM section s, instructor i
WHERE s.instructor_id = i.instructor_id
AND c.course_no = s.course_no
AND last_name = 'Morris'
AND first_name = 'Anita')

SELECT *
FROM v_morris
3) Write a table creation script for a table called OWNER. Include the following:
Column for the owner id; primary key
Column for the owner username (i.e., the logon name); unique and not null
Column for the owners first name; can be null
Column for the owners last name; can be null
Determine the appropriate data types.

CREATE TABLE owner (


owner_id
NUMBER NOT NULL
CONSTRAINT OWNER_PK PRIMARY KEY,
user_name
VARCHAR2(30) NOT NULL
CONSTRAINT OWNER_UK1 UNIQUE,
first_name VARCHAR2(30) NULL,
last_name
VARCHAR2(30) NULL)
a) Add a row to OWNER using the following values:
Owner id = 20
Owner username = Cartman_E
Owner first name = Eric
Owner last name = Cartman
INSERT INTO owner
VALUES (20,'Cartman_E','Eric','Cartman')
COMMIT
b) Create a sequence called SEQ_OWNER. Write a SELECT statement to find out the highest
value currently in the OWNER table. Start the sequence at that value plus one when you create
it.
SELECT MAX(owner_id)
FROM owner
CREATE SEQUENCE seq_owner
START WITH 21
c) Add to OWNERS table all the entries in the STUDENT table using an INSERT INTOSELECT.
Use the sequence number to generate the owner id. Construct the owner username using string
functions and appending the student id to insure uniqueness (use the format
MORRISON_A_122).
INSERT INTO owner
SELECT seq_owner.NEXTVAL,
last_name||'_'||SUBSTR(first_name,1,1)||TO_CHAR(student_id),
first_name, last_name
FROM student
SELECT *
FROM owner

Você também pode gostar