sql error code table - mysql

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.

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.

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)

Syntax error creating table

When i try to create table using the below code,
CREATE TABLE `audit` (
`audit_id` int(10) unsigned NOT NULL auto_increment,
`order_id` int(11) NOT NULL default '0',
`datestamp` datetime NOT NULL default '0000-00-00 00:00:00',
`message` text NOT NULL,
`message_number` int(11) NOT NULL default '0',
PRIMARY KEY (`audit_id`)
) TYPE=MyISAM AUTO_INCREMENT=196 ;
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 'TYPE=MyISAM AUTO_INCREMENT=8'
Can someone help me to rectify the error?
Thanks
yes... use ENGINE instead TYPE like this
CREATE TABLE `category` (
`category_id` int(10) unsigned NOT NULL auto_increment,
`department_id` int(10) unsigned NOT NULL default '0',
`name` varchar(50) NOT NULL default '',
`description` varchar(200) default NULL,
PRIMARY KEY (`category_id`)
) ENGINE=MyISAM

MySQL error syntax

I'm sort of confused here, what is wrong with my syntax?
CREATE TABLE `users` (
`userId` int(7) NOT NULL AUTO_INCREMENT,
`firstName` varchar(30) NOT NULL,
`lastName` varchar(30) NOT NULL,
`gender` varchar(1) NOT NULL,
`birthday` datetime NOT NULL,
`city` varchar(20) NOT NULL,
`province` varchar(20) NOT NULL,
`postalCode` varchar(6) NOT NULL,
`country` varchar(20) NOT NULL,
`email` varchar(30) NOT NULL,
`password` varchar(32) NOT NULL,
`bio` text NOT NULL,
`active` int(1) NOT NULL DEFAULT(0),
PRIMARY KEY (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
This was auto generated from an SQL export. It gives the following 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), PRIMARY KEY (userId) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCR' at line 14
Specifying the default value is wrong.There is no need of braces
Replace
`active` int(1) NOT NULL DEFAULT (0),
with
`active` int(1) NOT NULL DEFAULT 0,

SQL CREATE TABLE Error

The Answer was that I was using incorrect quotation marks instead of backticks. Stupid syntax hilighter tricked me.
I've been stuck on this one simple(ish) thing for the last 1/2 hour so I thought I might try to get a quick answer here.
What exactly is incorrect about my SQL syntax, assuming I'm using mysql 5.1
CREATE TABLE 'users' (
'id' INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
'username' VARCHAR(20) NOT NULL,
'password' VARCHAR(40) NOT NULL,
'salt' VARCHAR(40) DEFAULT NULL,
'email' VARCHAR(80) NOT NULL,
'created_on' INT(11) UNSIGNED NOT NULL,
'last_login' INT(11) UNSIGNED DEFAULT NULL,
'active' TINYINT(1) UNSIGNED DEFAULT NULL,
)
ENGINE InnoDB;
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 ''users';
CREATE TABLE 'users' (
'id' INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,' at line 3
Elapsed Time: 0 hr, 0 min, 0 sec, 0 ms.
Also, does anyone have any good tutorials about how to use Zend_Auth for complete noobs?
Thanks.
Table and column identifiers are quoted using backticks (or double quotes if you've configured them).
Additionally you have a comma at the end of your column list.
CREATE TABLE `users` (
`id` MEDIUMINT( 8 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR( 20 ) NOT NULL,
`password` VARCHAR( 40 ) NOT NULL,
`salt` VARCHAR( 40 ) DEFAULT NULL,
`email` VARCHAR( 80 ) NOT NULL,
`created_on` INT( 11 ) UNSIGNED NOT NULL,
`last_login` INT( 11 ) UNSIGNED DEFAULT NULL,
`active` TINYINT( 1 ) UNSIGNED DEFAULT NULL
) ENGINE InnoDB
You are using single quotes instead of backticks for your table and field names, which is wrong. There should also be an equals sign between ENGINE and InnoDB.
Here's the fixed SQL:
CREATE TABLE `users` (
`id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(20) NOT NULL,
`password` VARCHAR(40) NOT NULL,
`salt` VARCHAR(40) DEFAULT NULL,
`email` VARCHAR(80) NOT NULL,
`created_on` INT(11) UNSIGNED NOT NULL,
`last_login` INT(11) UNSIGNED DEFAULT NULL,
`active` TINYINT(1) UNSIGNED DEFAULT NULL
)
ENGINE = InnoDB;