Creating foreign keys from entites that are located in separate tables - sql

I've been scratching my head at this for hours.
I have four tables Book, Author, Allocation and WorkSession
I need to make a constraint that the combination of the bid and authID exists in the Allocation table from the WorkSession table.
I tried to create a foreign key constraint like you would normally do but it won't work because the id's referenced are both foreign keys already and I can't create a new key as an identifier for the allocations table
CREATE TABLE Book(
bid NUMBER(4),
title VARCHAR2(30) NOT NULL,
SellingPrice NUMBER(6,2),
PRIMARY KEY (bid),
CONSTRAINT EM_SellingPrice CHECK(SellingPrice >= 0)
);
CREATE TABLE Author(
authID NUMBER(4),
fName VARCHAR2(30),
sName VARCHAR2(30),
PRIMARY KEY(authID),
CONSTRAINT EM_Name UNIQUE (fName,SName)
);
CREATE TABLE Allocation(
bid NUMBER(4),
authID NUMBER(4),
payRate NUMBER(6,2),
CONSTRAINT AL_PayRate CHECK(payRate >= 1 AND payRate < 80),
FOREIGN KEY(bid) REFERENCES Book(bid),
FOREIGN KEY(authID) REFERENCES Author(authID)
);
CREATE TABLE WorkSession(
bid NUMBER(4),
authID NUMBER(4),
WorkYear NUMBER(4),
WorkWeek NUMBER(2),
WorkHours NUMBER(4,2),
CONSTRAINT WY_Range CHECK(WorkYear > 2010 AND WorkYear < 2014),
CONSTRAINT WW_Range CHECK(WorkWeek >= 1 AND WorkWeek <= 52),
CONSTRAINT WH_Range CHECK(WorkHours >= 0.5 AND WorkHours <100),
CONSTRAINT FK_Check FOREIGN KEY(bid, AuthID) REFERENCES Allocation(bid, AuthID),
PRIMARY KEY(workYear, WorkWeek)
);

CREATE TABLE Allocation(
bid NUMBER(4),
authID NUMBER(4),
payRate NUMBER(6,2),
CONSTRAINT AL_PayRate CHECK(payRate >= 1 AND payRate < 80),
FOREIGN KEY(bid) REFERENCES Book(bid),
FOREIGN KEY(authID) REFERENCES Author(authID)
);
A foreign key constraint has to target something unique. You can do that with either a PRIMARY KEY constraint or a UNIQUE constraint on one or more columns. Your table "allocation", above, doesn't have either of those constraints.
If {bid, authid} are unique in the table "allocation", then you can declare PRIMARY KEY (bid, authid) in that table. A foreign key that REFERENCES allocation (bid, authid) should work then.

Related

Oracle SQL missing smth

