首页 > 解决方案 > PHP 没有设置 FOREIGN 键

问题描述

再会,

我面临一个谜,我想通过PHP在数据库中导入以下内容。

$sql = "
  CREATE TABLE IF NOT EXISTS `whtsdg_field_products` 
  ( 
    `whtsdg_field_products_id` int(11) NOT NULL auto_increment,
    `whtsdg_field_list_id` int(11) unsigned NOT NULL,
    `product_number` varchar(255) NOT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (whtsdg_field_products_id),
    FOREIGN KEY (whtsdg_field_list_id) REFERENCES whtsdg_field_list (whtsdg_field_list_id)
  );";

$db->executeQuery($sql);

到目前为止,这一切都有效,只是他没有给我设置 FK,我不知道为什么。如果我在 MySQL 中输入代码,那么一切正常。有人可以告诉我我做错了什么或错误在哪里吗?

编辑:我使用“SELECT table_name,table_schema FROM information_schema.tables WHERE engine = 'InnoDB'”检查了表,因此存储引擎似乎是InnoDB。我的表的架构是:

whtsdg_config_list

whtsdg_config_list_id int(11) noNULL noStandard AUTO_INCREMENT Primarykey Unique whtsdg_fiel_list_id int(11) noNULL noStandard >应该是一个 FK< Unique whtsdg_fiel_config_id int(11) noNULL noStandard >应该是一个 FK< Unique created_at timestamp attribute="on update CURRENT_TIMESTAMP" NULLallowd标准=CURRENT_TIMESTAMP 额外="ON UPDATE CURRENT_TIMESTAMP"

whtsdg_field_config

whtsdg_field_config_id int(11) noNUL noStandard AUTO_INCREMENT Primarykey 唯一名称 varchar(255)utf8_general_ci noNULL noStandard config json noNULL noStandard created_at timestamp attribute="on update CURRENT_TIMESTAMP" NULLallowd standard=CURRENT_TIMESTAMP extra="ON UPDATE CURRENT_TIMESTAMP"

whtsdg_field_list

whtsdg_field_list_id int(11) noNUL noStandard AUTO_INCREMENT Primarykey Unique Setname varchar(255)utf8_general_ci noNULL noStandard created_at timestamp attribute="on update CURRENT_TIMESTAMP" NULLallowd standard=CURRENT_TIMESTAMP extra="ON UPDATE CURRENT_TIMESTAMP"

whtsdg_field_proudcts

whtsdg_field_proudcts_id int(11) noNUL noStandard AUTO_INCREMENT Primarykey Unique whtsdg_field_list_id int(11) noNULL noStandard >应该是一个 FK< Unique product_number varchar(255)utf8_general_ci noNULL noStandard created_at timestamp attribute="on update CURRENT_TIMESTAMP" NULLallowd standard=CURRENT_TIMESTAMP extraON更新 CURRENT_TIMESTAMP”

标签: phpmysqlsql

解决方案


推荐阅读