SQL Creating Table Dependencies - sql

I've been stuck at creating tables.
Here is the code I am stuck at how to make the code work in loan_type to write office worker or non office worker
create table loaner
(
loan_id number(5) primary key,
loan_type VARCHAR2 (16),
loan_start_date date,
loan_end_date date,
)
create table office_worker
(
worker_id number(5) primary_key,
loan_id number(5) references loaner(loan_id),
worker_name varchar2(50)
)
create table nonoffice_worker
(
nonworker_id number(5) primary_key,
loan_id number(5) references loaner(loan_id),
nonworker_name varchar2(50)
);
commit;

You can't create a constrain to check that with the existing table structures. A common way to do it is like this:
create table loaner (
loan_id number(5) primary key,
loan_type VARCHAR2 (16),
loan_start_date date,
loan_end_date date,
constraint loaner_uk unique (loan_id, loan_type)
);
create table office_worker (
worker_id number(5) primary_key,
loan_id number(5),
loan_type VARCHAR2 (16),
worker_name varchar2(50),
constraint office_worker_loaner_fk foreeign key (loan_id, loan_type) references loaner (loan_id, loan_type),
constraint office_worker_loan_type_chk check (loan_type = 'OFFICE')
);
create table nonoffice_worker (
nonworker_id number(5) primary_key,
loan_id number(5),
loan_type VARCHAR2 (16),
nonworker_name varchar2(50),
constraint nonoffice_worker_loaner_fk foreeign key (loan_id, loan_type) references loaner (loan_id, loan_type),
constraint nonoffice_worker_loan_type_chk check (loan_type = 'NONOFFICE')
);
That is:
Create a redundant UNIQUE constraint in (load_id, loan_type) in the first table.
Add loan_type to the subtype tables and base the foreign key on (loan_id, loan_type).
Add a check constraint to each subtype table to ensure the correct loan_type is used.

You can also add a check constraint to your table, but you must check that column loan_type only contains desired values(office worker or non office worker), else this won't work:
alter table loaner add (CONSTRAINT chk_loan_type CHECK
(loan_type='office worker' or loan_type='non office worker'));

Related

Oracle DDL Error - ORA 00907: missing right parenthesis

This DDL query does not work and I cannot figure out why, would appreciate help from more experienced individuals :)
When I try running it in Oracle it returns the error message ORA-00907: Missing right parenthesis
CREATE TABLE "Attendees" (
"attendee_id" number(8) PRIMARY KEY,
"attendee_name" varchar2(50),
"attendee_class" number(4),
"attendee_school" varchar2(50),
"attendee_status" varchar2(50)
);
CREATE TABLE "History" (
"history_id" number(4) PRIMARY KEY,
"history_dt" date,
"history_time" timestamp,
"history_status" varchar2(50)
);
CREATE TABLE "Event" (
"event_id" number(10) PRIMARY KEY,
"event_name" varchar2(200),
"event_location" varchar2(100),
"event_size" number(4),
"start_dt" date,
"end_dt" date,
"class_restriction" number(4),
"school_restriction" varchar2(100),
---"booking_id" number(10) references Booking(booking_id)
--- "reservation_id" number(3) references Reservation(reservation_id)
);
CREATE TABLE "Booking" (
"booking_id" number(4) PRIMARY KEY,
"booking_date" date,
"booking_cost" number(8),
"booking_status" varchar2(50),
"history_id" number(4) references History(history_id),
"event_id" number(10) references Event(event_id)
);
CREATE TABLE "Reservation" (
"reservation_id" number(3) PRIMARY KEY,
"event_id" references Event(event_id),
"attendee_id" references Attendees(attendee_id),
"reservation_status" varchar2(50)
);

ORA-00957 duplicate column name error, when trying to reference the same primary key with 3 foreign keys

