MySQL foreign keys' referenced table name always forced to lowercase - mysql

First, create these two tables:
CREATE TABLE IF NOT EXISTS TAB_COMPANY (
ID INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
NAME VARCHAR(100) NOT NULL,
PRIMARY KEY(ID),
UNIQUE KEY(NAME)
) ENGINE=INNODB DEFAULT CHARSET=UTF8;
CREATE TABLE IF NOT EXISTS TAB_DEPARTMENT (
ID INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
NAME VARCHAR(100) NOT NULL,
COMPANYID INT(10) UNSIGNED NOT NULL,
PRIMARY KEY(ID),
INDEX FK_TAB_DEPARTMENT_TAB_COMPANY_COMPANYID(COMPANYID ASC),
CONSTRAINT FK_TAB_DEPARTMENT_TAB_COMPANY_COMPANYID
FOREIGN KEY (COMPANYID) REFERENCES TAB_COMPANY(ID) ON DELETE CASCADE
) ENGINE=INNODB DEFAULT CHARSET=UTF8;
Then show create table TAB_DEPARTMENT:
CREATE TABLE `TAB_DEPARTMENT` (
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`NAME` varchar(100) NOT NULL,
`COMPANYID` int(10) unsigned NOT NULL,
PRIMARY KEY (`ID`),
KEY `FK_TAB_DEPARTMENT_TAB_COMPANY_COMPANYID` (`COMPANYID`),
CONSTRAINT `FK_TAB_DEPARTMENT_TAB_COMPANY_COMPANYID` FOREIGN KEY (`COMPANYID`)
REFERENCES `tab_company` (`ID`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
The table name in foreign key reference clause is forced to lower case.
mysql> show variables like '%lower%';
lower_case_file_system ON
lower_case_table_names 0
mysql> show variables like 'version';
version 5.1.43-community
My platform is Window XP with SP3. I tested this on Linux, it is OK.
Has anyone encountered this issue before? I already reported a bug to MySQL.
I just tried on version 5.1.49-community, and the issue is still there.

Got the reply from MySQL:
From Miguel Solorzano:
Thank you for the bug report. This a documented restriction of InnoDB
table:
http://dev.mysql.com/doc/refman/5.1/en/innodb-restrictions.html
"On Windows, InnoDB always stores database and table names internally
in lowercase. To move databases in a binary format from Unix to Windows
or from Windows to Unix, you should create all databases and tables
using lowercase names. "

Related

errno: 150 "Foreign key constraint is incorrectly formed

I have a simple database in which I have two tables, Applications and Category. Categories are dependent on Application, so I want to delete Category when Apps are deleted, For this purpose I used Foreign key in Category table. It works fine in local server, however when I upload it to online server, it show me error:
I tried a lot, but can't solve this issue. Can anyone suggest me please how to change my database. My database snippets are:
CREATE TABLE `app_name` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(256) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
CREATE TABLE `app_category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`app_id` int(11) DEFAULT NULL,
`name` varchar(256) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `app_category` (`app_id`),
CONSTRAINT `app_category` FOREIGN KEY (`app_id`) REFERENCES `app_name` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
Please cross check with table names because its working fine in my machine actually this error occurs when we have different data types
ex: id in app_name,app_id in app_category need to have same data type with same count int (11) not null.

MySQL InnoDB Foreign Key Problems

I have the following tables and I am trying to run them in MySQL but I keep getting an errno 150. Just not sure why, but MySQL cannot seem to create the foreign key constraint. I have already looked at the rules for setting FKs for InnoDB and everything seems ok. Could someone please lend me another set of eyes and expertise?
-- Table publication_type (parent table)
CREATE TABLE publication_type (
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
publication_type varchar(55) NOT NULL,
tstamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT publication_type_pk PRIMARY KEY (id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
-- Table publication (child table)
CREATE TABLE publication (
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
authors varchar(255) NOT NULL,
publication_title varchar(100) NOT NULL,
publication_type_id int(11) UNSIGNED NOT NULL,
user_id int(11) UNSIGNED NOT NULL,
CONSTRAINT publication_pk PRIMARY KEY (id),
CONSTRAINT publication_type_fk FOREIGN KEY (publication_type_id) REFERENCES publication_type(id) ON DELETE SET NULL ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
The types do not match:
publication_type.id is of type int UNSIGNED
publication.publication_type_id is of type int
See also the docs: http://dev.mysql.com/doc/refman/5.1/en/create-table-foreign-keys.html, specifically:
Corresponding columns in the foreign key and the referenced key must
have similar data types. The size and sign of integer types must be
the same. The length of string types need not be the same. For
nonbinary (character) string columns, the character set and collation
must be the same.
emphasis mine.

How do I add a foreign key to a table in Sequel Pro?

I am trying to add a foreign key to a table in Sequel Pro (using the UI).
I have two tables: "titles" and "categories" as below:
CREATE TABLE `titles` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` tinytext NOT NULL,
`category` varchar(256) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `category` (
`key` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(256) NOT NULL DEFAULT '',
PRIMARY KEY (`key`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
I want to create a foreign key, but nothing I try works.
The category table should be a simple lookup table. I want to assign each title a category from about 6 - 8 different choices.
Originally I had the category fields as tinytext, but I would get the error:
"MySQL Error 1170 (42000): BLOB/TEXT Column Used in Key Specification Without a Key Length".
Searched here and discovered you can't use text field that way, so I switched to Varchar and added a length of 256. Now I get:
MySQL said: Can't create table 'lit.#sql-2bf3_2' (errno: 150).
How can I create a foreign key for my table?
In Access this is pretty easily done. Somehow Access associates the unique key in the table with the lookup, but then hides the key and shows you the text field instead. How can I get a similar result with Sequel Pro and MySQL?
EDIT:
So, to clarify this is where I'm at right now. I've added an index on the category field in the titles table (first picture).
I've changed the "key" field in the category table to CategoryID (second picture).
However, I still can't seem to create the relationship between the two tables. I get the same error
As category will be your lookup table off of titles, you'd need to create an index on category which would refer to the foreign key. They would both need to be the same datatype (usually an INT, though sometimes you could use a CHAR(2) variable in some cases, but usually not necessary). Since you only expect 6-8 categories, I'd make it INT(1) (or may be INT(2) to be safe).
In this case, you would need to create something like categoryId which would first need to be indexed, then connect to the foreign key on categorywhich does not appear to exist; I'm not sure you want to use a term like key. Why not just make categoryId the primary key on category? this way when you create the foreign key on titles with the same name, it should link up fine.
Edit:
To clarify a little, after you've created categoryID on category you can do this under titles
ALTER TABLE Orders
ADD CONSTRAINT fk_categoryID
FOREIGN KEY (`categoryId`) REFERENCES `category`(`categoryId`)
Edit:
Here's a modification using your original layout. this should work for you:
CREATE TABLE IF NOT EXISTS `category` (
`key` int(2) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(256) NOT NULL DEFAULT '',
PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
CREATE TABLE IF NOT EXISTS `titles` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`title` tinytext NOT NULL,
`categoryID` int(2) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `categoryID` (`categoryID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
ALTER TABLE `titles`
ADD CONSTRAINT `titles_ibfk_1` FOREIGN KEY (`categoryID`) REFERENCES `category` (`key`);

Simple add foreign key returns #1215 cannot add foreign key constraint

I am sure I am missing something simple.
RequestLog table:
CREATE TABLE `requestlog` (
`RequestID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`RequestName` varchar(30) NOT NULL,
`RequestData` varchar(150) NOT NULL,
`RequestDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Version` varchar(15) NOT NULL,
PRIMARY KEY (`RequestID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
ResponseLog table:
CREATE TABLE `responselog` (
`ResponseID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`FK_RequestID` int(10) NOT NULL,
`ResponseText` text NOT NULL,
PRIMARY KEY (`ResponseID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Trying to add a foreign key on ResponseLog.FK_RequestID with
ALTER TABLE ResponseLog
ADD FOREIGN KEY (FK_RequestID) REFERENCES RequestLog(RequestID)
Don't shoot me, what am I missing?
ALTER TABLE references tables ResponseLog and RequestLog. Your CREATE TABLE statements create tables named requestlog and responselog. Try changing your ALTER TABLE statement so that it uses table identifiers with the same case.
Also, and it is probably the main problem, the referenced fields have different data types. One is an int, the other an unsigned int. Data types have to match, otherwise the fields could become inconsistent. MySQL knows this and prevents you from creating a broken foreign key.

mysql won't allow foreign key

Many people had this problem already, but there was no fitting solution in other posts.
I have two tables, one named "sales", the other named "host_flags". I would like to have a foreign key for host_flags.sales_id to sales.id, but mysql won't let me! I have primary indexes defined in each table, so I wonder why...
The host_flags table already has a foreign key on the column host_id, but even when I tried and created the foreign key for the sales id first, it wouldn't let me.
The tables look like:
CREATE TABLE `sales` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`creation` datetime DEFAULT NULL,
`lastupdate` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
CREATE TABLE `host_flags` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`host_id` int(11) DEFAULT NULL,
`sales_id` int(11) DEFAULT NULL,
`creation` datetime DEFAULT NULL,
`lastupdate` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `host_id6` (`host_id`),
CONSTRAINT `host_id6` FOREIGN KEY (`host_id`) REFERENCES `hosts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `hosts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`creation` datetime NOT NULL,
`lastupdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=32225 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
I get this error message:
MySQL said: Can't create table 'primarydata.#sql-191_1' (errno: 150)
Thanks!
Charles
SOLUTION FOUND
All ints of the primary indexes have to be either signed or unsigned - not mixed.
Typically:
I like to declare the FK constraints outside of the table definition after all tables have been constructed.
ALTER TABLE `tbl`
ADD CONSTRAINT `constr`
FOREIGN KEY `fk_id` REFERENCES `ftbl`(`id`)
ON UPDATE CASCADE
ON DELETE CASCADE;
This way I can make sure the problem isn't something like the datatype of tbl.fk_id not being the same as the one of ftbl.id (including UNSIGNED as #Devart said). Or not having declared ftbl.id as unique. Regardless of the order of declaration of the tables.
After i do this i can integrate the constraint back into the table definition and take into account the order in which the tables need to be created to allow the constraint to be added.
You problem:
-- creating the sales table
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-- creating the host_flags table
`sales_id` int(11) DEFAULT NULL,
-- the sales.id is declared as unsigned
-- the host_flags.sales_id is declared signed
Additonally to Recursed's answer:
There is a requirement that foreign keys contstraints' names must be unique in database scope. Maybe changing the name will work?
There is also a huge thread on MySQL community forums about the problem containing several solutions for some specific situations.
Possible two errors:
Reference and referenced columns must have the same type - int(11) unsigned
Unknown referenced table hosts.