no matching unique or primary key for this column-list. Im not sure how to solve it for my case - sql

I've been trying to create this table in my database. We were told to use Oracle-Apex for creating the database. So I keep getting this error that I cant solve:
If I remove the last line of the code, it creates the table fine without any errors.
Here are screenshots of the other tables being referenced here:
Company Table
Branch Table
IDK if this is a rookie mistake, I only learnt apex/sql in like an hour and went off to make the database. Thank you for helping me! :)

The column(s) referenced by a foreign key must have a unique index in the source table (or they must be the primary key of that table). Your code fails because of the following foreign key declaration, where the target is not unique:
foreign key (BranchNo) references Branch(BranchNo)
Here, I think that you want a compound foreign key that references the primary key of Branch rather than two different keys. Branch(CCode) references Company(CCode) already so there is no need to put that relationship in the Equipment table.
create table Equipment(
CCode int,
BranchNo int,
EquipNo it,
Description varchar2(50),
NumberOfEquip int,
primary key(CCode, BranchNo, EquipNo),
foreign key (CCode, BranchNo) references Branch(CCode, BranchNo)
);

Related

How do I create a bridge table? Do I create two primary keys and two foreign keys?

I have a table that I want to make which is a bridge table for teacher and class table. This is the ERD
I initially thought I'm going to create this table by
CREATE TABLE class_teacher
(
teacher_id number(3),
class_id number(2),
CONSTRAINT class_teacher_pk
PRIMARY KEY(teacher_id, class_id),
CONSTRAINT class_teacher_teacher_fk
FOREIGN KEY(teacher_id) REFERENCES teacher(teacher_id),
CONSTRAINT class_teacher_class_fk
FOREIGN KEY(class_id) REFERENCES class(class_id)
);
But on the web I see people just having two foreign keys and no primary key, or table with no foreign key and having a primary key for two columns.
Am I doing it incorrectly?
Am I doing it incorrectly?
No, it looks correct.
Although I would question the size of the numeric data types as you are restricted to only have 1999 teachers and 199 classes (including negative numbers); this may be enough for immediate use but after several years when classes get re-organised or when the syllabus is re-written and new classes are created then you may run out of primary keys.
Does create statement returns any error? Otherwise you should be good.
Try to insert some data in all 3 tables and run some delete statements to see how it goes.

Why can't I add this foreign key?

I'll post only the main part. I have two tables, each one has to have the PK of the other as a FK.
CREATE TABLE apartment
(
cod_apartment INT NOT NULL PRIMARY KEY,
cod_offer INT NOT NULL
);
CREATE TABLE offer
(
cod_offer INT NOT NULL PRIMARY KEY,
cod_apartment INT NOT NULL
);
First I inserted the values on both tables and it was working, I could even search using "select * from...". But then I tried to add the foreign key:
This worked.
ALTER TABLE offer
ADD FOREIGN KEY (cod_apartment ) REFERENCES apartment;
And this not.
ALTER TABLE apartment
ADD FOREIGN KEY (cod_offer) REFERENCES offer;
This is the error message:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__apartment__cod_offer__6383C8BA". The conflict occurred in database "kleber_apartment", table "dbo.offer", column 'cod_offer'.
The problem is, every time I try to execute, the FK name changes. And this FK actually doesn't exist. I already dropped both tables and tried to insert the values again, but the same happens.
What could be?
That means you're trying to add a foreign key when existing data doesn't obey that constraint. So you have a record in your apartment table where the cod_offer column does not match any value in the cod_apartment table.
Adding a foreign key not only constrains future data, but it requires that any existing data must also follow the rule.
And regarding the 6383C8BA, whenever you add a constraint without giving it a name, SQL Server picks one for you. Personally, I'd recommend something like:
alter table dbo.apartment
add constraint FK_apartment__cod_offer
foreign key (cod_offer) references dbo.offer (cod_offer);
This lets you define names the way you want, and is a little more clear about what you're actually building.

Create table with two references in a foreign key

