删除主行和所有子级 mysql 和 php

发布于 2024-09-11 05:43:38 字数 1836 浏览 6 评论 0原文

我继承了一个 PHP 项目,客户想要向他们的 CMS 添加一些功能,基本上 CMS 允许他们创建一些新闻,所有新闻都以相同的内容开头,并且保存在一个表中,实际上是新闻标题文章保存在另一个表中,新闻的图像保存在另一个表中,基本上,如果删除新闻的基行,我需要删除所有相关行,数据库未设置为使用外键,所以我无法使用级联删除,那么当我只知道基础新闻行的ID时,如何删除我需要的所有内容?

任何帮助都会非常有帮助,很抱歉,我无法为您提供更多帮助,这是原始的 SQL 表方案,如果有帮助的话?

    --
-- Table structure for table `mailers`
--

CREATE TABLE IF NOT EXISTS `mailers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `mailer_title` varchar(150) NOT NULL,
  `mailer_header` varchar(60) NOT NULL,
  `mailer_type` enum('single','multi') NOT NULL,
  `introduction` varchar(80) NOT NULL,
  `status` enum('live','dead','draft') NOT NULL,
  `flag` enum('sent','unsent') NOT NULL,
  `date_mailer_created` int(11) NOT NULL,
  `date_mailer_updated` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

-- --------------------------------------------------------

--
-- Table structure for table `mailer_content`
--

CREATE TABLE IF NOT EXISTS `mailer_content` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `headline` varchar(60) NOT NULL,
  `content` text NOT NULL,
  `mailer_id` int(11) NOT NULL,
  `position` enum('left','right','centre') DEFAULT NULL,
  `created_at` int(10) NOT NULL,
  `updated_at` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

-- --------------------------------------------------------

--
-- Table structure for table `mailer_images`
--

CREATE TABLE IF NOT EXISTS `mailer_images` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(150) NOT NULL,
  `filename` varchar(150) NOT NULL,
  `mailer_id` int(11) NOT NULL,
  `content_id` int(11) DEFAULT NULL,
  `date_created` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

值得注意的是,模式不能更改,也不能将数据库更改为 MYISAM,以便我可以使用外键。

I have inherited a PHP project and the client is wanting to add some functionality to their CMS, basically the CMS allows them to create some news, all the news starts with the same content, and that is saved in one table, the actually news headline and articles are saved in another table, and the images for the news are saved in another, basically if the base row for the news is deleted I need all the related rows to be deleted, the database is not setup to work with foreign keys so I cannot use cascade deletion, so how can I delete the all the content I need to, when I only what the ID of the base news row is?

Any help would be very helpful I am sorry I cannot give you much more help, here is this the original SQL of tables scheme if that helps?

    --
-- Table structure for table `mailers`
--

CREATE TABLE IF NOT EXISTS `mailers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `mailer_title` varchar(150) NOT NULL,
  `mailer_header` varchar(60) NOT NULL,
  `mailer_type` enum('single','multi') NOT NULL,
  `introduction` varchar(80) NOT NULL,
  `status` enum('live','dead','draft') NOT NULL,
  `flag` enum('sent','unsent') NOT NULL,
  `date_mailer_created` int(11) NOT NULL,
  `date_mailer_updated` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;

-- --------------------------------------------------------

--
-- Table structure for table `mailer_content`
--

CREATE TABLE IF NOT EXISTS `mailer_content` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `headline` varchar(60) NOT NULL,
  `content` text NOT NULL,
  `mailer_id` int(11) NOT NULL,
  `position` enum('left','right','centre') DEFAULT NULL,
  `created_at` int(10) NOT NULL,
  `updated_at` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

-- --------------------------------------------------------

--
-- Table structure for table `mailer_images`
--

CREATE TABLE IF NOT EXISTS `mailer_images` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(150) NOT NULL,
  `filename` varchar(150) NOT NULL,
  `mailer_id` int(11) NOT NULL,
  `content_id` int(11) DEFAULT NULL,
  `date_created` int(10) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

