3

以下是 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 表以方便高效地访问?

4

5 回答 5

2

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

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

于 2013-08-20T05:03:57.760 回答
2

很难说应该如何构造数据,因为这可能取决于您的应用程序。然而,作为第一次切割,一些好的经验法则可能是:

  1. 单个 JSON 对象的同一“级别”的所有非数组数据都是一个表。我所说的级别是指对象的嵌套程度。因此,例如,给定{"a": 100, "b": "hello", "c": {"x": 100, "y": "foo"}}ab处于同一级别,而xy处于不同级别。
  2. 您有几个选项可用于处理不同级别的数据:
    1. “展平”嵌套,这样,对于上面的示例,您将拥有一个包含abxy.
    2. 为每个嵌套级别创建新表。给定上面的例子,这是一张包含aand的表格b,一张包含xand的表格y。这两个表之间显然存在关系,它会告诉您是否如何构造链接键。有关详细信息,请参阅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 等,具体取决于你期望的值的数量等。还有索引注意事项, 外键约束等,但以上希望能给你正确的想法,并提供足够的信息来开始。

于 2013-08-20T05:11:04.260 回答
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)
)
于 2013-08-20T04:50:24.587 回答
0

我认为您应该使用一个cardSet包含外键的(name, code, releaseDate, border, type)表和另一个表cardscardSet

您还需要制作与卡片表具有多对多关系的表格,因为您可以拥有一张以上的type卡片,或者colorsubtypetypecolorsubtype

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`);
于 2013-08-20T04:56:30.447 回答
0

这是原始形式的规范化模式,您可以根据需要更改它并使用 Null、主键、外键属性、关于您正在使用的数据库的类型来更新它

粗体(突出显示)是表名称,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
于 2013-08-20T05:09:19.770 回答