CREATE TABLE Album (
name VARCHAR(50),
lenght FLOAT,
genre VARCHAR(30),
nrSongs INT,
PRIMARY KEY (name, writer),
FOREIGN KEY (writer) REFERENCES Musician(name) OR Band(name),
FOREIGN KEY (Studio) REFERENCES Studio(name)
);
Ok this is what I'm trying to do, I have two tables called Musician and Band, and in the foreign key called writer I want to use the name of a Musician or a Band, but this gives me an error in the OR statement, do you know the correct way to implement this? I can't find it.
Thanks :)
Not possible. A foreign key is a direct link between one field in one table, and another field in another table. It is 1:1. You cannot have n:1, 1:n, or n:n mappings in a foreign key definition.
Plus, your overall table definition is invalid. You have no writer field, so your primary key and the writer FK will fail anyways.
For this particular purpose, why not just making everyone a "band". A solo artist is simply a band that happens to have one member.

Postgres - How to reference a primary key to a non unique value

so I've been working on this assignment I got lately and it has me stumped for how I have to reference a specific table. So let's say I have a table of works from the museum and it contains two primary keys, combined makes it alphanumeric.
CREATE TABLE Works (
wrk_charid pdkeyone,
wrk_numid pdkeytwo,
wrk_name workname,
wrk_type worktype,
wrk_subtype worktype,
wrk_donate donator,
wrk_creation workDate,
wrk_acquistion workdate,
wrk_insurance insurance,
wrk_desc description,
wrk_curloc locationname DEFAULT 'Storage'
REFERENCES LocationAreas
ON UPDATE CASCADE ,
PRIMARY KEY (wrk_charid, wrk_numid),
UNIQUE (wrk_charid, wrk_numid)
);
So that is my table for works, and we have a separate table for materials. However, many works have more than one value for their materials, causing an error that it is not unique. So far I have the table for my materials as follows:
CREATE TABLE Materials (
mt_charid pdkeyone,
mt_numid pdkeytwo,
mt_material materialdesc,
PRIMARY KEY (mt_charid, mt_numid)
);
I don't know exactly how I can reference my works to my materials without running into a uniqueness error. Could someone please help push me in the right direction for what I'm supposed to do?
As the relationship is many to many (one work can have many materials, one material can be present in many works)
You should create a new Table to add the relationships between them. (Basically which work has which material).
Something like this:
CREATE TABLE Works_Materials (
wrk_charid work_pdkeyone,
wrk_numid work_pdkeytwo,
mt_charid material_pdkeyone,
mt_numid material_pdkeytwo,
PRIMARY KEY (work_pdkeyone, work_pdkeytwo, material_pdkeyone, material_pdkeytwo)
FOREIGN KEY (work_pdkeyone, work_pdkeytwo) REFERENCES Work(pdkeyone, pdkeytwo)
FOREIGN KEY (material_pdkeyone, material_pdkeytwo) REFERENCES Material(pdkeyone, pdkeytwo)
);

Adding Constraint with multiple foreign keys

I have a SQL database opened with visual studio, and I need to add some constraints to a table already created. I need a foreign key, which already has a foreign key from a third table. To explain better ,
Table ANIMALI needs a foreign key from table GABBIA, which has already a foreign key from table STANZA. This was the code I came up with:
ALTER TABLE ANIMALE ADD CONSTRAINT REF_ANIMA_GABBI_FK FOREIGN KEY (n_stanza, n_gabbia) REFERENCES GABBIA(n_stanza, n_gabbia);
This gives me an error, n_stanza is a column id not valid. I think it's about the fact that the ID for the class GABBIA is taken from joining n_gabbia and n_stanza, the latter being a key in class STANZA.
Can anyone help me out?
In order for your ALTER TABLE statement to work as written, both tables (not classes) "ANIMALE" and "GABBIA" must include the columns "n_stanza" and "n_gabbia".
In addition, in the table "GABBIA", there must be either a primary key constraint or a unique constraint on the pair of columns "n_stanza" and "n_gabbia". That is, you need something like either primary key (n_stanza, n_gabbia) or unique (n_stanza, n_gabbia) in the table "GABBIA".