mySql Constraint fields is not working - php

I'm trying to constraint two different tables fields through mySql.
table(field) : comments(item_id) with item(item_ID) with this code:
ALTER TABLE 'comments' ADD CONSTRAINT comment_item
FOREIGN KEY(item_id) REFERENCES items(item_ID)
ON UPDATE CASCADE
ON DELETE CASCADE;
but I'm getting this error message :
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 ''comments' ADD CONSTRAINT comment_item
FOREIGN KEY(item_id) REFERENCES items(it' at line 1
what is the problem ?
I'm using InnoDB as a Storage Engine, and utf_general_ci as a Collation .

Actually you dont need single quotes, according to MySQL documentation you could write your statement like this:
ALTER TABLE comments ADD CONSTRAINT comment_item
FOREIGN KEY(item_id) REFERENCES items(item_ID)
ON UPDATE CASCADE
ON DELETE CASCADE;
You can also check here for an example

change
ALTER TABLE 'comments' ADD CONSTRAINT comment_item
to
ALTER TABLE `comments` ADD CONSTRAINT comment_item
Use ` instead of '.
or just write without ' like this:
ALTER TABLE comments

Related

MySQL Error Code 1215: Cannot add foreign key Constraint on ALTER TABLE : Mysql

I want to add foreign key Constraint on my existing tables(type InnoDB)
,I have two tables listed below:
tbl_users : [ user_id(INT,AUTO_INCREMENT), user_name,--- etc]
tbl_users_meta : [ user_meta_id(INT,AUTO_INCREMENT), user_id(INT),--- etc]
I have already created index 'user_id' on 'tbl_users_meta' listed below :
Here is my Query but i am getting (MySQL Error Code 1215: ) each time.what is the problem i can getting it ?
ALTER TABLE `tbl_users_meta`
ADD CONSTRAINT `fk users user_id`
FOREIGN KEY (`user_id`) REFERENCES
`sanskrut`.`tbl_users`(`user_id`)
ON DELETE NO ACTION ON UPDATE CASCADE;
TRY with the same query you have writen with a small modification:
ALTER IGNORE TABLE `tbl_users_meta`
ADD CONSTRAINT `fk users user_id`
FOREIGN KEY (`user_id`) REFERENCES
`sanskrut`.`tbl_users`(`user_id`)
ON DELETE NO ACTION ON UPDATE CASCADE;
Hope it'll work
I can't see anything wrong with what you've written, so there must be something you haven't posted affecting the problem. For example, on MySQL version 5.5.44, I can successfully execute the following:
CREATE DATABASE sanskrut;
USE sanskrut;
CREATE TABLE tbl_users (user_id INT AUTO_INCREMENT PRIMARY KEY, user_name VARCHAR(50));
CREATE TABLE tbl_users_meta (user_meta_id INT AUTO_INCREMENT PRIMARY KEY, user_id INT);
CREATE INDEX user_id ON tbl_users_meta (user_id);
ALTER TABLE `tbl_users_meta`
ADD CONSTRAINT `fk users user_id`
FOREIGN KEY (`user_id`) REFERENCES
`sanskrut`.`tbl_users`(`user_id`)
ON DELETE NO ACTION ON UPDATE CASCADE;
...which finishes with your exact problem statement, copied and pasted, and works just fine.
I'd suspect, as I mentioned in the comments, that there's an index missing or possibly a datatype mismatch. You may find it helpful to add the output of:
DESC tbl_users;
DESC tbl_users_meta;
SHOW indexes FROM tbl_users;
SHOW indexes FROM tbl_users_meta;
...to your question.

How to add "ON UPDATE SET NULL" to foreign key constraint?

How can I use the Laravel Schema Builder to add "ON UPDATE SET NULL" to my foreign key constraint?
I do not understand why to set as null on update; but this is the idea:
You can delete the existent constraint.
ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol;
And create it again
ALTER TABLE tbl_name
ADD CONSTRAINT my_new_FK
FOREIGN KEY (column_in_this_table)
REFERENCES other_table (other_table_pk_column)
ON DELETE SET NULL;
You can find more information about constraints and the alter table statement in the mysql documentation.
Using FOREIGN KEY Constraints
ALTER TABLE Syntax

Alter Table gives error #1064 [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I am trying to create two tables in phpmyadmin: Users and Keys, the schema is as follows:
Users:
id int auto_incerement primary key
name varchar(50) not null
Keys:
user_id int
keys varchar(50) not null
Now I am running the following query to make the user_id in Keys a foreign key referencing the id in Users
ALTER TABLE Keys
FOREIGN KEY(user_id) REFERENCES Users(id)
ON UPDATE CASCADE
ON DELETE CASCADE
But after executing, i am getting 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 'Keys FOREIGN KEY(user_id) REFERENCES Users(id) ON UPDATE CASCADE ON DELETE CA' at line 1
Can anyone tell me what am I doing wrong?
Thanks in advance.
Keys is a reserved word and must be encased in backticks:
ALTER TABLE `Keys`
ADD FOREIGN KEY (user_id) REFERENCES `Users` (`id`)
ON UPDATE CASCADE
ON DELETE CASCADE
It's best practice to encase all system related words in backticks.
KEYS is a reserved keyword. See also: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
You need to wrap them in backticks:
ALTER TABLE `Keys`
...
You need to enclose keys with back tick.
ALTER TABLE `Keys`

MySql error #1005 - Can't create table

I write a query when doing a tutorial Agile web application development with yii. I was trying to alter a table with this query and got this error in phpmyadin
#1005 - Can't create table 'trackstar_dev.#sql-152_16' (errno: 121) (Details...)
Here is my query
ALTER TABLE `tbl_project_user_assignment` ADD CONSTRAINT `FK_project_
user` FOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON
DELETE CASCADE ON UPDATE RESTRICT
Can Anyone help please ?
This happens when table tbl_project_user_assignment has already records which project_id is not found on the table (which you want to reference) tbl_project.id. The best way you can do is to empty the tbl_project_user_assignment and alter it again to add the constraint.

Setting up foreign key constraint in MySQL for use with Yii framework

So -- I have two tables that I'm trying to relate: tournament and match. They can be outlined as followed:
tournament
-id (int)
-league (varchar)
-status (varchar)
-create_time (datetime)
-update_time (datetime)
match
-id (int)
-tournament_id (int)
-status (varchar)
-create_time (datetime)
-update_time (datetime)
I'm attempting to add a foreign key constraint on the match table with the following SQL:
ALTER TABLE 'match' ADD CONSTRAINT
('FK_match_tournament') FOREIGN KEY
('tournament_id') REFERENCES
'tournament' ('id') ON DELETE CASCADE
ON UPDATE RESTRICT;
however, I am getting the following error message from MySQL:
#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
''match' ADD CONSTRAINT ('FK_match_tournament') FOREIGN KEY ('tournament_id') REF'
at line 1
I looked at the syntax for adding FK constraints on the MySQL website, and everything looks right to me. Any ideas?
First Suggestion (manuelpedrera):
ALTER TABLE `match` ADD CONSTRAINT ('FK_match_tournament') FOREIGN KEY ('tournament_id') REFERENCES `tournament` ('id') ON DELETE CASCADE ON UPDATE RESTRICT;
Results:
#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 '('FK_match_tournament') FOREIGN KEY ('tournament_id') REFERENCES `tournament` ('' at line 1
Turns out 'match' is a reserved word.
You cannot use regular quotes when referring to a table.
Instead of ALTER TABLE 'match'
use
ALTER TABLE match
or
ALTER TABLE `match`
Edit 1:
Try with this
ALTER TABLE `match` ADD FOREIGN KEY (`tournament_id`) REFERENCES `tournament`(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;

Categories