What's wrong with this simple MySQL CREATE TABLE statement?
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 varcha开发者_运维技巧r(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;
精彩评论