mySQL table creation fails - error 1064 - mysql

I am using the mysql admin GUI tool to create a table. Here is the query language that results in an error:
CREATE TABLE `environment`.`moisture` (
`city_id` INT,
`date_holding` VARCHAR,
`time_holding` VARCHAR,
`open` REAL,
`high` REAL,
`low` REAL,
`close` REAL,
)
CHARACTER SET utf8;
My date and time are not of the format mysql likes so I selected VARCHAR as the type for those columns and referred to them as holding columns until I can run queries to make the necessary conversions.
When I try to execute this via the GUI I get the following:
Error executing SQL commands to create table.
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 'NOT NULL,
`time_holding` VARCHAR NOT NULL,
`open` REAL NOT NULL,
`high` RE' at line 3 (error 1064)
Any idea why I am getting this? I have checked to see if I am using any reserved words but that does not seem to be the case.
Your help would be greatly appreciated.

The first error you have is due to that VARCHAR fields need a length like this: VARCHAR(30).
The second error (you'll get if you fix the 2 varchars) is the comma before the last parenthesis.
CREATE TABLE `environment`.`moisture` (
`city_id` INT,
`date_holding` VARCHAR(30), --or whatever length you want
`time_holding` VARCHAR(20), --same
`open` REAL,
`high` REAL,
`low` REAL,
`close` REAL --- no comma here
)
CHARACTER SET utf8;
Side note: You do know that MySQL has specific types to handle dates and times, don't you? So you could have:
CREATE TABLE `environment`.`moisture` (
`city_id` INT,
`date_holding` date,
`time_holding` time,
...

Related

hi I am new to stack overflow and programming. I was solving an EASY problem of mysql on leetcode and ran into an error

got this error message "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 'INSERT INTO 'employees1'"
Write your MySQL query statement below
create table employees1 (emp_id INT(6) UNSIGNED, event_day DATE NOT NULL, in_time int NOT NULL,
out_time int NOT NULL )
INSERT INTO 'employees1' ('emp_id', 'event_day', 'in_time', 'out_time')
values (1,20201128,4,32),
(1,20201128,55,200)
Please use a semicolon at the end of a statement, and use backticks for table names and column names. Those are fundamental rules which can be easily learned should you decide to spend a little more efforts.
create table employees1 (emp_id INT(6) UNSIGNED, event_day DATE NOT NULL, in_time int NOT NULL,
out_time int NOT NULL );
INSERT INTO `employees1` (`emp_id`, `event_day`, `in_time`, `out_time`)
values (1,20201128,4,32),
(1,20201128,55,200);

Receiving error in an attempt to define my table [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 3 years ago.
I'm working on a group project and I'm trying to drop my database, but I'm receiving the same error, no matter how I try to manipulate what I wrote. Hopefully someone can help me understand where I went wrong.
For the
type VARCHAR NOT NULL,
description VARCHAR,
image VARCHAR,
I've tried adding in "DEFAULT NULL" and adding in numbers next to VARCHAR. I keep getting the same error message. I left it as what I put above because with my API, I don't want to restrict the amount of characters I'll be receiving when I call on it.
CREATE DATABASE recycleThis_db;
USE recycleThis_db;
CREATE TABLE materials (
id INT(11) NOT NULL AUTO_INCREMENT,
type VARCHAR NOT NULL,
description VARCHAR,
image VARCHAR,
materialID INT(11) NOT NULL,
PRIMARY KEY (id)
);
I keep getting this 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 'NOT NULL,
description VARCHAR,
image VARCHAR,
materialID INT NOT NULL' at line 3
I believe VARCHAR requires a length specifier e.g. VARCHAR(10) or VARCHAR(250)
CREATE TABLE `materials`
( `id` INT(11) NOT NULL AUTO_INCREMENT
, `type` VARCHAR(80) NOT NULL
, `description` VARCHAR(255)
, ...
Reference: https://dev.mysql.com/doc/refman/8.0/en/char.html
excerpt:
The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters you want to store. For example, CHAR(30) can hold up to 30 characters.

SQL query syntax error while creating a table

I am getting a SQL query syntax error while creating a table. Here is the SQL query:
CREATE TABLE ACCOUNT (
ACCNO NUMBER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BA L NUMBER(8,2) NOT NULL,
CREATION-DT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
Here is the 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 'NUMBER(5) NOT NULL.
What is wrong with my syntax?
Using SQLfiddle you can see this is not valid.
You have multiple problems with your syntax; invalid data types, invalid column names, etc. You can encapsulate the invalid names in backticks but then you will have to remember to encapsulate them later.
Instead of Number you probably meant numeric but i would suggest trying Integer instead.
Or just try this:
CREATE TABLE ACCOUNT (
ACCNO INTEGER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BAL INTEGER(8) NOT NULL,
CREATIONDT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
NUMBER is not a valid datatype in MySQL, you should choose the one from the list (maybe you've meant NUMERIC).
You should also escape some identifiers (in your case, column names BA L, which includes a space within it, and CREATION-DT, which includes a dash) with backticks ` in MySQL:
CREATE TABLE `ACCOUNT` (
`ACCNO` NUMERIC(5) NOT NULL,
`NAME` VARCHAR(20) NOT NULL,
`BA L` NUMERIC(8,2) NOT NULL,
`CREATION-DT` DATE NOT NULL,
PRIMARY KEY ( `ACCNO` )
);
SQLFiddle

mysql and sql developer

I found how to make sql developer connect to a mysql server and then I made a database. The problem I now have is that I cannot add my tables to it. Is it a syntax problem or am I doing something wrong? I have used (use database;) but now I see and error on some lines. Like in line 352, but it looks fine....
CREATE TABLE acctmanager
(amid VARCHAR2(4) PRIMARY KEY,
amfirst VARCHAR2(12) NOT NULL,
amlast VARCHAR2(12) NOT NULL,
amedate DATE DEFAULT SYSDATE,
region CHAR(2) NOT NULL);
there is no varchar2 in MySQL
replace varchar2 with varchar

SQL reserved word syntax error in phpMyAdmin

I am having the following error when trying to execute the following code in phpMyAdmin. I am using WAMP Server with MySQL version 5.5.8 ::
The error is:
#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 'NUMERIC(6) not null, payee VARCHAR2(20) not null, amount DECIMAL(6,2) no' at line 3
the SQL is:
CREATE TABLE checks
(
check NUMERIC(6) not null,
payee VARCHAR2(20) not null,
amount DECIMAL(6,2) not null,
remarks VARCHAR2(20) not null
)
EDIT
I am also trying to execute this but it gives also error:
CREATE TABLE deposits
(
deposit NUMBER(8),
whopaid VARCHAR(25),
amount DECIMAL(6,2),
remarks VARCHAR(20)
)
You need to escape check with backticks since it is a reserved word. And in MySQL there is no varchar2 data type, just varchar.
CREATE TABLE checks
(
`check` NUMERIC(6) not null,
payee VARCHAR(20) not null,
amount DECIMAL(6,2) not null,
remarks VARCHAR(20) not null
)
Also replace NUMBER with NUMERIC in your 2nd create statement.
CREATE TABLE deposits
(
deposit NUMERIC(8),
whopaid VARCHAR(25),
amount DECIMAL(6,2),
remarks VARCHAR(20)
)
SQLFiddle example
See MySQL String data types and MySQL numeric data types
check is a reserved work, that's why you have this error
you need to put quotes around it
CHECK is a reserved keyword in mysql. If you choose a different name it should work fine.
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html