Adding foreign key - Error Code 1064 - mysql

I'm trying to add a foreign key to my table by the following code:
ALTER TABLE scenes ADD CONSTRAINT fk_dancer_charachter FOREIGN KEY (dancer_pick) references characters(character);
But it keeps saying: Error Code 1064: You have an error in your sql syntax: check the manual that corresponds to your MySQL sever version for the right syntax to use near 'charachter)' at line 1.
Why is that? The syntax seems correct! Isn't it? What am I doing wrong?
Thanks

You are using a reserved word - CHARACTER.
More information in Reserved Words in MySQL 5.5

Related

Implementing ER Diagram in MySQL, receiving Error 1064 during altering

I'm trying to establish an ER diagram for my database. There are 5 tables in my database.
When I want to make the relationship between industry_number and company_inustry, it always said there is a 1064 error.
my code is:
ALTER TABLE company_inustry
ADD FOREIGN KEY (Detailed Industry)
REFERENCES Industry_number(Detailed Industry)
Error is #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 'Industry)
REFERENCES Industry_number(Detailed Industry)' at line 2.
I'm a new learner. Please help me... Thank you!
You have spaced in the column names so you need quote them.
It would be a better idea to use an underscore Detailed_Industry as you have done in the table names. If you do not you will have the same problem in every query that refers to these columns.
ALTER TABLE company_inustry ADD FOREIGN KEY ("Detailed Industry") REFERENCES Industry_number("Detailed Industry")
You need to use double quotes (" ") if you have space in name of the tables. use underscore as a good industry practice.
Use this query for correct syntax:
ALTER TABLE company_inustry ADD FOREIGN KEY ("Detailed Industry") REFERENCES Industry_number("Detailed Industry")

Why does MYSQL error when I try to add a foreign key?

I am trying to make a relational database in MYSQL.
Currently, I am making the foreign keys and connecting them to the parent table.
The problem is when I try to do this(in MYSQL workbench) MYSQL adds this line of code:
ADD INDEX `FK_party_coalitionparty_idx` (`partyId` ASC) VISIBLE;
After some research, I found out that it does this because when I delete or update the parent table, it is really handy when the child tables also delete or update the connected values(or columns).
The problem is when I run the foreign key code without the add index line it runs without trouble, but with it added (and I think I understand why it is good to have it added) it errors and does not want to execute the code to update my database.
When I try to execute the code in a SQL file it gives me the following error with the word VISIBLE:
VISIBLE is not valid at this position.
When I only try to delete the visible word, it cannot add my constraint (I think because you cannot put 2 times add below each other). I will include some screenshots and the message log to make my problem more clear.
Message log:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
ALTER TABLE `testdatabase`.`coalitionparty`
ADD INDEX `FK_party_coalitionparty_idx` (`partyId` ASC) VISIBLE;
;
ALTER TABLE `testdatabase`.`coalitionparty`
ADD CONSTRAINT `FK_party_coalitionparty`
FOREIGN KEY (`partyId`)
REFERENCES `testdatabase`.`party` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
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 '' at line 2
SQL Statement:
ALTER TABLE `testdatabase`.`coalitionparty`
ADD INDEX `FK_party_coalitionparty_idx` (`partyId` ASC) VISIBLE
My SQL File (when I try to run the SQL code but not with the workbench menu:
SQL file with the error included at the bottom
The question:
How do I need to fix this problem, so that I am able to use cascade and I don't get the error?
Thanks in advance!
Mariadb has no VISIBLE
check the manual for more information
So you can only do
ALTER TABLE `coalitionparty`
ADD INDEX `FK_party_coalitionparty_idx` (`partyId` ASC) ;
or switch to MySsQL

You have an error in your SQL syntax; ... 'UNIQUE `UK6i9q4suadww4j167aqe2h6aqj`' at line 4

I am trying to remove Unique constraint from a column using flyway DB migration in spring-boot. But I am not able to figure out the right query for that. Here is my existing query
ALTER TABLE `choices` DROP UNIQUE `UK6i9q4suadww4j167aqe2h6aqj`;
Here is the error
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 'UNIQUE `UK6i9q4suadww4j167aqe2h6aqj`' at line 4
Note: the above query is working fine if I am running it through PHPMyAdmin. Its ask for confirmation and then remove it.
Use
ALTER TABLE `choices` DROP index `UK6i9q4suadww4j167aqe2h6aqj`;
This will only work , if there in no foreign key defined that references your key.
.
In which case you have to drop the foreign keys first.

MySQL Workbench 1064 error in syntax

MySQL Workbench created this code, not me. I just used the GUI.
Operation failed: There was an error while applying the SQL script to the database.
Executing:
ALTER TABLE `isometr1_keyboard`.`records`
ADD CONSTRAINT `fk_records_layout_id`
FOREIGN KEY ()
REFERENCES `isometr1_keyboard`.`layouts` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION;
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 `isometr1_keyboard`.`layouts` ()
ON DELETE NO ACTION
ON UPDAT' at line 3
SQL Statement:
ALTER TABLE `isometr1_keyboard`.`records`
ADD CONSTRAINT `fk_records_layout_id`
FOREIGN KEY ()
REFERENCES `isometr1_keyboard`.`layouts` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION
I don't understand where the error is. Also don't understand why the GUI is creating code that is buggy.
Did I make a mistake?
The data types of the columns were not matching. This caused MySQL Workbench to create improperly formed code.

Where do I look for the database script generated from JPA Entities?

I am having a few errors like the following when I deploy my JPA entities into JBoss.
18:34:53,462 ERROR [SchemaExport] 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 'Group (id)' at line 1
18:34:53,684 ERROR [SchemaExport] Unsuccessful: alter table Value add index FK4E9A15132C855E3 (groupId), add constraint FK4E9A15132C855E3 foreign key (groupId) references Group (id)
Where do I look for the generated database script to create the tables ?I would like to take a look at it to debug my errors
Thanks.
You can not use Group as a table name. GROUP is a SQL keyword, so we need to rename it.
Do the following
#Entity
#Table(name="GROUPI") // or other name
public class Group {
}
regards,