0

I got a simple message system where every message has one sender and always exact one receiver which is never the sender. So my design is like follow:

create table user { PersonID int, Name varchar(255) } create table message { MessageID int, FromPersonID int, ToPersonID int, Message varchar(160) } 

To get all messages of a given PersonID I write:

SELECT MessageID FROM message WHERE PersonID=FromPersonID OR PersonID=ToPersonID 

Now I got two question:

  1. Is this the proper (and fasted) way to design that relation.
  2. How is this relation described in a Database Diagram?
1
  • Seems as good a way as any. You might consider naming your user table person. Commented Apr 2, 2015 at 9:36

2 Answers 2

1

Yup, that's pretty much the textbook way to do it.

Not sure what you mean by "how is it described in a diagram". In a diagram you would draw two boxes, one for each table. Then there would be two lines connecting User and Message, one labeled "from" and the other labeled "to". The exact shape of the boxes and appearance of the lines depends on what diagramming convention you are using.

Sign up to request clarification or add additional context in comments.

Comments

0

You can normalize it according to your query.

for the query

SELECT MessageID FROM message WHERE PersonID=FromPersonID OR PersonID=ToPersonID 

you can create a normalized structure

create table user { PersonID int, Name varchar(255) } create table message_meta { FromPersonID int, ToPersonID int, } create table message_data { MessageID int, Message varchar(160) } 

and fire a query like

SELECT MessageID FROM message_meta WHERE PersonID=FromPersonID OR PersonID=ToPersonID 

This will be more efficient. TC

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.