我正在爲一個網站的通知系統工作,這裏是基本的想法; 我有一個表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`));
我需要知道幾件事情;
- 什麼功能請問
index
服務在那裏? - 從這裏,我怎麼插入
notifications
表和多個接收器價值爲notif_msg
,notif_url
到receivers
列notification_target
在notifications
表引用一個特定的行? - 如何從
notifications
表及其對應的表中的receiver
表中返回值?
這裏就是我打算讓與特定的用戶,並在同一時間的所有用戶面前現在notifications
;
選擇
notif_msg
,notif_url
FROMnotifications
WHERE receiver_s = somename和WHERE receiver_s =」'
我怎麼做到這一點,現在我有接收器不同的表?
現在即時得到它。我們在通知中插入值並將其保存在notification_id字段中。真棒。 – Victor
它使查詢更快。看看我在答案中添加的鏈接,瞭解它是如何工作的。 –