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

アプリケーションに依存する可能性があるため、データをどのように構造化する必要があるかを言うのは困難です。ただし、最初のカットとして、いくつかの適切な経験則は次のとおりです。

  1. 単一の JSON オブジェクトの同じ「レベル」にあるすべての非配列データは、単一のテーブルです。レベルとは、オブジェクトがどれだけ深くネストされているかを意味します。したがって、たとえば、与えられた{"a": 100, "b": "hello", "c": {"x": 100, "y": "foo"}}a、およびbは同じレベルにありますが、xyは異なるレベルにあります。
  2. さまざまなレベルでデータを処理するためのいくつかのオプションがあります。
    1. a入れ子を「平坦化」して、上記の例のようにb、、、、xおよびを含む単一のテーブルを作成しyます。
    2. ネスト レベルごとに新しいテーブルを作成します。上記の例を考えると、それは and を含む 1 つのテーブルab、 andxを含むテーブルyです。これら 2 つのテーブルの間には明らかに関係があり、リンク キーを作成する方法がわかります。詳細については、 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のいずれかにする必要があります。 、外部キー制約などがありますが、上記で正しいアイデアが得られ、開始するのに十分な情報が得られることを願っています。

于 2013-08-20T05:11:04.260 に答える
2

MySQL はリレーショナル データベースです。これは、思いつくソリューションには、主キー外部キー、および正規化を含める必要があることを意味します。これは、何をすべきかを示す簡単なチュートリアルです。楽しむ!

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

于 2013-08-20T05:03:57.760 に答える
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

これは生の形式の正規化されたスキーマです。必要に応じて変更し、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 に答える