use part of composite key as foreign key - mysql

I have a table with a composite key from 3 different tables.
What i want to do is to add a reference as foreignkey from all the 3 tables.
The problem is that 2 of the tables uses composite key. Is it possible to use just a part of the compositekey as a foreign key in another table?
So the table i want to add the foreign key to is ProductCategory and i want to add it to the property id and take that from id of the table Product.
when i do this
ALTER TABLE [AAES_PAM_DEV].[dbo].[ProductCategory]
ADD FOREIGN KEY (id)
REFERENCES [AAES_PAM_DEV].[dbo].[Product](id)
i get this error:
There are no primary or candidate keys in the referenced table 'AAES_PAM_DEV.dbo.Product' that match the referencing column list in the foreign key 'FK__ProductCateg__id__5F492382'.

Related

Reference to one foreign key from multiple tables

I'm trying to connect one foreign key from multiple table's primary keys. Like this :
enter image description here
I've tried :
ALTER TABLE Table1 ADD CONSTRAINT entity FOREIGN KEY (entity_id)
REFERENCES MainTable(entitiy_id);
or
ALTER TABLE MainTable
ADD FOREIGN KEY (entity_id) REFERENCES Table1(id),Table2(id),Table3(id);
What should I do?
Thanks.
it is not really clear what you want to achieve here.
The foreign key should go from a non-key column of the "child" table to the key column of the referenced table.
In your diagram, I would expect each one of Table, Table2 and Table3 to have a column "parent_id", and in every one of them you add a foreign key, which goes from the "parent_id" column to the key of the MainTable.
That said, the code is
ALTER TABLE Table1
ADD COLUMN parent_id INT NULL;
ALTER TABLE Table1 ADD CONSTRAINT fk_table1 FOREIGN KEY (parent_id)
REFERENCES MainTable(uniqueId);

In M:N database relationship it's better to reference by a composite PK or two foreign keys

I have a database that involves a table called U and a table called P .
table U
U_Id primary key.
table P
P_Id primary key
There is many to many relationship between the two tables.
I have two ways to create relationship between the two table:
1) create a new table that contains a composite Primary Key (U_Id,P_Id)
2) create a new table that contains references from U table and P table as foreign keys.
(U_id and P_id as foreign keys )
Third_Table
U_id FK not null
P_Id FK not null
What the better option?
Options 1 and 2 aren't opposed. Your relationship table will contain U_Id and P_Id as foreign keys, and the combination of these two columns should at least be marked as a unique key, if not a primary key.
Some DB designers prefer to introduce a surrogate identifier as primary key. Others (including myself) prefer to use the combination of foreign keys as the primary key.
2) create a new table that contains references from U table and P table as foreign keys.
That you need for referential integrity, else the 3rd table could have values that do not refer to anything. (Hurrah for many-to-nothing rows!)
1) create a new table that contains a composite Primary Key (U_Id,P_Id)
If you don't do that, what will be the primary key to the 3rd table? It will have one, right?
A primary key is one form of unique constraint. If a table has more than one unique constraint, the choice of which one to call the primary key is arbitrary. In your case, though, there's only one.
You need some kind of unique constraint on the 3rd table to prevent duplicate rows. Primary Key (U_Id,P_Id) says than any {U_Id,P_Id} pair is unique, and identifies that relationship. From what you say, that's what you want.
I would expect a composite key consisting of two foreign keys. Example code (untested):
CREATE TABLE Q
(u_id INT NOT NULL FOREIGN KEY REFERENCES U (u_id),
p_id INT NOT NULL FOREIGN KEY REFERENCES P (u_id),
PRIMARY KEY (u_id, pi_id));

How can I link multiple tables in MySQL database other than using primary key-foreign key relation?

I have tables named 'studentdetails', 'class', 'obtainedmarks', and 'subject' in a database.
I have a primary key named 'STUDENTID' in table 'studentdetails' which is connected to other tables as a foreign key.
I want to make one more primary key in the same table 'studentdetails' taking three columns ('STUDENTID','CLASS','ROLLNO') so that I can use the foreign key relation to the three columns of 'obtainedmarks' table.
How can I establish such relation as we can make only one primary key in a table?
Mysql certainly allows foreign key relationships to span multiple columns. In fact there is even a full example in the manual. Thus your relationship might look something like
CONSTRAINT fk_multi FOREIGN KEY (`studentid`, `class`,`subject`)
REFERENCES other_table (`studentid`, `class`,`subject`)
ON DELETE CASCADE ON UPDATE CASCADE,
Note however that what goes into the REFERENCES section is the names of the columns and not the name of the index on that other table.
Getting onto the Primary Key problem, it's true that you can only have one primary key per table. But there's nothing to prevent you from creating a composite unique key on the three columns that are referenced.

How we can make composite primary key and pass it into an other table as a foreign key?

I have created a junction table named as result.I am trying to make a composite primary key and trying to an other table as a foreign key.But I don't know how have to make strong textcomposite primary key and pass it into another table as foreign key.

Specific foreign key explanation

Basically I'm rather new to MySQL and in an example I see this foreign key being added:
ALTER TABLE Department ADD FOREIGN KEY managerIsInDepartment
(manager,id) REFERENCES Employee(id,worksAt);
I suppose one foreign key is being added (of the name managerIsInDepartment).
But I thought the attributes in the parentheses were the attributes to turn into foreign keys?
Why is managerIsInDepartment being displayed?
managerIsInDepartment is simply a name for the key to help identify it. You can omit it and one will be automatically generated.
ALTER TABLE Department <-- Table that will have the foreign key constraint
ADD <-- Option
FOREIGN KEY managerIsInDepartment <-- Name of the key
(manager,id) <-- Columns included in the key
REFERENCES Employee <-- The table being referenced
(id,worksAt) <-- Columns being referenced in foreign table
Hope this helps.
A good reference.