MySQL在同一表中添加2个主键,然后在另一个表中引用它们

发布于 2025-02-08 22:56:25 字数 1858 浏览 1 评论 0原文

我有两个表:cunitals_cardcard_infocunitude_card表中我有两个主要键:id> id and cid。在card_info表中,我有1个主键:id。我想将2个外键添加到card_info表中,但是当我尝试引用card_info.cid conderes>

cesultes_card Table

表a href =“ https://i.sstatic.net/h1uvh.png” rel =“ nofollow noreferrer”>

card_info表

CREATE TABLE card_info (
   id INT(11) PRIMARY KEY AUTO_INCREMENT,
   uid INT(11) NOT NULL,
   cid VARCHAR(255) NOT NULL,
   number BIGINT NOT NULL,
   holder VARCHAR(255) NOT NULL,
   type VARCHAR(255) NOT NULL,
   provider VARCHAR(255) NOT NULL,
   FOREIGN KEY (uid)
      REFERENCES customers (id)
      ON DELETE CASCADE,
   FOREIGN KEY (cid)
      REFERENCES customers__card (cid)
      ON DELETE CASCADE
);

以及我尝试创建card_info表的错误

cunitals_card

CREATE TABLE `customers_card` (
 `id` int NOT NULL,
 `uid` int NOT NULL,
 `cid` varchar(255) COLLATE utf8mb4_hungarian_ci NOT NULL,
 `cardname` varchar(255) COLLATE utf8mb4_hungarian_ci NOT NULL,
 `cardnum` int NOT NULL,
 `expiry` date NOT NULL,
 `cvc` int NOT NULL,
 `value` int NOT NULL,
 `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  ADD PRIMARY KEY (`id`,`cid`),
  ADD CONSTRAINT `customers__card_ibfk_1` FOREIGN KEY (`uid` REFERENCES `customers` (`id`) ON DELETE CASCADE
)

我的摘要问题:如何在card_info.cid.cid中创建一个引用到该的外键COUPTERUS_CARD.CID而无需制作customers_card.cid a 主键

I have two tables: customers_card and card_info In the customers_card table I have two primary keys: id and cid. In the card_info table I have 1 primary key: id. I want to add 2 foreign keys to the card_info table but I got an error when I try to reference to the card_info.cid

customers_card table

enter image description here

card_info table

CREATE TABLE card_info (
   id INT(11) PRIMARY KEY AUTO_INCREMENT,
   uid INT(11) NOT NULL,
   cid VARCHAR(255) NOT NULL,
   number BIGINT NOT NULL,
   holder VARCHAR(255) NOT NULL,
   type VARCHAR(255) NOT NULL,
   provider VARCHAR(255) NOT NULL,
   FOREIGN KEY (uid)
      REFERENCES customers (id)
      ON DELETE CASCADE,
   FOREIGN KEY (cid)
      REFERENCES customers__card (cid)
      ON DELETE CASCADE
);

And the error I get when I try to create the card_info table:
enter image description here

customers_card

CREATE TABLE `customers_card` (
 `id` int NOT NULL,
 `uid` int NOT NULL,
 `cid` varchar(255) COLLATE utf8mb4_hungarian_ci NOT NULL,
 `cardname` varchar(255) COLLATE utf8mb4_hungarian_ci NOT NULL,
 `cardnum` int NOT NULL,
 `expiry` date NOT NULL,
 `cvc` int NOT NULL,
 `value` int NOT NULL,
 `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  ADD PRIMARY KEY (`id`,`cid`),
  ADD CONSTRAINT `customers__card_ibfk_1` FOREIGN KEY (`uid` REFERENCES `customers` (`id`) ON DELETE CASCADE
)

My summarized question: How can I create a foreign key in the card_info.cid that references to the customers_card.cid without making the customers_card.cid a primary key?

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

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

发布评论

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

评论(1

夜空下最亮的亮点 2025-02-15 22:56:25

不要使用两个主要键。将ID设置为unique并保留auto_increment,然后将主键设置为cid> cid < /代码>,因此您可以参考它们。

CREATE TABLE `customers_card` (
  `id` int UNIQUE AUTO_INCREMENT,
  `uid` int NOT NULL,
  `cid` varchar(255) PRIMARY KEY,
  `cardname` varchar(255) COLLATE utf8mb4_hungarian_ci NOT NULL,
  `cardnum` int NOT NULL,
  `expiry` date NOT NULL,
  `cvc` int NOT NULL,
  `value` int NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   ADD CONSTRAINT `customers__card_ibfk_1` FOREIGN KEY (`uid` REFERENCES `customers` (`id`) ON DELETE CASCADE
)

Don't use two primary keys. Set the id to UNIQUE and keep the AUTO_INCREMENT, and set the primary key to the cid, so you can reference to them.

CREATE TABLE `customers_card` (
  `id` int UNIQUE AUTO_INCREMENT,
  `uid` int NOT NULL,
  `cid` varchar(255) PRIMARY KEY,
  `cardname` varchar(255) COLLATE utf8mb4_hungarian_ci NOT NULL,
  `cardnum` int NOT NULL,
  `expiry` date NOT NULL,
  `cvc` int NOT NULL,
  `value` int NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   ADD CONSTRAINT `customers__card_ibfk_1` FOREIGN KEY (`uid` REFERENCES `customers` (`id`) ON DELETE CASCADE
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文