Implementing ER Diagram in MySQL, receiving Error 1064 during altering - mysql

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

Related

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

Sql syntax error caused by reserved names

i have an syntax error in my sql
it has to do with the use of reserved names(user security). i cant seem to figure out how to put the quotes can someone please help me fix this.
ALTER TABLE `user` ADD FOREIGN KEY `security_check_id` REFERENCES `security_check`(security_check_id)
#1064 - Er is iets fout in de gebruikte syntax bij 'REFERENCES security_check(security_check_id)' in regel 1
Translated sorry for the bad translation
#1064 - There is an mistake in the used syntax at 'REFERENCES security_check(security_check_id)' at line 1
Pease check this answer, it might be helpful -
Selecting a column that is also a keyword in MySQL
In your case try -
ALTER TABLE `user` ADD FOREIGN KEY (`security_id`) REFERENCES `security`(`security_id`)
ADD FOREIGN KEY security_check_id is incorrect if security_check_id is a column you wish to reference then it must be enclosed in parenthesis ie ADD FOREIGN KEY (security_check_id) if security_check_id is a name you intend to give the FK then it should be followed by the column in parenthesis that you wish to check..
BTW none of the column or table names are reserved words so they don't need to be escaped

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.

Adding foreign key - Error Code 1064

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

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,