Trying to build these tables and there's always an error or missing something, can someone help me?
Where's max_mice i'm checking if it's inbetween those values and doesn't work why?
Don't know what's wrong, already searched everywhere, don't know why they won't be created...
updated: now i got problems on table incidents...
update: the error was that i had: CONSTRAINT fun_maxmi_ch CHECK (200 > max_mice >= min_mice) instead of the code below.
CREATE TABLE Functions (
function VARCHAR(10) CONSTRAINT fun_fu_pk PRIMARY KEY,
min_mice NUMBER(3) CONSTRAINT fun_minmi_ch CHECK (min_mice > 5),
max_mice NUMBER(3),
CONSTRAINT fun_maxmi_ch CHECK (max_mice >= min_mice and max_mice < 200)
);
BUT still have a problem creating table Incidents don't know what's the problem!!!
CREATE TABLE Incidents (
nickname VARCHAR2(15),
enemy_name VARCHAR2(15),
incident_date DATE CONSTRAINT inc_indate_nn NOT NULL,
incident_desc VARCHAR2(50),
CONSTRAINT inc_con_pk PRIMARY KEY (nickname, enemy_name),
CONSTRAINT inc_nic_fk FOREIGN KEY (nickname) REFERENCES Cats(nickname),
CONSTRAINT inc_enname_fk FOREIGN KEY (enemy_name) REFERENCES Enemies(enemy_name),
);
Here's the full code:
CREATE TABLE Enemies (
enemy_name VARCHAR2(15),
hostility_degree NUMBER(2) CONSTRAINT hos_degree_ch CHECK (hostility_degree BETWEEN 1 AND 10),
species VARCHAR2(15),
bride VARCHAR2(20),
CONSTRAINT ene_name_pk PRIMARY KEY(enemy_name)
);
CREATE TABLE Functions (
function VARCHAR(10) CONSTRAINT fun_fu_pk PRIMARY KEY,
min_mice NUMBER(3) CONSTRAINT fun_minmi_ch CHECK (min_mice > 5),
max_mice NUMBER(3),
CONSTRAINT fun_maxmi_ch CHECK (max_mice >= min_mice and max_mice < 200)
);
CREATE TABLE Bands (
Band_no NUMBER(2) CONSTRAINT ban_no_pk PRIMARY KEY,
name VARCHAR2(20) CONSTRAINT ban_name_nn NOT NULL,
site VARCHAR2(15) CONSTRAINT ban_site_un UNIQUE,
band_chief VARCHAR(15) CONSTRAINT ban_chief_un UNIQUE
);
CREATE TABLE Cats (
name VARCHAR2(15) CONSTRAINT cat_name_nn NOT NULL,
gender VARCHAR2(1) CONSTRAINT cat_gen_ch CHECK (gender IN('M', 'W')),
nickname VARCHAR2(15) CONSTRAINT cat_pk PRIMARY KEY,
function VARCHAR2(10),
chief VARCHAR2(15),
in_herd_since DATE DEFAULT SYSDATE CONSTRAINT cat_inherd_nn NOT NULL,
mice_ration NUMBER(3),
mice_extra NUMBER(3),
band_no NUMBER(2),
CONSTRAINT cat_banno_fk FOREIGN KEY (band_no) REFERENCES Bands(band_no),
CONSTRAINT cat_chief_fk FOREIGN KEY (chief) REFERENCES Cats(nickname),
CONSTRAINT cat_fun_fk FOREIGN KEY (function) REFERENCES Functions(function)
);
ALTER TABLE Bands
ADD CONSTRAINT ban_chief_fk FOREIGN KEY (band_chief) REFERENCES Cats(nickname);
CREATE TABLE Incidents (
nickname VARCHAR2(15),
enemy_name VARCHAR2(15),
incident_date DATE CONSTRAINT inc_indate_nn NOT NULL,
incident_desc VARCHAR2(50),
CONSTRAINT inc_con_pk PRIMARY KEY (nickname, enemy_name),
CONSTRAINT inc_nic_fk FOREIGN KEY (nickname) REFERENCES Cats(nickname),
CONSTRAINT inc_enname_fk FOREIGN KEY (enemy_name) REFERENCES Enemies(enemy_name),
);
here's max_mice i'm checking if it's in between those values and doesn't work why?
When it comes to the first table in your code, Functions, the problem is with the declaration of the last check constraint:
max_mice NUMBER(3) CONSTRAINT fu_maxmi_ch CHECK (200 > max_mice >= min_mouse)
Issues:
the double inequality condition is not supported
a multi-column check constraint need to be declared at table level rather than at column level
This works:
CREATE TABLE Functions (
function VARCHAR(10) CONSTRAINT fu_fu_pk PRIMARY KEY,
min_mice NUMBER(3) CONSTRAINT fu_minmi_ch CHECK (min_mice > 5),
max_mice NUMBER(3),
CONSTRAINT fu_maxmi_ch CHECK (max_mice >= min_mice and max_mice < 200)
);
Note: I would not recommend naming a column function, since it obviously conflicts with a SQL keyword.

'Missing keyword' this error is coming while i create a table

'Missing keyword' this error is coming while I create a table using the below query:
CREATE TABLE ACCTS
(
ACCT_NO NUMBER (12),
ACCTH_NAME VARCHAR2(50),
ACCTH_ADD VARCHAR2(100),
ACCTH_STATE VARCHAR2(50),
ACCTH_DOB DATE ,
ACCT_DT_CREATED DATE,
BRANCH_CODE NUMBER(5),
ACCT_TYPE_CODE NUMBER(6),
CONSTRAINT ACCT_NO_PK PRIMARY KEY (ACCT_NO),
CONSTRAINT ACCTH_STATE_FK1 FOREIGN KEY (ACCTH_STATE) REFERENCES STATES (STATE_ID),
CONSTRAINT ACCT_TYPE_CODE_FK2 FOREIGN KEY (ACCT_TYPE_CODE) REFERENCES ACCT_TYPES(ACCT_TYPE_CODE)
)
I added your query to SQL Fiddle (adding the missing tables so the foreign key constraints will work) and the query works:
CREATE TABLE STATES
(
STATE_ID VARCHAR2(50),
CONSTRAINT ACCT_STATE_ID PRIMARY KEY (STATE_ID)
);
CREATE TABLE ACCT_TYPES
(
ACCT_TYPE_CODE NUMBER(6),
CONSTRAINT ACCT_TYPES_ID PRIMARY KEY (ACCT_TYPE_CODE)
);
CREATE TABLE ACCTS
(
ACCT_NO NUMBER (12),
ACCTH_NAME VARCHAR2(50),
ACCTH_ADD VARCHAR2(100),
ACCTH_STATE VARCHAR2(50),
ACCTH_DOB DATE ,
ACCT_DT_CREATED DATE,
BRANCH_CODE NUMBER(5),
ACCT_TYPE_CODE NUMBER(6),
CONSTRAINT ACCT_NO_PK PRIMARY KEY (ACCT_NO),
CONSTRAINT ACCTH_STATE_FK1 FOREIGN KEY (ACCTH_STATE) REFERENCES STATES (STATE_ID),
CONSTRAINT ACCT_TYPE_CODE_FK2 FOREIGN KEY (ACCT_TYPE_CODE) REFERENCES ACCT_TYPES(ACCT_TYPE_CODE)
)
http://sqlfiddle.com/#!4/b726da

