what is my syntax error here in mySQL query? - mysql

CREATE TABLE IF NOT EXISTS `creditors` (
`id` int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(50) NOT NULL,
`route` varchar(255) NOT NULL,
`mobile` int(10) NOT NULL,
`credit_amount` double(100) NOT NULL,
`start_date` date NOT NULL,
`due_date` date NOT NULL,
UNIQUE KEY `idUnique` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
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 ') NOT NULL,
start_date date NOT NULL,
due_date date NOT NULL, ' at line 6
What is my syntax error in this SQL query?

If you set a Column to primary key, it consists only Unique value. Then why did you try to made the same to UNIQUE KEY?
And also Delete the RANGE for Double.
SQLFiddle Example
CREATE TABLE `creditors` (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(50) NOT NULL,
`route` varchar(255) NOT NULL,
`mobile` int NOT NULL,
`credit_amount` double NOT NULL,
`start_date` date NOT NULL,
`due_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

The error is with this statement:
`credit_amount` double(100) NOT NULL
Give precision after 100 in double data type.
Solution:
`credit_amount` double(100,0) NOT NULL
Instead of 0 after 100 give number of digits after decimal point.

If you want to use double you must specify both M and D where
M is number digits in total, of which D digits may be after the decimal point, i.e. double(10,0). Otherwise use float which which allows syntax like float(10).
Also, unless you really benefit from this, do not use latin1 charset, but rather UTF-8

Related

MYSQL Error 1064 in DateTime and TimeStamp

Good day, I'm creating some tables on my database and when i try to save, it throws 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 '0) NOT NULL , date_registered DATETIME NOT NULL , last_login TIMESTAMP NOT' at line 1
This sort of error has been debugged here on Stackoverflow before and the solution was to remove the length from the TIMESTAMP column. Other solutions asked the advised that quotes should be removed from column identifiers or ticks should be used instead. Unfortunately, my DATETIME and TIMESTAMP columns do not have lengths and my column identifiers have ticks around them.
Here's what my SQL statement looks like. Can someone please assist?
CREATE TABLE `inventoryman_db`.`users`
( `id` INT NOT NULL AUTO_INCREMENT ,
`full_name` VARCHAR(255) NOT NULL ,
`email` VARCHAR(255) NOT NULL ,
`password` VARCHAR(300) NOT NULL ,
`usertype` ENUM(0) NOT NULL ,
`date_registered` DATETIME NOT NULL ,
`last_login` TIMESTAMP NOT NULL ,
`notes` VARCHAR(255) NOT NULL COMMENT 'For verification and other purposes, we will store a random key here' ,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
try this, you only need to add quotes in enum('0') as enum consider as string with indexing his arrays.
CREATE TABLE `percept`.`users1` ( `id` INT NOT NULL AUTO_INCREMENT ,
`full_name` VARCHAR(255) NOT NULL , `email` VARCHAR(255) NOT NULL ,
`password` VARCHAR(300) NOT NULL ,
`usertype` ENUM('0') NOT NULL ,
`date_registered` DATETIME NOT NULL ,
`last_login` TIMESTAMP NOT NULL ,
`notes` VARCHAR(255) NOT NULL COMMENT 'For verification and other purposes, we will store a random key here' ,
PRIMARY KEY (`id`)) ENGINE = InnoDB `
just add quotes in enum

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB

CREATE TABLE IF NOT EXISTS `tbl_product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`price` double(11) NOT NULL,
PRIMARY KEY (id))
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
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 ') NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT
CHARSET=latin1 AUTO_INCRE' at line 5
try using float without the number of showing digit
CREATE TABLE IF NOT EXISTS `tbl_product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`price` double NOT NULL,
PRIMARY KEY (id))
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
and for price could be you need a fixed decimal length so you should use decimal
CREATE TABLE IF NOT EXISTS `tbl_product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`price` decimal(10,2) NOT NULL,
PRIMARY KEY (id))
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
You're missing a value in your double specifier:
`price` double(11) NOT NULL
It needs both the total digits and the digits following the decimal. Something like:
`price` double(11,2) NOT NULL
Though for currency values you may be better off using decimal instead:
`price` decimal(11,2) NOT NULL
As this uses a more fixed precision. Using a double may result in unexpected calculation results from how floating point arithmetic works.

Why I can not create a table in this way?

Why I can not create a table in this way, I get an error?
CREATE TABLE `dynamusic_album` (
`id` VARCHAR(32) NOT NULL,
`title` VARCHAR(100) NULL DEFAULT NULL,
`cover` VARCHAR(100) NULL DEFAULT NULL,
`artist` VARCHAR(32) NULL DEFAULT NULL,
`published` DATETIME NULL DEFAULT NULL,
`description` MEDIUMTEXT NULL,
`genre` INT(11) NULL DEFAULT NULL,
`rating` DOUBLE(11) NOT NULL DEFAULT '1.0',
PRIMARY KEY (`id`)
)
My Error:
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 ') NOT NULL DEFAULT '1.0',
PRIMARY KEY (`id`)
)' at line 9
SQL version = 5.1.15
While specifying numeric datatypes, especially handling decimal point values, you need to specify two arguments. So, DOUBLE(11) needs to be changed.
CREATE TABLE `dynamusic_album` (
`id` VARCHAR(32) NOT NULL,
`title` VARCHAR(100) NULL DEFAULT NULL,
`cover` VARCHAR(100) NULL DEFAULT NULL,
`artist` VARCHAR(32) NULL DEFAULT NULL,
`published` DATETIME NULL DEFAULT NULL,
`description` MEDIUMTEXT NULL,
`genre` INT(11) NULL DEFAULT NULL,
-- assuming you need at most 2 decimal places for rating
`rating` DOUBLE(11,2) NOT NULL DEFAULT '1.0',
PRIMARY KEY (`id`)
)
Refer: https://dev.mysql.com/doc/refman/8.0/en/floating-point-types.html
MySQL permits a nonstandard syntax: FLOAT(M,D) or REAL(M,D) or DOUBLE
PRECISION(M,D). Here, (M,D) means than values can be stored with up to
M digits in total, of which D digits may be after the decimal point.
For example, a column defined as FLOAT(7,4) will look like -999.9999
when displayed. MySQL performs rounding when storing values, so if you
insert 999.00009 into a FLOAT(7,4) column, the approximate result is
999.0001.
Just use DOUBLE instead of DOUBLE(11). AFAICS, MySQL does not know a DOUBLE(x), only DOUBLE(M,D) (see MySQL docs)

How to create mysql table with column timestamp default current_date?

I need to create mysql table with default value on column CURRENT_DATE()
I try
DROP TABLE IF EXISTS `visitors`;
CREATE TABLE `visitors` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`ip` VARCHAR(32) NOT NULL,
`browser` VARCHAR(500) NOT NULL,
`version` VARCHAR(500) NOT NULL,
`platform` ENUM('w','l','m') NOT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_DATE(),
PRIMARY KEY (`id`),
UNIQUE KEY `person` (`ip`,`date`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
but it is writting an error
Query: CREATE TABLE `visitors` ( `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, `ip` VARCHAR(32) NOT NULL, `browser` VARCHAR(500) NO...
Error Code: 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 'CURRENT_DATE() NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `person` (`ip`,`date`)
) ENGINE=INNODB AUTO_INCREMENT=1' at line 7
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0.063 sec
what is the problem?
I need to uniques only date
not with full time info...
can you help me?
Use CURRENT_TIMESTAMP function instead of CURRENT_DATE() function
Try this:
DROP TABLE IF EXISTS `visitors`;
CREATE TABLE `visitors` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`ip` VARCHAR(32) NOT NULL,
`browser` VARCHAR(500) NOT NULL,
`version` VARCHAR(500) NOT NULL,
`platform` ENUM('w','l','m') NOT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `person` (`ip`,`date`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
you just have to replace CURRENT_DATE() by NOW() in your query.
I tried it and it's look ok.
This may be a little late but I think it might be helpful to someone else.
My approach has been to use getdate() like this:
[TimeStamp] [datetime] NOT NULL CONSTRAINT [DF_myTable_TimeStamp] DEFAULT (getdate()).
Where [TimeStamp] is the column in question.
The result is for example: 2017-11-02 11:58:34.203
To trim this, I use the following
declare #mydate datetime
set #mydate = '2017-11-02 11:58:34.203'
SELECT try_convert(nvarchar(20), #mydate, 120)
This final result is: 2017-11-02 11:58:34
You can actually set this in MSSQL Management Studio

Create Table Syntax Error

I received a MySQL data dump and am trying to insert the data into a set of temporary tables. The creation statement for the first table is shown below. When I run this I receive 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 ''temp_books'( 'ID'int( 11 ) NOT NULL AUTO_INCREMENT, 'start'varchar( 20 ) ' at line 1". I've checked the documentation for MySQL syntax, and I don't see that the problem is.
CREATE TABLE 'temp_books' (
'ID' int(11) NOT NULL AUTO_INCREMENT,
'start' varchar(20) NOT NULL,
'customer_id' int(11) NOT NULL DEFAULT '0',
'total_num' int(11) NOT NULL,
'amount' double(5,2) NOT NULL DEFAULT '0.00',
'changed' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('ID'),
UNIQUE KEY 'start' ('start')
) ENGINE=MyISAM AUTO_INCREMENT=4853 DEFAULT CHARSET=latin1;
You shouldn't put single-quotes on your identifiers. If you're going to quote them use the "back tick" character (“`”). You can also use double-quotes but you have to specify that mode:
SET sql_mode='ANSI_QUOTES';
http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
I've ALWAYS had issues with CREATE TABLE. Not sure why. Takes some trial-and-error.
Try this:
CREATE TABLE temp_books (
ID int(11) NOT NULL AUTO_INCREMENT,
start varchar(20) NOT NULL,
customer_id int(11) NOT NULL DEFAULT '0',
total_num int(11) NOT NULL,
amount double(5,2) NOT NULL DEFAULT '0.00',
changed timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (ID),
UNIQUE KEY start (start)
) ENGINE=MyISAM AUTO_INCREMENT=4853 DEFAULT CHARSET=latin1;
I had to delete the quote marks, as well as the default for the changed field, as well as the default charset. Hopefully that won't affect the data.
Here's another way of writing it that might work for some: (left away most of the columns for brevity)
create table temp_books
(
id int not null,
start varchar(255) null,
constraint six_cb_datasource_pk
primary key (id)
);