Mysql FullText Syntax Error #1215 - mysql

I have 3 table
CREATE TABLE tag (
name VARCHAR(294) NOT NULL,
inserted_at DATETIME NOT NULL,
status INTEGER NOT NULL,
PRIMARY KEY (name)
) Engine=innoDB;
CREATE TABLE item (
id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(294) NOT NULL,
price INTEGER NOT NULL,
discounted_price INTEGER DEFAULT NULL,
unit VARCHAR(294) NOT NULL,
additional_message TEXT NOT NULL,
stock INTEGER NOT NULL,
is_featured INTEGER NOT NULL,
inserted_at DATETIME NOT NULL,
status INTEGER NOT NULL,
PRIMARY KEY (id)
) Engine=innoDB;
ALTER TABLE item ADD FULLTEXT(name, additional_message);
CREATE TABLE item_tag (
id INTEGER NOT NULL AUTO_INCREMENT,
item_id INTEGER NOT NULL,
tag_name VARCHAR(294) NOT NULL,
inserted_at DATETIME NOT NULL,
status INTEGER NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (tag_name) REFERENCES tag(name),
FOREIGN KEY (item_id) REFERENCES item(id)
) Engine=innoDB;
ALTER TABLE item_tag ADD FULLTEXT(tag_name);
The first Alter Command works perfectly, but the second does not. It return #1215 - Cannot add foreign key constraint.
Do you know why?, or better, how to fix it ?

As far as I know you cannot have MySQL 5.5 does not support FullText and Foreign key constraint together on the same. You can create either of them to get it working.

Related

Error code:1215 Cannot add foreign key constraint while running script in MySQL workbench version 6.3

