Syntax error, creating database - mysql

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.

Related

error when forward engineering MySQL model

I have created a model in MySQL workbench, when I want to forward engineer it to create the "create" and "insert" script I get the following error:
ERROR:
Executing SQL script in server
ERROR: Error 1366: Incorrect integer value: 'G1' for column 'gebruiker_id' at row 1
SQL Code:
INSERT INTO `databaseher`.`gebruiker` (`gebruiker_id`, `voornaam`, `achternaam`, `E-mail`) VALUES ('G1', 'Ronny', 'Giezen', 'r.giezen#gmail.com')
I don't understand whats wrong with it, because the datatype of the column where the value "G1" inserts into is "VARCHAR(4)". It should be possible to insert both a letter and a number.... At least that's what I thought...
This is the create table:
CREATE TABLE IF NOT EXISTS `databaseher`.`gebruiker` (
`gebruiker_id` VARCHAR(4) NOT NULL,
`voornaam` VARCHAR(25) NOT NULL,
`achternaam` VARCHAR(20) NOT NULL,
`E-mail` VARCHAR(30) NOT NULL,
PRIMARY KEY (`gebruiker_id`),
UNIQUE INDEX `E-mail_UNIQUE` (`E-mail` ASC) VISIBLE)
ENGINE = InnoDB;
If someone could help, that'll be awesome.
Thank you in advance!
Just a guess here because we don't have the full picture.
Can you run this:
Drop table 'databaseher'.'gebruiker'
And after recreate the table
CREATE TABLE IF NOT EXISTS `databaseher`.`gebruiker` (
`gebruiker_id` VARCHAR(4) NOT NULL,
`voornaam` VARCHAR(25) NOT NULL,
`achternaam` VARCHAR(20) NOT NULL,
`E-mail` VARCHAR(30) NOT NULL,
PRIMARY KEY (`gebruiker_id`),
UNIQUE INDEX `E-mail_UNIQUE` (`E-mail` ASC) VISIBLE)
ENGINE = InnoDB;
rerun the insert.
I am guessing that the table was initially created with the column gebruiker as integer

table does not exist after succesfull creation

I used MySQL through command line in order to create a database named javafxsample. The code is as folls:
create database javafxsample;
use javafxsample;
create table if not exists user(
id int(11) not null auto_increment,
username varchar(255) not null unique,
last_name varchar(255) not null,
first_name varchar(255) not null,
password varchar(255) not null,
created_at datetime not null default current_timestamp,
primary key(id)
);
The database javafxsample is created successfully in MySQL command line. (I know this because I can see it using SHOW DATABASES;.) However, when I try to see it using DESCRIBE javafxsample;, I got this error message:
ERROR 1146 (42S02): Table 'javafxsample.javafxsample' doesn't exist.
I do not know how to solve this issue, nor why I can not see the table javafxsample. I am using MySQL server version 5.7.24. Any help or suggestion(s) in order to get this table work is really appreciated.
javafxsample is your database. you cannot use describe on database.
Try describe user instead
The error "ERROR 1146 (42S02): Table 'javafxsample.javafxsample' doesn't exist" says table "javafxsample" does not exists in "javafxsample" database.
You create database "javafxsample" and table "user".
So try describing your user table like DESCRIBE user

Error in importing sql file [#1064]

I am trying to create a database for my login/signup forms. I creted this database using SQL. However, when importing it in phpmyadmin it says "Import has been successfully finished, 5 queries executed."
then the error:
Error
SQL query:
CREATE TABLE if not exists LoginTable(
name varchar(100) not null,
email varchar(100) not null default "",
password varchar(50) not null default "",
age integer(50) not null,
primary key ('email', 'password')
)
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 ''email', 'password')
)' at line 7
this is my sql code:
drop database if exists loginInfo;
create database if not exists loginInfo;
use loginInfo;
drop table if exists LoginTable;
CREATE TABLE if not exists LoginTable(
name varchar(100) not null,
email varchar(100) not null,
password varchar(50) not null,
age integer(50) not null,
primary key ('email', 'password')
);
Remove Single quotes in email, password.
While defining primary key you don't need to add quotes.
drop database if exists loginInfo;
create database if not exists loginInfo;
use loginInfo;
drop table if exists LoginTable;
CREATE TABLE if not exists LoginTable(
name varchar(100) not null,
email varchar(100) not null,
password varchar(50) not null,
age integer(50) not null,
primary key (email, password)
);

What is the proper way to run an sql script in my MySql shell utility, for win10?

I want to create a script written by someone else in order to create tables and entries etc. in my own database.
The command I want to run is:
mysql -h localhost -u root shop < C:/myInstance/web/website/sql/myWeb.sql
I got this line by reading a couple of posts on SO. Yet I got these error message:
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 'mysql -h localhost -u root shop < C:/myInstance/web/website/sql/myWeb.sql' at line 1
This suggests that there is clearly a syntax error, at this point I'm not even sure if the error is within my command of that file. Can you help me? I'm quite new to this, here is that file:
create table store_product
(
id bigint auto_increment primary key,
name varchar(32) not null,
description varchar(500),
imageSrc varchar(500),
price double not null
);
create table store_user
(
id bigint auto_increment primary key,
name varchar(32) unique not null,
password varchar(16) not null,
address varchar(100),
postCode varchar(10),
email varchar(50),
homePhone varchar(32),
cellPhone varchar(32),
officePhone varchar(32),
type varchar(20),
workNo varchar(20)
);
insert into store_user values(1,'throne212','123',' XXX ','621000','throne212#xxx.com','123456789','123456789','123456789','common',null);
insert into store_user values(2,'admin','123','','','admin#xxx.com','','','','admin','001');
create table store_order(
id bigint primary key,
orderNum varchar(17) unique not null,
status integer not null,
user_id bigint references store_user(id),
cost double(10,2)
);
create table store_order_item(
id bigint auto_increment primary key,
amount integer not null,
product_id bigint references store_product(id),
order_id bigint references store_order(id)
);

SQL Error #1007

I have this #1007 error with my SQL code. When I try to import into my database it gives me this #1007 error. The data base is called company. I'm new to SQL and it would be good if someone could help me out. Thanks
CREATE DATABASE company;
CREATE TABLE login(
id int(10) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
)
It looks like the database "company" may already exist. If you look at the link provided by #Marc B, the error "#1007" corresponds to:
"Error: 1007 SQLSTATE: HY000 (ER_DB_CREATE_EXISTS)
Message: Can't create database '%s'; database exists
An attempt to create a database failed because the database already exists.
Drop the database first if you really want to replace an existing database, or add an IF NOT EXISTS clause to the CREATE DATABASE statement if to retain an existing database without having the statement produce an error."
Check your schema to make sure that you don't already have a database called "company" created.
Perhaps you are only trying to create the table "login", whereas you would simply need the code:
CREATE TABLE login(
id int(10) NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
)
making sure you create this table in the already existing "company" database.
You are trying to create a database that has already been created.
Staring at your two commands, you could do CREATE TABLE IF NOT EXISTS
you also need to set default database before creating the table
CREATE DATABASE IF NOT EXISTS company;
USE company
CREATE TABLE login (
id int NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
);
or you could put the DB name before the table name
CREATE DATABASE IF NOT EXISTS company;
CREATE TABLE company.login (
id int NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL,
password varchar(255) NOT NULL,
PRIMARY KEY (id)
);