mysql error #1064 while creating table - mysql

CREATE TABLE Exhibitor_Info
(Ex_id int AUTO_INCREMENT,User_id int,Category varchar(150),Description varchar(400), PRIMARY KEY(Ex_id),FOREIGN KEY(User_id));
while executing this sql I got 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 ')' at line 2
Can anyone help me to fix the problem
thanks

Add reference to foreign key by replacing
FOREIGN KEY(User_id)
with
foreign key(user_id) references referred_table(referred_col)

You have to add the reference to a foreign key
CREATE TABLE Exhibitor_Info
(Ex_id int AUTO_INCREMENT,User_id int,Category varchar(150),Description varchar(400), PRIMARY KEY(Ex_id),FOREIGN KEY(User_id)
REFERENCES referred_parent_table(referred_col) ON DELETE CASCADE);
You can have a good example here

Related

I am unable to make a foreign key in the table. An error message keeps appearing

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.

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

I 'm trying to make that table as a many to many relationship table and this error occur
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 ')
SCREEN-SHOTSCREEN-SHOT 2
this is another test for my commands
CREATE TABLE `new_e`.`teach` (
`instructor_i_id` INT NOT NULL,
`course_c_id` INT NOT NULL,
PRIMARY KEY (`instructor_i_id`, `course_c_id`),
CONSTRAINT `i_id`
FOREIGN KEY (`i_id`)
REFERENCES `new_e`.`instructor` (`i_id`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `c_id`
FOREIGN KEY ()
REFERENCES `new_e`.`course` ()
ON DELETE RESTRICT
ON UPDATE CASCADE);
The second foreign key definition:
FOREIGN KEY ()
REFERENCES `new_e`.`course` ()
requires one or more columns between the parentheses.

#1064 - You have an error in your SQL syntax; while adding database

Error
SQL query:
ADD CONSTRAINT `ospos_sales_ibfk_3`
FOREIGN KEY (`vehicle_id`) REFERENCES `ospos_vehicles` (`person_id`);
MySQL said: Documentation
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 'ADD CONSTRAINT `ospos_sales_ibfk_3` FOREIGN KEY (`vehicle_id`) REFERENCES `ospos' at line 1
add ALTER TABLE in your mysql query and also remove quotes. try this
ALTER TABLE table_name ADD CONSTRAINT ospos_sales_ibfk_3
FOREIGN KEY (vehicle_id) REFERENCES ospos_vehicles(person_id);
i think it should work without errors, you can check here http://www.w3schools.com/sql/sql_foreignkey.asp it is correct way.

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

I get an MYSQL error #1064 when add foreign key constraint

I keep getting this sql 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 'Option (OptionId)' at line 1"
when I try and add a foreign key to the OptionId field from the Question Table to the OptionId(pk) field in the Option field. I don't get wy I keep getting the error because I don't see what is wrong with it.
Below is the foreign key constraint using ALTER TABLE:
ALTER TABLE Question ADD CONSTRAINT FK_OptionId FOREIGN KEY (OptionId) REFERENCES Option (OptionId)
Table names and syntax are correct, I made sure by double checking.
Why is it not working?
option is a reserved word in MySQL and must be surrounded by backticks.
ALTER TABLE Question
ADD CONSTRAINT FK_OptionId FOREIGN KEY (OptionId)
REFERENCES `Option` (OptionId)