aurora mysql 5.7 insert into failing - mysql

Can't seems to figure this one out. I come from the SQL Server world but would expect this to work the same.
insert into active_directory (ad_account,email,distinguished_name,employee_id,manager_distinguished_name,display_name,terminated)
select
ad_account,
email,
distinguished_name,
employee_id,
manager_distinguished_name,
display_name,
case when distinguished_name like '%Terminations%' then 1 else 0 end as terminated
from
active_directory_staging
fails with 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 'terminated)
select
ad_account,
email,
distinguished_name,
employee_id,
mana' at line 1
if I remove the terminated column from the insert/select query it works fine. Add it back and it fails.
If I run just the select part (both with and without the case statement for the terminated column) it works fine.
I am probably just missing something basic here...any help is appreciated.
active_directory table create statement
CREATE TABLE `active_directory` (
`ad_account` varchar(100) COLLATE latin1_general_ci DEFAULT NULL,
`display_name` varchar(100) COLLATE latin1_general_ci DEFAULT NULL,
`email` varchar(100) COLLATE latin1_general_ci DEFAULT NULL,
`distinguished_name` varchar(200) COLLATE latin1_general_ci DEFAULT NULL,
`employee_id` varchar(100) COLLATE latin1_general_ci DEFAULT NULL,
`manager_distinguished_name` varchar(200) COLLATE latin1_general_ci DEFAULT NULL,
`terminated` bit(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

MySQL has a bunch of reserved words that can't be used as column names/ alias.
Unfortunately for this query, terminated is one such word.
Excerpt from https://dev.mysql.com/doc/refman/5.7/en/keywords.html :
...
TEMPORARY
TEMPTABLE
TERMINATED (R)
TEXT
THAN
...

Related

How can I insert string value that includes a special char like ', ", /, - , +, =, dst?

CREATE TABLE `BasicInfo` (
`id` int(11) NOT NULL,
`Category` varchar(255) DEFAULT NULL,
`Title` varchar(255) DEFAULT NULL,
`Description` mediumtext DEFAULT NULL,
`createdBy` int(5) DEFAULT NULL,
`creationDate` timestamp NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
How to insert Category and Description that user inputs which includes a special char like ', ", /, -, +, =, dst, without syntax error? Doing so I get:
error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use 'S',3)'
You didn't mention the language you were using to do the insert.
You need to escape the ' by using a \ in front of it. Like this
thank\'s
Further to this like Ken Lee suggested it's highly recommended to use prepared statements with parameters for your queries.

Enum values and Error 1064

I have a problem with this error.
here is the code to create the table
CREATE TABLE `qa`.`question`(
`id` INT(9) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`password` VARCHAR(256) CHARACTER SET utf16 COLLATE utf16_general_ci NOT NULL,
`email` VARCHAR(512) NOT NULL,
`phone` INT(11) NOT NULL,
`content` TEXT NOT NULL,
`status` ENUM(0) NOT NULL,
PRIMARY KEY(`id`)
) ENGINE = InnoDB CHARSET = utf8 COLLATE utf8_general_ci;
I see the same question , but none of them occur in my code .
I think about default value too , but I receive this:
Invalid value for id (error #1067)
I can't reproduce the error you got. I got a different 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 '0) NOT NULL,
This is because the elements of an ENUM must be strings, not numbers. I changed it to:
`status` ENUM('OK', 'FAIL') NOT NULL,
and the table creation succeeded.
The error you got would happen if you tried to assign a default value to the id column, because it's also declared to use AUTO_INCREMENT. See #1067 - Invalid default value for 'bonusid' how can i fix this error?

Mysql i get Error when i try to edit after insert data

this is my one of database table structure and after insert some fields as row into that i can't update columns and i get error:
CREATE TABLE `channels` (
`id` int(11) NOT NULL,
`channelName` varchar(30) COLLATE utf8_persian_ci NOT NULL,
`channelLink` varchar(50) COLLATE utf8_persian_ci NOT NULL,
`channelDescription` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`channelImageFileName` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`channelAvatarFileName` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`channelState` tinyint(1) NOT NULL,
`channelType` enum('channels','userCreatedChannels') COLLATE utf8_persian_ci NOT NULL,
`channelOwnerUserId` int(11) NOT NULL,
`fileServerUrlId` int(11) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `channels`
--
ALTER TABLE `channels`
ADD PRIMARY KEY (`id`);
Error and notice:
This table does not contain a unique column. Features related to the grid edit, checkbox, Edit, Copy and Delete links may not work after saving.
Error
UPDATE `channels` SET `channelState` = '1' WHERE LIMIT 1
MySQL said: Documentation
#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 'LIMIT 1' at line 1
id of this table is unique and A_I, how can i resolve this problem?

Incorrect Syntax created by mysql when it exported this table?

So I had a set of tables working on an older version of MySQL, however on this new version the tables it exported give a syntax error.
CREATE TABLE `joblistings` (
`jobid` int(11) NOT NULL AUTO_INCREMENT COLLATE utf8_unicode_ci NOT NULL,
`id` varchar(255) NOT NULL,
`client` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`service` tinyint(1) COLLATE utf8_unicode_ci NOT NULL,
`firstline` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`city/town/village` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`county` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`postcode` varchar(9) COLLATE utf8_unicode_ci NOT NULL,
`daterequired` date(YYY-MM-DD) NOT NULL,
`timefrom` time(HH:MM),
`timeto` time(HH:MM),
PRIMARY KEY (`jobid`),
UNIQUE KEY `id` (`jobid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10 ;
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 '(YYY-MM-DD) NOT NULL, `timefrom` time(HH:MM),
`timeto` time(HH:MM), PRIMARY ' at line 10
These 3 seem to be the problem, from what I understand, however I am not versed well enough in versions and changes to see a way of fixing this.
`daterequired` date(YYY-MM-DD) NOT NULL,
`timefrom` time(HH:MM),
`timeto` time(HH:MM),
Any help would be much appreciated, keeping this format is important for my system to function.
You cannot change the format of the date and time. It will be stored in the default format('YYYY-MM-DD HH:MM:SS'). You can refer the documentation for more details.
The SQL Fiddle Demo after setting it to default format.
If you want to get the output in some other format then you have to use the DATE_FORMAT() function to get it in your required format.

Is .sql file different for MySQL and SQL?

I have a .sql file and I want to load it into MySQL database. I don't know from which database (MySQL or MS-SQL) it was created.
Now, I am trying to import that file into MySQL database. It is showing errors while importing and executing that file.
Q1. So, my question is whether the .sql file generated from MySQL and MS-SQL are different?
Note: I am using SQLYog software (graphical interface for MySQL) for importing the file.
Here is the ERROR:
Query:
CREATE TABLE ads (
id bigint(20) NOT NULL auto_increment,
city_id int(11) NOT NULL,
type text collate utf8_bin NOT NULL,
town text collate utf8_bin NOT NULL,
address text collate utf8_bin NOT NULL,
price text collate utf8_bin NOT NULL,
info text collate utf8_bin NOT NULL,
link text collate utf8_bin NOT NULL,
hasImage int(11) NOT NULL,
language varchar(2) collate utf8_bin NOT NULL,
time_added varchar(255) collate utf8_bin NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1
Error occured at:2009-09-08 17:41:01
Line no.:35
Error Code: 1050 - Table 'ads' already exists
Query:
CREATE TABLE ads (
id bigint(20) NOT NULL auto_increment,
city_id int(11) NOT NULL,
type text collate utf8_bin NOT NULL,
town text collate utf8_bin NOT NULL,
address text collate utf8_bin NOT NULL,
price text collate utf8_bin NOT NULL,
info text collate utf8_bin NOT NULL,
link text collate utf8_bin NOT NULL,
hasImage int(11) NOT NULL,
language varchar(2) collate utf8_bin NOT NULL,
time_added varchar(255) collate utf8_bin NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1
Error occured at:2009-09-08 17:41:21
Line no.:35
Error Code: 1050 - Table 'ads' already exists
Query:
CREATE TABLE ads (
id bigint(20) NOT NULL auto_increment,
city_id int(11) NOT NULL,
type text collate utf8_bin NOT NULL,
town text collate utf8_bin NOT NULL,
address text collate utf8_bin NOT NULL,
price text collate utf8_bin NOT NULL,
info text collate utf8_bin NOT NULL,
link text collate utf8_bin NOT NULL,
hasImage int(11) NOT NULL,
language varchar(2) collate utf8_bin NOT NULL,
time_added varchar(255) collate utf8_bin NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1
Error occured at:2009-09-08 17:41:35
Line no.:35
Error Code: 1050 - Table 'ads' already exists
Query:
CREATE TABLE ads (
id bigint(20) NOT NULL auto_increment,
city_id int(11) NOT NULL,
type text collate utf8_bin NOT NULL,
town text collate utf8_bin NOT NULL,
address text collate utf8_bin NOT NULL,
price text collate utf8_bin NOT NULL,
info text collate utf8_bin NOT NULL,
link text collate utf8_bin NOT NULL,
hasImage int(11) NOT NULL,
language varchar(2) collate utf8_bin NOT NULL,
time_added varchar(255) collate utf8_bin NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1
Error occured at:2009-09-08 17:42:07
Line no.:35
Error Code: 1050 - Table 'ads' already exists
Query:
1 Stanford University 6700 http://www.orkut.co.in
.
.
.
The file extension ".sql" is essentially meaningless: it's just there so that you know what the file is. It will just be a plain text file containing SQL, which you can open in Notepad. Therefore, there's no special "mysql" or "sql server" extensions.
The errors you're getting there "Table 'ads' already exists" are because you're trying to create a table which already exists in the database. (Did you read the error?) You have a few options:
Change the SQL to this:
CREATE TABLE IF NOT EXISTS ads ( id bigint(20) ...
Change the SQL to this:
DROP TABLE IF EXISTS ads;
CREATE TABLE ads (id bigint(20) ...
Clear out all the tables in the DB first.
MySQL and Microsoft SQL Server do, unfortunately, implement different dialects of SQL. So, the answer to your question :
Q1. So, my question is whether the
.sql file generated from MySQL and
MS-SQL are different?
is "yes, quite possiby". However, the specific error you're showing appears to be due strictly to a "Create table" statement on line 35 being executed over and over and over again, and that can't depend on such dialect differences, so the causes have to be other ones (such as bugs or subtle format differences in what that "SQLYog" program, which I'm not familiar with, expects as its input).
It depends on which software exported this file. Also check in the file's header which software did it and analyze the syntax to determine where this file belongs to. Note also that SQL is microsoft sql server.
Yes. SQL varies greatly from database to database. While there is a SQL standard which most databases support much of, every database has numerous, incompatible nonstandard features and functions above and beyond that specified by the standard.
There are also many migration docs avaiable:
MSSQL => MySQL:
http://dev.mysql.com/tech-resources/articles/migrating-from-microsoft.html
MySQL => MSSQL:
http://technet.microsoft.com/en-us/library/cc966396.aspx
There are two obvious things in your CREATE TABLE statement that MSSQL doesn't support which jump out immediately:
ENGINE=InnoDB
AUTO_INCREMENT=1
Those are both MySQL-specific statements.
It looks like your script is being executed multiple times (as the line number where the error occurs is the same each time). The error also indicates that you're trying to define a table that already exists, which may be from the first time this script ran. You may want to look into how the script is being run to see why it may be executing multiple times.
The SQL itself looks fine for MySQL. It wouldn't run with SQL Server.