如何获取多对多连接中涉及的表名和列名?

时间:2017-02-17 22:29:40

标签: oracle oracle11g many-to-many jointable database-metadata

使用" List of foreign keys and the tables they reference",我可以获取连接表的名称和它连接的表,但不能获取第三个表的名称。如何让所有三个表/字段涉及多对多?

例如,如何获得以下内容(请注意第二行的r2_ *):

first ex. second execute.
282       11340
336       69498
390       108918
82        3400
82        3397
27        745
270       10629090
27        19683

// Some object with date
var recipesData = {
    'test1': {
        'example1': 544,
        'example2': 323
    },
    'test2': {
        'example1': 123
    },
}

// Function that I call from main file
module.exports.getFinished = function (item, stock) {
    // Create temp 'recipes' variable
    var recipes = recipesData[item];
    // Execute recursive func
    var basics = getBasics(recipes);

    return basics;
};

// Create 'finished' empty object
var finished = {};

// My recursive fuction that works pretty nice at first
function getBasics(craft) {
    for (var part in craft) {
        for (var recipe in recipesData) {
            if (recipesData.hasOwnProperty(part)) {
                var child = recipesData[part];
                break;
            } else
                child = null;
        }

        if (child) {
            for (var child_part in child)
                // HERE it's CHANGE my 'recipesData' object at the top!
                child[child_part] *= craft[part];
            getBasics(child);
        } else {
            if (finished.hasOwnProperty(part))
                finished[part] += craft[part];
            else
                finished[part] = craft[part];
        }
    }
    return finished;
}

鉴于以下表格:

TableName   Field       r_TableName r_Field r2_TableName    r2_Field
Users       VehicleId   Vehicles    Id      NULL            NULL
UserGroups  UserId      Groups      Id      Users           Id
Cats        UserId      Users       Id      NULL            NULL

我一直在使用各种变体:

CREATE TABLE Users(
    Id NUMBER(10,0),
    UserName NVARCHAR2(20),
    VehicleId NUMBER(10,0),
    CONSTRAINT user_pk PRIMARY KEY(ID)
);
ALTER TABLE Users ADD CONSTRAINT
    users__vehicle_fk FOREIGN KEY(VehicleId) REFERENCES Vehicles(Id) ENABLE;
CREATE TABLE Vehicles(
    ID NUMBER(10,0),
    VehicleName NVARCHAR2(20),
    CONSTRAINT vehicle_pk PRIMARY KEY(ID)
);
CREATE TABLE Cats(
    ID NUMBER(10,0),
    CatName NVARCHAR2(20),
    UserId NUMBER(10,0),
    CONSTRAINT cat_pk PRIMARY KEY(ID)
);
ALTER TABLE Cats ADD CONSTRAINT
    cat__user_fk FOREIGN KEY(UserId) REFERENCES Users(ID) ENABLE;   
CREATE TABLE Groups(
    Id NUMBER(10,0),
    GroupName NVARCHAR2(20),
    CONSTRAINT group_pk PRIMARY KEY(ID)
);  
CREATE TABLE UserGroups (
    UserId NUMBER(10,0),
    GroupId NUMBER(10,0),
    CONSTRAINT user__groups_pk PRIMARY KEY(UserId, GroupId)
);  
ALTER TABLE UserGroups ADD CONSTRAINT
    userGroups__user_fk FOREIGN KEY(UserId) REFERENCES Users(Id) ENABLE;
ALTER TABLE UserGroups ADD CONSTRAINT
    userGroups__group_fk FOREIGN KEY(GroupId) REFERENCES GROUPS(ID) ENABLE;

我可以看到间接关系的原始数据(比如我的示例输出中的三行中的第二行),但我看不到用直接关系连接数据的明确方法,因此我知道它们都涉及相同的M :M加入。

注意:关系比许多更深入:很多人使用单个连接表不需要考虑。

如何将这些数据整合在一起?

1 个答案:

答案 0 :(得分:0)

我不是100%确定这是你想要的,但是你打算使用外连接而不是内连接吗?

SELECT ac1.owner
     , ac1.constraint_name
     , ac1.constraint_type
     , ac1.table_name
     , ac1.r_owner
     , ac1.r_constraint_name
     , '@@@@@@@@@@'
     , ac2.owner
     , ac2.constraint_name
     , ac2.constraint_type
     , ac2.table_name
     , ac2.r_owner
     , ac2.r_constraint_name
     , '@@@@@@@@@@'
     , conscols.*
     , '@@@@@@@@@@'
     , r_conscols.*
  FROM user_constraints ac1
       LEFT OUTER JOIN user_constraints ac2
           ON ac1.r_constraint_name = ac2.constraint_name
          AND ac1.owner = ac2.owner
       LEFT OUTER JOIN user_cons_columns conscols
           ON ac1.owner = conscols.owner
          AND ac1.constraint_name = conscols.constraint_name
       LEFT OUTER JOIN user_cons_columns r_conscols
           ON ac2.owner = r_conscols.owner
          AND ac2.constraint_name = r_conscols.constraint_name
          AND r_conscols.position = conscols.position