Mysql add foreign key constraint referring to view

Is it possible to add a foreign key constraint in MYSQL when the referenced table is actually a view?

Based on the following, I may be surprised that the table and the view are considered different formats .

It seems to me that this is not permitted, but I have not seen anyone who actually claims to be banned.

+6
source share
1 answer

For a field that should be defined as a foreign key , the specified parent field must have an index defined on it.

According to the documentation on foreign key restrictions :

LINKS parent_tbl_name (index_col_name, ...)

Since VIEWs are virtual tables, all its fields are also virtual.
And the definition of index not supported in virtual fields.

According to the documentation for Restrictions on Views :

Unable to create index in view.

And therefore, you cannot use a virtual table, i.e. viewing as a reference parent table (which does not support indexes) to define and map a foreign key to create a child table.

An example :
enter image description here

+9
source

All Articles