I have created a database with two tables user and userdiary.
In the "user" table I have id and email fields.
In the "userdiary" table I have a field called email. I want to link this field with the one in the "user" table.
I'm using phpmyAdmin to add sql statements.
I tried doing ALTER TABLE userdiary FOREIGN KEY (email) REFERENCES user(email)
but I get the 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 'FOREIGN KEY (email) REFERENCES user(email)' at line 1
I looked at many forums but could not find a solution to this issue.
It will be great if any of you could help me resolve this issue.
Thanks.
You are missing the keyword add:
ALTER TABLE userdiary ADD FOREIGN KEY (email) REFERENCES user(email)
----------------------^
Related
I have tried both the syntaxes:
Alter Table bc DROP FOREIGN KEY STUD_ID;
It is giving the error: Can't DROP 'STUD_ID'; check that column/key exists
Alter Table bc DROP CONSTRAINT STUD_ID;
It is giving the 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 'CONSTRAINT STUD_ID' at line 1
Suggest me the possible ways.
ALTER TABLE TableName DROP FOREIGN KEY ForeignKeyConstraintName;
hope this helps :)
Your first query works. It is telling you that there is no such key to drop. This means your key has another name. It has not the same name as the column it indexes. Run
show index from bc
to show all key names and then run your query again with the correct name
Alter Table bc DROP FOREIGN KEY <STUD_ID_index_name>
alter table bc drop foreign key forconstraintname
I have a table named 'Index'. I realize that this is a keyword in MySQL, and was wondering how I can reference this table in queries?
My 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 ''Index' (ID) )' at line 9
Use backticks:
references `Index` (ID)
You actually already did this when you created the Index table (and you must have used backticks or else you could not have created the table):
CREATE TABLE `Index`
You should avoid naming databases, tables, and columns using MySQL keywords.
you can use backticks instead of quotes
CONSTRAINT grants_fk_index
FOREIGN KEY (Index_fk)
references `Index` (ID)
I'm trying to execute the following SQL command in MySQLWorkbench but it's giving me an error.
Command:
ALTER TABLE `ABC`.`GroupMembers`
ADD CONSTRAINT `FK_PROFILES`
FOREIGN KEY ()
REFERENCES `ABC`.`profiles` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Error:
Operation failed: There was an error while applying the SQL script to the database.
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 `ABC`.`profiles` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION' at line 3
SQL Statement:
ALTER TABLE `ABC`.`GroupMembers`
ADD CONSTRAINT `FK_PROFILES`
FOREIGN KEY ()
REFERENCES `ABC`.`profiles` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION
Not sure what's up. This script was generated by MySQLWorkbench
There has to be a column (or columns) in both lists. Those can't be empty.
For example:
FOREIGN KEY (profile_id)
-- ^^^^^^^^^^
REFERENCES `ABC`.`profiles` (id)
-- ^^
The datatypes of the columns much match exhactly. And values stored in the foreign key column must match the value in a row in the referenced table. (In this example, all values in profile_id must match the value of the id column in the profiles table.
MYSQL is saying its an syntax issue, and as I can see, there is a couple of things missing in you code.
Please check this link and learn more about the sintaxis in mysql regarding constraints.
Hope it helps.
Edit:
Ok, so just to enforce spencer7593's answer (which should be marked as answer if it solves your issue):
...
/profile_id would be the name you will set to the foreign key
FOREIGN KEY (profile_id)
...
/The references should be of the same value.
/`table_name`.`column_name` is the referenced column
/ id is the column on the table which will hold foreign key
REFERENCES `ABC`.`profiles` (id)
I am upgrading my database from mssql to mysql, I am struct on creating foreign keys
In MSSQL I was using
alter table ac_master add constraint 'ac_master_table_conf' foreign key (ac_code) references table_conf (ac_code)
MySql is giving error on this
#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 ''ac_master_table_conf' foreign key (ac_code) references
table_conf(ac_code)' at line 1
Remove the quotes from around your constraint name e.g.
ALTER TABLE ac_master ADD CONSTRAINT ac_master_table_conf FOREIGN KEY (ac_code) REFERENCES table_conf (ac_code)
As far as I can tell, neither SQL Server nor MySQL use single quote syntax for identifiers. You probably want ac_master_table_conf rather than 'ac_master_table_conf'.
Other than that, the ALTER TABLE syntax is documented, you don't need to guess.
no quotes are involved in mysql if it is a name of a field or a table.
the only need of quotes is to specify a string as below
select * from abc where column = 'string';
so be careful next time.
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)