CREATE TABLE OWLUpdates(
id INT AUTO_INCREMENT NOT NULL,
website INT, INDEX website__idx (website), FOREIGN KEY (website) REFERENCES OWLWebsite (id) ON DELETE CASCADE,
suburl VARCHAR(255),
sendtimes INT,
title VARCHAR(255) UNIQUE,
description LONGTEXT,
is_show CHAR(1),
reads INT,
degrees INT,
mtime DATETIME,
PRIMARY KEY(id)
) ENGINE=InnoDB CHARACTER SET utf8;
What's the error ??
the web2py report :
(1064, u"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 'reads INT,\n
degrees INT,\n mtime DATETIME,\n PRIMARY KEY(id)\n) ENGINE=Inn' at
line 9")
READS is a Reserved Keyword in MySQL. In order to use keywords, you should wrap it with backticks,
`READS` INT
MySQL Reserved Keywords List
But as an advise, refrain from using reserved keywords to prevent future problems.
Related
I am new to SQL and I am trying to figure out what am I doing wrong here. The error that I am getting is:
#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 'Cast( movieId int, actorId int, salary decimal(7,2) default '77777.77', fore' at line 1
create table Cast(
movieId int,
actorId int,
salary decimal(7,2) default '77777.77',
primary key(movieId,actorId)
foreign key(actorId) references Actor(actorId),
foreign key(movieId) references Movie(movieId)
);
When you use Cast(... with no space between Cast and the (, MySQL thinks you are trying to use the CAST() builtin function. That makes no sense following CREATE TABLE, so MySQL is confused.
See https://dev.mysql.com/doc/refman/5.7/en/function-resolution.html
If you put a space before the paren, that fixes that problem.
create table Cast (
^
You also have another minor mistake, you forgot a comma after your PRIMARY KEY.
The first table was created with no problems:
mysql > CREATE TABLE nodes(
-> id varchar(10) PRIMARY KEY,
-> user text,
-> uid varchar(10),
-> version tinyint,
-> changeset smallint,
-> timestamp timestamp
-> );
It is when I tried to create the second table that MySQL is outputting an error:
mysql > CREATE TABLE node_tags(
-> id varchar(10),
-> key text,
-> value text,
-> type text,
-> CONSTRAINT pk_node_tag PRIMARY KEY (id, key),
-> CONSTRAINT fk_node_tags_id FOREIGN KEY (id)
-> REFERENCES nodes (id)
-> );
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 '
value text,
type text,
CONSTRAINT pk_node_tag PRIMARY KEY (id, key),
CONSTRAINT' at line 3
This succeeds. Key is a reserved word, so it needs back-ticks.
Also, a text in an index limits it size as to how much of it can be indexed. It is influenced by your character set in force.
So the following runs thru:
drop table if exists nodes;
CREATE TABLE nodes
( id varchar(10) PRIMARY KEY,
user text,
uid varchar(10),
version tinyint,
changeset smallint,
timestamp timestamp
)engine=innodb;
drop table if exists node_tags;
CREATE TABLE node_tags
( id varchar(10) not null,
`key` text,
value text,
type text,
CONSTRAINT pk_node_tag PRIMARY KEY (id, `key`(255)),
CONSTRAINT fk_node_tags_id FOREIGN KEY (id) REFERENCES nodes (id)
)engine=innodb;
I would highly suggest not having a TEXT in a primary key anyway.
Reserved keywords such as 'key, value, timestamp etc' shouldn't be preferred while creating columns. This induces bugs and isn't scalable.
As far as this is concerned, using backticks key will solve the issue here.
because key is the reserved word in MySQL. please refer to this document to see list of the reserved words: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
I am trying to add a column to my table and make it a foreign key but I keep 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 'FOREIGN KEY(idplayer) REFERENCES
players(playersid)' at line 1
below is my code before the alter statement:
CREATE TABLE transactions
(
transid INT UNSIGNED NOT NULL AUTO_INCREMENT,
type VARCHAR(20),
fromteam VARCHAR(30),
toteam VARCHAR(30),
idplayer INT UNSIGNED NOT NULL,
PRIMARY KEY(transid)
);
Now I try to alter the idplayer and make it a foreign key:
ALTER TABLE transactions
MODIFY idplayer INT UNSIGNED NOT NULL
FOREIGN KEY(idplayer) REFERENCES players(playersid)
Please an assistance would be great.
First off, you are missing a comma after NOT NULL, Second you need to tell mysql to add the CONSTRAINT. Give this a try:
ALTER TABLE `events`
MODIFY idplayer INT UNSIGNED NOT NULL,
ADD CONSTRAINT `FK_Name` FOREIGN KEY (idplayer) REFERENCES players(playersid);
first table:
create table login(
id int auto_increment,
user varchar(20),
pass varchar(20),
primary key(id)
);
first table is created with primary key..
second table:
create table check(
cid int,
rid int,
usname varchar(20),
word varchar(20),
FOREIGN KEY(rid) REFERENCES login(id)
);
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 'check
(cid int, rid int, usname varchar(20), word varchar(20), FOREIGN KEY(rid) R' at
line 1
The problem isn't the foreign key. check is a reserved word in MySQL. So you need to choose another name or escape it:
create table `check`(
cid int,
rid int,
usname varchar(20),
word varchar(20),
FOREIGN KEY(rid) REFERENCES login(id)
);
Ironically, although it is a reserved word, it is not actually used. Alas, I do wish that MySQL supported check constraints.
By the way, here is a SQL Fiddle.
check is a key word in MySQL; either surround it with backticks, `, or choose a different name.
Change the name of your table check. Check is a reserved word in MySQL
This is my SQL Script
CREATE TABLE account_profile
(
accnt_id int NOT NULL ,
first_name varchar(255),
last_name varchar(255),
biography varchar(255),
date_joined DATE,
country varchar(255),
gender varchar(255),
screename varchar(255),
path varchar(255),
FOREIGN KEY (accnt_id) REFERENCES accounts(id)
)
it kept giving me this error
SQL query:
CREATE TABLE ac
MySQL said:
#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 '' at line 1
What does this specifically mean? what should I do? from my point of view the script is quite okay,I just can't pin point where's the error
Make sure you're using the InnoDB engine. The other engines do not support foreign keys.
CREATE TABLE account_profile
(
...
) ENGINE = INNODB;
Also check if the column account_profile.accnt_id matches the data type of accounts.id exactly. The second column should have an index defined on it (a primary key will do.)
I have tested your query and it works with me too. Do you have a query before creating the table account_profile? because if you do, try to check if the query before has been terminated by a semi-colon. like this:
Create table TableName
(
-- fields list
); -- <== don't forget this before creating another table again.
CREATE TABLE account_profile
(
accnt_id int NOT NULL ,
first_name varchar(255),
last_name varchar(255),
biography varchar(255),
date_joined DATE,
country varchar(255),
gender varchar(255),
screename varchar(255),
path varchar(255),
FOREIGN KEY (accnt_id) REFERENCES accounts(id)
); -- <== and also this.
the error says near line 1.
Probably your table account does not exists :
FOREIGN KEY (accnt_id) REFERENCES accounts(id)
That's why you've got an error, the request is correct otherwise.