i have a table in sql that uses the adjacency model to create child/parent relationship. here is the exact schema:
id int primary key identity(1,1)
name nvarchar(max)
parent int
now i want to have exactly one name with the same parent. another word for parent 1 there should only be one name. if that name wants to show up in another record, it must have a different parent. how can i do this in t-sql?
I think you’re looking for unique constraints. This will allow you to define column combinations for which the data must be unique across the table, but are not part of the primary key.
Are you using SQL Server? If so, these two articles should help:
Overview: http://msdn.microsoft.com/en-us/library/ms191166.aspx
Creating/modifying: http://msdn.microsoft.com/en-us/library/ms177420.aspx
Edit to provide example