0% found this document useful (0 votes)
8 views

Q 1

The document contains SQL statements to create database tables and insert sample data into them. Tables are created for locations, faculty, students, terms, courses, course sections, and enrollments. Data is then inserted into these tables.

Uploaded by

Gunjan Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Q 1

The document contains SQL statements to create database tables and insert sample data into them. Tables are created for locations, faculty, students, terms, courses, course sections, and enrollments. Data is then inserted into these tables.

Uploaded by

Gunjan Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

SQL> DROP USER c##gunjan CASCADE;

User dropped.

SQL> CREATE USER c##gunjan IDENTIFIED BY gunjan;

User created.

SQL> GRANT connect , resource TO c##gunjan;

Grant succeeded.

SQL> ALTER USER c##gunjan QUOTA 100M on USERs;

User altered.

SQL> connect c##gunjan/gunjan;


Connected.
SQL> SELECT TO_CHAR(sysdate,'DD MM YYY Day HH:MI:SS Am' ) FROM dual;

TO_CHAR(SYSDATE,'DDMMYYYDAYHH:MI:SSAM')
----------------------------------------------------------
10 03 024 Sunday 02:21:10 Pm

SQL> -- script to create NORTHWOODS database


SQL> -- revised 06/12/2023 Huu Con Nguyen
SQL> connect sys/sys as sysdba;
Connected.
SQL> DROP USER c##des03 CASCADE;

User dropped.

SQL> CREATE USER c##des03 IDENTIFIED BY tiger;

User created.

SQL> GRANT connect , resource, create view TO c##des03;

Grant succeeded.

SQL> ALTER USER c##des03 QUOTA 100M on USERs;

User altered.

SQL> connect c##des03/tiger;


Connected.
SQL>
SQL>
SQL> DROP TABLE enrollment CASCADE CONSTRAINTS;
DROP TABLE enrollment CASCADE CONSTRAINTS
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> DROP TABLE course_section CASCADE CONSTRAINTS;


DROP TABLE course_section CASCADE CONSTRAINTS
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> DROP TABLE term CASCADE CONSTRAINTS;


DROP TABLE term CASCADE CONSTRAINTS
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> DROP TABLE course CASCADE CONSTRAINTS;


DROP TABLE course CASCADE CONSTRAINTS
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> DROP TABLE student CASCADE CONSTRAINTS;


DROP TABLE student CASCADE CONSTRAINTS
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> DROP TABLE faculty CASCADE CONSTRAINTS;


DROP TABLE faculty CASCADE CONSTRAINTS
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> DROP TABLE location CASCADE CONSTRAINTS;


DROP TABLE location CASCADE CONSTRAINTS
*
ERROR at line 1:
ORA-00942: table or view does not exist

SQL>
SQL> CREATE TABLE LOCATION
2 (loc_id NUMBER(6),
3 bldg_code VARCHAR2(10),
4 room VARCHAR2(6),
5 capacity NUMBER(5),
6 CONSTRAINT location_loc_id_pk PRIMARY KEY (loc_id));

Table created.

SQL>
SQL> CREATE TABLE faculty
2 (f_id NUMBER(6),
3 f_last VARCHAR2(30),
4 f_first VARCHAR2(30),
5 f_mi CHAR(1),
6 loc_id NUMBER(5),
7 f_phone VARCHAR2(10),
8 f_rank VARCHAR2(8),
9 f_pin NUMBER(4),
10 f_image BLOB,
11 CONSTRAINT faculty_f_id_pk PRIMARY KEY(f_id),
12 CONSTRAINT faculty_loc_id_fk FOREIGN KEY (loc_id) REFERENCES
location(loc_id));

Table created.

