How to reference two foreign key attributes within one column? - sql

I'm working on building a database in Oracle SQL developer. In context to the scenario I have a primary table which holds two rovers.
Here is the code for that table:
CREATE TABLE "ROVER" (
Rover_Model_ID varchar(7) NOT NULL,
Rover_Name varchar(50),
Manufacturer varchar(50),
CONSTRAINT Rover_PK PRIMARY KEY (Rover_Model_ID)
);
The way I currently have it, is that there are two rovers in this table. (Refer to the image at the end of the post)
There is also another table called Thermal_System_Components. Which has 3 component entries that both of the rovers use.
CREATE TABLE "THERMAL_SYSTEM_COMPONENT" (
Component_ID INT,
Component_Type varchar(20),
Rover_Model_ID INT NOT NULL,
CONSTRAINT TSC_PK PRIMARY KEY (Component_ID),
CONSTRAINT TSC_FK FOREIGN KEY (Rover_Model_ID) REFERENCES
ROVER(Rover_Model_ID)
);
My question relates to this scenario. There are 3 components which both rovers use, so I wondered how do I go about successfully inputting data into these tables to highlight that BOTH rovers use each of the 3 components. I've inserted the table of my initial concept down below.
If anyone could help clarify this for me I'd be most appreciative.

This is called a many-to-many relationship.
What you need is a third table, mapping rovers to components. Each pair of rover/component takes one row in this table, so there will be six rows total.
CREATE TABLE "ROVER_HAS_COMPONENT" (
Component_ID INT NOT NULL,
Rover_Model_ID INT NOT NULL,
CONSTRAINT TSC_PK PRIMARY KEY (Component_ID, Rover_Model_ID),
CONSTRAINT TSC_FK FOREIGN KEY (Component_ID) REFERENCES
THERMAL_UNIT_COMPONENT(Component_ID),
CONSTRAINT TSC_FK FOREIGN KEY (Rover_Model_ID) REFERENCES
ROVER(Rover_Model_ID)
);
You don't need the Rover_Model_ID in your component table.
PS: You also need the data type in each foreign key column to match the data type of the primary key they reference.

Related

Storing foreign key linked in a separate table? SQL

So I went through Odoo database design and I saw that they stored the relationship between 2 tables in a separate table that doesn't have primary key. How are you able to do this? I want to replicate this kind of behavior in SQL Server. Is it auto-inserted?
A table should always have a primary key. Look at the thousands of questions on Stackoverflow that ask how to delete one of two identical rows in a database table.
You typically model m-to-n relationships between tables with a separate table that has foreign keys to both tables:
CREATE TABLE person (
person_id bigint PRIMARY KEY,
name text,
...
);
CREATE TABLE game (
game_id bigint PRIMARY KEY,
name text NOT NULL,
...
);
CREATE TABLE likes_to_play (
person_id bigint REFERENCES person NOT NULL,
game_id bigint REFERENCES game NOT NULL,
PRIMARY KEY (person_id, game_id)
);
CREATE INDEX ON likes_to_play (game_id);
The primary key on the table makes sure there are no superfluous double entries and backs one of the foreign keys. The other index is created for the other foreign key.

Primary Key of Child Table as Foreign Key in Parent Table in Oracle

I have two tables:
Store
(PK: StoreID)
Store is overall parent and
Product
Product (PK: ProductID, FK: StoreID)
is a child.
I am stuck in a requirement where for an application Primary Key ProductID should be foreign key in table Store . Not sure if this is gonna work. I have tried this from a custom framework with Oracle backend, but that is not working as expected.
Note: I cannot change schema.
I fail to fully understand your question, but it's possible to have multiple references between two tables. For example:
create table store (
storeid number(6) primary key not null,
name varchar2(20) not null,
last_productid number(6) not null -- not yet a FK, but later it is.
);
create table product (
productid number(6) primary key not null,
name varchar2(50) not null,
storeid number(6) not null,
constraint fk1 foreign key (storeid) references store (storeid)
);
alter table store add
constraint fk2 foreign key (last_productid) references product (productid);
The column last_productid you want to add (do you want to?) is a little bit tricky. You have two options:
It's not nullable. This happens when you want every store to ALWAYS have a last_productid. You'll need to declare it DEFERRABLE to be able to insert the store and first product.
It's nullable. Then some stores will point to a product, and other ones will not. This is the easy case, since you can insert/update the store table easily.

Why is my create table failing? - does not match primary key

