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);
Related
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.
image of the code and error
CREATE TABLE Ticket(
-> Ticket_No INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
-> BusID INT,
-> Registration_No INT,
-> Seat_No INT NOT NULL,
-> FOREIGN KEY BusID REFERENCES Bus(BusID),
-> FOREIGN KEY Registration_No REFERENCES Passenger(Registration_No));
Error message:
ERROR 1064 (42000): 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 'REFERENCES Bus(BusID),
FOREIGN KEY Registration_No REFERENCES Passenger(Registra' at line 6
I am trying to add foreign keys to this table but this error message keeps appearing. The referenced table and column name is correct. I have also tried changing the column name. Please help.
You need to put parenthesis around the referencing column(s).
...
FOREIGN KEY (BusID) REFERENCES Bus(BusID),
...
And analog for the other one.
I am quite new at mysql.
I am trying to add a foreign key after having created two tables.
Here are the query used to create the tables
CREATE TABLE Categorie_article (
categorie_id INT UNSIGNED,
article_id INT UNSIGNED,
PRIMARY KEY (categorie_id, article_id)
);
CREATE TABLE Article (
id INT UNSIGNED AUTO_INCREMENT,
titre VARCHAR(150) NOT NULL,
texte LONGTEXT NOT NULL,
extrait TEXT,
FULLTEXT KEY (texte),
PRIMARY KEY (id)
);
and here is the query used to create the foreign key constraint:
ALTER Categorie_article ADD CONSTRAINT fk_categorie_article FOREIGN KEY (article_id) REFERENCES Article(id);
And here is the message I got :
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 'Categorie_article
ADD CONSTRAINT fk_categorie_article FOREIGN KEY (article_' at line 1
Can please someone tell me what I am doing wrong?
I tried to look up at others similar questions but it didn't help.
Thanks in advance ;-)!
The syntax is ALTER TABLE <table_name> not just ALTER <table_name>.
So try:
ALTER TABLE Categorie_article ADD CONSTRAINT fk_categorie_article FOREIGN KEY (article_id) REFERENCES Article(id);
db<>fiddle
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')
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);