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.
Related
I'm using MySQL 5.6 and I've read the MySql Reference guide regarding this but no where is it mentioned that the PK should be at the end of the list while creating a composite Foreign Key.
The only requirement in the guide that talks about columns is the following -
"In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order."
If so, then why doesn't the following work?
alter table table_1
add constraint "fk_key_1" FOREIGN KEY "ix_key_1" ("col_1", "col_2") REFERENCES "table_2" ("pk_col", "col_2") ON DELETE NO ACTION;
But this works -
alter table table_1
add constraint "fk_key_1" FOREIGN KEY "ix_key_1" ("col_1", "col_2") REFERENCES "table_2" ("col_2", "pk_col") ON DELETE NO ACTION;
The foreign key has to match the primary key (or some key, but preferably the primary one) -- both the types and order.
If this works:
FOREIGN KEY "ix_key_1" ("col_1", "col_2") REFERENCES "table_2 ("col_2", "id_col") ON DELETE NO ACTION;
It is because the primary key on table_2 is defined as (col_2, id_col) rather than (id_col, col_2).
Hi i need to make a relation between tblPersoneel and tblWoonplaats but it is not working.
This is the code i am using
ALTER TABLE tblPersoneel
ADD CONSTRAINT FK_Perwoon FOREIGN KEY
REFERENCES
tblWoonplaats
GO
Try the following:
ALTER TABLE tblPersoneel ADD
CONSTRAINT FK_Perwoon FOREIGN KEY (woonplaatsId)
REFERENCES tblWoonplaats (id)
Of course you need to use the correct column names for the ids.
You should specify the columns:
FOREIGN KEY (<woonplaatsID within tblPersoneel>) REFERENCES tblWoonplaats (<woonplaatsID within tblWoonplaats>)
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'.
I know there have been myriads of questions concerning primary and foreign keys. Looking through them, I cannot seem to find a simple answer to my question. My understanding of primary and foreign keys is that a foreign key is a column designated in a child table that refers back to a primary key as a column in a parent table. Is that correct, or do I have it backwards? If that is indeed correct, I am trying to find out why I am having difficulty creating a foreign key in a child table as such:
salesorders.sonumber (pk) < customer.sonumber (fk)
I am using Navicat with MariaDB (same as MySQL) and the error I get is:
1452 - Cannot add or update a child row: a foreign key constraint fails ('customer_orders','#sql7a8_3'; CONSTRAINT 'sonumber' FOREIGN KEY ('sonumber') REFERENCES 'salesorder' ('sonumber') ON DELETE NO ACTION ON UPDATE CASCADE)
Customer_orders is the database name. I am naming the foreign key 'sonumber' which is the same as the column name in the child table (customer) and the parent table (salesorders). Is that incorrect? Should I give the foreign key another name?
gitpicker
The foreign key should have its own unique name, yes. sonumber_fk or something similar is a simple naming schema.
You also need to make sure that every entry in the Foreign Key column has a corresponding entry in the Referenced table, otherwise you will not be able to create the foreign key.
Navicat does not show primary keys which are also foreign key on table report as foreign keys. Why?
I gave the image explaining the situation:
A foreign key is a constraint that applies only to the referencing table. In your case, the translate_talent_id field has a foreign key constraint that references another field of another table.
On the other hand, translator_id is probably referenced by foreign keys in other tables. However, such foreign keys won't appear (or have any effect) on the the referenced table (trl_translator in this case). That is why your MySQL client is not showing any foreign keys on translator_id.