2011-01-24 46 views

回答

4

这是一个比较复杂的问题,当我问到它的时候它似乎很复杂,但简单的答案是您完全规范了数据库。在完全标准化后,每个表代表一个谓词,一列值表示主体,一列值表示该对象。您可以在此基础上将任意SQL数据库转换为三重存储。

+0

这听起来真棒。这是真的吗? :-) ...好的,如下:你能引用论文,链接或其他内容吗?谢谢! – Benjohn 2017-10-14 22:15:59

+0

不,我不能举出任何东西,据我所知这是原创。它可以通过其他方式完成(即1个大表,包含三列A,B,以及它们之间的关系类型)。但是当你深入思考一张桌子是什么时,你意识到它可以代表关系的类型(或者不是,如人们所希望的那样)。 – tjb 2017-10-19 13:20:25

2

功能转换到三倍任何一种关系数据转换成三格式:

def transform_to_triple(source,db_name,table,result): 
    #get the list of relations for the selected DB 
    max_records = 100 
    response = [] 
    x_print = lambda *x : response.append("(%s)\n" %("".join(["%s"%(v) for v in x]))) 

    id = 1 

    x_print(id,',(db_name:string),',db_name) 
    logger.info("(%s,(db_name,string), %s)" %(id,db_name)) 

    tables = [] 
    table_list = [table,] 
    for i, _table in enumerate(table_list): 
     _table_id = id + i + 1 
     x_print(id,',(rel:id),', _table_id) 
     logger.info("(%s,(rel, id), %s)" %(id, _table_id)) 

     _schema = get_column_list(source, db_name,_table) 
     tables.append((_table_id, _table, _schema)) 
    for _table in tables: 
     _table_id = _table[0] 
     x_print(_table_id,',(rel_name:string),',_table[1]) 
     for j,row in enumerate(result): 
      #lets assume there is always less than 10 k tuples in a table 
      _tuple_id = _table_id * max_records + j + 1 
      x_print(_table[0],',(tuple:id),', _tuple_id) 
      logger.info("(%s,(tuple, id), %s)" %(_table[0],_tuple_id)) 
     for j,row in enumerate(result): 
      _tuple_id = _table_id * max_records + j + 1 
      for k,value in enumerate(row): 
       x_print(_tuple_id, ",(%s : %s)," %(_table[2][k][0], _table[2][k][1]), value) 
    return "%s" %("".join(response)) 

get_column_list函数返回一个数据库表中的列的列表:

def get_column_list(src_name,db_name,table_name): 
    cur = get_connect() #Connecting with tool DB 
    query = '''select db_name, host, user_name, password from "DataSource" where src_name = '%s' and db_name = '%s' '''%(src_name, db_name) 
    cur.execute(query) 
    data = cur.fetchall() 
    (db, host, username, password) = data[0] 
    _module = get_module(src_name) 
    cursor = _module.get_connection(db, host, username, password) 
    try: 
      _column_query = _module.COLUMN_LIST_QUERY %(db_name, table_name) 
    except TypeError, e: 
      try: 
       _column_query = _module.COLUMN_LIST_QUERY %(table_name) 
      except TypeError, e: 
       _column_query = _module.COLUMN_LIST_QUERY 

    cursor.execute(_column_query) 
    column_list = cursor.fetchall() 
    return column_list 
相关问题