"You cannot add or change a record because a related record is required in table" - sql

I am running into the error
You cannot add or change a record because a related record is required in table
whenever I try to add a new entry to my tables. Here is my SQL code:
SQL DDL
CREATE TABLE ART_OBJECT
(
Id_no NUMBER PRIMARY KEY,
Artist VARCHAR(20),
Year INT UNIQUE,
Title VARCHAR(20),
Description VARCHAR(20),
Style VARCHAR(20) UNIQUE
);
ALTER TABLE ART_OBJECT
ADD CONSTRAINT ArtistName FOREIGN KEY (Artist) REFERENCES ARTIST(Name)
ALTER TABLE ART_OBJECT
ADD CONSTRAINT EName FOREIGN KEY (Title) REFERENCES EXHIBITIONS(Name)
CREATE TABLE ARTIST
(
Name VARCHAR(20),
DateBorn DATE,
Date_died DATE ,
Country_of_origin VARCHAR(20),
Epoch INT,
Main_style VARCHAR(20),
Description VARCHAR(20)
);
ALTER TABLE ARTIST
ADD CONSTRAINT TimeMade FOREIGN KEY (Epoch) REFERENCES ART_OBJECT(Year)
CREATE TABLE PERMANENT_COLLECTION
(
Date_acquired DATE,
Status VARCHAR(20),
Cost NUMBER,
Year INT,
ArtID NUMBER
);
ALTER TABLE PERMANENT_COLLECTION
ADD CONSTRAINT TimeMade FOREIGN KEY (Year) REFERENCES ART_OBJECT(Year)
ALTER TABLE PERMANENT_COLLECTION
ADD CONSTRAINT PCID FOREIGN KEY (ArtID) REFERENCES ART_OBJECT(Id_no)
CREATE TABLE BORROWED
(
Id_no NUMBER,
Name VARCHAR(20),
Date_borrowed DATE,
Date_returned DATE
);
ALTER TABLE BORROWED
ADD CONSTRAINT BID FOREIGN KEY (Id_no) REFERENCES ART_OBJECT(Id_no)
ALTER TABLE BORROWED
ADD CONSTRAINT BName FOREIGN KEY (Name) REFERENCES COLLECTIONS(CName)
CREATE TABLE PAINTING
(
Paint_type VARCHAR(20),
Drawn_on VARCHAR(20),
Style VARCHAR(20),
ArtID NUMBER
);
ALTER TABLE PAINTING
ADD CONSTRAINT PStyle FOREIGN KEY (Style) REFERENCES ART_OBJECT(Style)
ALTER TABLE PAINTING
ADD CONSTRAINT PID FOREIGN KEY (ArtID) REFERENCES ART_OBJECT(Id_no)
CREATE TABLE SCULPTURE
(
Material VARCHAR(20),
Height VARCHAR(20),
Weight VARCHAR(20),
Style VARCHAR(20),
ArtID NUMBER
);
ALTER TABLE SCULPTURE
ADD CONSTRAINT SCStyle FOREIGN KEY (Style) REFERENCES ART_OBJECT(Style)
ALTER TABLE PAINTING
ADD CONSTRAINT SCID FOREIGN KEY (ArtID) REFERENCES ART_OBJECT(Id_no)
CREATE TABLE STATUE
(
Material VARCHAR(20),
Height VARCHAR(20),
Weight VARCHAR(20),
Style VARCHAR(20),
ArtID NUMBER
);
ALTER TABLE STATUE
ADD CONSTRAINT STStyle FOREIGN KEY (Style) REFERENCES ART_OBJECT(Style)
ALTER TABLE STATUE
ADD CONSTRAINT STID FOREIGN KEY (ArtID) REFERENCES ART_OBJECT(Id_no)
CREATE TABLE OTHER
(
Type VARCHAR(20),
Style VARCHAR(20),
ArtID NUMBER
);
ALTER TABLE OTHER
ADD CONSTRAINT OStyle FOREIGN KEY (Style) REFERENCES ART_OBJECT(Style)
ALTER TABLE OTHER
ADD CONSTRAINT OID FOREIGN KEY (ArtID) REFERENCES ART_OBJECT(Id_no)
CREATE TABLE COLLECTIONS
(
CName VARCHAR(20) PRIMARY KEY,
Type VARCHAR(20),
Description VARCHAR(20),
Address VARCHAR(20),
Phone VARCHAR(20),
Contact_person VARCHAR(20),
);
CREATE TABLE EXHIBITIONS
(
Name VARCHAR(20) PRIMARY KEY,
Start_date DATE,
End_date DATE,
E_id NUMBER,
);
ALTER TABLE EXHIBITIONS
ADD CONSTRAINT EID FOREIGN KEY (E_id) REFERENCES ART_OBJECT(Id_no)
CREATE TABLE EXHIBITS_IN
(
Id_no NUMBER,
CName VARCHAR(20)
);
ALTER TABLE EXHIBITS_IN
ADD CONSTRAINT EIID FOREIGN KEY (Id_no) REFERENCES ART_OBJECT(Id_no)
ALTER TABLE EXHIBITS_IN
ADD CONSTRAINT EIName FOREIGN KEY (CName) REFERENCES COLLECTIONS(CName)
Database relations table for reference:
DB Relations Table
EER diagram for reference:
EER Diagram

