0

我目前有3 个主表 animal, food and medicine

从动物表我得到表:

  1. 物种(猫、狗、鸟、鱼……)
  2. 尺寸(小,中,大,大...)
  3. 年龄(小狗,年轻成人,老......)
  4. 颜色(棕色、黑色、灰色……)

我想存储给动物提供的药物和食物的有用数据但是我不知道如何链接这些数据,以下方法是否可以接受或者我应该添加或删除什么?

我的主要问题是复合键的正确性以及在查询中检索数据的方式......

动物

idAn SEX AGE COMMENT           SPECIES color  HAIR   SIZE
----------------------------------------------------------
1     M   1  without ear         1      1     LONG    1
2     F   2  blue eyed all gray  2      2     short   1

物种

id name
-------
1  dog
2  cat
3  bird
4  fish
5  reptile
6  mouse  
7  other

年龄

id name
-------
1 puppy
2 young
3 adult
4 old

颜色

id  name
--------
1  black
2  gray
3  gold
4  green
5  red
6  brown

尺寸

id  name
--------
1  small
2  medium
3  large
4  big

食物

id name      label      
-------------------
1  sardine     so 
2  croquettes  dogchauw   
3  chicken     sirw
4  whiskas     whiskas  

food_Animal

idFood  idAnimal   quantity timesPerDay lastFood    LastWater
----------------------------------------------------------------------
2             1    70gr      3        12-12-12   12-12-12  
3             2    80gr      4        12-11-12   12-11-12

和上面类似的药物。

可以做什么或如何在 MySQL 中使用它

我从类似的东西开始

CREATE TABLE IF NOT EXISTS ANIMAL(
    idAn    int(3) NOT NULL AUTO_INCREMENT,
    sex     int(2) NOT NULL ,
    age     int(2) NOT NULL ,
    comment varchar(50)  ,                  
        species int(2) NOT NULL , 
    color   int(2) NOT NULL ,
        hair    varchar(50)     ,                   
        size    int(2) NOT NULL ,
    PRIMARY KEY (idAn)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;
4

1 回答 1

0
/* Create a table with 2 fields, Entity and Name. Primary key is BOTH fields (as we know that Entity->Name will be a unique value) */
CREATE TABLE master_lookup (
  Entity nvarchar(10) not null,
  Name nvarchar(20) not null,
  primary key(Entity, Name)
);

/* Insert some data! */
INSERT INTO master_lookup VALUES 
('Species', 'Dog'),
('Species', 'Cat'), 
('Species', 'Bird'),
('Color', 'Black'),
('Color', 'Brown');

/* Let's have a look... */
SELECT * FROM master_lookup;
 Entity  | Name
----------------
 Species | Dog
 Species | Cat
 Species | Bird
 Color   | Black
 Color   | Brown

/* Et voila. One lookup table, ID numbers need not apply. Let's assume you have a form that will populate the Animal table with a list of species for the user to pick from. The source of the list is easy: */
SELECT Name FROM master_lookup WHERE Entity = 'Species';

/* Your INSERT query into Animal will look like this (let's say they're entering a small brown long-haired dog): */
INSERT INTO Animal ('M', 11, 'a comment', 'Dog', 'Brown', 'Long', 'Small');

/* And now when you SELECT from the animal table... */
SELECT * FROM Animal

idAn SEX AGE COMMENT           SPECIES color  HAIR   SIZE
----------------------------------------------------------
1     M   11  a comment         Dog    Brown  Long   Small

/* Look ma! No joins! Which means your query is much simpler and much faster! Yay! */
于 2014-11-25T05:00:56.873 回答