I have two tables:
create table [dbo].[Main]
(
[ID] [int] identity(1,1) primary key not null,
[No] [int] not null,
[Sign] [char](1) not null
)
create table [dbo].[Names]
(
[ID_Main][int] primary key not null,
[Name][nvarchar](128) not null,
constraint [FK_Main_Users] foreign key ([ID_Main]) references [dbo].[Main]([ID]),
constraint [CK_Name] unique ([Name], [Sign])
)
The problem is with the second constraint CK_Name
Is there a way to make a constraint target column from a foreign table?
EDIT:
Explanation.
I'm using these tables in a Silverlight application using EntityFramework.
The entities are created by Table per type inheritance so the code is something like this:
public abstract class Main
{
// main properties
}
public class Names : Main
{
// names properties
}
This forces me not to use sql views.
Sample data.
--------------------------------------------- | Main | Names | --------------------------------------------- | ID | Sign | No | ID_Main | Name | --------------------------------------------- | 1 | A | 1 | 1 | 'qwe' | | 2 | B | 1 | 2 | 'qwe' | | 3 | B | 1 | 3 | 'qwe' | | 4 | C | 1 | 4 | 'qwe' | | 5 | A | 2 | 5 | 'asd' | | 6 | B | 2 | 6 | 'asd' | | 7 | B | 2 | 7 | 'asd' | | 8 | C | 2 | 8 | 'asd' |
As you can see there are some rows with the same Name but with different Sign. There can't be any non unique Name with the same Sign.
I'd like to enforce that there is only one Name with Sign = A and only one Name with Sign C but many Names with Sign = B