2013-08-20 28 views
1

以下是JSON文件...將JSON轉換爲MySQL表,該表的結構應該如何?

{ 
    "name":"Magic 2014 Core Set", 
    "code":"M14", 
    "releaseDate":"2013-07-19", 
    "border":"black", 
    "type":"core", 
    "cards": 
    [ 
     { 
      "layout":"normal", 
      "type":"Creature - Human Warrior", 
      "types":["Creature"], 
      "colors":["Red"], 
      "multiverseid":370735, 
      "name":"Academy Raider", 
      "subtypes":["Human","Warrior"], 
      "cmc":3, 
      "rarity":"Common", 
      "artist":"Karl Kopinski", 
      "power":"1", 
      "toughness":"1", 
      "manaCost":"{2}{R}", 
      "text":"Intimidate (This creature can't be blocked except by artifact creatures and/or creatures that share a color with it.)\n\nWhenever Academy Raider deals combat damage to a player, you may discard a card. If you do, draw a card.", 
      "number":"124", 
      "imageName":"academy raider" 
     }, 
     { 
      "layout":"normal", 
      "type":"Artifact - Equipment", 
      "types":["Artifact"], 
      "colors":[], 
      "multiverseid":370581, 
      "name":"Accorder's Shield", 
      "subtypes":["Equipment"], 
      "cmc":0, 
      "rarity":"Uncommon", 
      "artist":"Alan Pollack", 
      "manaCost":"{0}", 
      "text":"Equipped creature gets +0/+3 and has vigilance. (Attacking doesn't cause it to tap.)\n\nEquip {3} ({3}: Attach to target creature you control. Equip only as a sorcery.)", 
      "flavor":"An Auriok shield is polished to a mirror finish even on the inside, enabling its bearer to watch foes ahead and behind.", 
      "number":"204", 
      "imageName":"accorder's shield" 
     }, 
     { 
      "layout":"normal", 
      "type":"Creature - Spirit", 
      "types":["Creature"], 
      "colors":["Black"], 
      "multiverseid":370811, 
      "name":"Accursed Spirit", 
      "subtypes":["Spirit"], 
      "cmc":4, 
      "rarity":"Common", 
      "artist":"Kev Walker", 
      "power":"3", 
      "toughness":"2", 
      "manaCost":"{3}{B}", 
      "text":"Intimidate (This creature can't be blocked except by artifact creatures and/or creatures that share a color with it.)", 
      "flavor":"Many have heard the slither of dragging armor and the soft squelch of its voice. But only its victims ever meet its icy gaze.", 
      "number":"83", 
      "imageName":"accursed spirit" 
     }, 
     {...}, 
     {...}, 
     {...}, 
    ] 
} 

該卡數據本身我覺得這是在一個單一的表,但我不知道怎麼...

"name":"Magic 2014 Core Set", 
"code":"M14", 
"releaseDate":"2013-07-19", 
"border":"black", 
"type":"core", 

會與卡數據相關聯。我應該如何設計MySQL表以實現簡單高效的訪問?

+0

這不是一個答案,所以它是一個評論 - 但是MySQL的要求?或者您認爲這是存儲數據的最佳方式嗎?像Couch這樣的Postgres和noSQL解決方案處理JSON要好得多。 – Interrobang

+0

http://dba.stackexchange.com/ –

+0

@Interrobang MySQL是一項要求。 – rotaercz

回答

2

MySQL是一個關係數據庫。這意味着您提出的任何解決方案都需要包括主鍵外鍵標準化。這裏有一個簡單的教程,將告訴你該怎麼做。玩的開心!

http://www.dreamincode.net/forums/topic/179103-relational-database-design-normalization/

+0

這真的很有幫助,我正在閱讀它。 – rotaercz

+0

@Camille謝謝 –

+1

一個更復雜的教程在這裏: http://www.anchor.com.au/hosting/support/CreatingAQuickMySQLRelationalDatabase ...如果你決定你喜歡設計關係數據庫有一個偉大的系列畢業生來自休斯敦大學的Clearwater(UHCL)博士Gary D. Boetticher博士的講座(關於數據庫設計)。這裏是第一和第二範式的演講鏈接。 http://www.youtube.com/watch?v=cbJ-xaBfWLM&list=TLWNPhMMnSMmY – Camille

0

我認爲你必須有2個表來存儲這樣的數據。

create table tbl_card (
card_id int primary key auto_increment, 
name varchar(50) not null, 
code varchar(10) not null, 
release_date datetime not null, 
border varchar(20) not null, 
type varchar(20) not null 
) 

create table tbl_card_detail (
card_id int not null, 
type varchar not null, 
.... 
primary key (card_id,type) 
) 
0

我想你應該有一個表cardSet,將包含(name, code, releaseDate, border, type)和另一個表去cards與外鍵指cardSet

你也需要做出typecolorsubtype表那會與卡表有多對多的關係,因爲您可以擁有多張卡片typecolorsubtype

CREATE TABLE `card` (
    `id` INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`id`) 
); 

CREATE TABLE `type` (
    `id` INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`id`) 
); 

CREATE TABLE `cardType` (
    `card` INT, 
    `type` INT 
); 

CREATE TABLE `cardSet` (
    `id` INT NOT NULL AUTO_INCREMENT, 
    `` INT, 
    PRIMARY KEY (`id`) 
); 

CREATE TABLE `color` (
    `id` INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`id`) 
); 

CREATE TABLE `cardColor` (
    `card` INT, 
    `color` INT 
); 

CREATE TABLE `subType` (
    `id` INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`id`) 
); 

CREATE TABLE `cardSubType` (
    `card` INT, 
    `subType` INT 
); 