SQL>
SQL> CREATE TABLE student
2 (s_id NUMBER(6),
3 s_last VARCHAR2(30),
4 s_first VARCHAR2(30),
5 s_mi CHAR(1),
6 s_address VARCHAR2(25),
7 s_city VARCHAR2(20),
8 s_state CHAR(2),
9 s_zip VARCHAR2(10),
10 s_phone VARCHAR2(10),
11 s_class CHAR(2),
12 s_dob DATE,
13 s_pin NUMBER(4),
14 f_id NUMBER(6),
15 time_enrolled INTERVAL YEAR TO MONTH,
16 CONSTRAINT student_s_id_pk PRIMARY KEY (s_id),
17 CONSTRAINT student_f_id_fk FOREIGN KEY (f_id) REFERENCES faculty(f_id));

Table created.

SQL>
SQL> CREATE TABLE TERM
2 (term_id NUMBER(6),
3 term_desc VARCHAR2(20),
4 status VARCHAR2(20),
5 CONSTRAINT term_term_id_pk PRIMARY KEY (term_id),
6 CONSTRAINT term_status_cc CHECK ((status = 'OPEN') OR (status = 'CLOSED')));

Table created.

SQL>
SQL> CREATE TABLE COURSE
2 (course_id NUMBER(6),
3 call_id VARCHAR2(10),
4 course_name VARCHAR2(25),
5 credits NUMBER(2),
6 CONSTRAINT course_course_id_pk PRIMARY KEY(course_id));

Table created.

SQL>
SQL> CREATE TABLE COURSE_SECTION
2 (c_sec_id NUMBER(6),
3 course_id NUMBER(6) CONSTRAINT course_section_courseid_nn NOT NULL,
4 term_id NUMBER(6) CONSTRAINT course_section_termid_nn NOT NULL,
5 sec_num NUMBER(2) CONSTRAINT course_section_secnum_nn NOT NULL,
6 f_id NUMBER(5),
7 c_sec_day VARCHAR2(10),
8 c_sec_time DATE,
9 c_sec_duration INTERVAL DAY TO SECOND,
10 loc_id NUMBER(6),
11 max_enrl NUMBER(4) CONSTRAINT course_section_maxenrl_nn NOT NULL,
12 CONSTRAINT course_section_csec_id_pk PRIMARY KEY (c_sec_id),
13 CONSTRAINT course_section_cid_fk FOREIGN KEY (course_id) REFERENCES
course(course_id),
14 CONSTRAINT course_section_loc_id_fk FOREIGN KEY (loc_id) REFERENCES
location(loc_id),
15 CONSTRAINT course_section_termid_fk FOREIGN KEY (term_id) REFERENCES
term(term_id),
16 CONSTRAINT course_section_fid_fk FOREIGN KEY (f_id) REFERENCES faculty(f_id));

Table created.

SQL>
SQL> CREATE TABLE ENROLLMENT
2 (s_id NUMBER(6),
3 c_sec_id NUMBER(6),
4 grade CHAR(1),
5 CONSTRAINT enrollment_pk PRIMARY KEY (s_id, c_sec_id),
6 CONSTRAINT enrollment_sid_fk FOREIGN KEY (s_id) REFERENCES student(s_id),
7 CONSTRAINT enrollment_csecid_fk FOREIGN KEY (c_sec_id) REFERENCES
course_section (c_sec_id));

Table created.

SQL>
SQL>
SQL>
SQL> ---- inserting into LOCATION table
SQL> INSERT INTO location VALUES
2 (1, 'CR', '101', 150);

1 row created.

SQL>
SQL> INSERT INTO location VALUES
2 (2, 'CR', '202', 40);

1 row created.

SQL>
SQL> INSERT INTO location VALUES
2 (3, 'CR', '103', 35);

1 row created.

SQL>
SQL> INSERT INTO location VALUES
2 (4, 'CR', '105', 35);

1 row created.

SQL>
SQL> INSERT INTO location VALUES
2 (5, 'BUS', '105', 42);

1 row created.

SQL>
SQL> INSERT INTO location VALUES
2 (6, 'BUS', '404', 35);
1 row created.

SQL>
SQL> INSERT INTO location VALUES
2 (7, 'BUS', '421', 35);

1 row created.

SQL>
SQL> INSERT INTO location VALUES
2 (8, 'BUS', '211', 55);

1 row created.

