0

我设计了移动电话和关税的数据库表。

它应该支持多个移动网络、销售类型和相关联属网络。

积分(针对运营商)因所选电话、资费、网络和销售类型而异。

示例价格计划:

T-Mobile消费者升级

  • 三星Galaxy S3
  • 关税:Super One(每月费用:12.00 美元)
  • 此销售会员与“零售商一号”
  • 总分:3.3

T-Mobile :消费者新连接

  • 三星Galaxy S3
  • 关税:Super One(每月费用:35.00 美元)
  • 此销售附属于“零售商二”
  • 总分:7.3

AT&T:“Sim Only Deal”(无电话)

  • 关税:X-Deal 2(每月费用:18.00 美元)
  • 此销售会员与“零售商一号”
  • 总分:10.0

请参阅下面的表格设计,这是应该怎么做的?或者如何改进?

数据库设计:

CREATE TABLE IF NOT EXISTS `affiliate` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

INSERT INTO `affiliate` (`id`, `name`) VALUES
(1, 'Retailer One'),
(2, 'Retailer Two');

CREATE TABLE IF NOT EXISTS `network` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

INSERT INTO `network` (`id`, `name`) VALUES
(1, 'T-Mobile'),
(2, 'AT&T');

CREATE TABLE IF NOT EXISTS `network_saletype` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `network_id` int(11) NOT NULL,
  `saletype_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


INSERT INTO `network_saletype` (`id`, `network_id`, `saletype_id`) VALUES
(1, 1, 1),
(2, 2, 3);

CREATE TABLE IF NOT EXISTS `phone` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `description` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


INSERT INTO `phone` (`id`, `name`, `description`) VALUES
(1, 'Samsung Galaxy S3', ' Quad-core, NFC, AMOLED'),
(2, 'Apple iPhone 4S', 'A5 chip, 8MP camera, Siri voice');

CREATE TABLE IF NOT EXISTS `phone_points` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `phone_id` int(11) NOT NULL,
  `tarrif_id` int(11) NOT NULL,
  `affilicate_id` int(11) NOT NULL,
  `point` float NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

INSERT INTO `phone_points` (`id`, `phone_id`, `tarrif_id`, `affilicate_id`, `point`) VALUES
(1, 1, 1, 1, 3.3),
(2, 1, 2, 2, 7.3);

CREATE TABLE IF NOT EXISTS `saletype` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

INSERT INTO `saletype` (`id`, `name`) VALUES
(1, 'Consumer Upgrade'),
(2, 'Consumer New Connection'),
(3, 'Sim-Only Deal');


CREATE TABLE IF NOT EXISTS `tariff_point` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `tarrif_id` int(11) NOT NULL,
  `affilicate_id` int(11) NOT NULL,
  `point` float NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

INSERT INTO `tariff_point` (`id`, `tarrif_id`, `affilicate_id`, `point`) VALUES
(3, 3, 1, 10);

CREATE TABLE IF NOT EXISTS `tarrif` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `network_saletype_id` int(11) NOT NULL,
  `name` varchar(50) NOT NULL,
  `tariff_duration` int(11) NOT NULL,
  `monthly_cost` decimal(6,2) NOT NULL,
  `description` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

INSERT INTO `tarrif` (`id`, `network_saletype_id`, `name`, `tariff_duration`, `monthly_cost`, `description`) VALUES
(1, 1, 'Super One', 12, '12.00', '200 Mins, 400 Texts, 500MB Internet'),
(2, 1, 'Super One', 12, '35.00', '200 Mins, 400 Texts, 500MB Internet'),
(3, 2, 'X-Deal Two', 12, '18.00', '');

结果

mysql> select * from network;
+----+----------+
| id | name     |
+----+----------+
|  1 | T-Mobile |
|  2 | AT&T     |
+----+----------+

mysql> select * from affiliate;
+----+--------------+
| id | name         |
+----+--------------+
|  1 | Retailer One |
|  2 | Retailer Two |
+----+--------------+

mysql> select * from saletype;
+----+-------------------------+
| id | name                    |
+----+-------------------------+
|  1 | Consumer Upgrade        |
|  2 | Consumer New Connection |
|  3 | Sim-Only Deal           |
+----+-------------------------+

mysql> select * from network_saletype;
+----+------------+-------------+
| id | network_id | saletype_id |
+----+------------+-------------+
|  1 |          1 |           1 |
|  2 |          2 |           3 |
+----+------------+-------------+


  mysql> select * from tarrif;
+----+---------------------+------------+-----------------+--------------+-------------------------------------+
| id | network_saletype_id | name       | tariff_duration | monthly_cost | description                         |
+----+---------------------+------------+-----------------+--------------+-------------------------------------+
|  1 |                   1 | Super One  |              12 |        12.00 | 200 Mins, 400 Texts, 500MB Internet |
|  2 |                   1 | Super One  |              12 |        35.00 | 200 Mins, 400 Texts, 500MB Internet |
|  3 |                   2 | X-Deal Two |              12 |        18.00 |                                     |
+----+---------------------+------------+-----------------+--------------+-------------------------------------+

mysql> select * from  tariff_point;
+----+-----------+---------------+-------+
| id | tarrif_id | affilicate_id | point |
+----+-----------+---------------+-------+
|  3 |         3 |             1 |    10 |
+----+-----------+---------------+-------+

mysql> select * from  phone;
+----+-------------------+---------------------------------+
| id | name              | description                     |
+----+-------------------+---------------------------------+
|  1 | Samsung Galaxy S3 |  Quad-core, NFC, AMOLED         |
|  2 | Apple iPhone 4S   | A5 chip, 8MP camera, Siri voice |
+----+-------------------+---------------------------------+

mysql> select * from phone_points;
+----+----------+-----------+---------------+-------+
| id | phone_id | tarrif_id | affilicate_id | point |
+----+----------+-----------+---------------+-------+
|  1 |        1 |         1 |             1 |   3.3 |
|  2 |        1 |         2 |             2 |   7.3 |
+----+----------+-----------+---------------+-------+

编辑:我的问题是电话、收费、网络和销售类型之间的关系设计可以吗?基本上运营商可以选择一个零售商,然后选择移动网络..然后选择什么类型的销售(例如:消费者升级),然后选择具有相关关税的手机。根据他们选择的关税和/或电话 - 运营商获得积分。

4

1 回答 1

2

此表暗示 2 个或 5 个或 1,847 个关联公司可以使用相同的名称。

CREATE TABLE IF NOT EXISTS `affiliate` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

这些插入将起作用。他们可能不应该。

INSERT INTO `affiliate` (`id`, `name`) VALUES
(1, 'Retailer One'),
(2, 'Retailer One'),
(3, 'Retailer One'),
(4, 'Retailer One'),
(5, 'Retailer One');

问题是,虽然您已经定义了一个无意义的(代理)整数作为主键,但您还没有定义那个东西. 除了代理 ID 号之外,所有这些表都应该对某些列组合具有唯一约束。

您还根本没有定义外键。这几乎可以肯定是一个错误。

当像这样的插入语句成功时,你就有工作要做了。

insert into phone_points values (1078, 3000, 2743, 10234, -33344.03);
于 2012-09-17T18:42:41.223 回答