2011-01-24 59 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 
相關問題