I have this SQL code I have used on another server to create a table if it doesn't exist. It has always worked perfectly. Today I decided to install mySQL on my own computer and use the same script, but this time I get an error. I have tried to rewrite the code in case there are any compatibility issues, however, without any success...
The SQL code:
$table = "Test";
$sql = "CREATE TABLE IF NOT EXIST $table (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Email VARCHAR(50) NOT NULL,
Country VARCHAR(50) NOT NULL,
Date TIMESTAMP
)";
This is the error message i receive:
Error creating table: 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 'EXIST Test ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, ' at line 1
I tried to create a new table manually in phpMyAdmin and simply copy the generated SQL code to my PHP-script and create a table that way. It didn't work either...
It's mariaDB on a Kali computer:
mysql version 15.1 Distrib 10.1.29-MariaDB, for debian-linux-gnu (i686) using readline 5.2
Any ideas?
I think you have made one spelling mistake in EXIST should write like EXISTS
Please re write your query as
$sql = "CREATE TABLE IF NOT EXISTS $table (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Email VARCHAR(50) NOT NULL,
Country VARCHAR(50) NOT NULL,
Date TIMESTAMP
)";
this will surely work in your case please try this.
Related
I am trying to import a schema into MySQL (MariaDB 10.1.36) and I am getting the above error. I also tried direct forward engineering in MySQL Workbench but the results are the same.
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 ')
ENGINE = InnoDB
Sample code that fails is:
CREATE TABLE IF NOT EXISTS `example`.`adhere` (
`adhere_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`id_uuid` VARCHAR(36) NULL,
PRIMARY KEY (`adhere_id`),
INDEX `ix_tmp_autoinc` (`adhere_id` ASC) VISIBLE)
ENGINE = InnoDB
AUTO_INCREMENT = 19
DEFAULT CHARACTER SET = latin1;
I tried changing the backticks into single quotes in vain, I later removed them to same result. The expectation is to have the table created so do the rest structured like so.
remove the VISIBLE word
DEMO
CREATE TABLE IF NOT EXISTS `example`.`adhere`
(
`adhere_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`id_uuid` VARCHAR(36) NULL,
PRIMARY KEY (`adhere_id`),
INDEX `ix_tmp_autoinc` (`adhere_id` ASC)
)
Check that your version of mariadb supports invisible/visible indexes here https://mariadb.com/kb/en/library/invisible-columns/ and if not turn off the option in mysql workbench here https://dev.mysql.com/doc/workbench/en/wb-table-editor-indexes-tab.html
I am trying to create database android_api and table users but I am getting error
#1064 - Something is wrong in your syntax near 'use android_api
create table 'users'(
id int(11) NOT NULL primary KEY AU' w linii 3
Here is the code
create database android_api
use android_api
create table users(
id int(11) NOT NULL primary KEY AUTO_INCREMENT,
unique_id varchar(23) not null unique,
name varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null
);
You should separate each SQL statement with ;, otherwise your syntax is incorrect.
Here is the correct syntax:
create database android_api;
use android_api;
create table users(
id int(11) NOT NULL primary KEY AUTO_INCREMENT,
unique_id varchar(23) not null unique,
name varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null
);
In your code what you did was actually:
create database android_api use android_api
(Which is not a valid CREATE statement).
Actually, the accepted answer is enough. This is a very small idea to share after running into the same problem as you report it, but in a beginner's tutorial Load PostgreSQL Sample Database in an SQL Shell.
In general, if you have the reported error in an SQL shell, and if you are not sure whether you have typed ; at the end of the last line, just type ; as a new command and press Enter to be sure that you type your next command in LINE 1 and not in LINE 2.
Without this "trick", some stupidity like this can happen:
postgres=# CREATE DATABASE dvdrental
postgres-# show databases
postgres-# create database dvdrental;
ERROR: syntax error at or near »show«
LINE 2: show databases
^
postgres=# create database dvdrental
postgres-# CREATE DATABASE dvdrental;
ERROR: syntax error at or near »CREATE«
LINE 2: CREATE DATABASE dvdrental;
^
postgres=# CREATE DATABASE dvdrental;
CREATE DATABASE
Finally working, as can be seen by the output CREATE DATABASE.
I am working with a SQL database on a Ubuntu LAMP server. I am attempting to create a new database then a new table in it, which can hold blobs. I can login, create my database, use it, but then I cannot create the table. I am doing:
$mysql -u root -p mypass
mysql> use frame;
Database changed
mysql> create table 'frame_info' ( 'frame_data' BLOB NOT NULL, 'frame_name' VARCHAR(45) NULL, 'creator_name' VARCHAR(45) NULL, PRIMARY KEY ('frame_data'));
From there, I get the error:
ERROR 1064 (4200): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to user near ''fram
e_info' ( create table 'frame_info' ( 'frame_data' BLOB NOT NULL, 'frame_na' at
line 1
Obviously that error is truncated by the mysql display, but you get the gist.
You should use backticks ` instead of single quotes ' or (in your case) you can succeed without them at all :
create table `frame_info` (
`frame_data` BLOB NOT NULL,
`frame_name` VARCHAR(45) NULL,
`creator_name` VARCHAR(45) NULL,
PRIMARY KEY (`frame_data`)
);
Creating a database for a class in PHP and MYSQL. Running into an error when creating the database, phpMyAdmin gives me a useless error message. (Software that gives a better error message would be awesome BTW)
phpMyAdmin 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 'CREATE TABLE customers ( customer_id INT UNSIGNED NOT NULL auto increment, fir' at line 1
USE isys288_gottfrk
CREATE TABLE customers ( customer_id INT UNSIGNED NOT NULL
auto increment, first_name carchar(20) NOT NULL, last_name VARCHAR(40) NOT NULL,
PRIMARY KEY (customer_id), INDEX full_name (last_name, first_name) ) engine =
innodb;
There were several errors in your code.
USE isys288_gottfrk;
CREATE TABLE customers
(
customer_id INT UNSIGNED NOT NULL auto_increment,
first_name varchar(20) NOT NULL,
last_name VARCHAR(40) NOT NULL,
PRIMARY KEY (customer_id),
INDEX full_name (last_name, first_name)
) engine = innodb;
I suggest you use a DB tool like MySQL Workbench to generate your tables. Then you can see the problems right away.
Juergen d edited your question for formatting, this might have helped you as well: Your initial statement was all on one line, so the error was flagged in that one line. If you edited it on multi lines (like in your question, or better in his answer) you might have gotten a better message, e.g. one that flags that the line containing carchar(20) contains a problem (it's even better the way he has it in his answer, but it correctly uses varchar(20), not flagging a problem)
Yes, it would be nice if error messages would immediately flag the problem, but sometimes it's already better when the input is formatted in a more human-readable way. I've tested the same with a multiline statement, mysql stated
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 'carchar(20)
Try this:
CREATE TABLE customers (
customer_id INT UNSIGNED NOT NULL AUTO_INCREMENT
, first_name VARCHAR (20) NOT NULL
, last_name VARCHAR (40) NOT NULL
, PRIMARY KEY (customer_id)
, INDEX full_name (last_name, first_name)
) ENGINE = INNODB ;
You can use some GUI clients like SQLyog for this.
When trying to create this table, it doesn't want to work properly though it worked minutes before? Assume that $username is equal to "Test" for brevity.
The error I'm receiving is:
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 '( id(10) INT AUTO_INCREMENT, PRIMARY KEY(id), message
varchar(250), ' at line 2
This is the code I'm currently using:
$sql = "CREATE TABLE {$username}
(
id(10) INT AUTO_INCREMENT,
PRIMARY KEY(id),
message varchar(250),
sender varchar(100)
)";
You have a SQL syntax error.
Instead of id(10) INT AUTO_INCREMENT
It should be id INT(10) AUTO_INCREMENT