query not working for foreign key as a composite primary key of another table oracle SQL

I am trying to add two foreign keys for an associative entity in SQL Oracle. The primary key that I am referring from another table is a composite primary key. When I try to enter the SQL it says
no matching unique or primary key for this column-list
The code that I used to create the tbl_customer
CREATE TABLE tbl_Customer(
customer_id NUMBER(4)
CONSTRAINT pk_customer PRIMARY KEY,
CustomerName VARCHAR2(50) NOT NULL,
Telephone VARCHAR2(10),
CusEmail VARCHAR2(20),
CONSTRAINT cus_email UNIQUE(CusEmail),
location_id NUMBER(4)
CONSTRAINT fk_location_id references tbl_Location(location_id));
SQL to create tbl_Vehicle
CREATE TABLE tbl_Vehicle(
vehicle_id NUMBER(4),
PlateNo VARCHAR2(10),
CONSTRAINT pk_v PRIMARY KEY(vehicle_id,PlateNo),
Brand VARCHAR2(20),
Model VARCHAR2(10),
TotalNoSeats NUMBER(2),
Class VARCHAR2(4) NOT NULL,
CONSTRAINT no_seats CHECK (TotalNoSeats<100),
driver_id NUMBER(4)
CONSTRAINT fk_driveridV references tbl_Driver(driver_id),
location_id NUMBER(4)
CONSTRAINT fk_locationV references tbl_Location(location_id));
The associative table is
CREATE TABLE tbl_Customer_Vehicle(
customer_id NUMBER(4)
CONSTRAINT fk_customer_id references tbl_Customer(customer_id),
vehicle_id NUMBER(4)
CONSTRAINT fk_vehicle_id references tbl_Vehicle(vehicle_id)
);
where the error is in this line
CONSTRAINT fk_vehicle_id references tbl_Vehicle(vehicle_id)
*
Is this error because vehicle_id is a composite primary key?
Please help!!
You need to add PlateNo column in tbl_Customer_Vehicle table and define foreign key on it.
CREATE TABLE tbl_Customer_Vehicle
(
customer_id NUMBER(4)
CONSTRAINT fk_customer_id references tbl_Customer(customer_id),
vehicle_id NUMBER(4),
PlateNo VARCHAR2(10),
CONSTRAINT fk_vehicle_id_PlateNo FOREIGN KEY(vehicle_id,PlateNo)
references tbl_Vehicle(vehicle_id,PlateNo)
);

ORA-02270 - Can't find fault

Having trouble with a ORA-02270 error. After going through the primary keys and foreign keys over and over, I can't find what the problem is.
CREATE TABLE CUSTACC(
REFNO CHAR(4),
ACCNO NUMBER(7),
CONSTRAINT CUSTACC_PK PRIMARY KEY (REFNO, ACCNO));
CREATE TABLE CUST(
CUSTNO CHAR(4),
NAME VARCHAR2(30),
ADDRESS VARCHAR2(50),
AREA VARCHAR2(10),
CONSTRAINT CUSTNO_PK PRIMARY KEY(CUSTNO),
CONSTRAINT CUSTNO_FK FOREIGN KEY (CUSTNO)
REFERENCES CUSTACC(REFNO));
CREATE TABLE ACC(
ACCNO NUMBER(7),
BALANCE NUMBER(8,2),
BRANCH VARCHAR2(10),
OPENED DATE,
BONUS NUMBER(8,2),
CONSTRAINT ACCNO_PK PRIMARY KEY (ACCNO),
CONSTRAINT ACCNO_FK FOREIGN KEY (ACCNO)
REFERENCES CUSTACC(ACCNO));
I must be blind but I've checked everything people have suggested before.
The primary key on CUSTACC consists of two columns: REFNO and ACCNO.
The foreign key reference should include both of them. So, your tables need both columns.
I suspect, in fact, that you want the foreign key reference in the first table, something like this:
CREATE TABLE CUST (
CUSTNO CHAR(4),
NAME VARCHAR2(30),
ADDRESS VARCHAR2(50),
AREA VARCHAR2(10),
CONSTRAINT CUSTNO_PK PRIMARY KEY(CUSTNO)
);
CREATE TABLE ACC (
ACCNO NUMBER(7),
BALANCE NUMBER(8,2),
BRANCH VARCHAR2(10),
OPENED DATE,
BONUS NUMBER(8,2),
CONSTRAINT ACCNO_PK PRIMARY KEY (ACCNO)
);
CREATE TABLE CUSTACC (
REFNO CHAR(4),
ACCNO NUMBER(7),
CONSTRAINT CUSTACC_PK PRIMARY KEY (REFNO, ACCNO),
CONSTRAINT CUSTNO_FK FOREIGN KEY (REFNO)
REFERENCES CUST(CUSTNO),
CONSTRAINT ACCNO_FK FOREIGN KEY (ACCNO)
REFERENCES ACC(ACCNO)
);
Here is a SQL Fiddle.

