what is Correct syntax for executing sql syntax? - mysql

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

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

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

Foreign Key relationship error in MySQL.

I am a total beginner in MySQL
Whenever I try to add a foreign key to a field it produces this error:
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 'ADD FOREIGN KEY ID
Here's the code to add foreign key:
ALTER TABLE 'table_name'
ADD CONSTRAINT 'FK_FKName'
ADD FOREIGN KEY table_name(column_name)
REFERENCES OtherTable_name(OtherTable_column_name);
Please try to help me
you can try without constraint name :
ALTER TABLE 'table_name'
ADD FOREIGN KEY table_name(column_name)
REFERENCES OtherTable_name(OtherTable_column_name);
I think your syntax is a little wrong.
Try this:
ALTER TABLE 'table_name'
ADD CONSTRAINT 'FK_Name' FOREIGN KEY ('coloumn_name')
REFERENCES 'table_name'('coloumn_name');
The syntax is like , please update as
ALTER TABLE employee
ADD CONSTRAINT fk_department
FOREIGN KEY (departmentID)
references department (departmentID);

MySql Alter Syntax error with mulitple FK

If i do the first one i have no problem. When i do addition i get a syntax error. What is wrong with the syntax? The error says syntax error near [entire 2nd line]
alter table `ban_Status` add FOREIGN KEY (`banned_user`) REFERENCES `user_data`(`id`)
alter table `ban_Status` add FOREIGN KEY (`banned_user`) REFERENCES `user_data`(`id`),
FOREIGN KEY (`banning_user`) REFERENCES `user_data`(`id`),
FOREIGN KEY (`unban_user`) REFERENCES `user_data`(`id`)
I think you need "add" before lines 2 and 3. That or you need to name your constraints.