2017-02-27 171 views
0

我正在为一个网站的通知系统工作,这里是基本的想法; 我有一个表Notifications其中我有列如何设计一个通知系统表的一对多数据库关系?

notif_msgnotif_urlreceiver_s

其中receiver_s将具有多个逗号分隔值。但是,我知道这不是个好主意,并且建议有一个不同的表来存储接收者。按照指定here创建一对多关系。然而,我很困惑,因为我不是MySQL的专家。 这里是我的创建语句如何看起来像上面的链接的例子;

create table `notifications` (`id` int unsigned not null auto_increment,`notif_msg` varchar(500) not null,`notif_url` varchar(100) not null, primary key(`id`)); 
create table `notifications_target` (`id` int unsigned not null auto_increment, `notification_id` int unsigned not null, `receiver` varchar(25) not null, index pn_user_index(`notification_id`), foreign key (`notification_id`) references notifications(`id`) on delete cascade,primary key(`id`)); 

我需要知道几件事情;

  1. 什么功能请问index服务在那里?
  2. 从这里,我怎么插入notifications表和多个接收器价值为notif_msgnotif_urlreceiversnotification_targetnotifications表引用一个特定的行?
  3. 如何从notifications表及其对应的表中的receiver表中返回值?

这里就是我打算让与特定的用户,并在同一时间的所有用户面前现在notifications;

选择notif_msgnotif_url FROM notifications WHERE receiver_s = somename和WHERE receiver_s =”'

我怎么做到这一点,现在我有接收器不同的表?

回答

1

1.索引在那里有什么功能?

阅读下面的有关索引的详细信息在MySQL https://dev.mysql.com/doc/refman/5.7/en/optimization-indexes.html

  • 从这里,我如何插入值到notif_msg,notif_url在通知表和多个接收器到接收器列 notification_target引用通知 表中的特定行?

  • 如何从notification_target表中的通知表及其相应的接收方返回值?

  • 当U创建一个一对多的映射,这是你正在寻找的结构: One to many

    所以基本上你id是帮助你在另一个表作为notification_id

    行地图

    所以回答你的问题:

    1. 当您插入第二个表行,确保他们的notification_id(也就是外键)是一样的,在主表

    2. 当您选择从第二个表行id,上notification_id如果你想添加一个条件为给定的id提取记录,或使用join查询来提取结果。


    对于您的具体情况:指http://sqlfiddle.com/#!9/47d29/4

    你的表:

    create table `notifications` (`id` int unsigned not null auto_increment,`notif_msg` varchar(500) not null,`notif_url` varchar(100) not null, primary key(`id`)); 
    create table `notifications_target` (`id` int unsigned not null auto_increment, `notification_id` int unsigned not null, `receiver` varchar(25) not null, index pn_user_index(`notification_id`), foreign key (`notification_id`) references notifications(`id`) on delete cascade,primary key(`id`)); 
    

    数据:

    insert into notifications values (1, 'Hello', '../welcome.html'); 
    insert into notifications_target(`notification_id`, `receiver`) values (1, 'Jack'); 
    insert into notifications_target(`notification_id`, `receiver`) values (1, 'Marry'); 
    insert into notifications_target(`notification_id`, `receiver`) values (1, 'User'); 
    
    
    insert into notifications values (2, 'Good bye', '../logout.html'); 
    insert into notifications_target(`notification_id`, `receiver`) values (2, 'Jack'); 
    

    注意:notification_idnotifications_target表与notifications表中的id相同。

    并获取记录:

    将获取所有通知所有用户:

    select t1.notif_msg, t1.notif_url, t2.receiver 
    from notifications t1 inner join notifications_target t2 
    on t1.id = t2.notification_id; 
    

    将获取所有的通知,针对特定用户:

    select t1.notif_msg, t1.notif_url, t2.receiver 
    from notifications t1 inner join notifications_target t2 
    on t1.id = t2.notification_id 
    where t2.receiver = 'Jack'; 
    
    +0

    现在即时得到它。我们在通知中插入值并将其保存在notification_id字段中。真棒。 – Victor

    +0

    它使查询更快。看看我在答案中添加的链接,了解它是如何工作的。 –