将数据库规范化为3NF

时间:2016-04-22 11:21:36

标签: mysql sql database-normalization 3nf

我正在创建一个数据库,在3NF中至少有5个表。从我可以收集到的数据库是3NF当我查看3NF daabase的规格,但我不是100%肯定这就是为什么我在这里问,如果不是我想知道如何使它成为3NF ,所以我可以继续尝试在数据库上测试查询,触发器和程序。

我一直把我的工作基于this示例数据库,我认为这是我理解的3NF。

任何帮助或建议都非常感谢,因为我已经有几天这个问题。

CREATE TABLE IF NOT EXISTS xbox (
  game_id char(4) NOT NULL default '',
  game_name varchar(32) default NULL,
  developer varchar(32) default NULL,
  PRIMARY KEY  (game_id)
);

INSERT INTO xbox (game_id, game_name, developer) VALUES
('1001', 'Sunset Overdive', 'Insomniac Games'),
('1002', 'Quantum Break', 'Remedy Entertainment'),
('1003', 'Gears of War 4', 'The Coalition'),
('1004', 'Halo 5', '343 Industries'),
('1005', 'Forza 6', 'Turn10 Studios');


CREATE TABLE IF NOT EXISTS playstation (
  game_id char(4) NOT NULL default '',
  game_name varchar(32) default NULL,
  developer varchar(32) default NULL,
  PRIMARY KEY  (game_id)
);

INSERT INTO playstation (game_id, game_name, developer) VALUES
('2001', 'Bloodborne', 'From Software'),
('2002', 'The Witness', 'Thekla'),
('2003', 'Destiny', 'Bungie'),
('2004', 'Black Ops 3', 'Treyarch'),
('2005', 'DriveClub', 'Evolution');


CREATE TABLE IF NOT EXISTS pc (
  game_id char(4) NOT NULL default '',
  game_name varchar(32) default NULL,
  developer varchar(32) default NULL,
  PRIMARY KEY  (game_id)
);

INSERT INTO pc (game_id, game_name, developer) VALUES
('3001', 'Fallout 4', 'Bethesda'),
('3002', 'Tomb Raider', 'Crystal Dynamics'),
('3003', 'Overwatch', 'Blizzard'),
('3004', 'Rocket League', 'Psyonix'),
('3005', 'DiRT Rally', 'Codemasters');


CREATE TABLE IF NOT EXISTS nintendo (
  game_id char(4) NOT NULL default '',
  game_name varchar(32) default NULL,
  developer varchar(32) default NULL,
  PRIMARY KEY  (game_id)
);

INSERT INTO nintendo (game_id, game_name, developer) VALUES
('4001', 'Mario Kart 8', 'Nintendo'),
('4002', 'Mario Maker', 'Nintendo'),
('4003', 'Mario Party 9', 'Nintendo'),
('4004', 'Bayonetta 2', 'Nintendo'),
('4005', 'Splatoon', 'Nintendo');


CREATE TABLE IF NOT EXISTS sales (
  customer_id char(10) NOT NULL default '',
  game_id char(4) NOT NULL default '',
  quantity int(11) default NULL,
  paid double default NULL,
  payment varchar(32) default NULL,
  PRIMARY KEY  (game_id)
);

INSERT INTO sales (customer_id, game_id, quantity, paid, payment) VALUES
('1234567890', '1002', 1, 79.99, 'Debit'),
('0987654321', '2003', 1, 50, 'Credit'),
('1122334455', '2001', 2, 120, 'Cash'),
('6677889900', '4004', 3, 110.49, 'Cash'),
('1357924680', '3002', 1, 50, 'Credit');


CREATE TABLE IF NOT EXISTS products (
  game_id char(10) default NULL,
  platform varchar(32) default NULL,
  stock int(11) default NULL,
  price double default NULL,
  PRIMARY KEY  (game_id)
);

INSERT INTO products (game_id, platform, stock, price) VALUES
('1001', 'xbox', 33, 69.99),
('1002', 'xbox', 42, 79.99),
('1003', 'xbox', 50, 89.99),
('1004', 'xbox', 80, 59.99),
('1005', 'xbox', 10, 49.99),
('2001', 'playstation', 12, 55.99),
('2002', 'playstation', 44, 34.99),
('2003', 'playstation', 89, 45.99),
('2004', 'playstation', 65, 39.99),
('2005', 'playstation', 73, 19.99),
('3001', 'pc', 50, 69.99),
('3002', 'pc', 41, 49.99),
('3003', 'pc', 77, 59.99),
('3004', 'pc', 24, 19.99),
('3005', 'pc', 19, 24.99),
('4001', 'nintendo', 10, 19.99),
('4002', 'nintendo', 17, 39.99),
('4003', 'nintendo', 33, 28.99),
('4004', 'nintendo', 53, 34.99),
('4005', 'nintendo', 97, 15.99);

1 个答案:

答案 0 :(得分:0)

由于你的xbox,pc,nintendo和playstation表都完全相同,你可能需要考虑将它们合并为一个并添加某种类型的"类型"专栏(或者,正如Prix所提到的,如果游戏可以在多个平台上运行,平台和类型之间可能有一对多)。

使用当前设计,您将开始遇到任何非特定类型的查询的问题。如果我正确地关注了您的示例,您将不得不使用products.platform列的值来确定哪个平台表包含您要查找的详细信息。这导致一次一行的处理,这是非常低效的。