gpt4 book ai didi

php - 如何跟踪不同表中的 ID(主要)?

转载 作者:行者123 更新时间:2023-11-30 22:57:33 25 4
gpt4 key购买 nike

我将从一个例子开始,这样我想问的问题就很清楚了。

Templates
template_id, Name
1, Light Blue
2, Red
3, Sky Blue
4, Yellow

template_id 是主要的并且自动递增。

Products
product_id, product_name, template_id

.

Users
user_id, user_name, template_id

.

Categories
category_id, category_name, template_id

有 6 个以上的表引用了 template_id。

所有这些表中的默认 template_id 都是(这意味着没有分配模板)

我需要一种方法将这些引用存储在某处,这样如果我尝试删除模板,我的 php 应用程序将检查它是否在使用中。我知道这是一种外键概念,但我不确定外键列中是否可以有零值,所以我感觉有点卡住了。您对解决此问题有何建议?如何跟踪模板 ID 在不同表中的使用位置。

更新

我尝试按照建议添加外键...

ALTER TABLE `Templates`  
ADD CONSTRAINT `Templates_ibfk_1`
FOREIGN KEY (`template_id`) REFERENCES `Products` (`template_id`),
ADD CONSTRAINT `Templates_ibfk_2`
FOREIGN KEY (`template_id`) REFERENCES `Users` (`template_id`)

我在哪里可以查看这些是否已添加?我尝试使用 PhpMyAdmin 查看模板表的结构,但在其中找不到任何提及外键的内容??

最佳答案

在Mysql中使用Reference View ,这将有助于您将来自动管理/跟踪记录。

  ALTER TABLE `Templates`  ADD CONSTRAINT `Templates_ibfk_1` FOREIGN KEY (`template_id`) REFERENCES `Products` (`template_id`),ADD CONSTRAINT `Templates_ibfk_2` FOREIGN KEY (`template_id`) REFERENCES `Users` (`template_id`)

要了解更多详细信息,请阅读此 Link

关于php - 如何跟踪不同表中的 ID(主要)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25606594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com