I'm having problems with creating tables:
CREATE TABLE EMPLOYEE
(
employee_id NUMBER(5) NOT NULL UNIQUE,
position VARCHAR2(100) NOT NULL,
name VARCHAR2(255) NOT NULL,
salary NUMBER(6) NOT NULL
CONSTRAINT employee_pk PRIMARY KEY (employee_id)
);
CREATE TABLE PROJECT
(
project_id NUMBER(5) NOT NULL UNIQUE,
name VARCHAR(100) NOT NULL,
budget NUMBER(6) NOT NULL,
consultant_leader NUMBER(5) NOT NULL,
developer_leader NUMBER(5) NOT NULL,
project_leader NUMBER(5) NOT NULL,
CONSTRAINT project_pk PRIMARY KEY (PROJECT_ID),
CONSTRAINT fk_leader
FOREIGN KEY (consultant_leader, developer_leader, project_leader)
REFERENCES EMPLOYEE (employee_id, employee_id, employee_id)
);
In the last section, when I try to reference the employee's table employee_id, I'm getting ORA-00957. I think it's because the 3 different leader type foreign key references the same employee_id, but as far as I know, it should not be a problem. Is the syntax wrong?
Your immediate problem is that you need three foreign key relationships, not one with three columns.
But, there is no need to declare a primary key as being unique. So, I would recommend:
CREATE TABLE EMPLOYEE (
employee_id NUMBER(5) NOT NULL PRIMARY KEY,
position VARCHAR2(100) NOT NULL,
name VARCHAR2(255) NOT NULL,
salary NUMBER(6) NOT NULL
);
CREATE TABLE PROJECT (
project_id NUMBER(5) NOT NULL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
budget NUMBER(6) NOT NULL,
consultant_leader NUMBER(5) NOT NULL,
developer_leader NUMBER(5) NOT NULL,
project_leader NUMBER(5) NOT NULL,
CONSTRAINT fk_leader FOREIGN KEY (consultant_leader)
REFERENCES EMPLOYEE (employee_id),
CONSTRAINT fk_leader FOREIGN KEY (developer_leader)
REFERENCES EMPLOYEE (employee_id),
CONSTRAINT fk_leader FOREIGN KEY (project_leader)
REFERENCES EMPLOYEE (employee_id)
);
You don't need to put the PRIMARY KEY constraint in-line, of course. The advantage of declaring it separately is that you can give the constraint a name to your liking.
I think you should create three distinct FK: FK_Consultant, FK_developer, FK_projleader

Calculating overtime using rows from 3 different tables

I'm fairly new to SQL and I am trying to create a trigger to calculate overtime worked by subtracting hours contracted (GRADE_HOURS) in the COMPANY_GRADE table from hours worked (TIMESHEET_HOURS) in the COMPANY_TIMESHEET table. This will then go into the TIMESHEET_OT column of the COMPANY_TIMESHEET table whenever a row is inserted into the COMPANY_TIMESHEET table.
The tables concerned are as follows:
CREATE TABLE COMPANY_TIMESHEET
(
timesheet_ID number(3) constraint timesheet_pk primary key,
Timesheet_emp number(3) constraint timesheet_empnotnull not null references company_employee,
Timesheet_wc date constraint timesheet_wcnotnull not null,
Timesheet_hours number(2),
Timesheet_OT number(2) default 0,
Timesheet_approved number(3) references company_employee
);
CREATE TABLE COMPANY_GRADE
(
grade_ID number(3) constraint grade_pk primary key,
Grade_rate number(5,2) constraint grade_ratenotnull not null,
Grade_hours number(2)
) ;
CREATE TABLE COMPANY_EMPLOYEE
(
emp_ID number(3) constraint emp_pk primary key,
Emp_firstname varchar2(50) constraint emp_firstnamenotnull not null,
Emp_surname varchar2(50),
Emp_department number(2) constraint employeeFKdepartment references company_department,
emp_street varchar2(50),
emp_town varchar2(50),
emp_district varchar2(50),
Emp_grade number(3) default 4 constraint checkempgrade check (Emp_grade between 1 and 9) references company_grade,
Emp_site varchar2(30) default 'LONDON'
);
I would appreciate any help as I have been trying for many hours now only to be met by error after error.
I have tried numerous variations of this as a starting block to try and pass the hours contracted into a variable to then subtract from another variable:
CREATE OR REPLACE TRIGGER trg_ot
BEFORE INSERT ON company_timesheet
FOR EACH ROW
DECLARE t_contracted NUMBER;
BEGIN
SELECT grade_hours INTO t_contracted
FROM company_grade
WHERE company_employee.emp_id = :new.timesheet_emp;
END;
/
CREATE OR REPLACE TRIGGER trg_ot
BEFORE INSERT ON company_timesheet
FOR EACH ROW
DECLARE t_contracted NUMBER;
BEGIN
SELECT g.grade_hours
INTO t_contracted
FROM company_grade g
INNER JOIN
company_employee e
ON ( e.emp_grade = g.grade_id )
WHERE e.emp_id = :new.timesheet_emp;
:new.timesheet_ot := :new.timesheet_hours - t_contracted;
END;
/