This is what I am trying to create:
CREATE TABLE VEHICLEREPORT
(
DeptID char(2) not null,
Vin# char(3) not null,
Miles varchar(6) not null,
Bill# char(3) not null,
EID char(3) not null,
PRIMARY KEY (DeptID, Vin#),
FOREIGN KEY (bill#) REFERENCES billing,
FOREIGN KEY (EID) REFERENCES Employee
);
The issue is with my reference to billing. The error says:
The number of columns in the referencing column list for foreign key 'FK__VEHICLERE__Bill#__5AEE82B9' does not match those of the primary key in the referenced table 'Billing'.
but my billing table entered fine:
CREATE TABLE BILLING
(
VIN# char(3),
BILL# char(3),
PRIMARY KEY (VIN#, Bill#),
FOREIGN KEY (VIN#) REFERENCES vehicle
);
What am i missing with this?
Appreciate the help.
If you think of the foreign key as establishing a parent-child relationship between two tables, then the parent side column(s) need to be unique.
From Wikipedia:
In the context of relational databases, a foreign key is a field (or collection of fields) in one table that uniquely identifies a row of another table or the same table. ... In simpler words, the foreign key is defined in a second table, but it refers to the primary key or a unique key in the first table.
In your example, there is no guarantee that VIN# is unique in VEHICLEREPORT. Below are your options
VIN# is guaranteed to be unique in VEHICLEREPORT. In this case add a UNIQUE constraint on VIN# on the VEHICLEREPORT table. The error will go away.
VIN# is not unique in VEHICLEREPORT (doesn't seem likely). If this is the case, then likely there is a flaw in the design of your BILLING table as it could likely point to more than one row in VEHICLEREPORT. You should consider adding DeptID column to BILLING and creating a composite foreign key.
Also if VIN# is unique (case 1 above), you should think of why DeptID is present in the PK. Maybe the right fix at the end is to drop DeptID from the primary key.

Self Referencing table : UNIQUE constraint failed (SQL)

Architecture
I have the following self referencing table. Every Entity has a relation with 1 to many entities.
Tables
Since it's a 1 to many relationship I representation Relation by a table.
CREATE TABLE ENTITY (ID TEXT PRIMARY KEY NOT NULL, VALUE TEXT);
CREATE TABLE RELATION (ID_SOURCE TEXT NOT NULL, ID_DESTINATION TEXT NOT NULL, PREDICAT TEXT, PRIMARY KEY(ID_SOURCE, ID_DESTINATION), FOREIGN KEY(ID_SOURCE) REFERENCES ENTITE(ID), FOREIGN KEY(ID_DESTINATION) REFERENCES ENTITE(ID));
Problem
When I am inserting the values I am getting the following error:
UNIQUE constraint failed: RELATION_ENTITY.ID,
RELATION_ENTITY.ID_DESTINATION
I know why I am getting this error. I am inserting the same Ids in the table Relation But the value is different. THE entity m.06y3r references the entity m.02jvmvm with two different values. How can I fix this design error?
ID_SOURCE ID_DESTINATION VALUE
m.06y3r m.02jvmvm adopted_child.adoptive
m.06y3r m.02jvmvm people.person.parents
It is all because in Relation table you have PRIMARY KEY(ID_SOURCE, ID_DESTINATION) so this both values have to create unique set. If you change it to PRIMARY KEY(ID_SOURCE, ID_DESTINATION, VALUE), then it should work just fine.
EDIT
example of solution mentioned in comment below:
CREATE TABLE RELATION (
ID INT NOT NULL,
ID_SOURCE TEXT NOT NULL,
ID_DESTINATION TEXT NOT NULL,
VALUE TEXT,
PRIMARY KEY(ID),
UNIQUE (ID_SOURCE, ID_DESTINATION, VALUE)
FOREIGN KEY(ID_SOURCE) REFERENCES ENTITE(ID),
FOREIGN KEY(ID_DESTINATION) REFERENCES ENTITE(ID));
If you want rows with duplicate values for id_source and id_destination you can either use a surrogate primary key (like an autonumber int), maybe id_relation, together with a unique constraint for the three other columns or extend the primary key to include the value column too so that it would be PRIMARY KEY(ID_SOURCE, ID_DESTINATION, VALUE).
If you want to reference this table in joins, using a surrogate key might make your life easier.

create foreign key in oracle

is there anyone who can help me to create a foreign key for my Status table. I need to PLACE a foreign key constraint on the code in the status table, referring to the id in the Building table.
TABLE building
(
build_name VARCHAR2(50,0) NOT NULL,
id NUMBER (38,0) NOT NULL,
mapid NUMBER (10,0) NOT NULL
);
TABLE STATUS
(
code VARCHAR(2 BYTE) NOT NULL,
status_name VARCHAR2(40 BYTE) NOT NULL,
);
Bulding table has constraint building_gmidx with id as primary key.
Here is a quick syntax for your current requirement, however I recommend you to go through the oracle documentation for a proper understanding of what this means.
ALTER TABLE STATUS ADD (
CONSTRAINT status_fk_building FOREIGN KEY (code)
REFERENCES building (id)
ENABLE VALIDATE);
Did you leave out CREATE TABLE *name* to save time when writing the question? The first thing I see is that your Foreign key has to have the same data type and size as your Primary key. If the Tables already exist the code would be:
ALTER TABLE status MODIFY (code NUMBER(38));
From there you would need to
ALTER TABLE status ADD FOREIGN KEY (code) REFERENCES (building_gmidx)
Why is the constraint for building Primary key named building_gmidx and the column plain id?? All Normalized table attributes should be Unique. Wouldn't it be better if you just named the column "id" building_gmidx instead. Just in case there are other types of id's added later you do not have to think about which table "id" pertains to. Let me know if this works.