error ORA-02270: no matching unique or primary key for this column-list

I'm practicing a lab manual excercise in which I have to create 6 tables. Creation of 5 is
successful.
But one line is giving error
constraint GRADE_Designation_FK
FOREIGN KEY(Designation) References EMPLOYEE(Designation),
ERROR at line 7:
ORA-02270: no matching unique or primary key for this column-list
Queries of 2 linked tables are
create table EMPLOYEE
(
Empno number(4) constraint EMPLOYEE_Empno_PK PRIMARY KEY,
Name varchar2(10) not null,
Designation varchar2(50),
Qualification varchar2(10),
Joindate date
);
create table GRADE
(
Designation varchar2(50) constraint GRADE_Designation_PK PRIMARY KEY,
Grade number(2),
TotalPosts number(4),
PostsAvailable number(4),
constraint GRADE_Grade_CK check(Grade between 1 and 20),
constraint GRADE_PostsAvailable_CK check(PostsAvailable <= TotalPosts),
constraint GRADE_Designation_FK FOREIGN KEY(Designation) References EMPLOYEE(Designation)
);
Tried
create table GRADE
(
Designation varchar2(50) constraint GRADE_Designation_PK PRIMARY KEY,
Grade number(2),
TotalPosts number(4),
PostsAvailable number(4),
constraint GRADE_Grade_CK check(Grade between 1 and 20),
constraint GRADE_PostsAvailable_CK check(PostsAvailable <= TotalPosts)
);
create table EMPLOYEE
(
Empno number(4) constraint EMPLOYEE_Empno_PK PRIMARY KEY,
Name varchar2(10) not null,
Designation varchar2(50) NOT NULL UNIQUE,
Qualification varchar2(10),
Joindate date default sysdate
constraint EMPLOYEE_Designation_FK FOREIGN KEY(Designation) References GRADE(Designation),
);
Now new error
constraint EMPLOYEE_Designation_FK FOREIGN KEY(Designation) References GRADE(Designation)
*
ERROR at line 8:
ORA-02253: constraint specification not allowed here
You've got the constraint on the wrong table. You should create a foreign key on EMPLOYEE.DESIGNATION, referencing back to GRADE.DESIGNATION.
So your tables should look something like:
create table GRADE
(
Designation varchar2(50) constraint GRADE_Designation_PK PRIMARY KEY,
Grade number(2),
TotalPosts number(4),
PostsAvailable number(4),
constraint GRADE_Grade_CK check(Grade between 1 and 20),
constraint GRADE_PostsAvailable_CK check(PostsAvailable <= TotalPosts),
);
create table EMPLOYEE
(
Empno number(4) constraint EMPLOYEE_Empno_PK PRIMARY KEY,
Name varchar2(10) not null,
Designation varchar2(50)
constraint EMPLOYEE_FK1
REFERENCES GRADE(DESIGNATION),
Qualification varchar2(10),
Joindate date
);
Share and enjoy.
ORA-02270: no matching unique or primary key for this column-list
That error is very self explanatory and tells you what's wrong. In your case, you are trying to create foreign key on a non primary key column and so the error
constraint GRADE_Designation_FK FOREIGN KEY(Designation)
References EMPLOYEE(Designation)
<--Here
Designation in EMPLOYEE table is not a primary key and you can't create FK on a non primary key column. Your table creation rather should look like
create table GRADE
(
Designation varchar2(50) constraint GRADE_Designation_PK PRIMARY KEY,
employee_Empno number(4),
Grade number(2),
TotalPosts number(4),
PostsAvailable number(4),
constraint GRADE_Grade_CK check(Grade between 1 and 20),
constraint GRADE_PostsAvailable_CK check(PostsAvailable <= TotalPosts),
constraint GRADE_Designation_FK FOREIGN KEY(employee_Empno)
References EMPLOYEE(Empno));
It was the order of execution which has causes us this issue, make sure you have tables created with NOT NULL Enabled before adding those constraints to avoid this error.