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

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
);

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.

mySQL Syntax Error on Constraint

CREATE TABLE MARINA_SLIP
(
SLIP_ID VARCHAR(4),
MARINA_NUM VARCHAR(4),
SLIP_NUM VARCHAR(4),
LENGTH INT,
RENTAL_FEE DECIMAL(8,2),
BOAT_NAME VARCHAR(50),
BOAT_TYPE VARCHAR(50),
OWNER_NUM VARCHAR(4),
CONSTRAINT MARINA_SLIP_SLIP_ID_PK PRIMARY KEY (SLIP_ID),
CONSTRAINT MARINA_SLIP_MARINA_NUM_FK FOREIGN KEY (MARINA_NUM),
CONSTRAINT MARINA_SLIP_OWNER_NUM_FK FOREIGN KEY (OWNER_NUM) REFERENCES OWNER (OWNER_NUM)
);
The error states:
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 '
CONSTRAINT MARINA_SLIP_OWNER_NUM_FK FOREIGN KEY (OWNER_NUM) REFERENCES OWNER (O' at line 12
Does anyone know why this won't work?
Edit: this was figured out for me, small mistake on not including a foreign key reference.
add REFERENCES to CONSTRAINT MARINA_SLIP_MARINA_NUM_FK FOREIGN KEY (MARINA_NUM)
for example
CONSTRAINT MARINA_SLIP_MARINA_NUM_FK FOREIGN KEY (MARINA_NUM) REFERENCES OWNER (MARINA_NUM)

I am trying to create a table with only two foreign key

I have searched a lot but I could get only the concept that this is used for many to many linking. Or separate syntax for foreign key and primary key. But could not correct syntax as a whole.
CREATE TABLE cart
(
Customer varchar(40) FOREIGN KEY REFERENCES users(UserName),
Product varchar(40) FOREIGN KEY REFERENCES products(PID),
CONSTRAINT combination PRIMARY KEY (Customer,Product)
);
I am getting the 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 'FOREIGN KEY REFERENCES users(UserName), Product varchar(40) FOREIGN KEY RE' at line 3
Foreign key definitions go after all of the column definitions, they aren't defined inside the column definition. In your case, it would rather be:
CREATE TABLE cart
(
Customer varchar(40),
Product varchar(40),
FOREIGN KEY (Customer) REFERENCES users(UserName),
FOREIGN KEY (Product) REFERENCES products(PID),
PRIMARY KEY (Customer,Product)
);

what is Correct syntax for executing sql syntax?

String ag ="ALTER TABLE QUESTION"+
"(ADD FOREIGN KEY (a_status) REFERENCES ANSWER(a_status))";
stmt.executeUpdate(ag);
Error message:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'FOREIGN KEY
(a_status) REFERENCES ANSWER(a_status))' at line 1
This should be correct. See the offical documentation:
String ag ="ALTER TABLE QUESTION ADD FOREIGN KEY (a_status) REFERENCES ANSWER(a_status)";
String ag ="ALTER TABLE QUESTION ADD CONSTRAINT (question_answer_fk1) FOREIGN KEY (a_status) REFERENCES ANSWER(a_status)";
Also you can have more condition specific to add:
ON DELETE SET NULL/CASCADE
ON UPDATE SET NULL
String ag ="ALTER TABLE QUESTION ADD CONSTRAINT (question_answer_ibfk1) FOREIGN KEY (a_status) REFERENCES ANSWER(a_status)";
In general
ALTER TABLE Child_tblName
ADD CONSTRAINT 'give any name to the foreign key generally I do Child_tblName_Parent_tblName_ibfk_number' FOREIGN KEY ('Child_tblName's Column') REFERENCES Parent_tblName('Parent_tblName's Column')

MySQL Syntax Error #1064 with ON DELETE CASCADE

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