If I wanted to write mySQL for this database that's been already normalized, how would I add a constraint for the (Branch_ID, Employee_ID) composite key?
Branch Table
Branch ID
Branch Name
Address
Post Code
Town
County
Branch Employee Table
Branch ID
Employee ID
Employee Table
Employee ID
Employee First Name
Employee Surname
Employee NI No.
I made primary keys bald. I know how to make both the Branch Id and Employee ID primary keys using constraint, but should I also make the Employee_ID in the Branch Employee table a foreign key since I use it in another table?
To add a composite key to an existing table, use the code:
ALTER TABLE `Branch Employee`
ADD PRIMARY KEY (BranchID,Employee ID)
You are correct that each column in the Branch Employee table should be a foreign key as the values are used in other tables. Without the foreign key it could be possible to enter employees and branches that don't exist in their respective tables
Create the foreign keys as follows
ALTER TABLE `Branch Employee`
ADD FOREIGN KEY (EmployeeID_FK) REFERENCES Employee(EmployeeID);
ALTER TABLE `Branch Employee`
ADD FOREIGN KEY (BranchID_FK) REFERENCES Branch(BranchID);
Edited in light of Raymond's comment:
The order of columns in the composite key matters as a clustered index will be created with the primary key. The most selective (most queried) column of the two should go first and then less selective second. The right or wrong order will depend on the queries being run against the table
a PRIMARY KEY is an identifier for each table.
a FOREIGN KEY means that a value ina table is referenced by a PRIMARY KEY in another table.
In this case, you need to set your Branch ID Employee ID both, PRIMARY KEY and FOREIGN KEY;
FOREIGN KEY - because they are referenced by another table.
PRIMARY KEY - because you don't want 'employee a' to be twice in 'branch a'.
Caution: You can still have 'employee a' in both 'branch a' and 'branch b'.
To avoid this, is better that Employee table contains a parameter call branch id, and become it the foreign key.
Hope it helps
Related
Assume I have 2 tables with following attributes.
table 1 (customers): id (PRIMARY), customer (UNIQUE), totalspent
table 2 (receipts): id, cost
What indexes do I need to create on customers or receipts to make it so when a customer is deleted from the table all the receipts tied to him via id are deleted as well?
I set receipts.id to primary key, but when deleting entries from customers, they would not be deleted from receipts. Should I make customers.id a foreign key that references receipts.id? Can a primary key be a foreign key?
You are looking for a cascading foreign key constraint, rather than an index.
alter table receipts add constraint fk_receipts_cust
foreign key (cust) reference customers(id)
on delete cascade;
In MySQL, a foreign key also happens to create an index. This is a "special feature" of MySQL. The functionality you are looking for is the cascading part. You can read more about this in the documentation.
I have a table with following columns: "id, parent_id, name" , where parent_id points to a entity in the same table.
I want to add a constraint that stops me from deleting an entity, if there are other entities, that have the first one as parent_id.
Can a foreign key be used for this or is foreign key only allowed to point to other tables?
Yes, a foreign key can reference the same table.
A classic example of this, often included in the sample database that comes with the database, is:
create table employee (
id int not null primary key,
manager_id int not null references employee,
...
)
Such references are known as self-referencing foreign keys. If defined like the above,they automatically prevent deletion of a "parent" record if there are "children" that reference it.
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));
Let's say table B is dependent on table A.
Table B has a unique primary key B.B_ID and a foreign key B.A_ID
which references the parent table A's primary key.
Since B has a unique key, is B.A_ID, the foreign key, a non key (non-key)
in B?
Thank you.
In the context of discussing the 2nd normal form, "non-key columns" are all the columns that are not part of a candidate key.
You may certainly have foreign key columns in a table that are not part of that table's primary key.
Suppose you have a table Person that has a primary key PersonName because that is the column you use to identify each person uniquely.
You can also have a column in that table such as CountryOfCitizenship that is a foreign key referencing another table Country. This foreign key column is not part of the primary key in the Person table; it is not the way we identify each row in that table.
Re your comment:
The second normal form requires that non-key columns have a functional dependency on the whole primary key. This is different from first normal form only if your primary key has multiple columns.
Functional dependency means that the attribute column unambiguously relates to the primary key, and therefore the value in the attribute column belongs on the same row with that primary key.
So if a column like CountryOfCitizenship always contains the country name that is the country of citizenship for the person who is named in that same row's primary key, then the attribute satisfies 1NF and since the table has a single-column primary key, it's automatically in 2NF as well.
In MySQL terminology, "key" usually refers to an explicit key which has an index on it. If you use this definition, then a primary key is a key. And a unique key is a key. And an index key is a key. But a foreign key is not necessarily a key.
When you declare a foreign key constraint, MySQL does not necessarily build an index on the referring table (it does in innodb). Of course, you can declare the foreign key to also be a key and guarantee that an index is built.
A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table.
You can find more information here https://stackoverflow.com/a/18435114/7667467
I just started learning SQL so I am a bit confused.
If I have Table A that has a primary key : CustomerID & Table B with foreign key CustomerID
I added the foreign key constraint by using CASCADE so that the foreign key should update or delete automatically when primary key is deleted or updated.
However, it only works for delete. When I add a new record in the primary field table, that record is not shown in the foreign key table, why is that ?
Corresponding rows are updated or deleted in the referencing table
when that row is updated or deleted in the parent table. CASCADE
cannot be specified if a timestamp column is part of either the
foreign key or the referenced key. ON DELETE CASCADE cannot be
specified for a table that has an INSTEAD OF DELETE trigger. ON UPDATE
CASCADE cannot be specified for tables that have INSTEAD OF UPDATE
triggers.
As mention in MSDN. They have mentioned the only update and delete operation of primary key table will affect the Foreign key table's column. If any insert made to primary key, it will not affected the foreign key. Since the Main objective in primary key and foreign key relationship
"An each every record is available in the foreign key table, it should contain corresponding record should be present in the primay key table and vice versa is not applicable".
If you insert any record to foreign key table that it will throws foreign referential integrity error. It will not allows you to insert a record in the foreign table unless and until you will corresponding record in the primary key table.
for information take look in following in MSDN links
http://msdn.microsoft.com/en-us/library/ms179610.aspx
Note:
if you want to achieve this functionality then you have write a logic in Stored procedure or trigger.
No,is not automatic add in your foreign key table , it's not make sense , for example if you have two table ,"City" and "People" , People in the City , so there must be a FK refer for People , if you add record in City e.g. New York , How is database know who's need to insert to People table?How many people? , and what this people properties? e.g. People Name?
So database can't do that automatic , you have to do it manually!