Related
Following a tutorial for web development. Here's the SQL code
CREATE TABLE 'users_relationships' (
'users_relationship_id' INT(10) NOT NULL,
'from_user_id' INT(10) NOT NULL,
'to_user_id' INT(10) unsigned NOT NULL,
'users_relationship_type' VARCHAR(10) NOT NULL,
'users_relationship_timestamp' DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('users_relationship_id'),
INDEX 'from_user_id' ('from_user_id'),
INDEX 'to_user_id' ('to_user_id'),
INDEX 'from_user_id_to_user_id' ('from_user_id', 'to_user_id'),
INDEX 'from_user_id_to_user_id_users_relationship_type' ('from_user_id', 'to_user_id', 'users_relationship_type'));
Whenever I run it i get the following 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 ''users_relationships' (
'users_relationship_id' INT(10) NOT NULL,
'from_' at line 1
I tried multiple changes but it keeps giving me the same error. The code is directly from the book. Any ideas?
You don't need backticks or single quotes. Just strain on the body. Single quotes however are downright syntax errors waiting to happen.
CREATE TABLE users_relationships (
users_relationship_id INT(10) NOT NULL,
from_user_id INT(10) NOT NULL,
to_user_id INT(10) unsigned NOT NULL,
users_relationship_type VARCHAR(10) NOT NULL,
users_relationship_timestamp DATETIME not null,
PRIMARY KEY (users_relationship_id),
INDEX from_user_id(from_user_id),
INDEX to_user_id (to_user_id),
INDEX from_user_id_to_user_id (from_user_id, to_user_id),
INDEX from_user_id_to_user_id_users_relationship_type (from_user_id, to_user_id, users_relationship_type)
);
Backticks are cute. But needed only when the the names would otherwise violate reserved words. Though they are generated by tools for create table dumps, why bother with them unless you really hate your little left pinky. Just my opinion.
If you want to use MySQL quotes, you need to use `, or else MySQL will mess up and think there is some string being created instead of table columns (typically for column names, you don't use ', or else MySQL will think a string is being used for data selection)
CREATE TABLE `users_relationships` (
`users_relationship_id` INT(10) NOT NULL,
`from_user_id` INT(10) NOT NULL,
`to_user_id` INT(10) unsigned NOT NULL,
`users_relationship_type` VARCHAR(10) NOT NULL,
`users_relationship_timestamp` DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`users_relationship_id`),
INDEX `from_user_id` (`from_user_id`),
INDEX `to_user_id` (`to_user_id`),
INDEX `from_user_id_to_user_id` (`from_user_id`, `to_user_id`),
INDEX `from_user_id_to_user_id_users_relationship_type` (`from_user_id`, `to_user_id`, `users_relationship_type`));
Here's the working SQLFiddle here
first u should replace all your single quotes with backticks.
CREATE TABLE `users_relationships` (
`users_relationship_id` INT(10) NOT NULL,
`from_user_id` INT(10) NOT NULL,
`to_user_id` INT(10) unsigned NOT NULL,
`users_relationship_type` VARCHAR(10) NOT NULL,
`users_relationship_timestamp` DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`users_relationship_id`),
INDEX `from_user_id` (`from_user_id`),
INDEX `to_user_id` (`to_user_id`),
INDEX `from_user_id_to_user_id` (`from_user_id`, `to_user_id`),
INDEX `from_user_id_to_user_id_users_relationship_type` (`from_user_id`, `to_user_id`, `users_relationship_type`));
second u cannot have DATETIME DEFAULT CURRENT_TIMESTAMP
or DATETIME DEFAULT ''
or TIMESTAMP DEFAULT CURRENT_TIMESTAMP
I am trying to create a table in my database via terminal.
Here is my syntax:
CREATE TABLE `users` (
PRIMARY KEY(id) NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR NOT NULL,
`first_name` VARCHAR NOT NULL,
`gender` VARCHAR NOT NULL,
`fav_color` VARCHAR NOT NULL,
`birthdate` DATE NOT NULL
);
I am getting this error:
ERROR 1064 (42000): 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 AUTO_INCREMENT,
`last_name` VARCHAR NOT NULL,
`first_name` VARCHA' at line 2
What am i dong wrong here?
Besides the issues that Jens and Marc pointed out, You have to declare the length of your Varchar fields in order for this statement to work, like so:
CREATE TABLE `test`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR(45) NOT NULL,
`first_name` VARCHAR(45) NOT NULL,
`gender` VARCHAR(45) NOT NULL,
`fav_color` VARCHAR(45) NOT NULL,
`birthdate` DATE NOT NULL,
PRIMARY KEY (`id`));
the syntax of your create statement is wrong:
the correct one is this:
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR(255) NOT NULL,
`first_name` VARCHAR(255) NOT NULL,
`gender` VARCHAR(255) NOT NULL,
`fav_color` VARCHAR(255) NOT NULL,
`birthdate` DATE NOT NULL,
PRIMARY KEY(`id`)
);
For more Information see the offical documentation.
it should be
id int primary key auto_increment not null
You're trying to define a primary key on a field that doesn't exist. Keys cannot be "not null" and definitely cannot be "auto_increment".
How can I avoid getting this MySQL error Incorrect column specifier for column topic_id ?
MySQL Error...
#1063 - Incorrect column specifier for column 'topic_id'
SQL Schema...
CREATE TABLE discussion_topics (
topic_id char(36) NOT NULL AUTO_INCREMENT,
project_id char(36) NOT NULL,
topic_subject VARCHAR(255) NOT NULL,
topic_content TEXT default NULL,
date_created DATETIME NOT NULL,
date_last_post DATETIME NOT NULL,
created_by_user_id char(36) NOT NULL,
last_post_user_id char(36) NOT NULL,
posts_count char(36) default NULL,
PRIMARY KEY (topic_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
To use AUTO_INCREMENT you need to deifne column as INT or floating-point types, not CHAR.
AUTO_INCREMENT use only unsigned value, so it's good to use UNSIGNED as well;
CREATE TABLE discussion_topics (
topic_id INT NOT NULL unsigned AUTO_INCREMENT,
project_id char(36) NOT NULL,
topic_subject VARCHAR(255) NOT NULL,
topic_content TEXT default NULL,
date_created DATETIME NOT NULL,
date_last_post DATETIME NOT NULL,
created_by_user_id char(36) NOT NULL,
last_post_user_id char(36) NOT NULL,
posts_count char(36) default NULL,
PRIMARY KEY (topic_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
The auto_increment property only works for numeric columns (integer and floating point), not char columns:
CREATE TABLE discussion_topics (
topic_id INT NOT NULL AUTO_INCREMENT,
project_id char(36) NOT NULL,
topic_subject VARCHAR(255) NOT NULL,
topic_content TEXT default NULL,
date_created DATETIME NOT NULL,
date_last_post DATETIME NOT NULL,
created_by_user_id char(36) NOT NULL,
last_post_user_id char(36) NOT NULL,
posts_count char(36) default NULL,
PRIMARY KEY (topic_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
Quoting the doc:
Some attributes do not apply to all data types. AUTO_INCREMENT applies
only to integer and floating-point types. DEFAULT does not apply to
the BLOB or TEXT types.
In your case, you're trying to apply AUTO_INCREMENT modifier to char column. To solve this, either drop AUTO_INCREMENT altogether (that means you'll have to generate a unique id on the application level) or just change topic_id type to the relevant integer one.
As a sidenote, it makes little sense using char(36) to store the posts count, so that column's type probably has to be changed as well. It looks like you're going this way to prevent integer overflow - but if you're dealing with more than 18446744073709551615 posts (the biggest number that can be stored in BIGINT UNSIGNED column) in a single topic, you have far bigger problem on your side probably. )
You cannot auto increment the char values. It should be int or long(integers or floating points).
Try with this,
CREATE TABLE discussion_topics (
topic_id int(5) NOT NULL AUTO_INCREMENT,
project_id char(36) NOT NULL,
topic_subject VARCHAR(255) NOT NULL,
topic_content TEXT default NULL,
date_created DATETIME NOT NULL,
date_last_post DATETIME NOT NULL,
created_by_user_id char(36) NOT NULL,
last_post_user_id char(36) NOT NULL,
posts_count char(36) default NULL,
PRIMARY KEY (`topic_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
Hope this helps
I was having the same problem, but using Long type. I changed for INT and it worked for me.
CREATE TABLE lists (
id INT NOT NULL AUTO_INCREMENT,
desc varchar(30),
owner varchar(20),
visibility boolean,
PRIMARY KEY (id)
);
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).
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.