数据库架构,用于存储发送的消息和收到的消息给同一个人

时间:2013-03-10 16:47:15

标签: database postgresql relational-database

我正在尝试提供一个用于存储来自SMS应用程序的消息的架构。它需要存储从发件人发送的邮件和发送给该发件人的邮件。如果有人能告诉我最好的方法,我们非常感谢。我正在使用Postgresql,如果这有任何区别。

2 个答案:

答案 0 :(得分:2)

以下是你可以做的事情:

表联系人:

CREATE TABLE contact
(
  contact_id bigserial NOT NULL,
  contact_name character varying,
  contact_phone_number character varying(40),
  CONSTRAINT contact_pkey PRIMARY KEY (contact_id)
)
WITH (
  OIDS=FALSE
);

和短信表:

CREATE TABLE sms
(
  msg_id bigint NOT NULL DEFAULT nextval('message_msg_id_seq'::regclass),
  sender_id bigint,
  receiver_id bigint,
  msg_content text,
  CONSTRAINT message_pkey PRIMARY KEY (msg_id),
  CONSTRAINT sms_receiver_id_fkey FOREIGN KEY (receiver_id)
      REFERENCES contact (contact_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT sms_sender_id_fkey FOREIGN KEY (sender_id)
      REFERENCES contact (contact_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
    )
WITH (
  OIDS=FALSE
);

答案 1 :(得分:1)

你可以用两张表来做:

用户表:id, name, {other info about your user}

消息表:id, sender, receiver, message, {other info about message}