It is worth noting that the schema cannot be changed nor can I change to the DB to MYISAM so that I can use foreign keys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

メ斷腸人バ 2024-09-18 05:43:38

将外键添加到表 mailer_content

FOREIGN KEY (mailer_id)
REFERENCES mailers(id)
ON DELETE CASCADE

将外键添加到表 mailer_images

FOREIGN KEY (content_id)
REFERENCES mailer_content(id)
ON DELETE CASCADE

http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

Add foreign key to table mailer_content

FOREIGN KEY (mailer_id)
REFERENCES mailers(id)
ON DELETE CASCADE

Add foreign key to table mailer_images

FOREIGN KEY (content_id)
REFERENCES mailer_content(id)
ON DELETE CASCADE

http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

白昼 2024-09-18 05:43:38

值得注意的是,架构无法更改,也无法将数据库更改为 MYISAM,以便我可以使用外键。

为什么架构不能改变?该应用程序是您设计的,不是吗?即使您没有这样做,添加正确的键也只需添加正确的索引,然后更改正确的列即可。 @Michael Pakhantosv 的答案看起来是 SQL 的正确部分。

此外,是 InnoDB 处理外键,而不是 MyISAM。你在那里已经很好了。

如果您可以更改架构,则使适当的ID成为实际的、真正的外键并使用ON DELETE CASCADE就可以了。或者也许是触发器。但这只是要求而已。

现在,由于某种原因,ON DELETE CASCADE 在这里不太受欢迎。我不同意其他人不喜欢它的理由,但我不同意他们的观点。除非您的应用程序被设计为grok ON DELETE CASCADE,否则您将陷入困境。

但是,鉴于您的要求...

基本上,如果新闻的基行被删除,我需要删除所有相关行

...这就是要求 ON DELETE CASCADE。

因此,这可能会让人感到震惊,但如果您无法修改数据库,则只需在代码中完成工作即可。我想删除一篇新闻文章只发生在代码中的一个地方,对吧?如果没有的话,那就更好了。首先解决这个问题。然后确保您以适当的顺序删除所有适当的行。然后记录下来!

It is worth noting that the schema cannot be changed nor can I change to the DB to MYISAM so that I can use foreign keys.

Why can't the schema be changed? You designed the app, didn't you? Even if you didn't, adding the proper keys is just a matter of adding the right indexes and then altering the right columns. @Michael Pakhantosv's answer has what looks to be the right bits of SQL.

Further, it's InnoDB that does foreign keys, not MyISAM. You're fine there already.

If you could change the schema, making the appropriate IDs actual, real Foreign Keys and using ON DELETE CASCADE would work. Or maybe triggers. But that's just asking for it.

Now, for some reason, ON DELETE CASCADE isn't liked very much around here. I disagree with other people's reasons for not liking it, but I don't disagree with their sentiment. Unless your application was designed to grok ON DELETE CASCADE, you're in for a world of trouble.

But, given your requirement...

basically if the base row for the news is deleted I need all the related rows to be deleted

... that's asking for ON DELETE CASCADE.

So, this might come as a shock, but if you can't modify the database, you'll just have to do your work in the code. I'd imagine that deleting a news article happens in only one place in your code, right? If not, it'd better. Fix that first. Then just make sure you delete all the proper rows in an appropriate order. And then document it!

野侃 2024-09-18 05:43:38

如果您无法更改架构,则 触发器 不是一个选项。

InnoDB支持事务,因此从两个中删除表不应该是问题,你的问题到底是什么?

PS 值得注意的是您使用的是哪个版本的服务器。

If you can not change the schema then triggers are not an option.

InnoDB supports transactions, so deleting from two tables should not be an issue, what exactly is your problem?

P.S. It would be worth noting which version of the server are you using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文