foreign key not creating in table - mysql

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

Related

How to fix this it is showing Syntax error, ERROR 1064 (42000):

I'm trying to create a table with 2 foreign keys but unable to find the syntax error. Where do i need to make the changes?
create table Leave(
leaveId int primary key, noOfDays int, approverId varchar(50), requestorId varchar(50),
Foreign key (approverId) References Approver (approverId),
Foreign key(requestorId) References Requestor(requestorId)
);
These are the 2 tables create before creating leave table:
create table Approver(approverId int primary key, approverName varchar(50));
create table Requestor(requestorId int primary key, requestorName varchar(50), noOfLeavesApproved int, approverId int, dateOfApplication date, Foreign key(approverId) References Approver(approverId));
where can I make the changes for this syntax error?
It is showing the below error for the leave table
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 'Leave(leaveId int primary key, noOfDays int,
approverId varchar(50), requestorId' at line 1
Your problem is that Leave is a reserved word in MySQL (documentation). Therefore, it cannot be used by your objects
Change your table name to something else (example: leave_request) and it should work.

Syntax error while creating CONSTRAINT in MySQL

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

Why am I getting MySQL error 1064 on foreign key constraint?

CREATE TABLE EMP_1 (
EMP_NUM varchar(3),
EMP_LNAME varchar(15),
EMP_FNAME varchar(15),
EMP_INITIAL varchar(1),
EMP_HIREDATE datetime,
JOB_CODE varchar(3), FOREIGN KEY (JOB_CODE) REFERENCES JOB
);
This is the code that is given to create a table that is a subset of another table. It is part of an exercise.
#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 ' REFERENCES JOB)' at line 8
Is the error message that is output in PHPMyAdmin when the query is run.
Where is the syntax off on line 8? After checking w3 schools, and textbook, I cannot see the problem.
you're missing the referencing foreign key column on the JOB table
so it should be something like
FOREIGN KEY (JOB_CODE) REFERENCES JOB(job_code_col)
so if your JOB table also has a column called JOB_CODE, then the DDL would look like this:
CREATE TABLE EMP_1 (
EMP_NUM varchar(3),
EMP_LNAME varchar(15),
EMP_FNAME varchar(15),
EMP_INITIAL varchar(1),
EMP_HIREDATE datetime,
JOB_CODE varchar(3), FOREIGN KEY (JOB_CODE) REFERENCES JOB (JOB_CODE)
);

Why is this mysql syntax wrong?

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.

MySQL Syntax Error

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.