I have a problem where I need to check that two columns in each table in a database are unique.
We have the database with barcode entries called uid and rid.
Table 1: T1.uid And Table 2: T2.rid No barcodes in the two table columns must be the same. How can we ensure that. If a insertion of a barcode into table T1.uid matches an entry in T2.rid we want to throw an error.
The tables are cleaned up and is in a consistent state where the entries in T1.uid and T2.rid are unique over both table columns.
It is not possible to insert NULL values in the tables respective uid and tid column(T1.uid and T2.rid)
It is not possible to create a new table for all barcodes. Because we don't have full control of the database server.
EDIT 19-02-2015
This solution cannot work for us, because we cannot make a new table to keep track of the unique names(see table illustration). We want to have a constraint over two columns in different tables without changing the schema.
Per the illustration we want to make it impossible for john to exist in T2 because he already exists in table T1. So an error must be "thrown" when we try to insert John in T2.Name.
The reason is that we have different suppliers that inserts into these tables in different ways, if we change the schema layout, all suppliers would need to change their database queries. The total work is just to much, if we force every suppplier to make changes. So we need something unobtrusive, that doesnt require the suppliers to change their code.
A example could be that T1.Name is unique and do not accept NULL values. If we try insert an existing name, like "Alan", then an exception will occur because the column has unique values.
But we want to check for uniqueness in T2.Name at the same time. The new inserted value should be unique over the two tables.