MySQL Syntax Error #1064 with ON DELETE CASCADE - mysql

For the life of me, I cannot find out why MySQL doesn't like this statement:
CREATE TABLE IF NOT EXISTS personnel
(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
role VARCHAR(50) NOT NULL,
line_manager INTEGER NULL,
FOREIGN KEY (role) REFERENCES roles(name)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (line_manager) REFERENCES personnel(id)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=InnoDB;
The resulting output from MySQL after inserting this is ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELETE CASCADE
UPDATE CASCADE,
FOREIGN KEY (line_manager) REFERENCES personnel(id)
' at line 10.
Can anyone suggest what I'm doing wrong?

hey i have check your mysql query and it works I think you should check the following constraints
1) table roles must have same **engine** that is InnoDB
2) the length of both column that is personnel(role) and roles(name) must be **equal**
3) column roles(name) must be **primary key**
Just check this things and i think it will work

Related

Is there is any method to sove this error of MYSQL workbench when working on database?

Apply changes to exams Error 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 '
CONSTRAINT `reg`
FOREIGN KEY (`reg`)
REFERENCES `students`.`stdinf...
at line 6 SQL Statement:
CREATE TABLE `students`.`exams`
(
`reg`VARCHAR(25) NOT NULL,
`code`VARCHAR(10) NOT NULL,
`marks` INT NULL,
PRIMARY KEY (`reg`, `code`),
INDEX `code_idx` (`code`ASC) VISIBLE,
CONSTRAINT`reg`
FOREIGN KEY (`reg`)
REFERENCES `students`.`stdinfo` (`reg`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `code`
FOREIGN KEY (`code`)
REFERENCES `students`.`courses` (`code\`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
I wanted to make foreign key in third table for connecting my other two tables but it showing this error.

Error while adding a new foreign key column in MySql

ALTER TABLE cart
ADD COLUMN name varchar NOT NULL AFTER cartID
FOREIGN KEY (name) REFERENCES products(p_name);
error - #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 'AFTER cartID
FOREIGN KEY (name) REFERENCES products(p_name)' at line 2
Try this
ALTER TABLE cart
ADD COLUMN name varchar(10) NOT NULL;
ALTER TABLE cart
ADD FOREIGN KEY (name) REFERENCES products1(p_name);
Check this http://rextester.com/IJFDC23276
Try this
ALTER TABLE cart
ADD name VARCHAR NOT NULL,
ADD CONSTRAINT FOREIGN KEY(name) REFERENCES products(p_name);

ERROR 1604 (42000)

I have a mysql commands
mysql> create table enrolled (snum int,cname varchar(20), primary key (snum,cname),
foreign key(snum) references student (snum),
foreign key (came ) references class (cname)
on delete cascade );
When I run it , I get an ERROR
ERROR 1604 (42000) :You have an ERROR in your SQL syntax ; check manual that corresponds to your MYSQL server version for the right syntax to use near 'mysql> create table enrolled ( sum int, cname varchar(20) primary key(snum ,cnam' at line 1
Change
foreign key (came )
To this
foreign key (cname )

MySQL 1064 Error Creating a Linking Table (Many-to-Many)

I'm trying to understand why a parsing error occurred (1064) with the following code.
<#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 3>
CREATE TABLE Party_Library
(
Party INT(11)
Library varchar(40)
PRIMARY KEY (Library,Party)
FOREIGN KEY (Party) REFERENCES Party(PartyKey) ON DELETE CASCADE
FOREIGN KEY (Library) REFERENCES MusicLibraries(MusicSource) ON DELETE CASCADE
)
You are missing commas after each declaration:
CREATE TABLE Party_Library
(
Party INT(11),
Library varchar(40),
PRIMARY KEY (Library,Party),
FOREIGN KEY (Party) REFERENCES Party(PartyKey) ON DELETE CASCADE,
FOREIGN KEY (Library) REFERENCES MusicLibraries(MusicSource) ON DELETE CASCADE
);

MySQL InnoDB Create FK error

I am trying to create a self-referential FK:
DROP TABLE IF EXISTS `Company`;
CREATE TABLE `Company` (
`company_id` INTEGER(32) UNSIGNED AUTO_INCREMENT,
`parent_company_id` INTEGER(32),
PRIMARY KEY (`company_id`)
) ENGINE=InnoDB;
ALTER TABLE `Company`
ADD FOREIGN KEY `parent_company_id` REFERENCES `Company`(`company_id`);
I am getting the following error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REFERENCES Company(company_id)' at line 1
Got it.
You gotta change the definition of
parent_company_id INTEGER(32)
To match company_id except for the autoincrement and then use this statement
ALTER TABLE `Company`
ADD CONSTRAINT fk_parent_company_id FOREIGN KEY (`parent_company_id`) REFERENCES `Company`(`company_id`)
So basically remember to put unsigned on the column you are using as FK so it matches the definition of the referenced key