SQL>
SQL> INSERT INTO location VALUES
2 (9, 'BUS', '424', 1);

1 row created.

SQL>
SQL> INSERT INTO location VALUES
2 (10, 'BUS', '402', 1);

1 row created.

SQL>
SQL> INSERT INTO location VALUES
2 (11, 'BUS', '433', 1);

1 row created.

SQL>
SQL> INSERT INTO location VALUES
2 (12, 'LIB', '217', 2);

1 row created.

SQL>
SQL> INSERT INTO location VALUES
2 (13, 'LIB', '222', 1);

1 row created.

SQL>
SQL>
SQL> --- inserting records into FACULTY
SQL> INSERT INTO faculty VALUES
2 (1, 'Cox', 'Kim', 'J', 9, '7155551234', 'ASSO', 1181, EMPTY_BLOB());

1 row created.

SQL>
SQL> INSERT INTO faculty VALUES
2 (2, 'Blanchard', 'John', 'R', 10, '7155559087', 'FULL', 1075, EMPTY_BLOB());

1 row created.
SQL>
SQL> INSERT INTO faculty VALUES
2 (3, 'Williams', 'Jerry', 'F', 12, '7155555412', 'ASST', 8531, EMPTY_BLOB());

1 row created.

SQL>
SQL> INSERT INTO faculty VALUES
2 (4, 'Sheng', 'Laura', 'M', 11, '7155556409', 'INST', 1690, EMPTY_BLOB());

1 row created.

SQL>
SQL> INSERT INTO faculty VALUES
2 (5, 'Brown', 'Philip', 'E', 13, '7155556082', 'ASSO', 9899, EMPTY_BLOB());

1 row created.

SQL>
SQL> --- inserting records into STUDENT
SQL> INSERT INTO student VALUES
2 (1, 'Miller', 'Sarah', 'M', '144 Windridge Blvd.', 'Eau Claire',
3 'WI', '54703', '7155559876', 'SR', TO_DATE('07/14/1985', 'MM/DD/YYYY'), 8891,
1, TO_YMINTERVAL('3-2'));

1 row created.

SQL>
SQL> INSERT INTO student VALUES
2 (2, 'Umato', 'Brian', 'D', '454 St. John''s Place', 'Eau Claire',
3 'WI', '54702', '7155552345', 'SR', TO_DATE('08/19/1985', 'MM/DD/YYYY'), 1230,
1, TO_YMINTERVAL('4-2'));

1 row created.

SQL>
SQL> INSERT INTO student VALUES
2 (3, 'Black', 'Daniel', NULL, '8921 Circle Drive', 'Bloomer',
3 'WI', '54715', '7155553907', 'JR', TO_DATE('10/10/1982', 'MM/DD/YYYY'), 1613,
1, TO_YMINTERVAL('3-0'));

1 row created.

SQL>
SQL> INSERT INTO student VALUES
2 (4, 'Mobley', 'Amanda', 'J', '1716 Summit St.', 'Eau Claire',
3 'WI', '54703', '7155556902', 'SO', TO_DATE('09/24/1986', 'MM/DD/YYYY'), 1841,
2, TO_YMINTERVAL('2-2'));

1 row created.

SQL>
SQL> INSERT INTO student VALUES
2 (5, 'Sanchez', 'Ruben', 'R', '1780 Samantha Court', 'Eau Claire',
3 'WI', '54701', '7155558899', 'SO', TO_DATE('11/20/1986', 'MM/DD/YYYY'), 4420,
4, TO_YMINTERVAL('1-11'));

1 row created.
SQL>
SQL> INSERT INTO student VALUES
2 (6, 'Connoly', 'Michael', 'S', '1818 Silver Street', 'Elk Mound',
3 'WI', '54712', '7155554944', 'FR', TO_DATE('12/4/1986', 'MM/DD/YYYY'), 9188,
3, TO_YMINTERVAL('0-4'));

1 row created.

SQL>
SQL> --- inserting records into TERM
SQL> INSERT INTO term VALUES
2 (1, 'Fall 2005', 'CLOSED');

1 row created.

