2017-07-11 2 views
0

Je voudrais savoir comment identifier les tables qui sont liées entre elles dans ma base de données SQL Server en exécutant une requête SQL.Rechercher des relations de table à partir de SQL Server

Par exemple, supposons que j'ai 2 relations plusieurs-à-plusieurs et 3 tables initialisées avec quelques valeurs aléatoires.

create table A (pk int not null primary key identity, A int); 
create table B (pk int not null primary key identity, B int); 
create table C (pk int not null primary key identity, C int); 

create table AB 
(
    a_pk int not null references A, 
    b_pk int not null references B, 
    primary key(a_pk, b_pk) 
); 

create table AC 
(
    a_pk int not null references A, 
    c_pk int not null references C, 
    primary key(a_pk, c_pk) 
); 

Je voudrais exécuter une requête paramétrée ayant en sortie quelque chose comme:

Avec le paramètre 'A':

MM_TAB | REL_TAB 
------------------ 
    AB | B 
    AC | C 

Avec le paramètre 'B':

MM_TAB | REL_TAB 
------------------ 
    AB | A 

Avec le paramètre 'C':

MM_TAB | REL_TAB 
------------------ 
    AC | A 

Comment puis-je faire cela?

+0

départ ce poste: https://stackoverflow.com/questions/3907879/sql-server-howto-get-foreign-key-reference-from-information-schema –

Répondre

2

Check this out:

J'ai créé les tables et les relations en utilisant SQL Server 2014:

create database relation 

use relation 

create table A (pk int not null primary key identity, A int); 
create table B (pk int not null primary key identity, B int); 
create table C (pk int not null primary key identity, C int); 

create table AB 
(
    a_pk int not null foreign key references A(pk), 
    b_pk int not null foreign key references B(pk), 
    primary key(a_pk, b_pk) 
); 

create table AC 
(
    a_pk int not null foreign key references A(pk), 
    c_pk int not null foreign key references C(pk), 
    primary key(a_pk, c_pk) 
); 

Ensuite, vous pouvez utiliser cette requête pour trouver les tables connexes:

use relation 
go 

DECLARE @table_param NVARCHAR(2)='A'; 

with rel as(
     SELECT 
      K_Table = FK.TABLE_NAME, 
      --FK_Column = CU.COLUMN_NAME, 
      PK_Table = PK.TABLE_NAME 
      --PK_Column = PT.COLUMN_NAME, 
      --Constraint_Name = C.CONSTRAINT_NAME 
     FROM 
      INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C 
     INNER JOIN 
      INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME 
     INNER JOIN 
      INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME 
     INNER JOIN 
      INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME 
     wHERE PK.TABLE_NAME = @table_param 

) 

SELECT 
    K_Table = FK.TABLE_NAME, 
    --FK_Column = CU.COLUMN_NAME, 
    PK_Table = PK.TABLE_NAME 
    --PK_Column = PT.COLUMN_NAME, 
    --Constraint_Name = C.CONSTRAINT_NAME 
FROM 
    INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C 
INNER JOIN 
    INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME 
INNER JOIN 
    INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME 
INNER JOIN 
    INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME 
WHERE FK.TABLE_NAME IN(SELECT R.K_Table FROM REL R) 
+0

Votre requête fonctionne bien, mais peu importe le '@ table_param' I spec ify, il retourne toujours TOUTES les relations dans ma base de données, alors que j'ai seulement besoin du petit sous-ensemble que j'ai spécifié dans ma question. Toute suggestion sur la façon de l'améliorer? – jjhorn

+0

vous êtes rigth, le filtre ne fonctionnait pas correctement, je l'ai réparé, s'il vous plaît jeter un oeil –

+0

il ya encore quelque chose de mal, puisque avec '@table_param = 'A'' il retourne' AB | A' et 'AC | A' au lieu de 'AB | B' et 'AC | C'. En d'autres termes, 'K_Table' est correct, mais dans' PK_Table' j'ai besoin de l'autre table impliquée dans la relation (pas '@ table_param') – jjhorn