MySQL syntax error - dropping foreign key if exist - mysql

The following ALTER statement is working fine on my local (xamp), but on my server (Debian), I'm getting this 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
'IF EXISTS `foreign_key1`' at line 1
This is the query:
ALTER TABLE `table` DROP FOREIGN KEY IF EXISTS `foreign_key1`;
How do I fix the error?

Sounds like your development environment is running MariaDB, and your server is running MySQL.
MySQL doesn't support the IF EXISTS in an ALTER TABLE statement.
That syntax is supported in MariaDB 10.0.
To confirm the version running in each environment, we could execute
SHOW VARIABLES LIKE 'version'
MariaDB https://mariadb.com/kb/en/mariadb/alter-table/
MySQL https://dev.mysql.com/doc/refman/5.7/en/alter-table.html
I don't know if this answers the question that was asked... "What is the explanation for the observed behavior?" (Was that the question?)

Related

MariaDB - Error Code: 1064 when creating a table [duplicate]

Im using xampp control panel and from there i start the process for apache and mysql. Then i go to mysql workbench and server status seems to be ok, here is some info
Host: Windows-PC
Socket: C:/xampp/mysql/mysql.sock
Port: 3306
Version 10.1.31-MariaDB mariadb.org binary distribution
Compiled For: Win32(32)
Configuratin File: unknown
Then everytime when i try to add the foreign key for my dummy schema like:
ALTER TABLE `puppies`.`animals`
ADD INDEX `Breed_idx` (`BreedID` ASC) VISIBLE;
;
ALTER TABLE `puppies`.`animals`
ADD CONSTRAINT `Breed`
FOREIGN KEY (`BreedID`)
REFERENCES `puppies`.`breeds` (`Breed`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
I get the following error
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 `puppies`.`animals`
ADD INDEX `Breed_idx` (`BreedID` ASC) VISIBLE
So what can i do so that xampp will start using mysql syntax over mariaDb?
Or if im wrong in my understanding of the problem, then what should i do so that i dont have to face this kind of issues again when using xampp?
Problem is the word VISIBLE, remove it and it will work.
Index are visible by default.
Your question: "If i remove VISIBLE it works just fine, so why did mysql workbench decided to add visible?"
My answer: The option to mark index invisible is not yet implemented in MariaDB (afaik!).
Update:
The syntax for MariaDB is different, please see this reference: https://jira.mariadb.org/browse/MDEV-7317
Just to add to those who are using Maria DB with MySQL Workbench, you don't need to install mysql. You can just change 'Default Target MySQL Version' from Preferences to 5.7 or 5.6, and the VISIBLE keyword will be removed by workbench.
Here is a link from mysql bugs
https://bugs.mysql.com/bug.php?id=92269
I am using MySQL Workbench and have same problem. Change in the Preferences but it did not work.
Solution: If you export forward-engineer the model you need to change the configuration on another place.
Go to Model > Model Options
Inside the Model Options, go to MySQL
Then change the "Target MySQL Version" to 5.6

Why select json data not working in the mariadb?

My query like this :
select `information`->'$."full_name"' as `homeroom`
from `classes`
If the query run in my database local, it works. No error
But if the query run in my database server, it does not works. There exist error like this :
#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 '>'$."full_name"' as `homeroom`
from `classes` LIMIT 0, 25' at line 1
If I run select version(),
My version database local : 8.0.15
My version database server : 10.0.38-MariaDB
Seems it does not work because my database server using mariadb
How can I solve this problem?
Update :
I using data type text to infomation field
JSON functions weren't added to MariaDB until version 10.2.3. If you can't upgrade you will have to process the data in your application. Note that even in versions that support JSON, they don't support the -> notation (reference) so you will have to rewrite the query as
JSON_EXTRACT(information, '$.full_name')

setting the target database version for mysql workbench migration

I am running a migration from sql server to mariadb 10.0 and the generated code keeps failing due to a syntax error in the create table statements.
Specifcally it doesn't like the INDEX lines of the create table statements. And example of a statement it chokes on is
CREATE TABLE t1 (
`fk_manager` varchar(255),
INDEX `imgr` (`fk_manager` ASC) visible
);
This is the error I get
ERROR 1064 (42000): 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 'INDEX
`imgr` (`fk_manager` ASC) visible)'
at line 3
If i remove the visible keyword the same command works, so I assume this is syntax valid on newer versions of mysql but not mine. Is there any way I can tell mysql workbench to exclude it?
In the top menu click Model > Model Options
In the dialogue that pops up, select MySQL on the left side
Change Target MySQL Version to 5.7

MySql - unable to create new tables after changing my.ini (windows)

In order to be able to create tables with capital letters I had to modify the my.ini file in ProgramData\MySQL\MySQL Server 5.7, U added lower_case_table_names = 2 at the end of the file like it was suggested here, when I closed MySQL server and restarted it couldn't create any new tables, so I removed the line I added, restarted the server again, I keep having this error
Operation failed: There was an error while applying the SQL script to the database.
Executing:
CREATE TABLE `toys`.`new_table` (
);
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 ')' at line 2
SQL Statement:
CREATE TABLE `toys`.`new_table` (
)
You cannot create a table without columns - you need something between the brackets there. E.g.:
CREATE TABLE `toys`.`new_table` (
id INT -- just for example
);

SQL syntax error in MySQL unsing a cast(Partition as Signed)

I have to support an application on a MySQL server. I can´t change the source code so I can´t change the used syntax.
My problem is that I always get an syntax error like this:
Microsoft OLE DB Provider for ODBC Drivers [-2147217900]
[MySQL][ODBC 5.1 Driver][mysqld-5.6.26]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 'Partition as Signed)' at
line 1
I looked into the log file and found the problem in this line:
Select *
from XYZ
where `Type`='something'
and Client='{3DBEA33A-9F0A-4e86-8354-F652713EA458}'
order by Cast(Partition as Signed);
I always get this error when the "Cast(Partition as Signed)" appears.
Is there any way to configure the server to accept this syntax?
I´m using MySQL (x64) 5.6.26 with InnoDB.
Partition is reserved word.
http://dev.mysql.com/doc/refman/5.6/en/keywords.html
you should use it with backticks
`Partition`