Oracle alter table references - oracle

I am new with oracle. I create a simple table which is i refer from a note
CREATE TABLE employees (
employee_id NUMBER(6),
first_name VARCHAR2(20),
last_name VARCHAR2(25) CONSTRAINT emp_lname_nn NOT NULL,
email VARCHAR2(25) CONSTRAINT emp_email_nn NOT NULL,
phone_number VARCHAR2(20),
hire_date DATE CONSTRAINT emp_hdate_nn NOT NULL,
job_id VARCHAR2(10) CONSTRAINT emp_job_nn NOT NULL,
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
manager_id NUMBER(6),
department_id NUMBER(4),
CONSTRAINT emp_salary_min CHECK (salary > 0),
CONSTRAINT emp_email_uk UNIQUE (email)
) ;
Then tried to alter the table to adds integrity constraints to the employees table. Integrity constraints enforce business rules and prevent the entry of invalid information into tables.
ALTER TABLE employees
ADD (CONSTRAINT emp_emp_id_pk PRIMARY KEY (employee_id),
CONSTRAINT emp_dept_fk FOREIGN KEY (department_id)
REFERENCES departments,
CONSTRAINT emp_job_fk FOREIGN KEY (job_id)
REFERENCES jobs (job_id),
CONSTRAINT emp_manager_fk FOREIGN KEY (manager_id)
REFERENCES employees
) ;
But then i got an error
REFERENCES departments,
* ERROR at line 4: ORA-00942: table or view does not exist
I check the note, they said nothing about create departments tables. They just show how to create employees tables.

The error cleary says DEPARTMENTS table is missing... The below foreign key constratint referencing the department_id column of departments table.
CONSTRAINT emp_dept_fk FOREIGN KEY (department_id)
REFERENCES departments
So create the departments table with department_id column and also to add below constraint you need to create a jobs table with job_id column...
CONSTRAINT emp_job_fk FOREIGN KEY (job_id)
REFERENCES jobs (job_id),

Related

ORA-02291: integrity constraint (SQL_HLZTBRUASDUURQGIOAYPNRAFC.EMPLOYEE_ID) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721

