skip to Main Content
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;

got error:
SQL query:

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 – You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ”manufacturer_id’) REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCA’ at line 3

EXPORTED TABLE FROM WHICH I WANT TO GET MANUFACTURER_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

Answers


  1. Probably the single quotes might not be necessary and both the referenced table and the new table that is being created have to be on the same Engine (MyISAM in this case).

    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;
    
    Login or Signup to reply.
  2. Without knowing a lot about the other table that you are trying to reference, it’s hard for us to guess certain information. I put together this SQLFiddle to demonstrate a working solution.

    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;
    

    If you run the SELECT * from amf you will see that there are no values with the manufacturer_id as 6, meaning that the cascade worked correctly.

    Login or Signup to reply.
  3. try this, hope this would work:

      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
    
    Login or Signup to reply.
  4. After you fixed the single quotes that should have been backticks or removed altogether, you have:

    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;
    

    And you are getting this error:

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

    This has nothing to do with the foreign key; it results from specifying auto increment for a column that is not a key. Presumably you meant to say:

    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    

    (If you end up with yet another error after fixing this, please make sure to edit your question with the new create statement, error message, and output from show create table aucs_manufacturer.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search