简体   繁体   English

删除链接的MySQL表中的行并减少另一个表中的计数

[英]Deleting rows in linked MySQL tables and decrementing count in another table

I am trying to create (java-based) messaging webapp. 我正在尝试创建(基于Java的)消息传递webapp。 The messages are stored using MySQL using three tables: 消息使用三个表存储在MySQL中:

  • 'messages' : has two columns, an id primary key ('idmessage') and the message ('message'). 'messages' :有两列,一个id主键('idmessage')和一个消息('message')。
  • 'tags' : The user also inputs a tag associated with their message. '标签' :用户还输入与其消息关联的标签。 This has two columns - the tag (which is also a primary key) and a count ('count'). 它有两列-标签(也是主键)和计数('count')。
  • 'message_tag_link' : Since there is a many-to-one relationship between messages and tags I have a third link table. 'message_tag_link' :由于消息和标签之间存在多对一的关系,所以我有了第三个链接表。 This has two columns, both foreign keys to the messages ('fk_idmessage') and tags tables' ids ('fk_tag'). 它有两列,既是消息的外键('fk_idmessage'),又是标签表的ID('fk_tag')。

Inserting messages into the database works fine. 将消息插入数据库可以正常工作。 I use these following lines in Java: 我在Java中使用以下几行:

pst1 = connection.prepareStatement("INSERT INTO messages(message) VALUES (?)");
pst2 = connection.prepareStatement("INSERT INTO tags(tag,count) VALUES (?,1) ON     DUPLICATE KEY UPDATE count = count + 1");
pst3 = connection.prepareStatement("INSERT INTO message_tag_link(fk_idmessage,fk_tag) VALUES (? , ?)");

As can be seen 'count' increments in pst2 every time a duplicate tag entry is made. 可以看出,每次输入重复的标签时,pst2中的“ count”都会增加。 I am trying to work out how to fully delete a message. 我正在尝试找出如何完全删除邮件。 I have the on delete cascade description on my foreign keys in my link table's columns but am not sure what to do next. 我在链接表的列中的外键上具有on delete cascade描述,但是不确定下一步该怎么做。 For a given 'messageid' I need to: 对于给定的“ messageid”,我需要:

  1. Delete a row in 'messages' table 删除“邮件”表中的一行
  2. Delete the associated row in the 'message_tag_link' table 删除“ message_tag_link”表中的关联行
  3. Decrement the count value in the 'tags' table when a message deletion is successful, or delete this row entirely if count becomes zero. 消息删除成功后,减小“标签”表中的计数值;如果计数变为零,则完全删除该行。

Is it possible to do all this within MySQL using a Java PreparedStatement ? 是否可以使用Java PreparedStatement在MySQL中完成所有这些操作? Any pointers much appreciated. 任何指针,不胜感激。 I am a total MySQL novice, so please be kind! 我是MySQL的新手,所以请客气!

These queries should do what you need. 这些查询应该可以满足您的需求。 ? in each query should be the message id that is to be deleted 每个查询中的应该是要删除的消息ID

//Update count
UPDATE tags SET `count` = `count` - 1 WHERE tag_id IN (SELECT fk_tag FROM message_tag_link WHERE fk_idMessage = ?);
//Remove link
DELETE FROM message_tag_link WHERE fk_idmessage = ?;
DELETE FROM messages WHERE id = ?;
//Remove unused tags
DELETE FROM tags WHERE `count` = 0;

As you've found out however maintaining a count can be annoying. 如您所知,保持计数可能很烦人。 Better is to use the link itself. 更好的方法是使用链接本身。

SELECT COUNT(fk_idmessage) AS msgcount FROM message_tag_link WHERE fk_tag = ?;

Also you can add a CASCADE so that when messages are deleted the tag link gets deleted too (see here ). 您也可以添加CASCADE,以便在删除消息时也删除标记链接(请参见此处 )。 Be careful with this behavior however). 但是请小心这种行为)。

This solution still leaves tags with no messages but you can remove these using the query: 此解决方案仍然保留没有消息的标签,但是您可以使用查询将其删除:

DELETE FROM tags t WHERE NOT EXISTS (SELECT 1 FROM nessage_tag_link WHERE t.tag_id = fk_tag)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM