Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 需要约束的表。 Relationships ---------------------------------------- aID aID_role bID bID_role ---------------------------_Sql_Tsql_Azure Sql Database - Fatal编程技术网

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,