在MySQL中对外键进行详细说明?

时间:2017-07-12 18:18:28

标签: python mysql sql database database-design

我希望有人帮助我解决我正在处理的问题。我没有太多的数据库经验,但我正在尝试自学MySQL以获得与加密货币相关的个人项目。基本上,我正在尝试将来自python程序的键值对存储到数据库中,该程序将json从交换机的api解析。基于我对数据库的有限知识,我写了一个可能的解决方案:

OVERALL TABLE STRUCTURE:
TICKER = FK OF ENTRIES 
ORDERBOOKS = FK to table relating orderbooks and timestamps
MARKET_HISTORIES = FK to table relating market histories and timestamps
MARKET_SUMMARIES = FK to table relating market summaries and timestamps

 ------------------------------------------------------------------------------------------------------------------------------------------
|       KEY       | TICKER |         ORDERBOOKS            |         MARKET_HISTORIES        |        MARKET_SUMMARIES         | TIMESTAMP |
 ------------------------------------------------------------------------------------------------------------------------------------------
| autoincremented |BTC-LTC |hash("BTC-LTC, "ob",timestamp)| hash("BTC-LTC","mh",timestamp) | hash("BTC-LTC","ms",timestamp) |  datetime |
   (unused)                                                                                                 
 ------------------------------------------------------------------------------------------------------------------------------------------

MySQL schtuff:
CREATE TABLE test_table( 
p_key INT NOT NULL AUTO_INCREMENT, 
ticker VARCHAR(20) NOT NULL, 
orderbooks INT NOT NULL, 
market_histories INT NOT NULL, 
market_summaries INT NOT NULL, 
timestamp INT, 
PRIMARY KEY (p_key));


___________________________________________________________________________________________________________________

FOREIGN KEY = hash of ticker and "ob", used to access all orderbooks relating to the ticker
timestamp = unix timestamp from when the insertion began at the overall structure

ORDERBOOKS
 -----------------------------------------------------------------------------------------------------------------
|        KEY      | FOREIGN KEY | TIMESTAMP |             BUYS_KEY             |             SELLS_KEY            |
 -----------------------------------------------------------------------------------------------------------------
| autoincremented |  FK passed  | timestamp | hash(FK passed, "bk", timestamp) | hash(FK passed, "sk", timestamp) |
    (unused)

BUYS

 ----------------------------------------------------
|        KEY         | FOREIGN KEY | QUANTITY | RATE |
 ----------------------------------------------------
| autoincremented PK | buys_key    |   float  | float|
       (unused)



SELLS

 ----------------------------------------------------
|        KEY         | FOREIGN KEY | QUANTITY | RATE |
 ----------------------------------------------------
| autoincremented PK | sells_key   |   float  | float|
       (unused)

_______________________________________________________________________________________________



FOREIGN KEY = hash of ticker and "mh", used to reference Market History entries
timestamp = unix timestamp from when the insertion began at the overall structure

MARKET HISTORY
 ------------------------------------------------------------------------------
|        KEY      | FOREIGN KEY | TIMESTAMP |           RESULT_KEY             |
 ------------------------------------------------------------------------------
| autoincremented |  FK passed  | timestamp | hash(FK passed, "mh", timestamp) |
    (unused)

RESULT_MH

 ------------------------------------------------------------------------------------------------------------
|        KEY         | FOREIGN KEY | ID | UNIQUE_TIMESTAMP | QUANTITY | PRICE | TOTAL | FILLTYPE | ORDERTYPE |
 ------------------------------------------------------------------------------------------------------------
| autoincremented PK | results_key | int| datetime object? |  float   | float | float |  string  |  string   |
       (unused)
_______________________________________________________________________________________________________________


FOREIGN KEY = hash of ticker and "ms", used to reference Market Summary entries
timestamp = unix timestamp from when the insertion began at the overall structure

MARKET SUMMARY
 ------------------------------------------------------------------------------
|        KEY      | FOREIGN KEY | TIMESTAMP |           RESULT_KEY             |
 ------------------------------------------------------------------------------
| autoincremented |  FK passed  | timestamp | hash(FK passed, "ms", timestamp) |
    (unused)


RESULT_MS
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------
|        KEY         | FOREIGN KEY | HIGH | LOW | VOLUME | LAST | BASEVOLUME | UNIQUE_TIMESTAMP | BID | ASK | OPEN_BUY_ORDERS | OPEN_SELL_ORDERS | PREVDAY | CREATED |
 --------------------------------------------------------------------------------------------------------------------------------------------------------------------
| autoincremented PK | results_key | float|float| float  | float|   float    | datetime object? |float|float|       int       |        int       |  float  | datetime|
       (unused)

所以我遇到的问题/困惑是如何让所有这些外键相互关联,以及如何编程,我甚至可以让它工作。基于我所读到的,我假设MySQL代码类似于:外键(订单簿)引用订单簿(foreign_key),这听起来是对的吗?我希望能够存储市场的特定快照并随意访问它们。

例如,如果我现在在UNIX时间1499882777拍摄市场快照,然后在UNIX时间1499882800拍摄另一张快照,我希望能够访问这两个条目并在那些时间访问所有相关数据(订单,市场历史,市场总结)。

我在这里走在正确的轨道上吗?我对数据库很陌生,并希望尽可能高效/健壮。

提前感谢您的投入! :d

编辑1: 在重新阅读这个必须是第20次之后,我想知道我是否过于复杂的市场历史和市场汇总表

编辑2: 我在mysql可执行文件中使用了这个: create table fk_test( p_key INT NOT NULL AUTO_INCREMENT, fk INT, buys_key INT, sells_key INT, PRIMARY KEY (p_key), FOREIGN KEY (fk) REFERENCES test_table(orderbooks));

收到此错误:ERROR 1215 (HY000): Cannot add foreign key constraint

0 个答案:

没有答案