Create foreign key to a view

Mocking

Problem: I am trying to create a foreign key on a column to a view. The view was made as a SELECT * from another schema with a primary key constraint on the column I am using.

Is this possible? If this is, is this considered bad practice? If this isn't, is there an alternative? It doesn't seem to be possible through phpmyadmin, and I can't create a primary key on a view.

Reason why: I used two schemas for two different applications, but there is a 'user' table (not necessarily users in the mysql.user table) in one schema that I would like to reuse in the schema without granting permission to the other schema. This is why I used a view.

Drew

No. It is not possible.

Please see MySQL Forums post.

in the referenced table, there must be an index where the referenced columns are listed as the first columns ...

Views are materialized. They are not tables with left-most indexes on them natively. Views are also snapshots in time (the time of creation). They can fall out of favor of the current schema, unbeknownst until used the next time. So, for many reasons, they will not be candidates for FK use as the target, the referenced, as they call it.

From the Manual Page FAQ: Views

After a view has been created, it is possible to drop or alter a table or view to which the definition refers. To check a view definition for problems of this kind, use the CHECK TABLE statement.

Further reason why a view is a snapshot as I call it. Its validity at any point in time can become highly compromised. Not an FK referenced candidate.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create foreign key to a view

From Dev

Create a pseudo foreign key on a view using entity framework

From Dev

Create mapping to the foreign key

From Dev

MariaDB - Create Foreign key

From Dev

create phpMyAdmin foreign key

From Dev

Create mapping to the foreign key

From Dev

add a foreign key constraint on a view

From Dev

How to create foreign key in a table?

From Dev

InnoDb how to create a foreign key

From Dev

create primary and foreign key relationship

From Dev

How to create a foreign key in phpmyadmin

From Dev

Unable to create foreign key in PHPmyadmin

From Dev

Foreign key constraint error on create

From Dev

Unable to create table with foreign key

From Dev

ViewModel with foreign key and Create action

From Dev

How to create object with Foreign Key

From Dev

Create a foreign table pointing to a view

From Dev

Create a foreign table pointing to a view

From Dev

create a foreign key on a primary key of another table

From Dev

Create composite key from foreign key and DateTime

From Dev

How to display foreign key values in MVC View

From Dev

Can a foreign key in SQLite point to a view?

From Dev

mysql add foreign key constraint referencing a view

From Dev

query foreign key table for list view in django

From Dev

Displaying Foreign Key Options in django restframework view

From Dev

Primary And Foreign key mapping between view and table

From Dev

How to create fixtures with foreign key alias in rails?

From Dev

Django Create Foreign key to external table

From Dev

How to create foreign key for 2 tables?