I have a problem in creating a table with a column that has two foreign Key relationships. Let me explain in detail.
I have three tables:
Table A - columns ID (primary key), Name
Table B - columns ID (primary key), Name
Table C - columns ID, Name, Detail
In C.Detail I have to store data from both other tables (A.ID & B.ID). So I tried to add two foreign key into the column C.Detail. During insert operation in Table B the following error occurs, and the same error message occurs while trying to insert data into Table A.
"The INSERT statement conflicted with the FOREIGN KEY constraint
"FK_C_A". The conflict occurred in database "X", table "dbo.A", column
A.ID."
Please, can anyone help us to rectify this problem? We don't want to add two columns in table C for two foreign keys.
Hopefully waiting for the reply.
I will suggest to introduce two new columns in Table C. (i.e AID and BID).
Create Foregin key on this news columns.
I could be wrong, but I think the way to go about doing this is to create a "parent" table for A and B that has A_B_parent.id(primary_key) and then have A and B both have foreign keys on their id to the parent table. Then C can also have a foreign key to the parent table.
This obviously ends up being really complex, so the better solution might just be to programmatically enforce the constraint rather than using foreign keys and then put a comment on the table.
Related
I have a problem with this:
Database diagram
object_id is a foreign key references 5 tables' "id" columns.
So I can not insert for example 5th record to "connected_nodes" table because in "klapan_treh" table 5th record does not exist, but in "ns" table 5th record exists.
My solution is to create for each table separate columns like: ns_id references ns(id), klapan_treh_id references klapan_treh(id) etc.
But do you advise me another improved way?
Your proposed solution is the best one, I think. What you need is to have 5 separate foreign keys in your connected_nodes table. Each one of these points to the id in exactly one of the other tables. I think that is what you are suggesting.
Each foreign key can be optional (nullable) so that if you have 4 connected nodes you have 4 of the foreign keys filled in and the 5th one null.
So I have 3 tables:
Table: Albums
Columns: Id, Name, Description, Author, Folderpath, Thumbnail, Upvotes, Downvotes
Table: AlbumsConnection
Columns: Id, AlbumId, AlbumImagesId
Table: AlbumImages
Columns: Id, InAlbum, Imagepath
So far I've been using these tables without actually using foreign keys. Am I supposed to use foreign keys here? I understand that I'd have to add 2 foreign keys to AlbumsConnection, 1 for each table and each foreign key will reference to the primary keys ( which are the ids ) of the other 2 tables. Is that correct?
Foreign keys help ensure relational integrity of the database. There is no requirement for declaring them explicitly, but it is a good idea, particularly if you are learning to use databases.
The foreign key let's the database know that a column in one table is related to a column in another table. I don't think MySQL's optimizer uses this information explicitly, although it does create an index on the foreign key column (unlike most other databases).
In addition, a declared foreign key relationship can help you deal with changes to the database. It will prevent invalid albums from being inserted into the junction table. If you delete an album, it gives you control over how the deletion and updating is handled (via cascading constraints).
I have two tables inside my MySQL database which consists of awards and members. I want to set member_name as a foreign key into awards table instead of member_id.
I have set member_name as UNIQUE inside the members table and trying to set it as foreign key but I'm getting an error:
Cannot add or update a child row: a foreign key constraint fails
Can someone tell me why I'm getting this error?
Your error is showing that you are trying to insert a child row while its corresponding row does not exist in its master table. So first insert in master table then in child.
If this is not the case then you can also check that it seems you are trying to create foreign key member_name as string in member table with member_id as int in award table, while to make foreign key relationship both fields data type should be same.
If this is not the case then share your table structure and your alter query to make foreign key.
Note: Foreign key always perform with better performance by int data type than string. So you should keep member_id instead of member_name if there is no specific reason of it.
It would be easier to answer this question if you provided more details on
both table structures.
In any case, the error would most likely indicate that there’s a record in the existing data set that would violate the Foreign Key constraint, i.e., a member_name in the awards table which doesn’t exist in the members table.
I am stuck in a problem where i have to find cardinality of a relationship between tables using mysql. Following this post MySQL: How to determine foreign key relationships programmatically?
I have found all tables related to my table and the columns which are foreign key. Now i also want to find the cardinality of relationship i.e. one-to-one, one-to-many or many-to-many. Any ideas or snippets would be highly appreciated
Let us assume that table A has a foreign key f which refers to the primary key k of table B. Then you can learn the following from the schema:
If there is a UNIQUE constraint on A.f, then there can be at most one row in A for every row in B. Note that in the case of multi-column indices, all columns of the unique constraint must be part of the foreign key. You can use SHOW INDEX FROM tablename WHERE Non_unique = 0 to obtain information on the uniqueness constraints of a table.
If A.f is declared NOT NULL, then there will always be at least one row in B for every row in A. You can use SHOW COLUMNS FROM tablename to list the columns and see which of them allow NULL values.
If you interpret “one” as “zero or one”, then you get a one-to-one relation using a unique constraint, and a many-to-one relation (i.e. many rows in A referring to one row in B) without such a unique constraint.
A many-to-many relation would be modeled using a separate table, where each row represents one element of the relation, with many-to-one relations for both foreign keys it contains.
Here's an example: Originally I have 3 tables. Table B references Table A. So now Table B has two primary keys. One used as the original primary key and the other one to enforce its relationship with Tabe A. Then I want Table B to have a many-to-many relationship with Table X. As I'm adding the relationship, MySQL Workbench added Table Y with both of Table B primary keys and one primary key in Table X. So Table Y now has three primary keys.
It seems like the second primary key from Table B in the junction table is unnecessary since I can identify Table B with the original primary key. So do I still need the extra primary key? Or perhaps I should not have an identifying relationship between Table A and B?
Table A and B have a relationship something like User has many Post. Post must belong to a User. But Post already has a primary key of its own, so does the foreign key to User need to be a primary key?
EDIT
Here's the scenario (diagram link below). The tables I'm focusing on are snippet, snippet_topic and tag. From what I know, since every snippet must belong to a snippet_topic, it has an identifying relationship. So I used the identifying relationship in MySQL Workbench and they added snippet_topic ID as a primary key. Afterwhich I added a m:n relationship for tag and snippet. MySQL Workbench added snippet_topic ID to the junction table (but I removed it). Is there anything wrong with my design? Or is there a more correct way to this?
Legend:
Yellow icon - primary key
Red icon - not null
each table should only have one primary key which is only about this table. If you then want a second column in Table A containing the values of the table B primary key thats find. Just set up a second index to get performance if requires
Originally I have 3 tables.
Ok.
Table B references Table A. So now
Table B has two primary keys. One used
as the original primary key and the
other one to enforce its relationship
with Tabe A.
No. Table B has one primary key and one foreign key. The foreign key might be part of the primary key, and it might not.
Then I want Table B to have a
many-to-many relationship with Table
X.
Ok.
As I'm adding the relationship, MySQL
Workbench added Table Y with both of
Table B primary keys and one primary
key in Table X. So Table Y now has
three primary keys.
A many-to-many relationship is typically implemented as a table that contains two foreign keys as its primary key. In your case, one foreign key is the primary key of Table B, and the other foreign key is the primary key of Table X. The primary key of Table B seems to contain two columns. The tables might look like this.
Table A: {a1, a2}
Table B: {b1, a1, b2, b3}, a1 references Table A
Table X: {x1, x2}
Table Y, which implements the m:n relationship, contains the keys from B and from X.
Table Y: {b1, a1, x1}, the two columns b1, a1 reference Table B; the column x1 references Table X
If you want better answers, edit your question and include the SQL DDL for your tables. Get the DDL by using SHOW CREATE TABLE.
You can not have more than one primary key. What you have there might be indexes. If the user_id column from table posts is included in your primary key, you can take it out and leave the primary key composed of just the id column.
I hope this helps
From your edited post - the only way to do this correctly is to have another table to hold the many to many relationship. Sniipit & snippit_topic have single primary keys and this new table has two columns with each of the primary keys