First of all i apologize that the names of the tables and so on are in another language.
The issue is i can't seem to add a foreign key named ID_KOPIE from the table KOPIA_KNIHY into the table SKLAD for some reason. When it gets to adding the foreign key to to the table SKLAD it throws out an error 1215. Here is the code:
CREATE TABLE BOOK (
BOOK_NAME VARCHAR(30) NOT NULL,
YEAR CHAR(4) NOT NULL,
NAME_OF_EDITOR VARCHAR(30) NOT NULL,
WRITER_ID INTEGER NOT NULL,
ISBN VARCHAR(17) NOT NULL,
BOOK_ID INTEGER NOT NULL,
PRIMARY KEY (BOOK_ID),
);
CREATE TABLE BOOK_COPY(
BOOK_ID INTEGER NOT NULL,
LANGUAGE_CODE CHAR(3) NOT NULL,
COPY_ID INTEGER NOT NULL,
BOOK_PICTURES CHAR(1) NOT NULL
CHECK (BOOK_PICTURES IN ("Y", "N")),
PRIMARY KEY (BOOK_ID, LANGUAGE_CODE, COPY_ID)
FOREIGN KEY(BOOK_ID)
REFERENCES BOOK(BOOK_ID),
);
CREATE TABLE STORAGE (
BOOK_ID INTEGER NOT NULL,
COPY_ID INTEGER NOT NULL,
BUILDING_ID INTEGER NOT NULL,
ROOM_NUMBER NUMERIC(4,0) NOT NULL,
SHELF_NUMBER NUMERIC(4,0) NOT NULL,
PRIMARY KEY(BOOK_ID, BUILDING_ID, COPY_ID),
FOREIGN KEY(COPY_ID)
REFERENCES BOOK_COPY(BOOK_ID),
)
I researched the error code 1215 on the internet, i couldn't find anything wrong with my database. I checked if there's a typo or if i didn't forget to add the reference.
This is the error:
0 769 18:19:37 CREATE TABLE STORAGE (
BOOK_ID INTEGER NOT NULL,
COPY_ID INTEGER NOT NULL,
BUILDING_ID INTEGER NOT NULL,
ROOM_NUMBER NUMERIC(4,0) NOT NULL,
SHELF_NUMBER NUMERIC(4,0) NOT NULL,
PRIMARY KEY(BOOK_ID, BUILDING_ID, COPY_ID),
FOREIGN KEY(COPY_ID)
REFERENCES BOOK_COPY(BOOK_ID),
)
Error Code: 1215. Cannot add foreign key constraint 0.016 sec
My question is how can this be fixed that it would work.
Help would be greatly appreciated.
Try this way. Please do alter the ON UPDATE ... ON DELETE syntax in this example with the one you need.
CREATE TABLE `KNIHA` (
`NAZOV_KNIHY` VARCHAR(30) NOT NULL,
`ROK_PRVEHO_VYDANIA` CHAR(4) NOT NULL,
`NAZOV_VYDAVATELA` VARCHAR(30) NOT NULL,
`ID_AUTORA` INTEGER NOT NULL,
`ISBN` VARCHAR(17) NOT NULL,
`ID_KNIHY` INTEGER NOT NULL,
PRIMARY KEY (`ID_KNIHY`)
);
CREATE TABLE `KOPIA_KNIHY` (
`ID_KNIHY` INTEGER NOT NULL,
`KOD_JAZYKA` CHAR(3) NOT NULL,
`ID_KOPIE` INTEGER NOT NULL,
`ORAZKY_V_KNIHE` CHAR(1) NOT NULL
CHECK (`OBRAZKY_V_KNIHE` IN ("A", "N")),
INDEX(`ID_KOPIE`),
PRIMARY KEY (`ID_KNIHY`, `KOD_JAZYKA`, `ID_KOPIE`),
CONSTRAINT `idx_1` FOREIGN KEY `idx_1` (`ID_KNIHY`) REFERENCES `KNIHA`(`ID_KNIHY`) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE `SKLAD` (
`ID_KNIHY` INTEGER NOT NULL,
`ID_KOPIE` INTEGER NOT NULL,
`ID_BUDOVY` INTEGER NOT NULL,
`CISLO_MIESTNOSTI` NUMERIC(4,0) NOT NULL,
`CISLO_REGALU` NUMERIC(4,0) NOT NULL,
PRIMARY KEY(`ID_KNIHY`, `ID_BUDOVY`, `ID_KOPIE`),
CONSTRAINT `idx_2` FOREIGN KEY `idx_2` (`ID_KOPIE`) REFERENCES `KOPIA_KNIHY`(`ID_KOPIE`) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT `idx_3` FOREIGN KEY `idx_3` (`ID_KNIHY`) REFERENCES `KNIHA`(`ID_KNIHY`) ON UPDATE CASCADE ON DELETE CASCADE
);
Try it on SQL Fiddle
.

InnoDB CREATE TABLE throwing error 150

This create table is throwing an error 150:
CREATE TABLE IF NOT EXISTS published(
isbn VARCHAR(13) NOT NULL,
publisherid INTEGER NOT NULL,
year INTEGER NOT NULL,
lastupdate TIMESTAMP NOT NULL,
lastupdateby INTEGER NOT NULL,
FOREIGN KEY (lastupdateby) REFERENCES librarians (librarianid),
FOREIGN KEY (isbn) REFERENCES books,
FOREIGN KEY (publisherid) REFERENCES publishers,
PRIMARY KEY (isbn, publisherid)
) ENGINE = INNODB;
I've checked that the tables it references (librarians, books, and publishers) are already created when SQL gets to this bit. Here are their DDLs:
CREATE TABLE IF NOT EXISTS librarians(
librarianid INTEGER AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
canread BOOLEAN NOT NULL,
canwrite BOOLEAN NOT NULL,
canexec BOOLEAN NOT NULL,
lastupdate TIMESTAMP NOT NULL,
lastupdateby INTEGER NOT NULL,
FOREIGN KEY (lastupdateby) REFERENCES librarians (librarianid)
) ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS books(
isbn VARCHAR(13) PRIMARY KEY,
title VARCHAR(255) NOT NULL,
lastupdate TIMESTAMP NOT NULL,
lastupdateby INTEGER NOT NULL,
FOREIGN KEY (lastupdateby) REFERENCES librarians (librarianid)
) ENGINE = INNODB;
CREATE TABLE IF NOT EXISTS publishers(
publisherid INTEGER PRIMARY KEY AUTO_INCREMENT,
publishername VARCHAR(255) UNIQUE NOT NULL,
lastupdate TIMESTAMP NOT NULL,
lastupdateby INTEGER NOT NULL,
FOREIGN KEY (lastupdateby) REFERENCES librarians (librarianid)
) ENGINE = INNODB;
Further, I've checked the following guidelines I found here:
The referenced tables should be InnoDB as well. Well, I even had that explicitly specified.
Referenced tables must have an index and primary key. The foreign key fields are the (sole) primary keys of their respective reference tables. Unless I misunderstand this requirement, that should fulfill it.
SQL data types of FK column and referenced PK column must be identical. Unless I checked it wrong, this, too should be okay.
lastupdateby INTEGER NOT NULL --> librarianid INTEGER AUTO_INCREMENT PRIMARY KEY
isbn VARCHAR(13) NOT NULL --> isbn VARCHAR(13) PRIMARY KEY
publisherid INTEGER NOT NULL --> publisherid INTEGER PRIMARY KEY AUTO_INCREMENT
So, what did I miss? What can be the cause of this error 150?
You are missing column references in the foreign key specifications for tables books and publishers. You need to change those lines as below:
FOREIGN KEY (isbn) REFERENCES books (isbn),
FOREIGN KEY (publisherid) REFERENCES publishers(publisherid),

sql Error 1064 (42000) Syntax Error

CREATE TABLE IF NOT EXISTS message(
id INT NOT NULL auto_increment,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
PRIMARY KEY ('id')
FOREIGN KEY ('userid') REFERENCES users('id'));
I was just wondering if someone could help me in identifying a syntax error as I can not create a table.
Try to put , after the primary key declaration.
Update: I guess it should be
CREATE TABLE IF NOT EXISTS message (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id));
I'm assuming this is for MS SQL Server? If you get MS SQL Server Studio, you can script stuff which gives you an idea:
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[message]') AND type in (N'U'))
CREATE TABLE message(
id INT IDENTITY NOT NULL,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (userid) REFERENCES users(id))
GO
You query should be as below
CREATE TABLE IF NOT EXISTS message (
id INT auto_increment PRIMARY KEY,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id));
Provided you have id as a primary key in users table.
CREATE TABLE users (id INT PRIMARY KEY)
Your query should look like this:
CREATE TABLE IF NOT EXISTS message(
id INT NOT NULL auto_increment,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
PRIMARY KEY ('id'),
FOREIGN KEY ('userid') REFERENCES users('id')
) Engine=InnoDB;
Note the , after PRIMARY KEY ('id').
Small trick
You don't have to specify foreign keys in table definitions. It's practical when you do it like this (because dump may export tables in order that foreign keys will fail on creating/inserting):
CREATE TABLE 1; -- With references to table 2
CREATE TABLE 2;
INSERT INTO 1;
INSERT INTO 2;
ALTER TABLE 1 ADD FOREIGN KEY (user_id) REFERENCES 2 2(id);
Try to change the name of your table, May be message is an in-built keyword in MySQL.
Update to this: I guess it should be
CREATE TABLE IF NOT EXISTS myMessage (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id));