SQL ORA-02256: number of referencing columns must match referenced columns

these are my tables :
CREATE TABLE EMPLOYEE(
Emp_id number(4),
Emp_name varchar2(30),
Emp_gender varchar2(1),
Status varchar2(30),
Years_service number(4),
Primary Key (emp_id)
);
CREATE TABLE ACTIVITY(
Act_id number(4),
Description varchar2(30),
Category_code varchar2(1),
Primary Key(Act_id)
);
CREATE TABLE ALLOCATION(
Emp_id number(4) NOT NULL,
Act_id number(4) NOT NULL,
Hourly_rate number(5,2) NOT NULL,
Primary Key (Act_id, Emp_id),
Foreign Key (Act_id) REFERENCES ACTIVITY,
Foreign Key (Emp_id) REFERENCES EMPLOYEE,
CONSTRAINT CK_ALLOCATION_RATE CHECK(Hourly_rate > 0 and Hourly_rate<300)
);
CREATE TABLE ACTION(
Week_no number(2) NOT NULL,
Hrs_worked number(4,1) NOT NULL,
Act_id number(4) NOT NULL,
emp_id number(4) NOT NULL,
Primary Key (Week_no, Act_id, emp_id),
Foreign Key (Act_id) References Allocation,
Foreign Key (emp_id) References Allocation
);
Table employee, activity and allocation are created perfectly. but when i try to create table action and referencing the foreign key to table allocation, it says: ORA-02256: number of referencing columns must match referenced columns.
When using references I think it is a good idea to include the columns in both tables, even if the syntax makes this unnecessary (the default is to the primary key).
You have a composite primary key, so you need a composite key for the foreign reference:
CREATE TABLE ACTION (
Week_no number(2) NOT NULL,
Hrs_worked number(4,1) NOT NULL,
Act_id number(4) NOT NULL,
emp_id number(4) NOT NULL,
Primary Key (Week_no, Act_id, emp_id),
Foreign Key (Act_id, emp_id) References Allocation(Act_id, emp_id)
);

The code below is showing error of invalid identifier in oracle

create table productinfotwo
(
productId number(10),
CONSTRAINT primary_pk Primary Key(productId),
productname varchar2(100),
SUBCATEGORYID number(10),
CONSTRAINT subcategory_fk Foreign Key(SUBCATEGORYID ) REFERENCES ProductSubCategory(SUBCATEGORYID ),
COMPANYID varchar2(20),
CONSTRAINT company_fk Foreign Key(COMPANYID ) References CompanyInfo(COMPANYID ),
price float,
quantity number(10),
description varchar2(1000),
);
You need to have them in order
Create ProductSubCategory table
Create CompanyInfo table
There is no datatype called float in Oracle. You can use NUMBER(4,2) instead
Remove the comma after description
The code should be
CREATE TABLE productinfotwo
(
productid NUMBER(10),
CONSTRAINT primary_pk PRIMARY KEY(productid),
productname VARCHAR2(100),
subcategoryid NUMBER(10),
CONSTRAINT subcategory_fk FOREIGN KEY(subcategoryid ) REFERENCES
productsubcategory(subcategoryid ),
companyid VARCHAR2(20),
CONSTRAINT company_fk FOREIGN KEY(companyid ) REFERENCES companyinfo(
companyid ),
price NUMBER(4, 2),
quantity NUMBER(10),
description VARCHAR2(1000)
);