I am new to sql and have been trying to figure out what im doing wrong when adding the foreign keys. I am able to create all the tables and everything up to the point where i go to input values for the SALES table. I get an error :
ORA-02291: integrity constraint
(SQL_HLZTBRUASDUURQGIOAYPNRAFC.EMPLOYEE_ID) violated - parent key not
found ORA-06512: at "SYS.DBMS_SQL", line 1721:
Can anyone look over my code and help me with what to change?
CREATE TABLE EMPLOYEE
(EMPLOYEE_ID char(10) PRIMARY KEY,
EMPLOYEE_NAME varchar(30),
Address varchar(50),
PHONE number(10),
HIRING_DATE date not null,
SALARY number(6))
CREATE TABLE PRODUCTS
(PRODUCT_ID char(2) PRIMARY KEY,
PRICE CHAR(5),
TYPE varchar(10),
PRODUCT_NAME varchar(30),
AUTHOR varchar(20))
CREATE TABLE CUSTOMER
(CUSTOMER_ID char(5) PRIMARY KEY,
CUSTOMER_NAME varchar(30),
PHONE number(7),
ADDRESS varchar(30))
CREATE TABLE INVENTORY
(PRODUCT_NAME varchar(20) PRIMARY KEY,
PRODUCT_ID char(2),
UNIT_PRICE number(4),
SHELF_LOCATION varchar(2),
CURRENT_INVENTORY number(3),
MONTHLY_PURCHASES number(3))
CREATE TABLE SALES
(TRANSACTION_ID char(5) PRIMARY KEY,
PRODUCT_ID char(5),
SELL_DATE date not null,
CUSTOMER_ID char(5),
UNITS_SOLD number(2),
EMPLOYEE_ID char(10),
SALES_AMOUNT decimal(10,2),
CONSTRAINT CUSTOMER_ID
FOREIGN KEY (CUSTOMER_ID)
REFERENCES CUSTOMER(CUSTOMER_ID),
CONSTRAINT PRODUCT_ID
FOREIGN KEY(PRODUCT_ID)
REFERENCES PRODUCTS(PRODUCT_ID),
CONSTRAINT EMPLOYEE_ID
FOREIGN KEY (EMPLOYEE_ID)
REFERENCES EMPLOYEE(EMPLOYEE_ID))
When inserting the code below is when I get an error:
INSERT INTO SALES
VALUES('1','42','01-JAN-2015','4269','3','5678901234','135.00’)
The employee_id 5678901234 doesn't exist in the EMPLOYEE table.
Which causes the foreign key constraint on EMPLOYEE_ID to complain.
Those foreign key's are there to ensure referential integrity after all.
(The constraint is used to give a name to it)
So add the user with that id to the EMPLOYEE table first.
An extra advice.
It's safer to list the column names in the INSERT statement.

Problems creating table after normalisation

I have been given a task to normalise a sales order, create a relational schema and input the data into SQL developer.
I have normalised to 3NF and got this:
Customer(Customer_ID, Customer_name) ... Primary key = Customer_ID
Employee(Employee_ID, Employee_name) ... Primary key = Employee_ID
Sales_Order(Sales_order_ID, Employee_ID, Customer_ID, Sale_date, Order_total) ... Primary key = Sales_order_ID ... Foreign keys = Employee_ID, Customer_ID
Order_Line(Sales_order_ID, Product_ID, Quantity, Line_total) ... Composite key = Sales_order_ID ... Foreign key = Product_ID
Product(Product_ID, Product_name, Product_price, Product_colour) ... Primary key = Product_ID
I then inputted the tables, this is my SQL:
CREATE TABLE EMPLOYEE(
Employee_ID CHAR(3),
Employee_name CHAR(20),
CONSTRAINT pk_Employee PRIMARY KEY (Employee_name)
);
CREATE TABLE CUSTOMER(
Customer_ID CHAR(5),
Customer_Name CHAR(20),
CONSTRAINT pk_Customer PRIMARY KEY (Customer_ID)
);
CREATE TABLE PRODUCT(
Product_ID CHAR(5),
Product_Name CHAR(30),
Product_Colour CHAR(10),
Product_Price CHAR(5),
CONSTRAINT pk_Product PRIMARY KEY (Product_ID)
);
CREATE TABLE SALES_ORDER(
Sales_order_ID CHAR(6),
Employee_ID CHAR(3),
Customer_ID CHAR(5),
Sale_Date CHAR(10),
Order_total CHAR(7),
CONSTRAINT pk_Order PRIMARY KEY (Sales_order_ID),
CONSTRAINT fk_Order FOREIGN KEY (Employee_ID) REFERENCES EMPLOYEE (Employee_ID),
FOREIGN KEY (Customer_ID) REFERENCES CUSTOMER (Customer_ID)
);
CREATE TABLE ORDER_LINE(
Sales_order_ID CHAR(6),
Product_ID CHAR(5),
Quantity CHAR(3),
Line_total CHAR(5),
CONSTRAINT pk_Order_Line PRIMARY KEY (Sales_order_ID),
FOREIGN KEY (Product_ID) REFERENCES PRODUCT (Product_ID)
);
I am able to input tables Employee, Customer, Product, Sales_order but unable to input Order_line
I am told that the table or view does not exist!
What does this mean?
Have I normalised incorrectly?
Have I designed the relational schema incorrectly?
Any help would be deeply appreciated
When I run your code in this db fiddle, creation of table SALE_ORDER fails with the following message:
ORA-02270: no matching unique or primary key for this column-list
This is because of this foreign key:
CONSTRAINT fk_Order FOREIGN KEY (Employee_ID) REFERENCES EMPLOYEE (Employee_ID),
The underlying problem is that you have defined Employee_name as the primary key of table EMPLOYEE.
CREATE TABLE EMPLOYEE (
Employee_ID CHAR(3),
Employee_name CHAR(20),
CONSTRAINT pk_Employee PRIMARY KEY (Employee_ID)
);
This does not seem like a sensible option. In the real life, it is possible that two different employees would have the same name. Instead, you probably want to use Employee_ID as the primary key for EMPLOYEE.
Consider this definition for table EMPLOYEE:
CREATE TABLE EMPLOYEE (
Employee_ID CHAR(3),
Employee_name CHAR(20),
CONSTRAINT pk_Employee PRIMARY KEY (Employee_ID)
);
With this new set up, all tables are created successfully. You may now insert your data.
Demo on DB Fiddle
Side note: I forsee issues with table ORDER_LINE:
primary key should be Order_line_ID instead of Sales_order_ID
Sales_order_ID should have a foreign key constraint referencing SALE_ORDER(Sales_order_ID).
Consider this new definition for ORDER_LINE:
CREATE TABLE ORDER_LINE(
Order_line_ID CHAR(6),
Sales_order_ID CHAR(6),
Product_ID CHAR(5),
Quantity CHAR(3),
Line_total CHAR(5),
CONSTRAINT pk_Order_Line PRIMARY KEY (Order_line_ID),
FOREIGN KEY (Product_ID) REFERENCES PRODUCT (Product_ID),
FOREIGN KEY (Sales_order_ID) REFERENCES SALES_ORDER(Sales_order_ID)
);

NEW_COURSE pl/sql procedure is raising exception and i cant figure it out

The course procedure is supposed to :
The prerequisite courses for it (if any) already exist in the COURSE table. For simplicity,
we will pretend that a course can have at most one course as prerequisite.
The prerequisite for a course must be at a lower level. For instance, the prerequisite for
ISYS326 must be a100- or 200-level course, and cannot be a 300 level course.
If either of the above requirements fails, the procedure raises an exception WRONG_PREREQ
that prints an appropriate alert message, and does not modify the COURSE table.
so i have done the procedure as mentioned:
create or replace PROCEDURE NEW_COURSE (
CID IN COURSE.COURSEID%TYPE,
CNAME IN COURSE.COURSENAME%TYPE,
PID IN HAS_PREREQUISITE.PREREQUISITEID%TYPE,
DID IN DEPARTMENT.DEPTID%TYPE
)
IS
CONDITION1 NUMBER;
CONDITION2 NUMBER;
WRONG_PREQ EXCEPTION;
BEGIN
/* this can be checked before any SQL */
IF (SUBSTR(CID,5,3) <= SUBSTR(PID,5,3)) THEN /*PREREQUISITE HAS A HIGHER ID THAN COURSEID */
RAISE WRONG_PREQ;
END IF;
SELECT COUNT(COURSEID)
INTO CONDITION1
FROM COURSE
WHERE COURSEID = PID;
/* this can be checked here, without running the second select */
IF (CONDITION1 <>1) THEN /*VIOLATION OF CONDITION 1, NO EXISTING
PREREQUISITE COURSE*/
RAISE WRONG_PREQ;
END IF;
SELECT COUNT(*)
INTO CONDITION2
FROM HAS_PREREQUISITE
WHERE COURSEID = CID
AND PREREQUISITEID = PID
AND SUBSTR(CID,5,3) > SUBSTR(PID,5,3);
IF (CONDITION2 = 1) THEN /*THE RECORD ALREADYS EXISTS IN HAS_PREREQUISITE
TABLE*/
RAISE WRONG_PREQ;
END IF;
INSERT INTO COURSE VALUES(CID,CNAME,DID,NULL,NULL,NULL,NULL,NULL,NULL); /* edit the statement to fit the table structure also 'NULL' or null ?*/
INSERT INTO HAS_PREREQUISITE VALUES(CID,PID); /* the table only has 2 colums */
INSERT INTO DEPARTMENT VALUES (DID,NULL,NULL,NULL); /* edit the statement to fit the table structure. also 'NULL' or null ? */
EXCEPTION
WHEN WRONG_PREQ THEN
DBMS_OUTPUT.PUT_LINE('COURSEID OR PREREQUISITE ID IS INVALID');
END NEW_COURSE;
And when i tried to call the procedure through anonymous block like this
SET SERVEROUTPUT ON
DECLARE
CID char(8):='isys228';
CNAME varchar2(20):='Greendata';
PID char(8):='isys114';
DID char(8):='comp3647';
BEGIN
DBMS_OUTPUT.PUT_LINE('The course has been enrolled');
NEW_COURSE(CNAME,CID,PID,DID);
END;
/
UPDATE: the anonymous block works but i cant see the data entered in the table
And i was trying to Open a new course ISYS228, “Green Data”, with ISYS114 as prerequisite, through this procedure.
PLEASE HELP!! I'm fairly new to this sql so i have difficulty seeing where im going wrong
just incase heres the ddl
/*==============================================================*/
/* DBMS name: ORACLE Version 11g */
/* Created on: 11/10/2016 3:47:29 PM */
/*==============================================================*/
alter table ACADEMIC_REC
drop constraint FK_ACADEMIC_RELATIONS_STUDENT;
alter table ACADEMIC_REC
drop constraint FK_ACADEMIC_RELATIONS_COURSE;
alter table COURSE
drop constraint FK_COURSE_OFFERS_DEPARTME;
alter table ENROLS
drop constraint FK_ENROLS_ENROLS_STUDENT;
alter table ENROLS
drop constraint FK_ENROLS_ENROLS2_TUT_PRAC;
alter table HAS_PREREQUISITE
drop constraint FK_HAS_PRER_HAS_PRERE_COURSE;
alter table HAS_PREREQUISITE
drop constraint FK_HAS_PRER_HAS_PRERE_COURSE;
alter table STAFF
drop constraint FK_STAFF_RELATIONS_DEPARTME;
alter table TEACHING_INFORMATION
drop constraint FK_TEACHING_RELATIONS_STAFF;
alter table TEACHING_INFORMATION
drop constraint FK_TEACHING_RELATIONS_COURSE;
alter table TUT_PRAC
drop constraint FK_TUT_PRAC_HAS_COURSE;
alter table TUT_PRAC
drop constraint FK_TUT_PRAC_RELATIONS_STAFF;
drop table ACADEMIC_REC cascade constraints;
drop table COURSE cascade constraints;
drop table ENROLS cascade constraints;
drop table HAS_PREREQUISITE cascade constraints;
drop table STAFF cascade constraints;
drop table STUDENT cascade constraints;
drop table TEACHING_INFORMATION cascade constraints;
drop table TUT_PRAC cascade constraints;
/*==============================================================*/
/* Table: ACADEMIC_REC */
/*==============================================================*/
create table ACADEMIC_REC
(
STUID CHAR(8) not null,
COURSEID CHAR(8) not null,
STATUS VARCHAR2(5),
YEAR NUMBER(4),
SEMESTER CHAR(2),
GRADE VARCHAR2(2)
constraint CKC_GRADE_ACADEMIC check (GRADE is null or (GRADE in ('HD','D','CR','P','F'))),
constraint PK_ACADEMIC_REC primary key (STUID, COURSEID)
);
/*==============================================================*/
/* Table: COURSE */
/*==============================================================*/
create table COURSE
(
COURSEID CHAR(8) not null,
DEPTID CHAR(8) not null,
COURSENAME VARCHAR2(20),
TEXTBOOK VARCHAR2(20),
CREDITHOUR NUMBER(2),
MAX_ENROL NUMBER(4),
ACTUAL_ENROL NUMBER(4),
AVAILABILITY NUMBER(4),
COURSE_TIME CHAR(7),
constraint PK_COURSE primary key (COURSEID)
);
/*==============================================================*/
/* Table: DEPARTMENT */
/*==============================================================*/
create table DEPARTMENT
(
DEPTID CHAR(8) not null,
DEPTNAME VARCHAR2(20),
DEPTCONTACTNO NUMBER(10),
BUILDING VARCHAR2(5),
constraint PK_DEPARTMENT primary key (DEPTID)
);
/*==============================================================*/
/* Table: ENROLS */
/*==============================================================*/
create table ENROLS
(
STUID CHAR(8) not null,
CLASSID CHAR(8) not null,
constraint PK_ENROLS primary key (STUID, CLASSID)
);
/*==============================================================*/
/* Table: HAS_PREREQUISITE */
/*==============================================================*/
create table HAS_PREREQUISITE
(
COURSEID CHAR(8) not null,
PREREQUISITEID CHAR(8) not null,
constraint PK_HAS_PREREQUISITE primary key (COURSEID, PREREQUISITEID)
);
/*==============================================================*/
/* Table: STAFF */
/*==============================================================*/
create table STAFF
(
STAFFID CHAR(8) not null,
DEPARTMENTID CHAR(8) not null,
STAFFNAME VARCHAR2(50),
STAFFADDRESS VARCHAR2(70),
STAFFCONTACTNO NUMBER(10),
STAFFEMAIL VARCHAR2(50),
OFFICENO NUMBER(5),
ROLE VARCHAR2(10),
constraint PK_STAFF primary key (STAFFID)
);
/*==============================================================*/
/* Table: STUDENT */
/*==============================================================*/
create table STUDENT
(
STUID CHAR(8) not null,
DEGREE VARCHAR2(10),
MAJOR VARCHAR2(10),
STU_NAME VARCHAR2(50),
STU_ADDRESS VARCHAR2(70),
CONTACTNO NUMBER(10),
EMAIL VARCHAR2(50),
constraint PK_STUDENT primary key (STUID)
);
/*==============================================================*/
/* Table: TEACHING_INFORMATION */
/*==============================================================*/
create table TEACHING_INFORMATION
(
STAFFID CHAR(8) not null,
COURSEID CHAR(8) not null,
SEMESTER CHAR(2) not null,
YEAR NUMBER(4) not null,
constraint PK_TEACHING_INFORMATION primary key (STAFFID, COURSEID, SEMESTER, YEAR)
);
/*==============================================================*/
/* Table: TUT_PRAC */
/*==============================================================*/
create table TUT_PRAC
(
CLASSID CHAR(8) not null,
COURSEID CHAR(8) not null,
STAFFID CHAR(8) not null,
TYPE VARCHAR2(5),
DAY VARCHAR2(10),
TIME DATE,
ROOMID CHAR(10),
NO_OF_SEATS NUMBER(2),
constraint PK_TUT_PRAC primary key (CLASSID)
);
alter table ACADEMIC_REC
add constraint FK_ACADEMIC_RELATIONS_STUDENT foreign key (STUID)
references STUDENT (STUID);
alter table ACADEMIC_REC
add constraint FK_ACADEMIC_RELATIONS_COURSE foreign key (COURSEID)
references COURSE (COURSEID);
alter table COURSE
add constraint FK_COURSE_OFFERS_DEPARTME foreign key (DEPTID)
references DEPARTMENT (DEPTID);
alter table ENROLS
add constraint FK_ENROLS_ENROLS_STUDENT foreign key (STUID)
references STUDENT (STUID);
alter table ENROLS
add constraint FK_ENROLS_ENROLS2_TUT_PRAC foreign key (CLASSID)
references TUT_PRAC (CLASSID);
alter table HAS_PREREQUISITE
add constraint FK_HAS_PRER_HAS_PRERE_COURSE foreign key (COURSEID)
references COURSE (COURSEID);
alter table HAS_PREREQUISITE
add constraint FK_HAS_PRER_HAS_PRERE_COURSE foreign key (PREREQUISITEID)
references COURSE (COURSEID);
alter table STAFF
add constraint FK_STAFF_RELATIONS_DEPARTME foreign key (DEPARTMENTID)
references DEPARTMENT (DEPTID);
alter table TEACHING_INFORMATION
add constraint FK_TEACHING_RELATIONS_STAFF foreign key (STAFFID)
references STAFF (STAFFID);
alter table TEACHING_INFORMATION
add constraint FK_TEACHING_RELATIONS_COURSE foreign key (COURSEID)
references COURSE (COURSEID);
alter table TUT_PRAC
add constraint FK_TUT_PRAC_HAS_COURSE foreign key (COURSEID)
references COURSE (COURSEID);
alter table TUT_PRAC
add constraint FK_TUT_PRAC_RELATIONS_STAFF foreign key (STAFFID)
references STAFF (STAFFID);
SET SERVEROUTPUT ON
DECLARE
CID char(8):='isys228';
CNAME varchar2(20):='Greendata';
PID char(8):='isys114';
DID char(8):='comp3647';
BEGIN
DBMS_OUTPUT.PUT_LINE('The course has been enrolled');
END;
Try that instead
what error message are you getting?
try to use this code.
SET SERVEROUTPUT ON
DECLARE
CID COURSE.COURSEID%TYPE:='isys228';
CNAME COURSE.COURSENAME%TYPE:='Greendata';
PID HAS_PREREQUISITE.PREREQUISITEID%TYPE:='isys114';
DID DEPARTMENT.DEPTID%TYPE:='comp3647';
BEGIN
DBMS_OUTPUT.PUT_LINE('The course has been enrolled');
NEW_COURSE(CNAME,CID,PID,DID);
END;
/
You need to commit your inserts.
Try putting
commit;
after
NEW_COURSE(CNAME,CID,PID,DID);
Or if you want to commit in the Procedure
INSERT INTO DEPARTMENT VALUES (DID,NULL,NULL,NULL); /* edit the statement to fit the table structure. also 'NULL' or null ? */
COMMIT;
EDIT
Reorder your insert orders since COURSE has an FK to DEPARTMENT
-- FIRST THE INSERT IN DEPARTMENT
INSERT INTO DEPARTMENT VALUES (DID,NULL,NULL,NULL); /* edit the statement to fit the table structure. also 'NULL' or null ? */
INSERT INTO COURSE VALUES(CID,CNAME,DID,NULL,NULL,NULL,NULL,NULL,NULL); /* edit the statement to fit the table structure also 'NULL' or null ?*/
INSERT INTO HAS_PREREQUISITE VALUES(CID,PID); /* the table only has 2 colums */

I keep getting errors in my procedure that help add new course and i have put requirements in description

the procedure needs to ensure that:
1.The prerequisite courses for it (if any) already exist in the COURSE table. For simplicity,
we will pretend that a course can have at most one course as prerequisite.
The prerequisite for a course must be at a lower level. For instance, the prerequisite for ISYS326 must be a100- or 200-level course, and cannot be a 300 level course.
If either of the above requirements fails, the procedure raises an exception WRONG_PREREQ that prints an appropriate alert message, and does not modify the COURSE table.
in the procedure CID =courseid PID=prerequisiteid and DID=deptid they are all not null values
so this procedure will be used to enter new course
CREATE OR REPLACE PROCEDURE NEW_COURSE (
CID IN COURSE.COURSEID%TYPE,
CNAME IN COURSE.COURSENAME%TYPE,
PID IN HAS_PREREQUISITE.PREREQUISITEID%TYPE,
DID IN DEPARTMENT.DEPTID%TYPE
)
IS
CONDITION1 NUMBER;
CONDITION2 NUMBER;
WRONG_PREQ EXCEPTION;
BEGIN
/* this can be checked before any SQL */
IF (SUBSTR(CID,5,3) <= SUBSTR(PID,5,3)) THEN /*PREREQUISITE HAS A HIGHER ID
THAN COURSEID */
RAISE WRONG_PREQ;
END IF;
SELECT COUNT(COURSEID)
INTO CONDITION1
FROM COURSE
WHERE COURSEID = PID;
/* this can be checked here, without running the second select */
IF (CONDITION1 <>1) THEN /VIOLATION OF CONDITION 1, NO EXISTING
PREREQUISITE COURSE/
RAISE WRONG_PREQ;
END IF;
SELECT COUNT(*)
INTO CONDITION2
FROM HAS_PREREQUISITE
WHERE COURSEID = CID
AND PREREQUISITEID = PID
AND SUBSTR(CID,5,3) > SUBSTR(PID,5,3);
IF (CONDITION2 = 1) THEN /THE RECORD ALREADYS EXISTS IN HAS_PREREQUISITE
TABLE/
RAISE WRONG_PREQ;
END IF;
INSERT INTO COURSE VALUES(CID,CNAME,'NULL','NULL' );
INSERT INTO HAS_PREREQUISITE VALUES(CID,PID);
INSERT INTO DEPARTMENT VALUES (DEPTID,'NULL');
EXCEPTION
WHEN WRONG_PREQ THEN
DBMS_OUTPUT.PUT_LINE('COURSEID OR PREREQUISITE ID IS INVALID');
END NEW_COURSE;
This is done is Oracle SQL Developer.
DDL
/==============================================================/
/* DBMS name: ORACLE Version 11g */
/* Created on: 11/10/2016 3:47:29 PM */
/==============================================================/
alter table ACADEMIC_REC
drop constraint FK_ACADEMIC_RELATIONS_STUDENT;
alter table ACADEMIC_REC
drop constraint FK_ACADEMIC_RELATIONS_COURSE;
alter table COURSE
drop constraint FK_COURSE_OFFERS_DEPARTME;
alter table ENROLS
drop constraint FK_ENROLS_ENROLS_STUDENT;
alter table ENROLS
drop constraint FK_ENROLS_ENROLS2_TUT_PRAC;
alter table HAS_PREREQUISITE
drop constraint FK_HAS_PRER_HAS_PRERE_COURSE;
alter table HAS_PREREQUISITE
drop constraint FK_HAS_PRER_HAS_PRERE_COURSE;
alter table STAFF
drop constraint FK_STAFF_RELATIONS_DEPARTME;
alter table TEACHING_INFORMATION
drop constraint FK_TEACHING_RELATIONS_STAFF;
alter table TEACHING_INFORMATION
drop constraint FK_TEACHING_RELATIONS_COURSE;
alter table TUT_PRAC
drop constraint FK_TUT_PRAC_HAS_COURSE;
alter table TUT_PRAC
drop constraint FK_TUT_PRAC_RELATIONS_STAFF;
drop table ACADEMIC_REC cascade constraints;
drop table COURSE cascade constraints;
drop table ENROLS cascade constraints;
drop table HAS_PREREQUISITE cascade constraints;
drop table STAFF cascade constraints;
drop table STUDENT cascade constraints;
drop table TEACHING_INFORMATION cascade constraints;
drop table TUT_PRAC cascade constraints;
/==============================================================/
/* Table: ACADEMIC_REC */
/==============================================================/
create table ACADEMIC_REC
(
STUID CHAR(8) not null,
COURSEID CHAR(8) not null,
STATUS VARCHAR2(5),
YEAR NUMBER(4),
SEMESTER CHAR(2),
GRADE VARCHAR2(2)
constraint CKC_GRADE_ACADEMIC check (GRADE is null or (GRADE in
('HD','D','CR','P','F'))),
constraint PK_ACADEMIC_REC primary key (STUID, COURSEID)
);
/==============================================================/
/* Table: COURSE */
/==============================================================/
create table COURSE
(
COURSEID CHAR(8) not null,
DEPTID CHAR(8) not null,
COURSENAME VARCHAR2(20),
TEXTBOOK VARCHAR2(20),
CREDITHOUR NUMBER(2),
MAX_ENROL NUMBER(4),
ACTUAL_ENROL NUMBER(4),
AVAILABILITY NUMBER(4),
COURSE_TIME CHAR(7),
constraint PK_COURSE primary key (COURSEID)
);
/==============================================================/
/* Table: DEPARTMENT */
/==============================================================/
create table DEPARTMENT
(
DEPTID CHAR(8) not null,
DEPTNAME VARCHAR2(20),
DEPTCONTACTNO NUMBER(10),
BUILDING VARCHAR2(5),
constraint PK_DEPARTMENT primary key (DEPTID)
);
/==============================================================/
/* Table: ENROLS */
/==============================================================/
create table ENROLS
(
STUID CHAR(8) not null,
CLASSID CHAR(8) not null,
constraint PK_ENROLS primary key (STUID, CLASSID)
);
/==============================================================/
/* Table: HAS_PREREQUISITE */
/==============================================================/
create table HAS_PREREQUISITE
(
COURSEID CHAR(8) not null,
PREREQUISITEID CHAR(8) not null,
constraint PK_HAS_PREREQUISITE primary key (COURSEID, PREREQUISITEID)
);
/==============================================================/
/* Table: STAFF */
/==============================================================/
create table STAFF
(
STAFFID CHAR(8) not null,
DEPARTMENTID CHAR(8) not null,
STAFFNAME VARCHAR2(50),
STAFFADDRESS VARCHAR2(70),
STAFFCONTACTNO NUMBER(10),
STAFFEMAIL VARCHAR2(50),
OFFICENO NUMBER(5),
ROLE VARCHAR2(10),
constraint PK_STAFF primary key (STAFFID)
);
/==============================================================/
/* Table: STUDENT */
/==============================================================/
create table STUDENT
(
STUID CHAR(8) not null,
DEGREE VARCHAR2(10),
MAJOR VARCHAR2(10),
STU_NAME VARCHAR2(50),
STU_ADDRESS VARCHAR2(70),
CONTACTNO NUMBER(10),
EMAIL VARCHAR2(50),
constraint PK_STUDENT primary key (STUID)
);
/==============================================================/
/* Table: TEACHING_INFORMATION */
/==============================================================/
create table TEACHING_INFORMATION
(
STAFFID CHAR(8) not null,
COURSEID CHAR(8) not null,
SEMESTER CHAR(2) not null,
YEAR NUMBER(4) not null,
constraint PK_TEACHING_INFORMATION primary key (STAFFID, COURSEID, SEMESTER, YEAR)
);
/==============================================================/
/* Table: TUT_PRAC */
/==============================================================/
create table TUT_PRAC
(
CLASSID CHAR(8) not null,
COURSEID CHAR(8) not null,
STAFFID CHAR(8) not null,
TYPE VARCHAR2(5),
DAY VARCHAR2(10),
TIME DATE,
ROOMID CHAR(10),
NO_OF_SEATS NUMBER(2),
constraint PK_TUT_PRAC primary key (CLASSID)
);
alter table ACADEMIC_REC
add constraint FK_ACADEMIC_RELATIONS_STUDENT foreign key (STUID)
references STUDENT (STUID);
alter table ACADEMIC_REC
add constraint FK_ACADEMIC_RELATIONS_COURSE foreign key (COURSEID)
references COURSE (COURSEID);
alter table COURSE
add constraint FK_COURSE_OFFERS_DEPARTME foreign key (DEPTID)
references DEPARTMENT (DEPTID);
alter table ENROLS
add constraint FK_ENROLS_ENROLS_STUDENT foreign key (STUID)
references STUDENT (STUID);
alter table ENROLS
add constraint FK_ENROLS_ENROLS2_TUT_PRAC foreign key (CLASSID)
references TUT_PRAC (CLASSID);
alter table HAS_PREREQUISITE
add constraint FK_HAS_PRER_HAS_PRERE_COURSE foreign key (COURSEID)
references COURSE (COURSEID);
alter table HAS_PREREQUISITE
add constraint FK_HAS_PRER_HAS_PRERE_COURSE foreign key (PREREQUISITEID)
references COURSE (COURSEID);
alter table STAFF
add constraint FK_STAFF_RELATIONS_DEPARTME foreign key (DEPARTMENTID)
references DEPARTMENT (DEPTID);
alter table TEACHING_INFORMATION
add constraint FK_TEACHING_RELATIONS_STAFF foreign key (STAFFID)
references STAFF (STAFFID);
alter table TEACHING_INFORMATION
add constraint FK_TEACHING_RELATIONS_COURSE foreign key (COURSEID)
references COURSE (COURSEID);
alter table TUT_PRAC
add constraint FK_TUT_PRAC_HAS_COURSE foreign key (COURSEID)
references COURSE (COURSEID);
alter table TUT_PRAC
add constraint FK_TUT_PRAC_RELATIONS_STAFF foreign key (STAFFID)
references STAFF (STAFFID);
Well you have clear error message. All regarding following tables:
The first table say not enough values. It means you try insert less values than columns in table
INSERT INTO COURSE VALUES(CID,CNAME,'NULL','NULL' );
-- Error(29,3): PL/SQL: SQL Statement ignored Error(29,15): PL/SQL: ORA-00947: not enough values
You may add values or specify a columns for instance `INSERT INTO COURSE (course_id, course_name, val1, val2 ) VALUES(CID,CNAME,'NULL','NULL' );`
Next;
INSERT INTO HAS_PREREQUISITE VALUES(CID,PID,'NULL','NULL');
-- Error(30,3): PL/SQL: SQL Statement ignored Error(30,15): PL/SQL: ORA-00913: too many values
Its clear too. table HAS_PREREQUISITE has less columns then you try to insert. Delete extra values
INSERT INTO DEPARTMENT VALUES (DEPTID,'NULL');
-- Error(31,3): PL/SQL: SQL Statement ignored Error(31,15): PL/SQL: ORA-00947: not enough values
Is the same as first example.
I tried to edit your code moving some checks around to avoid unuseful queries and showing you what to edit; follow the comments:
CREATE OR REPLACE PROCEDURE NEW_COURSE (
CID IN COURSE.COURSEID%TYPE,
CNAME IN COURSE.COURSENAME%TYPE,
PID IN HAS_PREREQUISITE.PREREQUISITEID%TYPE,
DID IN DEPARTMENT.DEPTID%TYPE
)
IS
CONDITION1 NUMBER;
CONDITION2 NUMBER;
WRONG_PREQ EXCEPTION;
BEGIN
/* this can be checked before any SQL */
IF (SUBSTR(CID,5,3) <= SUBSTR(PID,5,3)) THEN /*PREREQUISITE HAS A HIGHER ID THAN COURSEID */
RAISE WRONG_PREQ;
END IF;
SELECT COUNT(COURSEID)
INTO CONDITION1
FROM COURSE
WHERE COURSEID = PID;
/* this can be checked here, without running the second select */
IF (CONDITION1 <>1) THEN /*VIOLATION OF CONDITION 1, NO EXISTING
PREREQUISITE COURSE*/
RAISE WRONG_PREQ;
END IF;
SELECT COUNT(*)
INTO CONDITION2
FROM HAS_PREREQUISITE
WHERE COURSEID = CID
AND PREREQUISITEID = PID
AND SUBSTR(CID,5,3) > SUBSTR(PID,5,3);
IF (CONDITION2 = 1) THEN /*THE RECORD ALREADYS EXISTS IN HAS_PREREQUISITE
TABLE*/
RAISE WRONG_PREQ;
END IF;
INSERT INTO COURSE VALUES(CID,CNAME,'NULL','NULL' ); /* edit the statement to fit the table structure also 'NULL' or null ?*/
INSERT INTO HAS_PREREQUISITE VALUES(CID,PID); /* the table only has 2 colums */
INSERT INTO DEPARTMENT VALUES (DEPTID,'NULL'); /* edit the statement to fit the table structure. also 'NULL' or null ? */
EXCEPTION
WHEN WRONG_PREQ THEN
DBMS_OUTPUT.PUT_LINE('COURSEID OR PREREQUISITE ID IS INVALID');
END NEW_COURSE;

Unable to use composite key as foreign key

CREATE TABLE departments
( department_id number(10) NOT NULL,
department_name varchar2(50) NOT NULL,
department_code varchar2(50) NOT NULL,
CONSTRAINT departments_pk PRIMARY KEY (department_id, department_code)
);
CREATE TABLE employees
( employee_number number(10) NOT NULL,
employee_name varchar2(50) NOT NULL,
department_id number(10),
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number),
CONSTRAINT fk_departments
FOREIGN KEY (department_id, department_code)
REFERENCES departments(department_id,department_code));
Your employees table doesn't have a department_code field, so the FOREIGN KEY (department_id, department_code) part is trying to use something that doesn't exist. Which is what the error you get from running the second statement tells you:
ERROR at line 8:
ORA-00904: "DEPARTMENT_CODE": invalid identifier
You would either have to include the department code in that table, which would denormalise the data; or change the primary key on departments to just department_id, which would be much more normal anyway, i.e.:
CREATE TABLE departments
( department_id number(10) NOT NULL,
department_name varchar2(50) NOT NULL,
department_code varchar2(50) NOT NULL,
CONSTRAINT departments_pk PRIMARY KEY (department_id)
);
CREATE TABLE employees
( employee_number number(10) NOT NULL,
employee_name varchar2(50) NOT NULL,
department_id number(10),
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number),
CONSTRAINT fk_departments
FOREIGN KEY (department_id)
REFERENCES departments(department_id));
It doesn't usually make sense to have a composite primary key like that, where there is a single column that looks like it should be unique anyway. You wouldn't expect to have the same department_id with two department_code values.
But if you do have a legitimate reason to have a composite primary key then all columns in that key will have to be duplicated on the child tables and their foreign key constraints:
CREATE TABLE employees
( employee_number number(10) NOT NULL,
employee_name varchar2(50) NOT NULL,
department_id number(10),
department_code varchar2(50) NOT NULL,
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number),
CONSTRAINT fk_departments
FOREIGN KEY (department_id, department_code)
REFERENCES departments(department_id,department_code));
Table EMPLOYEES created.
Which means that whenever you insert a record into that table you will have to supply both the ID and code for an existing department, of course.

Resources