Creating Oracle tables for Intercollegiate Athletic Database.
Part of Assignment one of Data Warehousing for Business Intelligence on Coursera
--1 CREATE TABLE FACILITY CREATE TABLE FACILITY( FACNO VARCHAR(12), FACNAME VARCHAR(255) NOT NULL, CONSTRAINT PK_FAC PRIMARY KEY (FACNO) ); --2.REATE TABLE EMPLOYEE CREATE TABLE EMPLOYEE( EMPNO VARCHAR(12), EMPNAME VARCHAR(255) NOT NULL, DEPARTMENT VARCHAR(100) NOT NULL, EMAIL VARCHAR(50) NOT NULL, PHONE VARCHAR(30) NOT NULL, CONSTRAINT PK_EMP PRIMARY KEY (EMPNO), CONSTRAINT CHK_EMP CHECK(LENGTHB(PHONE) BETWEEN 5 AND 30) ); --3 RESOURCETBL CREATE TABLE RESOURCETBL( RESNO VARCHAR(12), RESNAME VARCHAR(255) NOT NULL, RATE NUMBER(8,2) NOT NULL, CONSTRAINT PK_RES PRIMARY KEY (RESNO) ); /* ALTER TABLE CUSTOMER drop constraint CHK_CUS_ZIP; ALTER TABLE CUSTOMER ADD CONSTRAINT CHK_CUS_ZIP CHECK(LENGTHB(ZIP) <=10) */ --4 CUSTOMER CREATE TABLE CUSTOMER( CUSTNO VARCHAR(12), CUSTNAME VARCHAR(255) NOT NULL, ADDRESS VARCHAR(255) NOT NULL, INTERNAL CHAR(1) NOT NULL, CONTACT VARCHAR(255) NOT NULL, PHONE VARCHAR(30) NOT NULL, CITY VARCHAR(50) NOT NULL, STATE CHAR(2) NOT NULL, ZIP INTEGER NOT NULL, CONSTRAINT PK_CUS PRIMARY KEY (CUSTNO), CONSTRAINT CHK_CUS CHECK (LENGTHB(PHONE) BETWEEN 5 AND 30), CONSTRAINT CHK_CUS_ZIP CHECK(LENGTHB(ZIP) <=10) ); --5 LOCATION CREATE TABLE LOCATION( LOCNO VARCHAR(12), FACNO VARCHAR(12), LOCNAME VARCHAR(255) NOT NULL, CONSTRAINT PK_LOC PRIMARY KEY (LOCNO), CONSTRAINT FK_LOC FOREIGN KEY (FACNO) REFERENCES FACILITY ); -- 6 EVENTREQUEST CREATE TABLE EVENTREQUEST( EVENTNO VARCHAR(12), DATEHELD DATE NOT NULL, DATEREQ DATE NOT NULL, CUSTNO VARCHAR(12), FACNO VARCHAR(12), DATEAUTH DATE NOT NULL, STATUS VARCHAR(20) NOT NULL, ESTCOST NUMBER(8,2) NOT NULL, ESTAUDIENCE INTEGER NOT NULL, BUDNO VARCHAR(12), CONSTRAINT PK_EVR PRIMARY KEY (EVENTNO), CONSTRAINT FK_EVR_CUS FOREIGN KEY (CUSTNO) REFERENCES CUSTOMER, CONSTRAINT FK_EVR_FAC FOREIGN KEY (FACNO) REFERENCES FACILITY, CONSTRAINT CHK_AUD CHECK(ESTAUDIENCE >= 1), CONSTRAINT CHK_STU CHECK(STATUS IN ('Approved', 'Pending', 'Denied')) ); /** LET'S ALLOW NULL VALUES IN THE AUTHORISED DATE, AS we might not as yet have an authorised date**/ ALTER TABLE EVENTREQUEST MODIFY ( DATEAUTH NULL ); --7 EVENTPLAN CREATE TABLE EVENTPLAN( PLANNO VARCHAR(12), EVENTNO VARCHAR(12), WORKDATE DATE NOT NULL, NOTES VARCHAR(1000) NOT NULL, ACTIVITY VARCHAR(500) NOT NULL, EMPNO VARCHAR(12), CONSTRAINT PK_EVP PRIMARY KEY (PLANNO), CONSTRAINT FK_EVP_EVR FOREIGN KEY (EVENTNO) REFERENCES EVENTREQUEST ); /*ALLOW NULL VALUES IN NOTES COLUMN*/ ALTER TABLE EVENTPLAN MODIFY ( NOTES NULL ); -- 8 EVENTPLANLINE CREATE TABLE EVENTPLANLINE( PLANNO VARCHAR(12), LINENO INTEGER NOT NULL, TIMESTART DATE NOT NULL, TIMEEND DATE NOT NULL, NUMBERFLD INTEGER NOT NULL, LOCNO VARCHAR(12), RESNO VARCHAR(12), CONSTRAINT PK_EVPLL PRIMARY KEY (PLANNO, LINENO), CONSTRAINT FK_EVPLL_EVP FOREIGN KEY (PLANNO) REFERENCES EVENTPLAN, CONSTRAINT FK_EVPLL_LOC FOREIGN KEY (LOCNO) REFERENCES LOCATION, CONSTRAINT FK_EVPLL_RES FOREIGN KEY (RESNO) REFERENCES RESOURCETBL );
After creating the tables lets insert values
--INSERT RECORDS INTO EVENTPLANLINE Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P100',1, to_date('25-OCT-13 8:00:00','DD-MON-RR HH24:MI:SS'), to_date('25-OCT-13 17:00:00','DD-MON-RR HH24:MI:SS'),2,'L100','R100'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P100',2, to_date('25-OCT-13 12:00:00','DD-MON-RR HH24:MI:SS'),to_date('25-OCT-13 17:00:00','DD-MON-RR HH24:MI:SS'), 2,'L101','R101'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P100',3, to_date('25-OCT-13 7:00:00','DD-MON-RR HH24:MI:SS'), to_date('25-OCT-13 16:30:00','DD-MON-RR HH24:MI:SS'), 1,'L102','R102'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P100',4, to_date('25-OCT-13 18:00:00','DD-MON-RR HH24:MI:SS'),to_date('25-OCT-13 22:00:00','DD-MON-RR HH24:MI:SS'),2,'L100','R102'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P101',1, to_date('3-DEC-13 18:00:00','DD-MON-RR HH24:MI:SS'),to_date('3-DEC-13 20:00:00','DD-MON-RR HH24:MI:SS'),2,'L103','R100'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P101',2, to_date('3-DEC-13 18:30:00','DD-MON-RR HH24:MI:SS'),to_date('3-DEC-13 19:00:00','DD-MON-RR HH24:MI:SS'),4,'L105','R100'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P101',3, to_date('3-DEC-13 19:00:00','DD-MON-RR HH24:MI:SS'),to_date('3-DEC-13 20:00:00','DD-MON-RR HH24:MI:SS'),2,'L103','R103'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P102',1, to_date('5-DEC-13 18:00:00','DD-MON-RR HH24:MI:SS'),to_date('5-DEC-13 19:00:00','DD-MON-RR HH24:MI:SS'),2,'L103','R100'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P102',2, to_date('5-DEC-13 18:00:00','DD-MON-RR HH24:MI:SS'),to_date('5-DEC-13 21:00:00','DD-MON-RR HH24:MI:SS'),4,'L105','R100'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P102',3, to_date('5-DEC-13 19:00:00','DD-MON-RR HH24:MI:SS'),to_date('5-DEC-13 22:00:00','DD-MON-RR HH24:MI:SS'),2,'L103','R103'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P103',1, to_date('12-DEC-13 18:00:00','DD-MON-RR HH24:MI:SS'),to_date('12-DEC-13 21:00:00','DD-MON-RR HH24:MI:SS'),2,'L103','R100'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P103',2, to_date('12-DEC-13 18:00:00','DD-MON-RR HH24:MI:SS'),to_date('12-DEC-13 21:00:00','DD-MON-RR HH24:MI:SS'),4,'L105','R100'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P103',3, to_date('12-DEC-13 19:00:00','DD-MON-RR HH24:MI:SS'),to_date('12-DEC-13 22:00:00','DD-MON-RR HH24:MI:SS'),2,'L103','R103'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P104',1, to_date('26-OCT-13 18:00:00','DD-MON-RR HH24:MI:SS'),to_date('26-OCT-13 22:00:00','DD-MON-RR HH24:MI:SS'),4,'L101','R104'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P104',2, to_date('26-OCT-13 18:00:00','DD-MON-RR HH24:MI:SS'),to_date('26-OCT-13 22:00:00','DD-MON-RR HH24:MI:SS'),4,'L100','R104'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P105',1, to_date('25-OCT-13 18:00:00','DD-MON-RR HH24:MI:SS'),to_date('25-OCT-13 22:00:00','DD-MON-RR HH24:MI:SS'),4,'L101','R104'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P105',2, to_date('25-OCT-13 18:00:00','DD-MON-RR HH24:MI:SS'),to_date('25-OCT-13 22:00:00','DD-MON-RR HH24:MI:SS'),4,'L100','R104'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P199',1, to_date('10-DEC-13 8:00:00','DD-MON-RR HH24:MI:SS'), to_date('10-DEC-13 12:00:00','DD-MON-RR HH24:MI:SS'),1,'L100','R100'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P349',1, to_date('12-DEC-13 12:00:00','DD-MON-RR HH24:MI:SS'),to_date('12-DEC-13 15:30:00','DD-MON-RR HH24:MI:SS'),1,'L103','R100'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P85',1, to_date('25-OCT-13 9:00:00','DD-MON-RR HH24:MI:SS'), to_date('25-OCT-13 17:00:00','DD-MON-RR HH24:MI:SS'),5,'L100','R100'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P85',2, to_date('25-OCT-13 8:00:00','DD-MON-RR HH24:MI:SS'), to_date('25-OCT-13 17:00:00','DD-MON-RR HH24:MI:SS'),2,'L102','R101'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P85',3, to_date('25-OCT-13 10:00:00','DD-MON-RR HH24:MI:SS'), to_date('25-OCT-13 15:00:00','DD-MON-RR HH24:MI:SS'),3,'L104','R100'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P95',1, to_date('26-OCT-13 8:00:00','DD-MON-RR HH24:MI:SS'), to_date('26-OCT-13 17:00:00','DD-MON-RR HH24:MI:SS'),4,'L100','R100'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P95',2, to_date('26-OCT-13 9:00:00','DD-MON-RR HH24:MI:SS'), to_date('26-OCT-13 17:00:00','DD-MON-RR HH24:MI:SS'),4,'L102','R101'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P95',3, to_date('26-OCT-13 10:00:00','DD-MON-RR HH24:MI:SS'), to_date('26-OCT-13 15:00:00','DD-MON-RR HH24:MI:SS'),4,'L106','R100'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P95',4, to_date('26-OCT-13 13:00:00','DD-MON-RR HH24:MI:SS'), to_date('26-OCT-13 17:00:00','DD-MON-RR HH24:MI:SS'),2,'L100','R103'); Insert into EVENTPLANLINE (PLANNO,LINENO,TIMESTART,TIMEEND,NUMBERFLD,LOCNO,RESNO) values ('P95',5, to_date('26-OCT-13 13:00:00','DD-MON-RR HH24:MI:SS'), to_date('26-OCT-13 17:00:00','DD-MON-RR HH24:MI:SS'),2,'L101','R104'); -- INSERT RECORDS INTO EVENTPLAN TABLE Insert into EVENTPLAN (PLANNO,EVENTNO,WORKDATE,NOTES,ACTIVITY,EMPNO) values ('P100','E100','25-OCT-13','Standard operation','Operation','E102'); Insert into EVENTPLAN (PLANNO,EVENTNO,WORKDATE,NOTES,ACTIVITY,EMPNO) values ('P101','E104','03-DEC-13','Watch for gate crashers','Operation','E100'); Insert into EVENTPLAN (PLANNO,EVENTNO,WORKDATE,NOTES,ACTIVITY,EMPNO) values ('P102','E105','05-DEC-13','Standard operation','Operation','E102'); Insert into EVENTPLAN (PLANNO,EVENTNO,WORKDATE,NOTES,ACTIVITY,EMPNO) values ('P103','E106','12-DEC-13','Watch for seat switching','Operation',null); Insert into EVENTPLAN (PLANNO,EVENTNO,WORKDATE,NOTES,ACTIVITY,EMPNO) values ('P104','E101','26-OCT-13','Standard cleanup','Cleanup','E101'); Insert into EVENTPLAN (PLANNO,EVENTNO,WORKDATE,NOTES,ACTIVITY,EMPNO) values ('P105','E100','25-OCT-13','Light cleanup','Cleanup','E101'); Insert into EVENTPLAN (PLANNO,EVENTNO,WORKDATE,NOTES,ACTIVITY,EMPNO) values ('P199','E102','10-DEC-13','ABC','Operation','E101'); Insert into EVENTPLAN (PLANNO,EVENTNO,WORKDATE,NOTES,ACTIVITY,EMPNO) values ('P299','E101','26-OCT-13',null,'Operation','E101'); Insert into EVENTPLAN (PLANNO,EVENTNO,WORKDATE,NOTES,ACTIVITY,EMPNO) values ('P349','E106','12-DEC-13',null,'Setup','E101'); Insert into EVENTPLAN (PLANNO,EVENTNO,WORKDATE,NOTES,ACTIVITY,EMPNO) values ('P85','E100','25-OCT-13','Standard operation','Cleanup','E102'); Insert into EVENTPLAN (PLANNO,EVENTNO,WORKDATE,NOTES,ACTIVITY,EMPNO) values ('P95','E101','26-OCT-13','Extra security','Cleanup','E102'); -- INSERT RECORDS INTO EVENTREQUEST Insert into EVENTREQUEST (EVENTNO,DATEHELD,DATEREQ,CUSTNO,FACNO,DATEAUTH,STATUS,ESTCOST,ESTAUDIENCE,BUDNO) values ('E100','25-OCT-13','06-JUN-13','C100','F100','08-JUN-13','Approved',5000,80000,'B1000'); Insert into EVENTREQUEST (EVENTNO,DATEHELD,DATEREQ,CUSTNO,FACNO,DATEAUTH,STATUS,ESTCOST,ESTAUDIENCE,BUDNO) values ('E101','26-OCT-13','28-JUL-13','C100','F100','','Pending',5000,80000,'B1000'); Insert into EVENTREQUEST (EVENTNO,DATEHELD,DATEREQ,CUSTNO,FACNO,DATEAUTH,STATUS,ESTCOST,ESTAUDIENCE,BUDNO) values ('E103','21-SEP-13','28-JUL-13','C100','F100','01-AUG-13','Approved',5000,80000,'B1000'); Insert into EVENTREQUEST (EVENTNO,DATEHELD,DATEREQ,CUSTNO,FACNO,DATEAUTH,STATUS,ESTCOST,ESTAUDIENCE,BUDNO) values ('E102','14-SEP-13','28-JUL-13','C100','F100','31-JUL-13','Approved',5000,80000,'B1000'); Insert into EVENTREQUEST (EVENTNO,DATEHELD,DATEREQ,CUSTNO,FACNO,DATEAUTH,STATUS,ESTCOST,ESTAUDIENCE,BUDNO) values ('E104','03-DEC-13','28-JUL-13','C101','F101','31-JUL-13','Approved',2000,12000,'B1000'); Insert into EVENTREQUEST (EVENTNO,DATEHELD,DATEREQ,CUSTNO,FACNO,DATEAUTH,STATUS,ESTCOST,ESTAUDIENCE,BUDNO) values ('E105','05-DEC-13','28-JUL-13','C101','F101','01-AUG-13','Approved',2000,10000,'B1000'); Insert into EVENTREQUEST (EVENTNO,DATEHELD,DATEREQ,CUSTNO,FACNO,DATEAUTH,STATUS,ESTCOST,ESTAUDIENCE,BUDNO) values ('E106','12-DEC-13','28-JUL-13','C101','F101','31-JUL-13','Approved',2000,10000,'B1000'); Insert into EVENTREQUEST (EVENTNO,DATEHELD,DATEREQ,CUSTNO,FACNO,DATEAUTH,STATUS,ESTCOST,ESTAUDIENCE,BUDNO) values ('E107','23-NOV-13','28-JUL-13','C105','F100','31-JUL-13','Denied',10000,5000,null); -- INSERT RECORDS INTO LOCATION Insert into LOCATION (LOCNO,FACNO,LOCNAME) values ('L100','F100','Locker room'); Insert into LOCATION (LOCNO,FACNO,LOCNAME) values ('L101','F100','Plaza'); Insert into LOCATION (LOCNO,FACNO,LOCNAME) values ('L102','F100','Vehicle gate'); Insert into LOCATION (LOCNO,FACNO,LOCNAME) values ('L103','F101','Locker room'); Insert into LOCATION (LOCNO,FACNO,LOCNAME) values ('L104','F100','Ticket Booth'); Insert into LOCATION (LOCNO,FACNO,LOCNAME) values ('L105','F101','Gate'); Insert into LOCATION (LOCNO,FACNO,LOCNAME) values ('L106','F100','Pedestrian gate'); -- INSERT RECORDS INTO FACILITY Insert into FACILITY (FACNO,FACNAME) values ('F100','Football stadium'); Insert into FACILITY (FACNO,FACNAME) values ('F101','Basketball arena'); Insert into FACILITY (FACNO,FACNAME) values ('F102','Baseball field'); Insert into FACILITY (FACNO,FACNAME) values ('F103','Recreation room'); -- INSERT RECORDS into RESOURCETBL Insert into RESOURCETBL (RESNO,RESNAME,RATE) values ('R100','attendant',10); Insert into RESOURCETBL (RESNO,RESNAME,RATE) values ('R101','police',15); Insert into RESOURCETBL (RESNO,RESNAME,RATE) values ('R102','usher',10); Insert into RESOURCETBL (RESNO,RESNAME,RATE) values ('R103','nurse',20); Insert into RESOURCETBL (RESNO,RESNAME,RATE) values ('R104','janitor',15); Insert into RESOURCETBL (RESNO,RESNAME,RATE) values ('R105','food service',10); -- insert records into CUSTOMER TABLE Insert into CUSTOMER (CUSTNO,CUSTNAME,ADDRESS,INTERNAL,CONTACT,PHONE,CITY,STATE,ZIP) values ('C100','Football','Box 352200','Y','Mary Manager','6857100','Boulder','CO','80309'); Insert into CUSTOMER (CUSTNO,CUSTNAME,ADDRESS,INTERNAL,CONTACT,PHONE,CITY,STATE,ZIP) values ('C101','Men''s Basketball','Box 352400','Y','Sally Supervisor','5431700','Boulder','CO','80309'); Insert into CUSTOMER (CUSTNO,CUSTNAME,ADDRESS,INTERNAL,CONTACT,PHONE,CITY,STATE,ZIP) values ('C103','Baseball','Box 352020','Y','Bill Baseball','5431234','Boulder','CO','80309'); Insert into CUSTOMER (CUSTNO,CUSTNAME,ADDRESS,INTERNAL,CONTACT,PHONE,CITY,STATE,ZIP) values ('C104','Women''s Softball','Box 351200','Y','Sue Softball','5434321','Boulder','CO','80309'); Insert into CUSTOMER (CUSTNO,CUSTNAME,ADDRESS,INTERNAL,CONTACT,PHONE,CITY,STATE,ZIP) values ('C105','High School Football','123 AnyStreet','N','Coach Bob','4441234','Louisville','CO','80027'); -- insert records into EMPLOYEE TABLE Insert into EMPLOYEE (EMPNO,EMPNAME,DEPARTMENT,EMAIL,PHONE) values ('E100','Chuck Coordinator','Administration','chuck@colorado.edu','3-1111'); Insert into EMPLOYEE (EMPNO,EMPNAME,DEPARTMENT,EMAIL,PHONE) values ('E101','Mary Manager','Football','mary@colorado.edu','5-1111'); Insert into EMPLOYEE (EMPNO,EMPNAME,DEPARTMENT,EMAIL,PHONE) values ('E102','Sally Supervisor','Planning','sally@colorado.edu','3-2222'); Insert into EMPLOYEE (EMPNO,EMPNAME,DEPARTMENT,EMAIL,PHONE) values ('E103','Alan Administrator','Administration','alan@colorado.edu','3-3333');
In case you want to change a constraint on a field, you can use a syntax examples as the following.
ALTER TABLE FACILITY
ADD CONSTRAINT UNQ_FAC UNIQUE (FACNAME)
This adds a UNIQUE constraint to Field name FACNAME