Related

Foreign key 'fk_orders_SalesRep' references invalid column 'snum' in referencing table 'orders'

What am I doing wrong? I'm trying to add a foreign key to an existing table
ALTER TABLE orders
ADD CONSTRAINT fk_orders_SalesRep
FOREIGN KEY (snum)
REFERENCES SalesRep(snum);
Table information
create table SalesRep(
snum varchar(5) primary key,
sname varchar(10),
hours Int
)
create table orders(
odId varchar(5) primary key,
itId varchar(5) foreign key(itId) references items(itId)
)
Like Martin said add the column first
ALTER TABLE orders
ADD snum varchar(5);
And then add the fk
ALTER TABLE orders
ADD CONSTRAINT fk_orders_SalesRep
FOREIGN KEY (snum)
REFERENCES SalesRep(snum);

ERROR :ORA-02264: name already used by an existing constraint

create table ward
(
wnum int primary key,
wname varchar(30),
phoneno int,
wloc varchar(50),
chnursename varchar(20) constraint ward_fk references charge_nurse
);
create table charge_nurse
(
chnurse varchar(20) constraint charge_nurse_pk primary key,
stnum int constraint charge_nurse_fk references staff
);
create table staff
(
stname varchar(20),
stnum int constraint staff_pk primary key,
addr varchar(20),
phoneno int,
stposition varchar(30),
specality varchar(30) unique,
shift varchar(10),
noofhoursperweek int
);
create table generalsupplies
(
itnum int constraint generalsupplies_pk primary key,
itname varchar(20) unique,
quantityinstock int,
reorder varchar(10),
despt varchar(10),
costperunit int
);
create table pharmasupplies
(
dnum int constraint pharmasupplies_pk primary key,
dname varchar(30) unique,
despt varchar(20),
dosage_Mg int,
quantityinstock int,
reorder varchar(10),
costperunit int
);
While creating the below table I am facing problem:
ORA-02264: name already used by an existing constraint
create table centralsupplies
(
wardnum int constraint centralsupplies_fk references ward,
itemnum int constraint centralsupplies_fk references generalsupplies,
drugnum int constraint centralsupplies_fk references pharmasupplies,
quantity_required varchar(20),
staffname varchar(10) references staff(stname),
staffnum int constraint centralsupplies_fk references staff,
regnum int unique,
dateord date,
daterec date
);
How do I solve this problem?
You use 3 times the same constraint name centralsupplies_fk in your centralsupplies table.
3 constraints = 3 constraint names
Your create table statement has four foreign keys all called centralsupplies_fk. That is not allowed: constraint names must be unique within a schema. You must give each one a different name.
It is usual practice to include the referenced table in the key name. So
create table centralsupplies
(
wardnum int constraint centralsupplies_ward_fk references ward,
itemnum int constraint centralsupplies_generalsupplies_fk references generalsupplies,
drugnum int constraint centralsupplies_pharmasupplies_fk references pharmasupplies,
quantity_required varchar(20),
staffname varchar(10) references staff(stname),
staffnum int constraint centralsupplies_staff_fk references staff,
regnum int unique,
dateord date,
daterec date
)
Also you have another foreign key constraint on STAFFNAME which you have not named. You do not need to name constraints, the system will generate a unique one for you, but it's generally a good idea to name them, not least because it is easier to diagnose relational integrity error messages with meaningfully named constraints.
However, in this case the correct solution is to drop the STAFFNAME column. You already have a foreign on the STAFF table, and you should join to that table whenever you need to display a value for STAFFNAME. Besides you do not have a unique constraint on staff.stname column, so the foreign key statement will fail: foreign keys can only reference primary or unique keys.

