What's wrong with this simple MySQL CREATE TABLE statement? - mysql

It's a simple CREATE TABLE statement that I'm writing in PHPMyAdmin. Ok, I know I could just do it the easy way in PHPMyAdmin but I like to have full control. Here's the statement:
CREATE TABLE profile
(
id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
type int(1) NOT NULL,
view int(1) NOT NULL default '1',
ver int(1) NOT NULL default '2',
email NOT NULL varchar(32),
password NOT NULL varchar(16),
first varchar(32),
last varchar(32),
site varchar(64),
address varchar(32),
city varchar(32),
zip int,
state char(2),
country varchar(50),
about text,
datereg int(20)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

i replaced
view int(1) NOT NULL default '1',
ver int(1) NOT NULL default '2',
email NOT NULL varchar(32),
password NOT NULL varchar(16),
with
view int(1) NOT NULL default 1,
ver int(1) NOT NULL default 2,
email varchar(32) NOT NULL ,
password varchar(16) NOT NULL ,
and it worked

There are more than one mistake which makes your SQL statement not working.
Try this instead:
CREATE TABLE `profile` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` int(1) NOT NULL,
`view` int(1) NOT NULL DEFAULT '1',
`ver` int(1) NOT NULL DEFAULT '2',
`email` varchar(32) NOT NULL,
`password` varchar(16) NOT NULL,
`first` varchar(32),
`last` varchar(32),
`site` varchar(64),
`address` varchar(32),
`city` varchar(32) ,
`zip` int(11),
`state` char(2),
`country` varchar(50),
`about` text,
`datereg` int(20),
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
A few additions:
Try avoiding reserved words (like view, last, first, and so on) in your tables. It is possible (if
escaped properly), but it helps using none at all.
Escape your field
names and your table name properly.
Read the manual:
http://dev.mysql.com/doc/refman/5.1/en/create-table.html

try this =)
CREATE TABLE `db`.`profile` (
`id` INT NOT NULL AUTO_INCREMENT ,
`type` INT(1) NOT NULL ,
`view` INT(1) NOT NULL DEFAULT 1 ,
`ver` INT(1) NOT NULL DEFAULT 2 ,
`email` VARCHAR(32) NULL ,
`password` VARCHAR(16) NULL ,
`first` VARCHAR(32) NULL ,
`last` VARCHAR(32) NULL ,
`site` VARCHAR(64) NULL ,
`address` VARCHAR(32) NULL ,
`city` VARCHAR(32) NULL ,
`zip` INT NULL ,
`state` CHAR(2) NULL ,
`country` VARCHAR(50) NULL ,
`about` TEXT NULL ,
`datereg` INT(20) NULL ,
PRIMARY KEY (`id`) )
ENGINE = MyISAM DEFAULT CHARSET=utf8;

view is a MySQL Keyword. If you want to use a column with the same name you must surround it with backticks like
`view` int(1) NOT NULL default 1,

Without seeing what the error is, I would say it's failing because you are trying to name a column with a MySQL reserved keyword - my guess is view
If you place backticks ` around your column names, it will allow you to use reserved words.
Just remember any other queries on that table will also need the backticks around the column names which use reserved words.
eg.
CREATE TABLE profile
(
`id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
`type` int(1) NOT NULL,
`view` int(1) NOT NULL default '1',
`ver` int(1) NOT NULL default '2',
`email` NOT NULL varchar(32),
`password` NOT NULL varchar(16),
`first` varchar(32),
`last` varchar(32),
`site` varchar(64),
`address` varchar(32),
`city` varchar(32),
`zip` int,
`state` char(2),
`country` varchar(50),
`about` text,
`datereg` int(20)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Related

insert data into mysql syntax error

Here is the sql that I am trying to insert and I get a error.
insert into instruments (symbol,exchange,FullName,IPOYear,Sector,Industry)
values('PIH','Nasdaq','1347 Property Insurance Holdings, Inc.','Finance','Property-Casualty Insurers','http://www.nasdaq.com/symbol/pih')
here is my ddl and I don't find anything wrong with my sql.
CREATE TABLE `instruments` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`symbol` VARCHAR(100) NOT NULL,
`exchange` VARCHAR(50) NOT NULL,
`FullName` VARCHAR(100) NULL DEFAULT NULL,
`IPOYear` VARCHAR(10) NULL DEFAULT NULL,
`Sector` VARCHAR(20) NULL DEFAULT NULL,
`Industry` VARCHAR(100) NULL DEFAULT NULL,
`LastUpdated` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`)
Sector VARCHAR(20) NULL DEFAULT NULL
You limited Sector to 20 characters and inserted 26.
Make it like this Sector VARCHAR(30) NULL DEFAULT NULL and it should work

Sql error syntaxes with phpmyadmin

Good day,
I have an issue using the phpmyadmin for my database, I'm in new in this and this is the mysql structure from previewmysql:
CREATE TABLE `mydb`.`attendant`
(
`id` INT NOT NULL auto_increment,
`first_name` VARCHAR(20) NOT NULL,
`names` VARCHAR(50) NOT NULL,
`gender` ENUM(0) NOT NULL,
`email` VARCHAR(20) NOT NULL,
`phone` INT(15) NULL,
`marital_status` ENUM(0) NOT NULL,
`added_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`membership` ENUM(0) NOT NULL,
`address` VARCHAR(20) NOT NULL,
`suburb` ENUM(0) NOT NULL,
`partner_name` VARCHAR(25) NULL,
PRIMARY KEY (`id`),
UNIQUE `email_address` (`email`)
)
engine = innodb;
The error 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 '0) NOT NULL , `email` VARCHAR(20) NOT NULL , `phone` INT(15) NULL , `marital_sta' at line 1
And also attached is my phpmyadmin table structure.
Will appreciate any help.
Try it.Hope errors goes boom.I just fixed your errors. But your table structure is not good enough. give time, then day by day you will also be expert on it.
CREATE TABLE `mydb`.`attendant`
(
`id` INT NOT NULL auto_increment,
`first_name` VARCHAR(20) NOT NULL,
`names` VARCHAR(50) NOT NULL,
`gender` ENUM('0','1') NOT NULL,
`email` VARCHAR(20) NOT NULL,
`phone` INT(15),
`marital_status` ENUM('0','1') NOT NULL,
`added_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`membership` ENUM('0','1') NOT NULL,
`address` VARCHAR(20) NOT NULL,
`suburb` ENUM('0','1') NOT NULL,
`partner_name` VARCHAR(25) NULL,
PRIMARY KEY (`id`),
UNIQUE `email_address` (`email`)
)
engine = innodb;
Modify ENUM declaration as ENUM ('male', 'female') for gender column and others also as shown in your table. It will not accept ENUM(0).
ENUM(0) is wrong format , if you want that for gender roles then you can use :-
ENUM('Male', 'Female') i.e you can run this query :-
CREATE TABLE `mydb`.`attendant`
(
`id` INT NOT NULL auto_increment,
`first_name` VARCHAR(20) NOT NULL,
`names` VARCHAR(50) NOT NULL,
`gender` ENUM('Male', 'Female') NOT NULL,
`email` VARCHAR(20) NOT NULL,
`phone` INT(15) NULL,
`marital_status` ENUM('Single','Married','divorced') NOT NULL,
`added_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`membership` ENUM('no','yes') NOT NULL,
`address` VARCHAR(20) NOT NULL,
`suburb` ENUM('Cape Town','Woodstock') NOT NULL,
`partner_name` VARCHAR(25) NULL,
PRIMARY KEY (`id`),
UNIQUE `email_address` (`email`)
)
engine = innodb;
You have used ENUM data type in your table. And provided 0 as argument but
An enumeration value must be a quoted string literal
You can refer the mySql documentation for more information
http://dev.mysql.com/doc/refman/5.7/en/enum.html

MySQL: errno: 105 can't create table

I wanted to make a database exactly as below:
So I wanted to define the primary and foreign keys for each table.
Based on this answer, I saw that:
"city" table has 1 PK (ID) and 1 FK (countrycode)
"countrylanguage" table has 2 PK (Language and countrycode) and 1 FK
(countrycode)
"country" table has 1 PK (Code)
So I tried to make some magic on a "pre-heated" code:
CREATE TABLE `City` (
`ID` int(11) NOT NULL,
`Name` varchar(35) NOT NULL ,
`CountryCode` varchar(3) NOT NULL DEFAULT '',
`District` varchar(20) NOT NULL ,
`Population` int(11) NOT NULL ,
PRIMARY KEY(`ID`) ,
FOREIGN KEY(`CountryCode`) REFERENCES `Country`(`Code`)
) ;
CREATE TABLE `CountryLanguage` (
`CountryCode` varchar(3) NOT NULL DEFAULT '',
`Language` varchar(30) NOT NULL ,
`IsOfficial` varchar(30) NOT NULL ,
`Percentage` float(4,1) NOT NULL ,
PRIMARY KEY(`Language`),
FOREIGN KEY(`CountryCode`) REFERENCES `Country`(`Code`)
) ;
CREATE TABLE `Country` (
`Code` varchar(3) NOT NULL DEFAULT '',
`Name` varchar(52) NOT NULL DEFAULT '',
`Continent` varchar(63),
`Region` varchar(26) NOT NULL DEFAULT '',
`SurfaceArea` float(10,2) NOT NULL DEFAULT '0.00',
`IndepYear` smallint(6) DEFAULT NULL,
`Population` int(11) NOT NULL DEFAULT '0',
`LifeExpectancy` float(3,1) DEFAULT NULL,
`GNP` float(10,2) DEFAULT NULL,
`GNPOld` float(10,2) DEFAULT NULL,
`LocalName` varchar(45) NOT NULL DEFAULT '',
`GovernmentForm` varchar(45) NOT NULL DEFAULT '',
`HeadOfState` varchar(60) DEFAULT NULL,
`Capital` int(11) DEFAULT NULL,
`Code2` varchar(2) NOT NULL DEFAULT '',
PRIMARY KEY(`Code`)
) ;
but my good ol' mysql command line client has the same ERROR 1005 thing twice, and says that it can't create tables 'test.city' and 'test.countrylanguage'
with the errno:150 thingy as an explanation.
So I searched a bit around here and I found some answers regarding to table elements not having the same type/parameter (f.e. INT(2) to INT(2) NOT NULL). As fas as I could see, Nothing like this happens here.
What is my coffee-drained brain missing here?
Thank you for your time in advance.
CREATE TABLE Country first, then CREATE TABLE City, and CREATE TABLE CountryLanguage, since TABLE Country is referenced by the other two tables.
wrong create sequnce you should create firts Country because City and CountryLanguage refer to country table
CREATE TABLE `Country` (
`Code` varchar(3) NOT NULL DEFAULT '',
`Name` varchar(52) NOT NULL DEFAULT '',
`Continent` varchar(63),
`Region` varchar(26) NOT NULL DEFAULT '',
`SurfaceArea` float(10,2) NOT NULL DEFAULT '0.00',
`IndepYear` smallint(6) DEFAULT NULL,
`Population` int(11) NOT NULL DEFAULT '0',
`LifeExpectancy` float(3,1) DEFAULT NULL,
`GNP` float(10,2) DEFAULT NULL,
`GNPOld` float(10,2) DEFAULT NULL,
`LocalName` varchar(45) NOT NULL DEFAULT '',
`GovernmentForm` varchar(45) NOT NULL DEFAULT '',
`HeadOfState` varchar(60) DEFAULT NULL,
`Capital` int(11) DEFAULT NULL,
`Code2` varchar(2) NOT NULL DEFAULT '',
PRIMARY KEY(`Code`)
) ;
CREATE TABLE `City` (
`ID` int(11) NOT NULL,
`Name` varchar(35) NOT NULL ,
`CountryCode` varchar(3) NOT NULL DEFAULT '',
`District` varchar(20) NOT NULL ,
`Population` int(11) NOT NULL ,
PRIMARY KEY(`ID`) ,
FOREIGN KEY(`CountryCode`) REFERENCES `Country`(`Code`)
) ;
CREATE TABLE `CountryLanguage` (
`CountryCode` varchar(3) NOT NULL DEFAULT '',
`Language` varchar(30) NOT NULL ,
`IsOfficial` varchar(30) NOT NULL ,
`Percentage` float(4,1) NOT NULL ,
PRIMARY KEY(`Language`),
FOREIGN KEY(`CountryCode`) REFERENCES `Country`(`Code`)
) ;

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.

Unexpected behaviour of the query

This particular query is supposed to enter the user into the database.
This query does not always insert the values of the firstname and the lastname fields along with others. firstname and lastname are empty for a few insertions and for the others its working as expected.
INSERT INTO `users` (mobile, passwordHash, firstname, lastname, ent_id, email )
VALUES ('913800341127', '678a1491514b7f1006d605e9161946b1', 'nat', 'sam', '108', NULL)
ON DUPLICATE KEY UPDATE `firstname` = VALUES(firstname),`lastname` = VALUES(lastname)
Related Info:
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`tag` varchar(5) NOT NULL default 'ind',
`username` varchar(50) default NULL,
`firstname` varchar(100) default NULL,
`lastname` varchar(100) default NULL,
`passwordhash` varchar(255) NOT NULL,
`secretq` varchar(255) default NULL,
`secreta` varchar(100) default NULL,
`email` varchar(50) default NULL,
`mobile` varchar(13) default NULL,
`last_login` datetime default NULL,
`ent_id` bigint(20) NOT NULL default '1',
`is_inactive` tinyint(1) NOT NULL COMMENT 'Whether the user is active or not',
PRIMARY KEY (`id`),
UNIQUE KEY `mobile_2` (`mobile`,`ent_id`),
UNIQUE KEY `email_2` (`email`,`ent_id`),
KEY `username` (`username`),
KEY `ent_id` (`ent_id`,`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
syntax error it should be :
ON DUPLICATE KEY UPDATE firstname = 'nat',lastname = 'sam'