SQL>
SQL> INSERT INTO term VALUES
2 (2, 'Spring 2006', 'CLOSED');

1 row created.

SQL>
SQL> INSERT INTO term VALUES
2 (3, 'Summer 2006', 'CLOSED');

1 row created.

SQL>
SQL> INSERT INTO term VALUES
2 (4, 'Fall 2006', 'CLOSED');

1 row created.

SQL>
SQL> INSERT INTO term VALUES
2 (5, 'Spring 2007', 'CLOSED');

1 row created.

SQL>
SQL> INSERT INTO term VALUES
2 (6, 'Summer 2007', 'OPEN');

1 row created.

SQL>
SQL> --- inserting records into COURSE
SQL> INSERT INTO course VALUES
2 (1, 'MIS 101', 'Intro. to Info. Systems', 3);

1 row created.

SQL>
SQL> INSERT INTO course VALUES
2 (2, 'MIS 301', 'Systems Analysis', 3);

1 row created.

SQL>
SQL> INSERT INTO course VALUES
2 (3, 'MIS 441', 'Database Management', 3);

1 row created.

SQL>
SQL> INSERT INTO course VALUES
2 (4, 'CS 155', 'Programming in C++', 3);

1 row created.

SQL>
SQL> INSERT INTO course VALUES
2 (5, 'MIS 451', 'Web-Based Systems', 3);

1 row created.

