I have a query
SELECT *
FROM table1
WHERE
documentId in
(
--select items from a second table where a third column happens to be null
select documentId from table2 t2 inner join table1 t1
on t1.documentId = t2.documentId and t1.itemId = t2.ItemId
WHERE t1.someOtherColumn is null
)
and itemId in
(
--similar query as above, just selecting itemId now
select itemId from table2 t2 inner join table1 t1
on t1.documentId = t2.documentId and t1.itemId = t2.ItemId
WHERE t1.someOtherColumn is null
)
order by 1
Given that table1 has composite PK = documentId + itemId, is this sufficient to select only values from table1 that are unique? My fear is that a situation may occur where documentId exists and itemId exists, but since they are not looked at together then incorrect selections can be made.
For example,
Let's assume that there exists a value
documentId = 1 and itemId = 1.
Let's assume there is no composite key
documentId = 1 and itemId = 1.
I do not want composite key
[documentId = 1 and itemId = 3]
to be included.
I also do not want composite key
[documentId = 2 and itemId = 1]
to be included.
If there were a composite key added later (it's not there now)
[documentId = 1 and itemId = 1]
then it should be included.