开发者

One to many database design problem

开发者 https://www.devze.com 2023-03-29 06:14 出处:网络
I am trying to create a database table for my game, and would like to see what is the best approach. I have lots of monsters, each monster has number of attacks, monsters do not necessarily have the

I am trying to create a database table for my game, and would like to see what is the best approach.

I have lots of monsters, each monster has number of attacks, monsters do not necessarily have the sam开发者_如何学Ce attacks.

Each monster has different hp, mp and other stats.

Each monster attack has power and speed and other stats.

Can anyone please help me design this table?

I am using MySQL for my database.


Tables required: MONSTER, MONSTER_ATTACKS.

DROP DATABASE IF EXISTS MONSTER_GAME;
CREATE DATABASE MONSTER_GAME;
USE MONSTER_GAME;
CREATE TABLE MONSTER (
      id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      hp VARCHAR(64),
      mp VARCHAR(64),
      stats1 VARCHAR(64)
    ) TYPE=innodb;

CREATE TABLE MONSTER_ATTACKS (
      id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      monster_id INT,
      power INT,
      speed INT,
      Foreign Key (monster_id) references MONSTER(id)
    ) TYPE=innodb;

The important parts: MONSTER has a primary key of id, MONSTER_ATTACKS has a foreign key of monster_id that refers back to MONSTER's id.


I would recommend having one table for the monsters, for example:

monsters
----------
monster_id
monster_hp
monster_mp

Then have an other table with the monster attacks, which is linked to the previous table by monster_id:

attacks
----------
attack_id
attack_monster #This field links to the monster_id field of other table
attack_power
attack_speed

Of course, you'll probably have more fields in there, but you get the idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号