column "parent_id" referenced in foreign key constraint does not exist when creating SQL table

I am new in SQL and trying to understand Foreign key syntax. I know this was asked in multiple questions but each question I found did not seem to teach me what I am doing wrong here.
This is my SQL code:
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
CREATE TABLE Adult
(
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
CREATE TABLE Shop
(
id int primary key
);
CREATE TABLE Drink
(
name varchar(30) primary key
);
CREATE TABLE AlcoholicDrink
(
FOREIGN KEY (name) REFERENCES Drink(name)
);
CREATE TABLE NonAlcoholicDrink
(
FOREIGN KEY (name) REFERENCES Drink(name)
);
And this is the error I am getting:
ERROR: column "parent_id" referenced in foreign key constraint does not exist
SQL state: 42703
You need to add fields in your tables to make the reference. Something like this :
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
minor_id serial primary key,
parent_id int,
other_fields text etc.
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
This is simply the reason why it's not working.
Like this
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
parent_id int ,
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
To add an answer without just a code snippet:
You've got the right idea with FOREIGN KEY (parent_id) REFERENCES Customer(id). This bit adds a constraint or a "rule" to your table. This constraint ensures that parent_id holds a real id in your Customer table.
The error message told us that column "parent_id" referenced in foreign key constraint does not exist. The confusion comes, understandably, from mistaking the constraint declaration for a column declaration.
As others have pointed out, we need to both 10 declare the foreign key constraint and 2) declare the column.
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
parent_id int, -- Declare the column
FOREIGN KEY (parent_id) REFERENCES Customer(id) -- Declare the constraint
);

I'm trying to alter one of my tables and adding a foreign key it I keep getting the same error

The error I get is:
Msg 1769, Level 16, State 1, Line 30
Foreign key 'fk_pid' references invalid column 'P_ID' in referencing table 'Detainee'.
My table structure:
create table Detainee
(
D_ID bigint primary key identity not null,
Fname nvarchar(50),
Lname nvarchar(50),
Address varchar(200),
DateOfBirth date,
DateOfArrest date,
SourceOfArrest nvarchar(max)
);
create table PrisonCell
(
P_ID bigint primary key not null,
Capacity int,
RemCap int,
C_Type varchar(20)
);
Code to alter table:
alter table Detainee
add constraint fk_pid foreign key(P_ID) references PrisonCell(P_ID)
What you need to do is:
add the column P_ID BIGINT to your table Detainee
then add the FK constraint to that column
Just adding the FK constraint to the table isn't going to automagically add a column as well...
You should be able to do this in a single step:
ALTER TABLE Detainee
ADD P_ID BIGINT
CONSTRAINT fk_pid FOREIGN KEY REFERENCES PrisonCell(P_ID);

