I'm converting a SQLite database to MySQL so I can import it to PHPMyAdmin.
This should be straightforward. I exported it to a dump, changed the autoincrements and changed all double quotes to backticks. This is what the start of the resulting file looks like:
DROP TABLE IF EXISTS `chars`;
CREATE TABLE chars(
charid INTEGER PRIMARY KEY AUTO_INCREMENT,
character TEXT
);
INSERT INTO `chars` VALUES(3,'a');
INSERT INTO `chars` VALUES(4,'b');
...
When trying to import to PHPMyAdmin it throws this error.
Error
SQL query:
CREATE TABLE chars(
charid INTEGER PRIMARY KEY AUTO_INCREMENT ,
CHARACTER TEXT
);
MySQL said:
#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 'character TEXT
)' at line 3
Is there too much space after the indented line? I left the "format" option as "SQL", left SQL compatibility mode as NONE and left "Do not use AUTO_INCREMENT for zero values" ticked.
Its going to be used in a django web app.
CHARACTER is a reserved word in mySQL. I'm betting that is the reason.
Use either a different column name (preferred), or use backticks:
`CHARACTER` TEXT
Related
CREATE TABLE Meta_data(Name_list_id VARCHAR DEFAULT (CONCAT('NL', AUTO_INCREMENT)),Result_id VARCHAR DEFAULT (CONCAT('RS', AUTO_INCREMENT)),No_of_subject int);
enter image description here
in above Name_list_id generate NL01,NL02,NL03....,
and Result_id generate RS01,RS02,RS03...,
can help to correct solution provide this query
for "mysql database"
It will be better if your ID values are numbers, not strings. Your syntax is incorrect - you are not allowed to create AUTO_INCREMENT on strings. Also, you are not able to use AUTO_INCREMENT column in GENERATED columns.
And now you are looking for the following:
NL01,NL02,NL03, ...
but what if we have 99+ records?
If you are using numbers, you will be able to format them when the data is read. For example, using LPAD function:
SELECT CONCAT('NL',LPAD(result_id,5,'0'))
FROM mytable
I've got table products, and want to add column with type time.
I've got statement as follows:
ALTER TABLE products ADD openTime1 TIME DEFAULT TIME(now());
it works on MariaDB, but it doesnt work on Mysql.
On mysql it produces Error -
[42000][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 '(now())' at line 1
Can someone tell me why? whats wrong here? I thought that this should be the same.
In MySQL the expression used in DEFAULT field attribute must be wrapped into the parenthesis:
ALTER TABLE products ADD openTime1 TIME DEFAULT (TIME(now()));
db<>fiddle here
I have a table called 'messages'. I wan't to add a column called 'key'.
When I try
ALTER TABLE messages ADD key BIGINT(20);
I get this error:
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 'bigint(20)' at line 1
What am I doing wrong?
Because key is a keyword in mysql. Reserved keywords
You can use
ALTER TABLE messages ADD `key` BIGINT(20);
by escaping the word key you should be fine with this query.
Syntax highlighting already shows the problem: key is a keyword (yeah I know that sounds funny). You can use backquotes to specify the column name:
ALTER TABLE messages ADD `key` BIGINT(20);
-- ^ ^ backquotes
Mind that the backquotes are not part of the name of the column: the name of the column will be key. By using a backquote you state explicitly that you write a column name, not the keyword key.
Always add ` backquotes around keywords.
https://dev.mysql.com/doc/refman/5.7/en/keywords.html
Problem is key is a reserve word in MySQL and thus needs to be escaped using backtique like
ALTER TABLE messages ADD `key`
(OR) using double quotes "" (ANSI92 standard) like
ALTER TABLE messages ADD "key"
Better yet, stop using reservewords/keywords for table/column names (in fact for any DB object names)
I have a database with tables already in the database. There is a table called Character that I am trying to access to add an additional column. However any command that I use with the table Character gives me the following error:
mysql> SHOW FIELDS FROM Character;
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 'Character' at line 1
I've tried the following commands:
mysql> SHOW FIELDS FROM "Character";
mysql> SHOW FIELDS FROM 'Character';
mysql> SHOW FIELDS FROM Character;
All three give me the same error. I have already selected the appropriate database that contains the Character table using the command "USE dbname;"
Character is a reserved word therefore you should use backticks:
SHOW FIELDS FROM `Character`;
See it here 9.3 Keywords and Reserved Words
Also, about the "Uppercase" (or not), read this answer Are table names in MySQL case sensitive?
On this image I'm using MySql Workbench
Converted sqlite3 to mysql, but the import fails in phpMyAdmin and produces this 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 'PRIMARY KEY NOT NULL ,sequence VARCHAR,items_count INTEGER,total FLOAT,`c' at line 1
What am I missing?
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (`id` VARCHAR PRIMARY KEY NOT NULL ,`sequence` VARCHAR,`items_count` INTEGER,`total` FLOAT,`change` FLOAT,`tax_subtotal` FLOAT,`surcharge_subtotal` FLOAT,`discount_subtotal` FLOAT,`payment_subtotal` FLOAT,`rounding_prices` VARCHAR,`precision_prices` VARCHAR,`rounding_taxes` VARCHAR,`precision_taxes` VARCHAR,`status` INTEGER,`service_clerk` VARCHAR,`service_clerk_displayname` VARCHAR,`proceeds_clerk` VARCHAR,`proceeds_clerk_displayname` VARCHAR,`member` VARCHAR,`member_displayname` VARCHAR,`member_email` VARCHAR,`member_cellphone` VARCHAR,`invoice_type` VARCHAR,`invoice_title` VARCHAR,`invoice_no` VARCHAR,`invoice_count` INTEGER,`destination` VARCHAR,`table_no` INTEGER,`check_no` INTEGER,`no_of_customers` INTEGER,......
There is a litte differnce between SQLite and MySQL syntax.
Below answer is taken from here
Here a list of ALL the differences in SQL syntax that I know about between the two file formats: The lines starting with:
BEGIN TRANSACTION
COMMIT
sqlite_sequence
CREATE UNIQUE INDEX
are not used in MySQL
SQLlite uses CREATE TABLE/INSERT INTO "table_name" and MySQL uses CREATE TABLE/INSERT INTO table_name
MySQL doesn't use quotes inside the schema definition
MySQL uses single quotes for strings inside the INSERT INTO clauses
SQLlite and MySQL have different ways of escaping strings inside INSERT INTO clauses
SQLlite uses 't' and 'f' for booleans, MySQL uses 1 and 0 (a simple regex for this can fail when you have a string like: 'I do, you don\'t' inside your INSERT INTO)
SQLLite uses AUTOINCREMENT, MySQL uses AUTO_INCREMENT
The above link will help more regarding this.