Table named 'Index' - mysql

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)

Related

Syntax error in MySQL 5.7 for use CONSTRAINT

I try to fix some error in Jira as mention in this page but when I try to run an SQL query, I got the below 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
fk_ao_8542f1_ifj_obj_attr_val_object_attribute_id' at line 1
My Query is:
ALTER TABLE AO_8542F1_IFJ_OBJ_ATTR_VAL
DROP CONSTRAINT `fk_ao_8542f1_ifj_obj_attr_val_object_attribute_id`
How I can fix this Query? I use MySQL version 5.7
Drop MySQL foreign key constraints
To drop a foreign key constraint, you use the ALTER TABLE statement:
ALTER TABLE table_name
DROP FOREIGN KEY constraint_name;
Judging by the name of the constraint, I believe it's a foreign key and MySQL has a different approach to drop foreign keys:
ALTER TABLE AO_8542F1_IFJ_OBJ_ATTR_VAL
DROP FOREIGN KEY `fk_ao_8542f1_ifj_obj_attr_val_object_attribute_id`

Drop foreign key using subquery to get its name

I need to drop a foreign key, but I don't know its name.
I don't want to drop all indexes from table.
I'm trying to do this with following subquery
ALTER TABLE `onboarding_requests`
DROP FOREIGN KEY (SELECT `CONSTRAINT_NAME` FROM `INFORMATION_SCHEMA.KEY_COLUMN_USAGE`
WHERE `TABLE_NAME` = 'onboarding_requests' AND `COLUMN_NAME` = 'partner_responsible');
but it returns:
SQL 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 '(SELECT CONSTRAINT_NAME FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE `TABL' at line 2
You cannot use variable substitution in data definition statements. In other words, you must give the literal name of the foreign key in a DROP FOREIGN KEY statement.
How can you work around this inherent limitation of SQL? All SQL, by the way, not just MySQL, prohibits this. Write a small program in your favorite language (python? php>) to retrieve the names of the data entities from the information schema, then use string concatenation to create the data definition statements you need.

PhpMyAdmin: MySql Field in one table refer to field in another table

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

add foreign key in mysql

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