MYSQL, When defining a table does the UNIQUE constraint have to be used in conjunction with an INTEGER or can it be any datatype?

I am wanting to have a label column (VARCHAR) and I want it to be unique, but when I try to create the table it seems to be throwing an error. Can a unique constraint only be used in conjunction with an INTEGER or will it work with other datatypes as well. The error I am getting is (ERRNO 150)
CREATE TABLE IF NOT EXISTS `user`(
user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
`password` VARCHAR(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS `element`(
element_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
label VARCHAR(5) NOT NULL DEFAULT '',
parent_id INT NULL,
user_id INT NOT NULL,
created_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
edited_on TIMESTAMP NOT NULL,
UNIQUE(label),
KEY element_1 (label),
CONSTRAINT FK_element_1 FOREIGN KEY (user_id) REFERENCES `user` (user_id),
CONSTRAINT FK_element_2 FOREIGN KEY (parent_id) REFERENCES `element` (element_id)
);
The only way I can have this error, if the first table is created with MyISAM engine and the second (tried to be created) with InnoDB.
Check the definition of the created table user, using:
SHOW CREATE TABLE user ;
If that's the case, drop it and recreate it with:
CREATE TABLE IF NOT EXISTS `user`(
user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
`password` VARCHAR(255) NOT NULL
)
ENGINE = InnoDB ;

How do I create this relational table?

Below is a portion of relational data base. I know how to create table Film and assign primary key to it.. but don't understate creating other tables and assigning primary key to it.
Any help?
table Film
CREATE TABLE Film (
Id INTEGER PRIMARY KEY,
Title VARCHAR(35) NOT NULL,
Description VARCHAR(256) NOT NULL,
Year INTEGER NOT NULL CHECK (Year > 1900),
Rating INTEGER NOT NULL DEFAULT 3 CHECK (Rating BETWEEN 1 AND 5)
);
how do I create table FilmFormat and OrderItem ?
CREATE TABLE `jy` (
`PKfield` INTEGER UNSIGNED NOT NULL DEFAULT NULL AUTO_INCREMENT,
`field2` VARCHAR(45) NOT NULL,
PRIMARY KEY (`PKfield`),
CONSTRAINT `FK` FOREIGN KEY `FK` (`PKfield`)
REFERENCES `Film` (`Id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT
)
ENGINE = InnoDB;
This is enough to show you how to create a foreign key constraint between the two tables. As commented, the CHECK constraint will be parsed but ignored.
CREATE TABLE Film (
Id INTEGER PRIMARY KEY,
Title VARCHAR(35) NOT NULL,
Description VARCHAR(256) NOT NULL,
Year INTEGER NOT NULL CHECK (Year > 1900),
Rating INTEGER NOT NULL DEFAULT 3 CHECK (Rating BETWEEN 1 AND 5)
);
CREATE TABLE FilmFormat (
FilmId INTEGER not null,
FormatId INTEGER not null,
Price decimal(16,4) null,
Primary Key(FilmId, FormatId),
Constraint FK_FilmFormat_FilmId FOREIGN KEY (FilmId) REFERENCES Film(Id)
);
Doing the last table will just be doing your work for you.