phpMyAdmin:即使类型相同,也无法设置特定的外键

发布于 2024-12-09 12:28:57 字数 1828 浏览 0 评论 0原文

我的 list_items 表具有以下列

  • `ListID`
  • `WordID`

我的列表表有一个自动递增的主键 `ListID`。我想让 list_items 中的“ListID”成为引用列表中主键的外键,但出现错误:

在“ListID”上创建外键时出错(检查数据类型)。

两列都是 int(8),并且两个表类型都是 InnoDB。可能是什么问题?

遵循这些说明在设置其他外键方面效果很好。

我唯一能想到的是在 phpMyAdmin 的主屏幕上,有一条消息:

“用于处理链接表的附加功能已被停用。要了解原因,请单击此处。”

但是那里的任何问题不会阻止我设置其他外键吗?

相关SQL:

--
-- Table structure for table `lists`
--

CREATE TABLE IF NOT EXISTS `lists` (
  ```ListID``` int(8) NOT NULL AUTO_INCREMENT,
  ```UserID``` int(8) NOT NULL,
  ```privacy``` varchar(25) NOT NULL,
  ```name``` varchar(50) NOT NULL,
  ```description``` text NOT NULL,
  ```date created``` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  ```date modified``` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (```ListID```),
  KEY ```UserID``` (```UserID```)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `lists`
--

--
-- Table structure for table `list_items`
--

CREATE TABLE IF NOT EXISTS `list_items` (
  ```ListID``` int(8) NOT NULL,
  ```WordID``` int(8) NOT NULL,
  KEY ```ListID``` (```ListID```),
  KEY ```WordID``` (```WordID```)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `list_items`
--

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  ```UserID``` int(8) NOT NULL AUTO_INCREMENT,
  ```email``` int(50) NOT NULL,
  ```username``` int(25) NOT NULL,
  ```password``` int(25) NOT NULL,
  ```join date``` int(11) NOT NULL,
  PRIMARY KEY (```UserID```)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

My list_items table has the following columns

  • `ListID`
  • `WordID`

My lists table has a primary, auto-incremented primary key `ListID`. I want to make the `ListID` in list_items a foreign key referencing the primary key in lists, but I get an error:

Error creating foreign key on `ListID` (check data types).

Both columns are int(8), and both table types are InnoDB. What could possibly be the issue?

Following these instructions has worked fine in setting up other foreign keys.

The only thing I can think of is on phpMyAdmin's main screen, there's a message:

"The additional features for working with linked tables have been deactivated. To find out why click here."

But wouldn't any issue there prevent me from setting up other foreign keys?

Relevant SQL:

--
-- Table structure for table `lists`
--

CREATE TABLE IF NOT EXISTS `lists` (
  ```ListID``` int(8) NOT NULL AUTO_INCREMENT,
  ```UserID``` int(8) NOT NULL,
  ```privacy``` varchar(25) NOT NULL,
  ```name``` varchar(50) NOT NULL,
  ```description``` text NOT NULL,
  ```date created``` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  ```date modified``` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (```ListID```),
  KEY ```UserID``` (```UserID```)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `lists`
--

--
-- Table structure for table `list_items`
--

CREATE TABLE IF NOT EXISTS `list_items` (
  ```ListID``` int(8) NOT NULL,
  ```WordID``` int(8) NOT NULL,
  KEY ```ListID``` (```ListID```),
  KEY ```WordID``` (```WordID```)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `list_items`
--

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  ```UserID``` int(8) NOT NULL AUTO_INCREMENT,
  ```email``` int(50) NOT NULL,
  ```username``` int(25) NOT NULL,
  ```password``` int(25) NOT NULL,
  ```join date``` int(11) NOT NULL,
  PRIMARY KEY (```UserID```)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

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

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

发布评论

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

评论(2

屋檐 2024-12-16 12:28:59

你的sql中有很多反引号
通常你只有一套,这可能会导致问题

示例

CREATE TABLE IF NOT EXISTS `list_items` (
  `ListID` int(8) NOT NULL,
  `WordID` int(8) NOT NULL,
  KEY `ListID` (`ListID`),
  KEY `WordID` (`WordID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

you have an awful lot of backticks in your sql
normally you would only have 1 set, that could be causing a problem

example

CREATE TABLE IF NOT EXISTS `list_items` (
  `ListID` int(8) NOT NULL,
  `WordID` int(8) NOT NULL,
  KEY `ListID` (`ListID`),
  KEY `WordID` (`WordID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
小糖芽 2024-12-16 12:28:59

我手动尝试了一下,下面的外键创建得很好

ALTER TABLE list_items ADD FOREIGN KEY fk_ListID (ListID) REFERENCES lists(ListID);

I tried it manually with this the below at the foreign key was created fine

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