2017-02-27 173 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

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