Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Constraint two columns so one of them always are NULL

Tags:

mysql

I have a table with references from two of it's columns to two other tables PK's. Is there a way I can restrict both of those columns from having values set?

I only want one of them to have a value set, while the other is NULL

Favorites:

id
table_1_id
table_2_id

Table_1:

id

Table_2:

id
like image 676
dropson Avatar asked Oct 14 '22 19:10

dropson


1 Answers

In SQL, you'd ideally handle this with a CHECK constraint.

In MySQL, there is not a direct mechanism to impose such a CHECK constraint. If you try to include one, the MySQL Reference Manual states (since it's part of the standard):

The CHECK clause is parsed but ignored by all storage engines.

like image 198
Riedsio Avatar answered Oct 31 '22 18:10

Riedsio