Sql 需要约束的表。 Relationships ---------------------------------------- aID aID_role bID bID_role ---------------------------
需要约束的表。Sql 需要约束的表。 Relationships ---------------------------------------- aID aID_role bID bID_role ---------------------------,sql,tsql,azure-sql-database,Sql,Tsql,Azure Sql Database,需要约束的表。 Relationships ---------------------------------------- aID aID_role bID bID_role ---------------------------------------- 4 1 5 2 Relationships ---------------------------------------- aID aID_role
Relationships
----------------------------------------
aID aID_role bID bID_role
----------------------------------------
4 1 5 2
Relationships
----------------------------------------
aID aID_role bID bID_role
----------------------------------------
4 1 5 2
5 2 4 1 <-- sort of a duplicate
CREATE VIEW dbo.Relationships_DRI_Unique
WITH SCHEMABINDING
AS
SELECT
CASE WHEN FromID < ToID THEN FromID ELSE ToID END as Col1,
CASE WHEN FromID < ToID THEN ToID ELSE FromID END as Col2
FROM
dbo.Relationships
GO
CREATE UNIQUE INDEX IX_Relationships_DRI_Unique on dbo.Relationships_DRI_Unique (Col1,Col2)
id1 AS (CASE WHEN aID < bID THEN aid ELSE bid END),
id2 AS (CASE WHEN aID < bID THEN bid ELSE aid END),
aID bID id1 id2
4 5 4 5
5 4 4 5
CONSTRAINT IX_Relationships UNIQUE (id1, id2)
table persons
id PK
first_name,
second_name
table roles
id PK
role_id
role_name (ex. Father, daugther,)
table relationship
id PK
person (ex. persons.id = 1, JOHN)
role (roles.id = 5, FATHER
person_to (persons.id = 3, CARLA,