Syntax error in MySQL CREATE TABLE - mysql

I want to make a table in phpmyadmin and I am using SQL command for that
CREATE TABLE userdetail(
detailid INT(255) UNSIGNED AUTO_INCREMENT PRIMARY,
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT NULL,
email_id varchar(255),
userId int(20) NOT NULL,
reg_date TIMESTAMP
)
I am 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 '
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT ' at line 2

It should be like this
CREATE TABLE userdetail(
detailid INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT NULL,
email_id varchar(255),
userId int(20) NOT NULL,
reg_date TIMESTAMP);

You are missing KEY after PRIMARY:
CREATE TABLE userdetail (
detailid INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
address text,
phone varchar(13) NOT NULL,
email_id varchar(255),
userId int(20) NOT NULL,
reg_date TIMESTAMP
)
Note that int(255) really doesn't make sense. Are you familiar with the integer data types and what the value in parentheses means? You can review the documentation here.

Related

Where is the error in this sql statement?

I keep getting an sql error when attempting to use this statement:
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(30) NOT NULL
);
The error I get 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 '
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR' at line 1
Using
MySql V5.7.26
Just Add KEY in PRIMARY like PRIMARY KEY, you have missed the SQL syntax.
CREATE TABLE users
(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(30) NOT NULL);
You should change the query.
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(30) NOT NULL
);
In your query KEY is missing after PRIMARY.
Run this query then, u will get no syntax.
CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(30) NOT NULL,
PRIMARY KEY (`id`)
);
You place the PRIMARY KEY identifier on a separate line

How to represent float values in SQL

I'm trying to create a database in Ubuntu using MySQL in the command line, I need to create a table with the following data:
CREATE TABLE Vehicles (
Vehicle_ID int NOT NULL,
Vehicle_Type VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Engine_Size float(1,1) NOT NULL,
Condition VARCHAR(255) NOT NULL,
Price float(9,2) NOT NULL,
PRIMARY KEY (Vehicle_ID)
);
It just returns an error that says "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 'Condition varchar(255) NOT NULL,
Price float(9,2) NOT NULL,
PRIMARY KEY (Vehicle' at line 6"
What is wrong with my code? The "Price" and "Engine_Size" columns need to only be a float/decimal values so they can't be varchar because I want to only be able to insert numbers.
This works:
CREATE TABLE Vehicles (
Vehicle_ID int NOT NULL,
Vehicle_Type VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Engine_Size int NOT NULL,
Vehicle_Condition VARCHAR(255) NOT NULL,
Price numeric(9,2) NOT NULL,
PRIMARY KEY (Vehicle_ID)
);
Notes:
For Price you seem to want numeric, not float, because you are specifying the precision and scale.
For Engine_Size, I have no idea what float(1, 1) is supposed to mean. I am guessing that int is an appropriate type.
Condition is a reserved word in MySQL, so I changed the name of the column.
The only apparent problem in your code is the use of 'condition' as a column name as condition is a reserved word in MySQL.
You can fix it in 2 ways:
Don't use 'condition' as a column name:
CREATE TABLE Vehicles (
Vehicle_ID int NOT NULL,
Vehicle_Type VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Engine_Size float(1,1) NOT NULL,
V_Condition VARCHAR(255) NOT NULL, //Just an example feel free to use any another name
Price float(9,2) NOT NULL,
PRIMARY KEY (Vehicle_ID)
);
Put 'condition' inside backticks (``)
CREATE TABLE Vehicles (
Vehicle_ID int NOT NULL,
Vehicle_Type VARCHAR(255) NOT NULL,
Model_name VARCHAR(255) NOT NULL,
Engine_Size float(1,1) NOT NULL,
`Condition` VARCHAR(255) NOT NULL,
Price float(9,2) NOT NULL,
PRIMARY KEY (Vehicle_ID)
);
Hope this helps

importing a statement into mysql

I can't find and error, when I try to execute:
create database android_api
use android_api
create table users(
id int(11) 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
);
I get and 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 '
use android_api
create table users(
id int(11) primary key auto_increme' at line 3"
just separate your code with ';'
create database android_api;
use android_api;
create table users(
id int(11) 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
);
The answer is that you can't do it.
Do those processes in 3 different queries.

Error creating table in MySQL 5.7

Can some one help me in correcting the syntax according to MySQL 5.7?
Table :
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
PRIMARY KEY (ID)
);
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 '(ID)
You forgot comma at the end of this line:
ADDRESS VARCHAR(20) NOT NULL
You dont have a column named ID, you may want EMPID?
Replace
ADDRESS VARCHAR(20) NOT NULL
By
ADDRESS VARCHAR(20) NOT NULL,
There are two problems here:
A primary key clause is it's own clause, and needs to be separated from the previous column definition by a comma.
You don't have an id column - your primary key should (probably) be empid:
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
PRIMARY KEY (EMPID)
);
I guess this is what you are looking for:
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
);

What's wrong with my query

Following is my query:
CREATE TABLE report_invoice(
'ID' INT(10) NOT NULL AUTO_INCREMENT,
'Invoice_No' VARCHAR(30) NOT NULL,
'Invoice_Type' INT(10) NOT NULL,
'Operator' VARCHAR(50) NOT NULL,
'Customer' VARCHAR(50) NOT NULL,
'Invoice_Date' DATE NOT NULL,
'Total' DECIMAL('10,2'),
PRIMARY KEY ('ID'));
I keep getting 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 ''ID'
INT(10) NOT NULL AUTO_INCREMENT,
'Invoice_No' VARCHAR(30) NOT NULL,
'Invoic' at line 2
You're using single quotes around your field names, use backticks instead:
CREATE TABLE report_invoice(
`ID` INT(10) NOT NULL AUTO_INCREMENT,
`Invoice_No` VARCHAR(30) NOT NULL,
`Invoice_Type` INT(10) NOT NULL,
`Operator` VARCHAR(50) NOT NULL,
`Customer` VARCHAR(50) NOT NULL,
`Invoice_Date` DATE NOT NULL,
`Total` DECIMAL(10,2),
PRIMARY KEY (`ID`));
Don't use simple quotes
You may replace them with backticks, or suppress them.
They would be usefull if you want to use reserved keywords as column names (bad idea anyway), or completely numeric column names, or special characters in column names.
So not in your case.
Don't put quotes around decimal scale and precision, too.
So this would do the job.
CREATE TABLE report_invoice(
ID INT(10) NOT NULL AUTO_INCREMENT,
Invoice_No VARCHAR(30) NOT NULL,
Invoice_Type INT(10) NOT NULL,
Operator VARCHAR(50) NOT NULL,
Customer VARCHAR(50) NOT NULL,
Invoice_Date DATE NOT NULL,
Total DECIMAL(10,2),
PRIMARY KEY (ID));
Remove the ticks or replace them with back ticks. Same for numbers.
CREATE TABLE report_invoice(
ID INT(10) NOT NULL AUTO_INCREMENT,
Invoice_No VARCHAR(30) NOT NULL,
Invoice_Type INT(10) NOT NULL,
Operator VARCHAR(50) NOT NULL,
Customer VARCHAR(50) NOT NULL,
Invoice_Date DATE NOT NULL,
Total DECIMAL(10,2),
PRIMARY KEY (ID));
CREATE TABLE report_invoice(
Id INT IDENTITY PRIMARY KEY,
Invoice_No VARCHAR(30) NOT NULL,
Invoice_Type INT NOT NULL,
Operator VARCHAR(50) NOT NULL,
Customer VARCHAR(50) NOT NULL,
Invoice_Date DATE NOT NULL,
Total DECIMAL );