if’ve got three tables in my MYSQL Database and want to connect two of this table with one table. The tables look like this
CONNECTTABLE
+----+-----------+---------+
| ID | search_id | room_id |
+----+-----------+---------+
SEARCHTABLE
+----+-----------+-----+
| ID | search_id | ... |
+----+-----------+-----+
SEARCHTABLE
+----+---------+-----+
| ID | room_id | ... |
+----+---------+-----+
Is it possible to ensure via MYSQL that in the CONNECTTABLE only search_id OR room_id is not null per datarow? If I can do so, how can I do so?
Valid rows:
+----+-----------+---------+
| ID | search_id | room_id |
+----+-----------+---------+
| 1 | 42 | NULL |
+----+-----------+---------+
| 2 | NULL | 1337 |
+----+-----------+---------+
Invalid row:
+----+-----------+---------+
| ID | search_id | room_id |
+----+-----------+---------+
| 3 | 42 | 17 |
+----+-----------+---------+
Best regards,
Gerrit
What you want is called a “CHECK constraint”. Unfortunately, MySQL does not support them. Well, it will let you define them, but it won’t actually check them. This article discusses using triggers to implement the same functionality.