How can one column field reference IDs from two different tables? - mysql

I have:
Table Review with column item_id (not pk)
Table Product with column id (pk)
Table Business with column id (pk)
Relationships:
Review.item_id references Product.id
Review.item_id references Business.id
These relationships are 1:M
What is the syntax in MySQL for one column field to reference IDs from two different tables?
My research lead me to believe you cannot have one foreign key reference two different tables.
My conclusions so far (based on links provided below):
Use a foreign key without a foreign key constraint
Use field to reference other records, but do not create a foreign key constraint
Polymorphic Associations. The foreign key may reference rows in any of several related tables.
One foreign key can reference only one table
These conclusions seem logical to me, but I can't figure out the syntax.
links:
MYSQL - One Column Referenced to Multiple Table
Is it possible to reference one column as multiple foreign keys?
MySQL - Conditional Foreign Key Constraints

Related

how can we make a column having mutiple values in it as foreign key to another table in mysql

i have two tables
intent_sample(id,sample_data,intent_id,entity_id)
entities(id,entity_name,entity_id)
Since a sample_data can have any no of entities in it, I want to store multiple values in a single column and also want to link entity_id to entities table as it foreign key.
How can we do it.
I thought of using JSON data type in mysql for entity_id but is it possible to make a column having json data type as foreign key to another table
Use a composite key
FOREIGN KEY (`id`,`entity_id`)
REFERENCES entities(`intent_id`,`entity_id`)
This will allow a relation between the foreign key in one table to the id's in another table.
I don't have all of the details so you can modify it to work with your tables.

Am I supposed to use foreign keys for these 3 tables which are connected?

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).

How to refer to different tables from one column?

i have following db design(kept just essential informations):
TABLE monument
PRIMARY KEY id
name
TABLE restorer
PRIMARY KEY id
name
TABLE project
PRIMARY KEY id
name
TABLE restorer2project
FOREIGN KEY restorer_id REFERENCES restorer(id),
FOREIGN KEY project_id REFERENCES project(id)
PRIMARY KEY (restorer_id, restoration_project_id)
TABLE monument2project
FOREIGN KEY monument_id REFERENCES monument(id)
FOREIGN KEY project_id REFERENCES project(id)
PRIMARY KEY (monument_id, project_id)
Project can have many restorers and have many monuments.Also project can repeat in future with same relations, but different date.
I want to create table witch will store pictures.
TABLE picture
PRIMARY KEY id
reference_to_different_tables
Is it possible point to different tables from one column?
if yes how?
Is this good design (fro me it is natural i can imagine to create more tables with pictures)
Other approach is to have references to pictures from other tables, but then i will need some kind of mapping table, but not sure if it is also good design.
From database prespective you can't have a foreign referring to different tables.
It can refer to one table at a time.
There can be three solutions I can think of:
The best solution in my opinion is to have a parent table say artifact to monument, restorer and project. The later three tables will have primary key as foreign key from artifact table. This way you can have single column in picture table which will refer to artifact table.
Or you can specify multiple columns in picture table as suggested by #CptMisery in the comments.
Create multiple picture table for all of them. This is not considered as good solution.
If you are using any persistence framework than the first solution can solve dynamic query problem as well.

MySQL index name and foreign key name must be different for different tables?

MySQL index name and foreign key name must be different for different tables?
For example,
Two tables both have the same field(profile_id) which are belonging to a third table(profiles). So I want to make the profile_id indexed and constrain it as a foreign key.
Could the index name be named "profile_id_idx" in both tables?
And "profile_id_fk" as name of foreign key for both too?
Foreign Key names must be unique across all tables in all databases. Index names may be re-used in different tables.

How to determine cardinality of foreign key using mysql

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.