2

Task:

At present, the database knows two types of messages:

  1. Messages that a user posts and that are public for anyone and everyone to read
  2. Messages that a user posts and that are non-public.

These messages can only be read by users that the posting user has marked as friends. In this step, you should add a third type of message. This third type of message should be readable by specified recipients only.

This means the database needs to provide the following:

  • A way of distinguishing between the three types of messages. This involves a change to the Message table.
  • A way of specifying who the recipients of a particular message are. This will probably require an additional table.

Your job is to implement the necessary changes and additional table for this purpose and any keys and foreign key relationships required.

here are two existing tables witch relate to the task(copies from my db).

User table

CREATE TABLE IF NOT EXISTS `User` (
  `user_id` int(10) unsigned NOT NULL auto_increment,
  `given_name` varchar(60) default NULL,
  `surname` varchar(60) default NULL,
  `address` varchar(255) default NULL,
  `city_id` int(10) unsigned NOT NULL,
  `date_of_birth` datetime default NULL,
  `email` varchar(80) default NULL,
  PRIMARY KEY  (`user_id`),
  KEY `ix_user_surname` (`surname`),
  KEY `ix_user_given_name` (`given_name`),
  KEY `ix_user_name` (`given_name`,`surname`),
  KEY `ix_user_date_of_birth` (`date_of_birth`),
  KEY `ix_user_email` (`email`),
  KEY `ix_user_city_id` (`city_id`)
) ENGINE=InnoDB

Message table

CREATE TABLE IF NOT EXISTS `Message` (
  `message_id` int(10) unsigned NOT NULL auto_increment,
  `owner_id` int(10) unsigned default NULL,
  `subject` varchar(255) default NULL,
  `body` text,
  `posted` datetime default NULL,
  `is_public` tinyint(4) default '0',
  PRIMARY KEY  (`message_id`),
  KEY `ix_message_owner_id` (`owner_id`)
) ENGINE=InnoDB
4

1 回答 1

1

好的,所以 is_public 使您能够区分两种类型(例如 is_public = '0' 表示私有,is_public = '1' 表示公共)。但是现在您有了指定收据的新概念,因此是/否模型将不再适用,因为您有 3 种类型。通常在这种情况下,您可以切换到标志或类型列。

因此,也许可以创建一个 message_type 列,它是“PUBLIC”、“PRIVATE”、“SPECIFIED”或类似内容之一。

之后,听起来您至少还需要两张桌子。用户必须能够指定朋友,并且用户必须能够指定用户接收特定消息。

于 2012-08-07T14:30:22.907 回答