SQL>
SQL> --- inserting records into COURSE_SECTION
SQL> INSERT INTO course_section VALUES
2 (1, 1, 4, 1, 2, 'MWF', TO_DATE('10:00 AM', 'HH:MI AM'), TO_DSINTERVAL('0
00:00:50.00'), 1, 140);

1 row created.

SQL>
SQL> INSERT INTO course_section VALUES
2 (2, 1, 4, 2, 3, 'TR', TO_DATE('09:30 AM', 'HH:MI AM'), TO_DSINTERVAL('0
00:01:15.00'), 7, 35);

1 row created.

SQL>
SQL> INSERT INTO course_section VALUES
2 (3, 1, 4, 3, 3, 'MWF', TO_DATE('08:00 AM', 'HH:MI AM'), TO_DSINTERVAL('0
00:00:50.00'), 2, 35);

1 row created.

SQL>
SQL> INSERT INTO course_section VALUES
2 (4, 2, 4, 1, 4, 'TR', TO_DATE('11:00 AM', 'HH:MI AM'), TO_DSINTERVAL('0
00:01:15.00'), 6, 35);

1 row created.

SQL>
SQL> INSERT INTO course_section VALUES
2 (5, 2, 5, 2, 4, 'TR', TO_DATE('02:00 PM', 'HH:MI PM'), TO_DSINTERVAL('0
00:01:15.00'), 6, 35);

1 row created.

SQL>
SQL> INSERT INTO course_section VALUES
2 (6, 3, 5, 1, 1, 'MWF', TO_DATE('09:00 AM', 'HH:MI AM'), TO_DSINTERVAL('0
00:00:50.00'), 5, 30);

1 row created.
SQL>
SQL> INSERT INTO course_section VALUES
2 (7, 3, 5, 2, 1, 'MWF', TO_DATE('10:00 AM', 'HH:MI AM'), TO_DSINTERVAL('0
00:00:50.00'), 5, 30);

1 row created.

SQL>
SQL> INSERT INTO course_section VALUES
2 (8, 4, 5, 1, 5, 'TR', TO_DATE('08:00 AM', 'HH:MI AM'), TO_DSINTERVAL('0
00:01:15.00'), 3, 35);

1 row created.

SQL>
SQL> INSERT INTO course_section VALUES
2 (9, 5, 5, 1, 2, 'MWF', TO_DATE('02:00 PM', 'HH:MI PM'), TO_DSINTERVAL('0
00:00:50.00'), 5, 35);

1 row created.

SQL>
SQL> INSERT INTO course_section VALUES
2 (10, 5, 5, 2, 2, 'MWF', TO_DATE('03:00 PM', 'HH:MI PM'), TO_DSINTERVAL('0
00:00:50.00'), 5, 35);

1 row created.

SQL>
SQL> INSERT INTO course_section VALUES
2 (11, 1, 6, 1, 1, 'MTWRF', TO_DATE('08:00 AM', 'HH:MI AM'), TO_DSINTERVAL('0
00:01:30.00'), 1, 50);

1 row created.

SQL>
SQL> INSERT INTO course_section VALUES
2 (12, 2, 6, 1, 2, 'MTWRF', TO_DATE('08:00 AM', 'HH:MI AM'), TO_DSINTERVAL('0
00:01:30.00'), 6, 35);

1 row created.

SQL>
SQL> INSERT INTO course_section VALUES
2 (13, 3, 6, 1, 3, 'MTWRF', TO_DATE('09:00 AM', 'HH:MI AM'), TO_DSINTERVAL('0
00:01:30.00'), 5, 35);

1 row created.

SQL>
SQL> --- inserting records into ENROLLMENT
SQL> INSERT INTO enrollment VALUES
2 (1, 1, 'A');

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (1, 4, 'A');

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (1, 6, 'B');

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (1, 9, 'B');

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (2, 1, 'C');

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (2, 5, 'B');

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (2, 6, 'A');

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (2, 9, 'B');

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (3, 1, 'C');

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (3, 12, NULL);

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (3, 13, NULL);

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (4, 11, NULL);

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (4, 12, NULL);

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (5, 1, 'B');

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (5, 5, 'C');

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (5, 9, 'C');

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (5, 11, NULL);

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (5, 13, NULL);

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (6, 11, NULL);

1 row created.

SQL>
SQL> INSERT INTO enrollment VALUES
2 (6, 12, NULL);

1 row created.

SQL>
SQL> COMMIT;

Commit complete.

SQL> CREATE OR REPLACE PROCEDURE display_faculty_students


2 AS
3 BEGIN
4 FOR faculty_rec IN (SELECT f_id, f_last, f_first, f_rank FROM faculty)
5 LOOP
6 DBMS_OUTPUT.PUT_LINE('Faculty Member: ' || faculty_rec.f_first || ' '
|| faculty_rec.f_last || ', Rank: ' || faculty_rec.f_rank);
7 DBMS_OUTPUT.PUT_LINE('------------------');
8
9 FOR student_rec IN (SELECT s_id, s_last, s_first, s_dob, s_class
10 FROM student
11 WHERE f_id = faculty_rec.f_id)
12 LOOP
13 DBMS_OUTPUT.PUT_LINE('Student ID: ' || student_rec.s_id || ',
Name: ' || student_rec.s_first || ' ' || student_rec.s_last || ', Birthdate: ' ||
student_rec.s_dob || ', Class: ' || student_rec.s_class);
14 END LOOP;
15
16 DBMS_OUTPUT.PUT_LINE('');
17 END LOOP;
18 END;
19 /

Procedure created.

SQL> SET SERVEROUTPUT ON;


SQL> EXEC display_faculty_students
Faculty Member: Kim Cox, Rank: ASSO
------------------
Student ID: 1, Name: Sarah Miller, Birthdate: 14-JUL-85, Class: SR
Student ID: 2, Name: Brian Umato, Birthdate: 19-AUG-85, Class: SR
Student ID: 3, Name: Daniel Black, Birthdate: 10-OCT-82, Class: JR
Faculty Member: John Blanchard, Rank: FULL
------------------
Student ID: 4, Name: Amanda Mobley, Birthdate: 24-SEP-86, Class: SO
Faculty Member: Jerry Williams, Rank: ASST
------------------
Student ID: 6, Name: Michael Connoly, Birthdate: 04-DEC-86, Class: FR
Faculty Member: Laura Sheng, Rank: INST
------------------
Student ID: 5, Name: Ruben Sanchez, Birthdate: 20-NOV-86, Class: SO
Faculty Member: Philip Brown, Rank: ASSO
------------------

PL/SQL procedure successfully completed.

SQL> spool off

You might also like