ALTER TABLE `cardType` ADD CONSTRAINT `cardType_fk1` FOREIGN KEY (`card`) REFERENCES card(`id`); 
ALTER TABLE `cardType` ADD CONSTRAINT `cardType_fk2` FOREIGN KEY (`type`) REFERENCES type(`id`); 
ALTER TABLE `cardSet` ADD CONSTRAINT `cardSet_fk1` FOREIGN KEY (``) REFERENCES cardSet(`id`); 

ALTER TABLE `cardColor` ADD CONSTRAINT `cardColor_fk1` FOREIGN KEY (`card`) REFERENCES card(`id`); 
ALTER TABLE `cardColor` ADD CONSTRAINT `cardColor_fk2` FOREIGN KEY (`color`) REFERENCES color(`id`); 

ALTER TABLE `cardSubType` ADD CONSTRAINT `cardSubType_fk1` FOREIGN KEY (`card`) REFERENCES card(`id`); 
ALTER TABLE `cardSubType` ADD CONSTRAINT `cardSubType_fk2` FOREIGN KEY (`subType`) REFERENCES subType(`id`); 
+0

桌子是怎麼樣的?你能提供一個多對多關係查詢的例子嗎?我以前只使用過單個表格。我知道有一種方法可以使用多個表格,但在這方面我沒有太多經驗。 – rotaercz

+0

@rotaercz:例如,爲了創建一個類型的顏色關係,你需要創建一個表'color',一個表'type'和一個表'colorType','colorType'將有兩個字段1個外鍵到'color'和一個外鍵關鍵到'類型'你會發現代碼[here](http://pastebin.com/St4QMpdx) – amdorra

+0

@ rotaercz:你可以查看我更新的答案,如果你有任何問題隨時問 – amdorra

0

這裏是原始形式的規範化模式,你可以改變它的需求,並與空,主鍵,外鍵的屬性,更新它相對於數據庫類型使用的是

粗體(高亮)是表名,PK =主鍵,FK =外鍵,U可以改變按您的需求

Template (TABLE) 
1- Name 
2- Code 
3- Release Date 
4- Border 
5- Type 
6- Id (PK) 

Template Cards (TABLE) 
1- Template Id (FK) (Template Table) 
2- Card Id (FK) (Cards Table) 

Cards (Has M-M relationship with Types, Cards ,Subtypes Table) (TABLE) 
1- layout 
2- type 
3- mutiverseid 
4- name 
5- Card Id (PK) 
6- Card Detail Id 

Cards Detail 
1- Card detail Id 
2- Card Id 
2- Object Type (0 = Types , 1 = Color , 2 = Subtypes) 
3- Object Id (This id corresponds to Types, Color , Subtypes Table with respect to Object Type) 

Types (TABLE) 
1- type id (PK) 
2- type Detail/Code 

Color (TABLE) 
1- Color id (PK) 
2- Color Detail/Code 

SubTypes (TABLE) 
1- Subtype id (PK) 
2- Subtype Detail/Code 
1

這是很難說的數據應該如何構建爲可以依賴於你的應用程序。但是,作爲第一次切割,一些經驗法則可能是:

  1. 單個JSON對象的同一「級別」處的所有非數組數據都是單個表。我的意思是指物體的嵌套程度。因此,舉例來說,假設{"a": 100, "b": "hello", "c": {"x": 100, "y": "foo"}},ab處於同一水平,而xy處於不同的水平。
  2. 您有不同層次處理數據時有幾個選項:
    1. 「扁平化」的嵌套,這樣,對於上面的例子,你就必須包含abxy一個表。
    2. 爲每個嵌套級別創建新表。鑑於上面的例子,這是一個包含ab的表格,其中一個包含xy。這兩個表格之間顯然存在關係,它告訴你是否如何構建鏈接鍵。有關詳情,請參閱https://stackoverflow.com/a/7296873/1431244
  3. 數組非常清楚地表明一對多關係,因此這些關係會在上面鏈接的帖子中描述的自己的表中。

上面的JSON文件是相當大的,所以我不打算構建所有表的所有領域,但在這裏是有希望解釋了粗略的想法樣本:

create table card_pack (
    # Primary key to uniquely identify the pack 
    id integer autoincrement primary key, 
    name TEXT, 
    # foreign key that links to the codes table 
    code_id integer, 
    # etc, etc... 
); 

create table codes (
    # This is what the code_id field in the card_pack table refers to 
    id integer autoincrement primary key, 
    name CHAR(10) 
); 

create table cards (
    # unique key for each card 
    id integer autoincrement primay key, 
    # Refers to the card_pack table for the card pack 
    # containing this card 
    pack_id integer, 
    name TEXT, 
    # This should probably be a foreign key referring to a layouts table 
    # which contains one row per layout 
    layout TEXT, 
    # etc, etc. 
) 

# Table with one row for every possible card color 
create table colors { 
    id integer autoincrement primay key, 
    name TEXT, 
) 

# table that defines a many-to-many relationship 
# indicating which cards are which colors, so a row with 
# card_id = 7 and color_id = 11 means that card 7 is color 11. 
# Note that another row might have card_id 7 and color_id 18 
# so that card 7 is two colors, both color 11 and color 18. 
create table cards_colors (
    card_id integer, 
    color_id integer 
) 

在上面有很多細節缺失。例如,您可能不需要所有字符串字段的通用TEXT類型。有些應該可能是CHAR和一些VARCHAR,具體取決於字段大小,空間和性能考慮因素等。同樣,如果我有整數,你可能需要bigint,mediumint等,這取決於你期望值的數量等。還有索引考慮因素,外鍵約束等等,但上面的內容有希望給你提供正確的想法並提供足夠的信息來開始。