使用外键创建MYSQL表ERROR

时间:2019-12-16 09:01:41

标签: mysql

CREATE TABLE `u914452720_yzawa`.`aucs_manufacturer_files` 
( `id` INT NOT NULL AUTO_INCREMENT , `manufacturer_id` INT NOT NULL,
FOREIGN KEY ('manufacturer_id') REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE)
ENGINE=INNODB;

得到错误: SQL查询:

CREATE TABLE `u914452720_yzawa`.`aucs_manufacturer_files` 
( `id` INT NOT NULL AUTO_INCREMENT , `manufacturer_id` INT NOT NULL,
FOREIGN KEY ('manufacturer_id') REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE)
ENGINE=INNODB
MySQL atsakymas: Dokumentacija
  

#1064-您的SQL语法有错误;请检查与您的MariaDB服务器版本相对应的手册,以获取在'manufacturer_id'附近使用的正确语法。)参考aucs_manufacturer(manufacturer_id)ON DELETE CASCA'在第3行

我要获取制造商ID的导出表:

-- phpMyAdmin SQL Dump
-- version 4.9.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: 2019 m. Grd 16 d. 11:37
-- Server version: 10.2.27-MariaDB
-- PHP Version: 7.2.23

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `u914452720_yzawa`
--

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

--
-- Sukurta duomenų struktūra lentelei `aucs_manufacturer`
--

CREATE TABLE `aucs_manufacturer` (
  `manufacturer_id` int(11) NOT NULL,
  `name` varchar(64) NOT NULL,
  `image` varchar(255) DEFAULT NULL,
  `instruction` varchar(255) DEFAULT NULL,
  `catalog` varchar(255) DEFAULT NULL,
  `sketch` varchar(255) DEFAULT NULL,
  `sort_order` int(3) NOT NULL,
  `id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


--
-- Indexes for dumped tables
--

--
-- Indexes for table `aucs_manufacturer`
--
ALTER TABLE `aucs_manufacturer`
  ADD PRIMARY KEY (`manufacturer_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `aucs_manufacturer`
--
ALTER TABLE `aucs_manufacturer`
  MODIFY `manufacturer_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=31;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

4 个答案:

答案 0 :(得分:0)

可能没有必要使用单引号,并且引用表和正在创建的新表都必须位于同一引擎(在本例中为MyISAM)上。

CREATE TABLE `aucs_manufacturer_files` 
( `id` INT PRIMARY KEY AUTO_INCREMENT , `manufacturer_id` INT NOT NULL,
FOREIGN KEY (manufacturer_id) REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE)
ENGINE=MyISAM DEFAULT CHARSET=utf8;

答案 1 :(得分:0)

在不了解您要引用的另一张表的情况下,我们很难猜测某些信息。我整理了this SQLFiddle来演示可行的解决方案。

create table am (
  id int unique not null auto_increment
)
ENGINE=INNODB;
CREATE TABLE amf
( `id` INT unique NOT NULL AUTO_INCREMENT , `manufacturer_id` INT NOT NULL,
FOREIGN KEY (manufacturer_id) REFERENCES am(id) ON DELETE CASCADE)
ENGINE=INNODB;

insert into am(id) values (null),(null),(null),(null),(null),(null),(null);
insert into amf(id, manufacturer_id) VALUES (null, 1),(null, 2),(null, 3),(null, 2),(null, 1),(null, 4),(null, 6);
delete from am where id = 6;

如果您运行SELECT * from amf,将会看到没有Manufacturer_id为6的值,这意味着级联工作正常。

答案 2 :(得分:0)

尝试一下,希望它能起作用:

  CREATE TABLE `u914452720_yzawa`.`aucs_manufacturer_files` 
    ( `id` INT NOT NULL AUTO_INCREMENT , `manufacturer_id` INT,
    FOREIGN KEY (manufacturer_id) REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE)
    ENGINE=INNODB

答案 3 :(得分:0)

在修复了应该被反引号或完全删除的单引号之后,您将:

CREATE TABLE `u914452720_yzawa`.`aucs_manufacturer_files` (
    `id` INT NOT NULL AUTO_INCREMENT , 
    `manufacturer_id` INT NOT NULL,
    FOREIGN KEY ('manufacturer_id') REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE
) ENGINE=INNODB;

您收到此错误:

#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

这与外键无关;它是由于为非键列指定自动递增而产生的。想必您想说:

`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

(如果在解决此问题后仍然遇到另一个错误,请确保使用新的create语句,错误消息和show create table aucs_manufacturer的输出来编辑问题。)