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
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).
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.
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.try this, hope this would work:
After you fixed the single quotes that should have been backticks or removed altogether, you have:
And you are getting this error:
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:
(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
.)