Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 数据库架构,用于存储发送给同一个人的消息和接收到的消息_Database_Postgresql_Relational Database - Fatal编程技术网

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

Database 数据库架构,用于存储发送给同一个人的消息和接收到的消息,database,postgresql,relational-database,Database,Postgresql,Relational Database,我正试图想出一个模式来存储来自SMS应用程序的消息。它需要存储发件人发送的邮件和发送给该发件人的邮件。如果有人能告诉我最好的方法,我将不胜感激。如果有什么不同的话,我正在使用Postgresql。您可以使用两个表: 用户表:id,name,{关于用户的其他信息} 消息表:id、发送者、接收者、消息、{关于消息的其他信息}您可以使用两个表来完成此操作: 用户表:id,name,{关于用户的其他信息} 消息表:id、发送者、接收者、消息、{关于消息的其他信息}以下是您可以执行的操作: 表格联系人:

我正试图想出一个模式来存储来自SMS应用程序的消息。它需要存储发件人发送的邮件和发送给该发件人的邮件。如果有人能告诉我最好的方法,我将不胜感激。如果有什么不同的话,我正在使用Postgresql。

您可以使用两个表:

用户表:
id,name,{关于用户的其他信息}


消息表:
id、发送者、接收者、消息、{关于消息的其他信息}

您可以使用两个表来完成此操作:

用户表:
id,name,{关于用户的其他信息}


消息表:
id、发送者、接收者、消息、{关于消息的其他信息}

以下是您可以执行的操作:

表格联系人:

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
);
和sms表:

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
);

以下是您可以轻松做到的:

表格联系人:

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
);
和sms表:

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
);

收件人、发件人、信息
以及您需要的其他信息?可能会将索引添加到
以快速查找。@Wrikken将
到的电话号码标准化为;想象不出还有什么是必要的。
到、从、消息
以及任何您需要的?可能会将索引添加到
以快速查找。@Wrikken将
到的电话号码标准化为;无法想象还有什么必要。