Would like to find out if there is a way to add a UNIQUE constraint across all tables in a PostgreSQL database for a common specific column?
For example, consider a PostgreSQL database named, "School". It has 2 tables named, "teachers" and "students" respectively.
"teachers" table has, teacher_id, first_name, last_name, homeroom_number, department, email, and phone columns.
"students" has, student_id, first_name, last_name, homeroom_number, phone, email, graduation_year columns.
I want to ensure that when data is inserted into the respective tables, the email and phone columns in both "teachers" and "students" are unique.
Typically when the UNIQUE constraint is added, it is added as only either a table constraint (table-wide) or column constraint (applies only to the specific column in a specific table). So if the UNIQUE constraint is added to the email and phone columns in the "teachers" table, the values are only unique within the "teachers" table. If the same thing is done to the "students" table, the emails and phone numbers will also be unique only within the "students" table. How can I add a UNIQUE constraint that applies to phone and email columns across both "teachers" and "students" tables such that each phone number and email address is truly unique to each individual regardless of whether that individual is a teacher or a student? For example, if a teacher's phone number is inserted as 123-456-789 in the "teachers" table, inserting the same phone number, 123-456-789 for a student in the "students" table will elicit an error.