SQL CREATE TABLE Error - mysql

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;

Related

ERROR 1067 Invalid defaul value for 'created_at'

I am creating a table with the following script
CREATE TABLE `ninjapizza`.`products` (
`ID` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(45) NULL,
`description` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
`created_on` TIMESTAMP(2) NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`));
But on Apply I get the following message
Operation failed: There was an error while applying the SQL script to the database.
Executing:
CREATE TABLE `ninjapizza`.`products` (
`ID` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(45) NULL,
`description` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
`created_on` TIMESTAMP(2) NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`));
ERROR 1067: Invalid default value for 'created_on'
SQL Statement:
CREATE TABLE `ninjapizza`.`products` (
`ID` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(45) NULL,
`description` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
`created_on` TIMESTAMP(2) NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`))
I have also executed the following command
SET sql_mode = 'ALLOW_INVALID_DATES';
But still getting the error. Can someone please guide.
'If a TIMESTAMP or DATETIME column definition includes an explicit fractional seconds precision value anywhere, the same value must be used throughout the column definition'
https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
SEE https://www.db-fiddle.com/f/sNoeVP5PATCsGxhVw2vPki/0

MySQL- Invalid default value for 'lasttime'

SQL sorgusu:
CREATE TABLE online(
idonline İNT( 10 ) UNSİGNED NOT NULL AUTO_INCREMENT ,
ip VARCHAR( 16 ) ,
domain VARCHAR( 100 ) ,
FKiduyeler İNT( 10 ) UNSİGNED,
lasttime TİMESTAMP DEFAULT 'CURRENT_TIMESTAMP',
PRIMARY KEY ( idonline ) ,
KEY online_index3587( ip ) ,
KEY online_index3588( domain ) ,
KEY online_index3592( FKiduyeler ) ,
KEY online_index3604( lasttime )
);
MySQL çıktısı: Belgeler
#1067 - Invalid default value for 'lasttime'
Before everthing sorry about my english; when l upload my db ,l got this proglem.Please help me.Thanks
CURRENT_TIMESTAMP instead of 'CURRENT_TIMESTAMP'
I instead of İ at multiple places.
CREATE TABLE `online`(
idonline INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
ip VARCHAR( 16 ) ,
domain VARCHAR( 100 ) ,
FKiduyeler INT( 10 ) UNSIGNED,
lasttime TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- lose the single quotes here
PRIMARY KEY ( idonline ) ,
KEY online_index3587( ip ) ,
KEY online_index3588( domain ) ,
KEY online_index3592( FKiduyeler ) ,
KEY online_index3604( lasttime )
);
Thank for helping, but when l do that I'm getting this problem:
CREATE TABLE visitor_chats ( visitor_id varchar(32) NOT NULL,
browser_id varchar(32) NOT NULL, visit_id varchar(32) NOT NULL,
chat_id int(11) unsigned NOT NULL, fullname varchar(255) NOT NULL,
email varchar(255) NOT NULL, company varchar(255) NOT NULL, status
tinyint(1) unsigned NOT NULL, typing tinyint(1) unsigned NOT NULL,
waiting tinyint(1) unsigned NOT NULL, area_code varchar(255) NOT NULL,
first_active int(10) unsigned NOT NULL, last_active int(10) unsigned
NOT NULL, qpenalty int(10) unsigned NOT NULL, request_operator
varchar(32) NOT NULL, request_group varchar(32) NOT NULL, question
varchar(255) NOT NULL, customs text NOT NULL, allocated int(11)
unsigned NOT NULL, internal_active tinyint(1) unsigned NOT NULL,
internal_closed tinyint(1) unsigned NOT NULL, internal_declined
tinyint(1) unsigned NOT NULL, external_active tinyint(1) unsigned NOT
NULL, external_close tinyint(1) unsigned NOT NULL, exit int(11) [...]
MySQL 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 'exit int(11) unsigned NOT NULL,PRIMARY KEY
(visitor_id,browser_id,visit_id,c' at line 26"

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.

Error in SQL Syntax (MYSQL 5.0)

Does anybody know what is wrong in this MYSQL 5.0 syntax?
CREATE TABLE IF NOT EXISTS target (
_id int(11) NOT NULL AUTO_INCREMENT,
time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
map_id int(11) DEFAULT NULL,
left int(11) DEFAULT NULL,
top int(11) DEFAULT NULL,
status tinyint(1) NOT NULL,
temperature int(11) DEFAULT NULL,
humidity float DEFAULT NULL,
lum int(11) DEFAULT NULL,
PRIMARY KEY (_id),
FOREIGN KEY (map_id) REFERENCES map(id) ON DELETE CASCADE
)
I'll show you 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 'left INTEGER DEFAULT NULL,
top INTEGER DEFAULT NULL,
status tinyint(1) NOT' at line 5
Because left is a MySQL 5.0 reserved word. Also, even though you can escape the field name, it's never a great idea to use reserved words in a table definition.
you must write it like this:
CREATE TABLE IF NOT EXISTS target (
_id int(11) NOT NULL AUTO_INCREMENT,
time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
map_id int(11) DEFAULT NULL,
`left` int(11) DEFAULT NULL,
top int(11) DEFAULT NULL,
status tinyint(1) NOT NULL,
temperature int(11) DEFAULT NULL,
humidity float DEFAULT NULL,
lum int(11) DEFAULT NULL,
PRIMARY KEY (_id),
FOREIGN KEY (map_id) REFERENCES map(id) ON DELETE CASCADE
)
look at the `` (backticks) characters in left row !
You're using reserved words as field names. You can do that, but then you have to properly escape them, like this:
CREATE TABLE IF NOT EXISTS target (
`_id` int(11) NOT NULL AUTO_INCREMENT,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`map_id` int(11) DEFAULT NULL,
`left` int(11) DEFAULT NULL,
`top` int(11) DEFAULT NULL,
`status` tinyint(1) NOT NULL,
`temperature` int(11) DEFAULT NULL,
`humidity` decimal(13,2) DEFAULT NULL,
`lum` int(11) DEFAULT NULL,
PRIMARY KEY (_id),
FOREIGN KEY (map_id) REFERENCES map(id) ON DELETE CASCADE
)
My advise would be to avoid reserved names.

why Mysql is giving me error 1280 "Wrong Index"

Can anyone explain why Mysql is giving me error 1280 ("wrong index for 'fk_chart_aid_aid' ") error whend I try to create the "CHART OF ACCOUNTS" table. I'm completly confused here. How can I fix this so I can create the table? The "ACCOUNT" table already exists in the database and has data in it.
Thanks for the help.
MYSQL Server version: 5.1.54
CHART OF ACCOUNTS:
DROP TABLE IF EXISTS `rst`.`acctg_chart_of_accounts` ;
CREATE TABLE IF NOT EXISTS `rst`.`acctg_chart_of_accounts` (
`acctg_chart_of_accounts_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`account_id` INT UNSIGNED NOT NULL ,
`account_nbr` VARCHAR(45) NULL ,
`description` VARCHAR(45) NULL ,
`account_type` INT UNSIGNED NULL ,
`commissionable` TINYINT UNSIGNED NULL ,
`hidden` TINYINT UNSIGNED NULL ,
`deduct_balance_from_owner_check` TINYINT UNSIGNED NULL ,
PRIMARY KEY (`acctg_chart_of_accounts_id`) ,
CONSTRAINT `fk_chart_aid_aid`
FOREIGN KEY (`account_id` )
REFERENCES `rst`.`account` (`account_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE INDEX `fk_chart_aid_aid` ON `rst`.`acctg_chart_of_accounts` (`account_id` ASC) ;
ACCOUNTS TABLE THAT IS BEING REFERENCED:
CREATE TABLE IF NOT EXISTS `account` (
`account_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_status_id` int(10) unsigned NOT NULL,
`company_name` varchar(155) DEFAULT NULL,
`address1` varchar(155) DEFAULT NULL,
`address2` varchar(155) DEFAULT NULL,
`city` varchar(155) DEFAULT NULL,
`state` varchar(155) DEFAULT NULL,
`zip` varchar(45) DEFAULT NULL,
`country` varchar(255) DEFAULT NULL,
`work_phone` varchar(45) DEFAULT NULL,
`mobile_phone` varchar(45) DEFAULT NULL,
`time_zone` varchar(45) DEFAULT NULL,
`subdomain` varchar(155) DEFAULT NULL,
`cname_URL` varchar(255) DEFAULT NULL,
`promotion_code` varchar(45) DEFAULT NULL,
`can_we_contact_you` tinyint(4) DEFAULT NULL COMMENT '0=false, 1=true',
`units_managed_nbr` varchar(10) DEFAULT NULL,
`a_hear_about_us_list_id` tinyint(3) unsigned DEFAULT NULL COMMENT 'populated from dropdown list.',
`receive_special_offers` tinyint(4) DEFAULT NULL,
`receive_announcements` tinyint(4) DEFAULT NULL,
`receive_newsletter` tinyint(4) DEFAULT NULL,
`create_ts` timestamp NULL DEFAULT NULL,
`expires` timestamp NULL DEFAULT NULL,
`storage_capacity` varchar(255) DEFAULT NULL COMMENT '1073741824 = 1GB',
`logo` varchar(455) DEFAULT NULL,
`max_active_connections` int(11) DEFAULT '3',
`_product_id` int(11) DEFAULT NULL,
`report_footer` varchar(455) DEFAULT NULL,
`welcome_dialog` tinyint(4) DEFAULT '1',
`ARB_subscription_id` int(11) DEFAULT NULL,
`trashbin` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`account_id`),
KEY `fk_account_account_status_id` (`account_status_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=58 ;
Are you getting the error after the CREATE TABLE statement, or after the subsequent CREATE INDEX?
Looks like you are attempting to name both a FOREIGN KEY constraint and an INDEX fk_chart_aid_aid. Try choosing a different name for either one of them.
Also, in the accounts table, account_id is INT(10). Try also to change the column definition in acctg_chart_of_accounts to:
`account_id` INT(10) UNSIGNED NOT NULL ,
Though, I think that mysql defaults type INT to INT(10) anyway...
I met the same issue; tried manually rename the index to a different name but don't like the idea of 'manually' and neither I don't really understand why we need to generate the index separately. so I decide to generate it within the create statement by unchecking the option of 'generate separate index statement' in 'forwarding engineer', and this fix the issue.