Posted January 03, 2012
This is probably better suited elsewhere, but the general forum is... well, general, so it's probably not too out of place given other discussions here.
Now, I have three tables, one with phone numbers (table_phone), one with rooms (table_rooms), and one with people (table_people). Each phone number is, of course, unique, and can be assigned to EITHER a room , a person, or neither, but never both.
table_people and table_rooms are quite different and serve different purposes elsewhere, so merging them isn't really an option. I COULD solve this through the application itself (PHP scripting), but would prefer an SQL solution. The server is currently running MySQL 5.0, but I might as well upgrade (or crossgrade to MariaDB) if a solution requires commands only available in newer versions.
If I make unique foreign keys id_people and id_rooms in the table_phone, they are different columns, so the phone number may very well be assigned to a person and a room at the same time.
If I make a unique foreign key id_phone in table_rooms and table_people, they have no inherent uniqueness (only in the same table) and the situation is the same as in the previous case.
I haven't actually begun building the application, beyond planning the database, so changes are quite easy to implement.
The question: How can I make sure a phone number is used at most once, across two different tables?
Now, I have three tables, one with phone numbers (table_phone), one with rooms (table_rooms), and one with people (table_people). Each phone number is, of course, unique, and can be assigned to EITHER a room , a person, or neither, but never both.
table_people and table_rooms are quite different and serve different purposes elsewhere, so merging them isn't really an option. I COULD solve this through the application itself (PHP scripting), but would prefer an SQL solution. The server is currently running MySQL 5.0, but I might as well upgrade (or crossgrade to MariaDB) if a solution requires commands only available in newer versions.
If I make unique foreign keys id_people and id_rooms in the table_phone, they are different columns, so the phone number may very well be assigned to a person and a room at the same time.
If I make a unique foreign key id_phone in table_rooms and table_people, they have no inherent uniqueness (only in the same table) and the situation is the same as in the previous case.
I haven't actually begun building the application, beyond planning the database, so changes are quite easy to implement.
The question: How can I make sure a phone number is used at most once, across two different tables?
This question / problem has been solved by wpegg
