MySQL database, multiple foreign keys - mysql

I'm new to the world of MySQL and I'm having a bit of trouble. Here's the problem:
I have 2 tables called TEAMS and FIXTURES. Here's the structure I'm trying to create (only showing relevant fields):
TEAMS table:
team_name (Primary key)
FIXTURES table:
fixture_id (Primary key)
home_team (Foreign key)
away_team (Foreign key)
home_team and away_team both get a team name from the same source (team_name).
I've successfully created the relationship with the FIXTURES(home_team) and TEAMS(team_name), but I can't get the other relationship working - FIXTURES(away_team) and TEAMS(team_name).
I get the "#1452 - Cannot add or update a child row: a foreign key constraint fails" error message. I'm assuming could be because I have 2 foreign keys in the same table referencing the same primary key. Is that correct? If so, how would I fix it?
Hopefully I've explained this ok and that someone can help me, thanks :-)

That means you already have value that doesn't satisfy the constraint, i.e. having no corresponding entry in the teams table. So the problem is not with the schema, but with your data. Fix the issue there.
SELECT * FROM fixtures LEFT JOIN teams ON (away_team=team_name)
WHERE team_name IS NULL;
will fetch you the offending rows.

Related

MySQL Failing to Replace in Parent Table due to Foreign Key Constraint

I have 3 tables.
TableA has id (pk) and name (varchar). - parent
TableB has id (fk to TableA) and name (varchar). - child
TableC has id (fk to TableA) and name (varchar). - child
All 3 tables have records for id's 1,2 and 3.
I have a new data set with data for records for id's 2,3,4,5 and 6. If i try to run REPLACE on TableA, then i run into foreign key constraint error. I cannot replace into the children first; it has to be the parent.
The only solution i could find was to turn off the FK Check, run the replace and then turn on the FK Check.
Is there a better or more elegant solution to this issue? I feel like i'm stuck in a catch 22. Thanks.

MySQL query - how to get relevant answer from table with foreign keys

I have one table with 3 columns where each of them is just foreign key referencing some other tables.
My question is how to get relevant results.
I do know I am looking for some sort of JOIN, just I do not know which one and how to do that.
Here are my tables:
Member_table with: id (int), member_firstname(varchar)
Category_table with: id(int), category(varchar)
and finally:
Relationship_table with: id(int), FK_member_id1, FK_category_id, FK_member_id2.
What I do need is simply query the Relationship table to get actually names, for example:
"first member with name ABC relates to/has category with second member with name DEF".
Thanx a lot for your time in advance.

Foreign keys in mysql

I have a doubt about the way of relating some tables. I have these tables:
User table: username (primary key)
Team table: team_name(primary key), username (foreign key references User(username))
With this relationship, I get that an user can have more than one team.
Group table: group_name (primary key)
I want that a group can have many teams, but these teams have to be of different users, so two teams of a user cannot be in the same group.
I have thought to do a relationship with the three tables of this way:
Group_teams table: (group_name, username, team_name). This table would have a composite primary key (group_name and username), in this way I would be sure that an user cannot has more than one team in a same group.
In addition, I think that I should create a composite foreign key references User(username) and Team (team_name) to be able to control that the team of a user exists. Finally, I should create another foreign key references Group (group_name) to control that a group exists.
I'm not sure that it would be of this way because I have errors when I try to do it. Could yo help me and tell me your opinions?
If a user can be on (at most) only one team, then you have a 0/1 - many relationship.
The easiest approach is to have TeamId in the Users table. This would be a foreign key reference to Teams.
There is no need for a third table to represent this relationship.
There's no need to reproduce the username field in the Group_teams table, just have group_name & team_name.
Create a trigger on the Group_teams table to fire before a new row is inserted, checking for more other teams with the same username.
Have a look at the question "How do you check constraints from another table when entering a row into a table?", specifically this answer from Jim V describing such a setup.

Unable to figure how to manage foreign keys for 'project-customers-contacts' relation

I have a CUSTOMERS table and a CONTACTS table the relation between them is one to many obviously.
also I have PROJECTS table and PROJECT_CUSTOMERS table with relation one to many and with relation one to one between CUSTOMERS and PROJECT_CUSTOMERS.
my problem is that I have a fifth table PROJECT_CONTACTS ....I can't figure which tables shall I refer to in this tables, currently I am refering to PROJECT_CUSTOMERS and CONTACTS table, is this correct or there is something better ?
Your title refers to "foreign keys" but your question just seems to be about what columns should go in what tables.
First, decide what situations can arise and what you want/need to say about them. (This will your tables, columns, candidate keys, foreign keys and constraints.)
Every table holds rows that make some predicate (statement template parameterized by column names) true. Your design seems to have:
CUSTOMERS(id, ...) -- ID identifies a customer and ...
CONTACTS(id, ...) -- ID identifies a contact and ...
PROJECTS(id, ...) -- ID identifies a project and ...
PROJECT_CUSTOMERS(pid, cust_id, ...) -- project PID has customer CUST_ID and ...
PROJECT_CONTACTS(pid, cont_id, cust_id)...)
-- project PID has contact CONT_ID and project pid has customer CUST_ID and ...
A foreign key has a table & column list referencing a table and column list that forms a candidate key. It says that lists of values in the first table appear as lists of values in the second table. Where that is so, declare a foreign key.

Relational database and PHP: one-to-many relations with multiple one-tables

Let’s assume there are some rows in a table cars, and each of these rows has an owner. If this owner were always a person (conveniently situated in a table persons), this would be your standard one-to-many relation.
However, what if the owner could not only be a person, but also a company (in a table companies)? How would this relationship be modeled and how would it be handled in PHP?
My first idea was to create a column person and a column company and check that one of them always stays NULL, while the other is filled – however, that seems somewhat inelegant and becomes impractical once there is a higher number of possible related tables.
My current assumption would be to not simply create the foreign key as an integer column person in the table, but to create a further table called tables, which gives IDs to the tables, and then split the foreign key into two integer columns: owner_table, containing the ID of the table (e.g. 0 for persons and 1 for companies), and owner_id, containing the owner ID.
Is this a viable and practical solution or is there some standard design pattern regarding such issues? Is there a name for this type of problem? And are there any PHP frameworks supporting such relations?
EDIT: Found a solution: Such structures are called polymorphic relations, and Laravel supports them.
There are multiple ways to do it.
You can go with two nullable foreign keys: one referencing company and the other user. Then you can have a check constraint which assure you one is null. With PostgreSQL:
CREATE TABLE car{
<your car fields>
company_id INT REFERENCES car,
person_id INT REFERENCES person,
CHECK(company_id IS NULL AND person_id IS NOT NULL
OR company_id IS NOT NULL AND person_id IS NULL)
};
Or you can use table inheritance (beware their limitations)
CREATE TABLE car_owner{
car_owner_id SERIAL
};
CREATE TABLE company{
<company fields>
} INHERITS(car_owner);
CREATE TABLE person{
<person fields>
} INHERITS(car_owner);
CREATE TABLE car{
<car fields>
car_owner_id INT REFERENCES car_owner
};