Jon Snow
Jon Snow

Reputation: 713

MySQL implementing tables into database

I have to implement the tasks below: Task: At present, the database knows two types of messages:

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:

All this must again be achieved with minimal amount of storage, i.e., you must choose the appropriate data types from the MySQL manual. You may assume that the total number of messages that are added over time may reach 1,000,000,000. Your job is to implement the necessary changes and additional table for this purpose and any keys and foreign key relationships required.

Here are my two tables first : User

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

2nd table :Message

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

MY SOLUTION: I was thinking of creating a new table called 'Message_level' and have columns 'message_level_id'(will refer to 1,2,3 as 1=public, 2=private, 3=specific) & 'message_level'(where it would state public,private and specific next to level). Then I can use the 'Message_level' as a foreign key into the 'Message' table and replace the 'is_public' column with 'message_level_id'.

Is my approach to this question right? is there another way I can do this to make it more efficient?

and how would I approach the second task of specifying who the recipients of a particular message are?

Upvotes: 0

Views: 161

Answers (1)

Nathan
Nathan

Reputation: 2775

I would go like this:

User: user_id, given_name, ...

Message: message_id, owner_id (fk User), subject, body, posted, message_type_id (fk Message_type)...

Message_recipients: user_id (fk User), message_id (fk Message)

Message_type: message_type_id, description (1:public, 2:friends, 3:specific_recipients)

Upvotes: 1

Related Questions