I have 2 tables departments and locations and locationID is the primary key in locations table and departments table also has that field.
Now after adding a data with locationID 101 in the departments table it won't let me add foreign key referencing locations table because the locations table doesn't have any data with locationID 101 so the data with locationID 101 is extra in the departments table that doesn't belong in locations table.
So how do I fix that without deleting the information from departments table or adding the info in locations table (if possible)?
This is foreign key constraint and the behavior is as expected.
If you don't want it at all, you can make the two tables independent of each other, i.e. don't make locationID a foreign key in department table, rather let it be a different locationID having no relation to the locations table.
Otherwise, you can temporarily disable the foreign key constraint by following Disabling foreign key constraint. But this can lead to a lot of problem in maintaining the tables, in future.
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 2 Primary key tables and 1 Compound table.
I want to have some other information apart from the 2 primary keys from the primary key table in the compound table.
Would I just repeat this data or is their a way to add other fields into a the compound field without repeating it?
Thanks.
Your current design looks close to right. But I think your EventVolunteer table should look like this:
CREATE TABLE EventVolunteer (
eventID INTEGER,
volunteerID INTEGER,
FOREIGN KEY eventID REFERENCES Event(eventID),
FOREIGN KEY volunteerID REFERENCES Volunteer(volunteerID),
PRIMARY_KEY(eventID, volunteerID)
)
This bridge table should exist to store relationships between events and their volunteers, and nothing else. All metadata for events and volunteers should be in the Event and Volunteer tables, respectively.
If you need to bring in some information, then you can do so via joining the Event and Volunteer tables with this bridge table. This join is much less of a penalty than you might think, if you have indices setup in the right places.
I just starting to use Foreign Keys to enforce the contents of certain Columns and want to be sure I'm putting the Key on the correct table.
In a situation with 2 tables that have a one to many relationship.
e.g.: customers (one) and orders (many).
Where I want to ensure a valid customer number is used in an orders column.
I'm I correct in saying the Foreign Key would placed on the orders table.
e.g.: orders (columnX) references customers (columnX)
Remember that a foreign key is a referential constraint that says the range of values in a table column(s) is being scoped by another table. Specifically, for your example, you would have a customer_id column in your order table that would be a foreign key back to the customer table, meaning that the in all cases you need a valid customer id in order to insert a record in the order table.
Yes, the primary key (typically a customer id) on the one table (customers) should be linked as a foreign key on the many table (orders) in a one-to-many relationship. Each order can belong to only one customer, but a customer can place many orders.
Is it possible to combine 2 primary keys from 2 different table into 1 foreign key in another table? I have a table named product_food with food_id as primary key and product_drinks with drink_id as primary key and then I want to combine food_id and drink_id as a foreign key in my orders table as product_ID? Is that possible?
There's many ways you can solve it, from the top of my head:
You get foods and drinks in the same table, using the same id, they may use different fields.
You make a table products with unique ids and make foods and drinks have a product id.
Make nullable foreign keys food_id, drink_id on the orders table and possibly add another field indicating which one are you gonna use.
You shouldn't use that as a primary key; define a different Product_Id primary key.
If you require uniqueness of the food-drink products, just add the two columns Food_Id and Drink_id to the products table, set them as foreign keys, and add a unique index on both of them, so that new products cannot be added, made of the same food-drink tuples.
Would that suffice your initial need?
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
MySQL Relationships
I am trying to create a one to many relationship in MySQL with foreign keys.
Two tables, user and location. Each user can have many locations, but each location can have only one user.
How do I configure this? I am using HeidiSQL if that helps, though I can input code as well.
MySQL does not know, nor does it need to know if a relationship is 1-1, or 1-many.
No SQL supports many-many relationships, all require a intermediate table which splits a many-many relationship into 2 separate 1-many.
The difference is in the logic that controls the relationships, which is in the code that you write.
A 1-1 relationship is maintained by having the tables share the same primary key (PK).
With the secondary table declaring that PK as a foreign key pointing to the other tables PK.
Table chinese_mother (
id integer primary key,
name....
Table chinese_child (
id integer primary key,
name ....
....,
foreign key (id) references chinese_mother.id
The direction of the relationship 1 -> many vs many <- 1 is determined by the location of the link field.
Usually every table has a unique id and the link field is called tablename_id.
The table that has the link field in it is the many side of the relationship, the other table is on the 1 side.
Each user can have many locations, but each location can have only one user.
Table user
id: primary key
name......
.....
Table location
id: primary key
user_id foreign key references (user.id)
x
y
.......
By placing the link field in the location table, you force things so that a location can only have 1 user. However a user can have many locations.
There is an example here that is almost exactly what you need foreign keys in innodb
CREATE TABLE parent (
id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (
id INT,
parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE CASCADE
) ENGINE=INNODB;
In your example user is the same as parent (a user has many locations, a parent has many childs) and location is the same as child (a location has one user, a child has one parent)