将JSON转换为MySQL表,表结构应该如何?

时间:2013-08-20 04:43:19

标签: mysql json

以下是JSON文件......

{
    "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表以便轻松高效地访问?

5 个答案:

答案 0 :(得分:2)

MySQL是一个关系数据库。这意味着您提出的任何解决方案都需要包含主键外键规范化。这是一个简单的教程,将向您展示如何做。玩得开心!

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

答案 1 :(得分:1)

很难说数据应该如何构建,因为这取决于您的应用程序。但是,作为第一个切入点,一些好的经验法则可能是:

  1. 单个JSON对象的相同“级别”的所有非数组数据都是单个表。按级别我的意思是对象的嵌套程度。因此,例如,给定{"a": 100, "b": "hello", "c": {"x": 100, "y": "foo"}}ab处于同一级别,而xy处于不同的级别。
  2. 您可以使用以下几种方法处理不同级别的数据:
    1. “展平”嵌套,以便在上面的示例中,您有一个包含abxy的表格。< / LI>
    2. 为每个嵌套级别创建新表。根据上面的示例,这是一个包含ab的表,一个包含xy的表。这两个表之间显然存在关系,它告诉您是否如何构造链接键。有关详细信息,请参阅https://stackoverflow.com/a/7296873/1431244
  3. 阵列非常清楚地表明了一对多的关系,所以这些关系会按照上面链接的帖子所描述的那样进入他们自己的表格。
  4. 上面的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等,具体取决于您期望的值的数量等。还有索引注意事项,外键约束,等等,但上面希望能给你正确的想法,并提供足够的信息来开始。

答案 2 :(得分: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)
)

答案 3 :(得分:0)

我认为你应该使用包含cardSet的表(name, code, releaseDate, border, type)和另一个cards表,其中一个外键引用cardSet

您还需要制作与卡表有多对多关系的typecolorsubtype表格,因为您可以拥有多张卡片{ {1}},typecolor

subtype

答案 4 :(得分:0)

以下是原始格式的规范化架构,您可以根据需要更改它,并使用Null,Primary Key,Foreign Key属性,与您正在使用的数据库相关的类型进行更新

粗体(突出显示)是表名,PK =主键,FK =外键,你可以根据需要改变

  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