Error when creating table in Oracle Database 11g SQL

I'm new to SQL and I'm trying to create a new table however I get an error when I run my script in the SQL command line, the errors I'm getting are either ORA-00942 missing right parenthesis or ORA-00942 table or view does not exist.
Oh and yes I know I have probably written some terrible script but as mentioned earlier I'm learning so any meaningful criticism would be appreciated along with some help :).
CREATE TABLE Branch
(
Branch_ID varchar(5),
Branch_Name varchar(255),
Branch_Address varchar(255),
Branch_Town varchar(255),
Branch_Postcode varchar(10),
Branch_Phone varchar(50),
Branch_Fax varchar(50),
Branch_Email varchar(50),
Property_ID varchar(5),
Contract_ID varchar(5),
Staff_ID varchar(5),
PRIMARY KEY (Branch_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_Id),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id)
);
CREATE TABLE Staff
(
Staff_ID varchar(5),
Staff_Forename varchar(255),
Staff_Surname varchar(255),
Staff_Address varchar(255),
Staff_Town varchar(255)
Staff_Postcode varchar(10),
Staff_Phone varchar(50),
Staff_DOB varchar(50),
Staff_NIN varchar(10),
Staff_Salary varchar(50),
Staff_Date_Joined varchar(100),
Staff_Viewing_Arranged varchar(100),
Branch_ID varchar(5),
Sales_ID varchar(5),
PRIMARY KEY (Staff_ID),
FOREIGN KEY (Branch_ID) REFERENCES Branch(Branch_ID),
FOREIGN KEY (Sales_ID) REFERENCES Sales(Sales_Id)
);
CREATE TABLE Sales
(
Sales_ID varchar(5),
Property_Address varchar(255),
Property_Town varchar(255)
Property_Postcode varchar(10),
Property_Type varchar(255),
Num_Rooms varchar(50),
Date_of_Sale varchar(10),
Sales_Bonus varchar(100),
Branch_ID varchar(5),
Property_ID varchar(5),
Staff_ID varchar(5)
Seller_ID varchar(5),
PRIMARY KEY (Sales_ID),
FOREIGN KEY (Branch_ID) REFERENCES Branch(Branch_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id),
FOREIGN KEY (Seller_ID) REFERENCES Seller(Seller_Id)
);
CREATE TABLE Contract
(
Contract_ID varchar(5),
Contract_Signed_Date varchar(50),
Property_ID varchar(5),
Buyer_ID varchar(5),
Seller_ID varchar(5),
Branch_ID varchar(5),
PRIMARY KEY (Contract_ID),
FOREIGN KEY (Branch_ID) REFERENCES Branch(Branch_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Seller_ID) REFERENCES Seller(Seller_Id),
FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_Id)
);
CREATE TABLE Buyer
(
Buyer_ID varchar(5),
Viewing_Data varchar(255),
Maximum_Budject varchar(255),
Purchase_Price varchar (50),
Buyer_Forename varchar(255),
Buyer_Surname varchar(255),
Buyer_Address varchar(255),
Buyer_Town varchar(255),
Buyer_Postcode varchar(10),
Property_ID varchar(5),
Contract_ID varchar(5),
Survey_ID varchar(5),
PRIMARY KEY (Buyer_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_Id),
FOREIGN KEY (Survey_ID) REFERENCES Survey(Survey_Id)
);
CREATE TABLE Seller
(
Seller_ID varchar(5),
Seller_Forename varchar(255),
Seller_Surname varchar(255),
Seller_Address varchar(255),
Seller_Town varchar(255),
Seller_Postcode varchar(10),
Seller_Property_ID varchar(5),
No_of_Bed varchar(5),
Contract_ID varchar(5),
Property_ID varchar(5),
Sales_ID varchar(5),
PRIMARY KEY (Seller_ID),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Sales_ID) REFERENCES Sales(Sales_Id)
);
CREATE TABLE Property
(
Property_ID varchar(5),
Property_Address varchar(255),
Property_Town varchar(255),
Property_Postcode varchar(10),
Asking_Price varchar(20),
Date_Registered varchar(50),
Property_Fixtures varchar(255),
Size_of_Rooms varchar(100),
Buyer_ID varchar(5),
Staff_ID varchar(5),
Contract_ID varchar(5),
Seller_ID varchar(5),
PRIMARY KEY (Property_ID),
FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_ID),
FOREIGN KEY (Seller_ID) REFERENCES Seller(Seller_ID),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_Id)
);
CREATE TABLE Survey
(
Survey_ID varchar(5),
No_of_Survey varchar(10),
Survey_Type varchar(255),
Organised_By varchar(255),
Property_ID varchar(5),
Staff_ID varchar(5),
Buyer_ID varchar(5),
PRIMARY KEY (Survey_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id),
FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_Id)
);
CREATE TABLE Advert
(
Survey_ID Advert_ID varchar(5),
No_of_Adverts varchar(10),
Advert_Website varchar(255),
Advert_Newspaper varchar(255),
Property_ID varchar(5),
PRIMARY KEY (Advert_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID)
);
STAFF Table miss a , at the end
Staff_Town varchar(255)
Sales Table too
Property_Town varchar(255)
Staff_ID varchar(5)
Also you can't define constrains to table not created yet. I could found the error removing those constrain.
Can't define the foreign key references until the table being referred to is created. So, if the foreign key references are circular, in that A => B => C => A, they you will first have to create the tables, then use ALTER TABLE to define the foreign keys. Otherwise, create the tables in order, where first you create a table, then create the table that is referencing it.
Your script references tables that haven't been created yet. I.e., look at your first create table. by that time, STAFF and other tables don't exist yet, but you're setting them as foreign key references.
You should create your tables first, and then apply constraints after the dependencies are in place, using alter:
ALTER TABLE BRANCH
ADD CONSTRAINT fk_staff_id
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id);
because you have tables that references each other you need create the references after the tables are created
--create table Buyer
CREATE TABLE Buyer
(
Buyer_ID varchar(5),
Property_ID varchar(5),
PRIMARY KEY (Buyer_ID)
);
--create table Property
CREATE TABLE Property
(
Property_ID varchar(5),
Buyer_ID varchar(5),
PRIMARY KEY (Property_ID)
);
--now you can set your reference constraints
ALTER TABLE Property Add FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_ID);
ALTER TABLE Buyer Add FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID);
Your syntax is not clear on your keys.
Here is an example of what a create table with a primary key and a foreign key (fk) would look like:
CREATE TABLE animals (
animal_id NUMBER(10) PRIMARY KEY,
animal_name VARCHAR2(50) NOT NULL,
animal_species_code NUMBER(50) NOT NULL
CONSTRAINT fk_animal_species REFERENCES animal_species
(species_code));
It appears that the modeling in this set of tables might benefit from some additions and changes.
As an example, the STAFF table references the BRANCH table - but BRANCH also references STAFF. This means that BRANCH can only reference one STAFF, and vice versa, which I suspect is not what's wanted. It appears that you want a many-to-many relationship between BRANCH and STAFF, and the standard solution for enabling this is a junction table - one which contains the primary keys of both tables. Such a junction table between BRANCH and STAFF might look like:
CREATE TABLE BRANCH_STAFF
(BRANCH_ID VARCHAR(5)
CONSTRAINT BRANCH_STAFF_FK1
REFERENCES BRANCH(BRANCH_ID),
STAFF_ID VARCHAR(5)
CONSTRAINT BRANCH_STAFF_FK2
REFERENCES STAFF(STAFF_ID),
CONSTRAINT PK_BRANCH_STAFF
PRIMARY KEY (BRANCH_ID, STAFF_ID)
USING INDEX);
Best of luck.