Why I can not create a table in this way? - mysql

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)

Related

#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.

what is my syntax error here in mySQL query?

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

insert error code 1241 operand should contain 1 column(s)

I am trying to Insert data into zone_sig
INSERT INTO zone_sig values (0,0,0,0,'24','Siliana','Kesra','2459',(0.95830721023,0.03643552658),'Région du Nord-Ouest',0);
but i get the error:
error code 1241. operand should contain 1 column(s)
Table schema:
zone_sig (
ID int(11) NOT NULL AUTO_INCREMENT,
ADM_IDE int(11) DEFAULT NULL,
SURFACE double DEFAULT NULL,
PERIMETRE double DEFAULT NULL,
ADM_COD varchar(255) DEFAULT NULL,
ADM_GOV varchar(255) DEFAULT NULL,
ADM_DEL varchar(255) DEFAULT NULL,
CODE_INS varchar(255) DEFAULT NULL,
ogc_geom geometry DEFAULT NULL,
NOM_ZONE varchar(250) DEFAULT NULL,
NIVEAU int(11) DEFAULT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB AUTO_INCREMENT=269 DEFAULT CHARSET=latin1;
When setting a geometry field, you can't use (0.95830721023,0.03643552658) directly in the query. MySQL doesn't understand how to use that.
You need to use the Point function here.
INSERT INTO zone_sig values (0,0,0,0,'24','Siliana','Kesra','2459',Point(0.95830721023,0.03643552658),'Région du Nord-Ouest',0);
DEMO: http://sqlfiddle.com/#!2/7128c8/1
Docs: http://dev.mysql.com/doc/refman/5.1/en/creating-spatial-values.html (see section 12.17.4.2.3)

create table error in mysql. 1071 error

hi all can some one plz help me,
while creating table in mysql i am getting the following error
MySQL error 1071: Specified key was too long; max key length is 767 bytes
my table definition is as follows.
CREATE TABLE oauth_consumer (id char(36) NOT NULL ,
name varchar(255) NULL ,
date_entered datetime NULL ,
date_modified datetime NULL ,
modified_user_id char(36) NULL ,
created_by char(36) NULL ,
description text NULL ,
deleted bool DEFAULT '0' NULL ,
assigned_user_id char(36) NULL ,
c_key varchar(255) NULL ,
c_secret varchar(255) NULL,
PRIMARY KEY (id),
UNIQUE ckey (c_key)
);
No problem in your query. May be you are running old version of MySql. Use new version. If you are using new version try to use following query to create table
CREATE TABLE `oauth_consumer` (
`id` CHAR(36) NOT NULL,
`name` VARCHAR(255) NULL DEFAULT NULL,
`date_entered` DATETIME NULL DEFAULT NULL,
`date_modified` DATETIME NULL DEFAULT NULL,
`modified_user_id` CHAR(36) NULL DEFAULT NULL,
`created_by` CHAR(36) NULL DEFAULT NULL,
`description` TEXT NULL,
`deleted` TINYINT(1) NULL DEFAULT '0',
`assigned_user_id` CHAR(36) NULL DEFAULT NULL,
`c_key` VARCHAR(255) NULL DEFAULT NULL,
`c_secret` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `ckey` (`c_key`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
First of all, your database character set is most likely set to utf8, so your 255 characters get blown up to 765 characters; though close, that's not greater than the imposed limit of 767, but it gives an idea of why you're hitting that limit nonetheless.
Regardless, it's not recommended to use such a wide key; you could calculate an md5 or sha1 of the value and use that for the unique key instead (btw, you should use the binary representation of the hash).

sql error code table

CREATE TABLE IF NOT EXISTS `users` ( 
`id` int(11) NOT NULL auto_increment, 
`username` varchar(32) NOT NULL, 
`password` varchar(32) NOT NULL, 
`online` int(20) NOT NULL default '0′, 
`email` varchar(100) NOT NULL, 
`active` int(1) NOT NULL default '0′, 
`rtime` int(20) NOT NULL default '0′, 
PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
This is the original phpmyadmin error message, when I enter the code shown above sql to create the table:
#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
'`password` varchar (32) NULL, `On-line` int NOT (20) NULL default '0 ', NON '
at line 4
I would like to understand how this code should be written correctly and what is wrong!
You should remove strange '0′ quotes for int default values. This should work for you:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
`online` int(20) NOT NULL default 0,
`email` varchar(100) NOT NULL,
`active` int(1) NOT NULL default 0,
`rtime` int(20) NOT NULL default 0,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
The problem is that you are not closing you quotes correctly. You have '0′ instead of '0'.
You can either fix them of get rid of the quotes at all, since the columns are defined as integer.