1215 - Cannot add foreign key constraint - mysql

I actualy try to create a database but i have an error with the foreign key. Can you hepl me please ?
DROP TABLE IF EXISTS `City`;
CREATE TABLE `City` (`id` int(11) NOT NULL AUTO_INCREMENT,
`idCountry` int(11) DEFAULT NULL,
`Name` char(35) CHARACTER SET latin1 NOT NULL DEFAULT '',
`CountryCode` char(3) CHARACTER SET latin1 NOT NULL DEFAULT '',
`District` char(20) CHARACTER SET latin1 NOT NULL DEFAULT '',
`Population` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`) USING BTREE,
KEY `fk_constraint_city_country` (`idCountry`),
CONSTRAINT `fk_constraint_city_country` FOREIGN KEY (`idCountry`)
REFERENCES `Country` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=utf8;
I have this error: MySQL said: Documentation
1215 - Cannot add foreign key constraint

DROP TABLE IF EXISTS `Country`;
CREATE TABLE `Country` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Code` char(3) CHARACTER SET latin1 DEFAULT NULL,
`Name` char(52) CHARACTER SET latin1 NOT NULL DEFAULT '',
`Continent` enum('Asia','Europe','North
America','Africa','Oceania','Antarctica','South America') CHARACTER SET
latin1 NOT NULL DEFAULT 'Asia',
`Region` char(26) CHARACTER SET latin1 NOT NULL DEFAULT '',
`SurfaceArea` float(10,2) NOT NULL DEFAULT '0.00',
`IndepYear` smallint(6) DEFAULT NULL,
`Population` int(11) NOT NULL DEFAULT '0',
`LifeExpectancy` float(3,1) DEFAULT NULL,
`GNP` float(10,2) DEFAULT NULL,
`GNPOld` float(10,2) DEFAULT NULL,
`LocalName` char(45) CHARACTER SET latin1 NOT NULL DEFAULT '',
`GovernmentForm` char(45) CHARACTER SET latin1 NOT NULL DEFAULT '',
`HeadOfState` char(60) CHARACTER SET latin1 DEFAULT NULL,
`Capital` int(11) DEFAULT NULL,
`Code2` char(2) CHARACTER SET latin1 NOT NULL DEFAULT '',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=240 DEFAULT CHARSET=utf8;
and there is the country langage:
DROP TABLE IF EXISTS `CountryLanguage`;
CREATE TABLE `CountryLanguage` (
`idCountry` int(11) NOT NULL DEFAULT '0',
`idLanguage` int(11) NOT NULL DEFAULT '0',
`IsOfficial` enum('T','F') CHARACTER SET latin1 NOT NULL DEFAULT 'F',
`Percentage` float(4,1) NOT NULL DEFAULT '0.0',
PRIMARY KEY (`idCountry`,`idLanguage`) USING BTREE,
KEY `fk_constraint_Language` (`idLanguage`),
CONSTRAINT `fk_constraint_Country` FOREIGN KEY (`idCountry`) REFERENCES
`Country` (`id`),
CONSTRAINT `fk_constraint_Language` FOREIGN KEY (`idLanguage`)
REFERENCES `Language` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf

Related

Error Code: 1005. Can't create table `v2_financieel`.`facturen_azie` (errno: 150 "Foreign key constraint is incorrectly formed")

I am using workbench and I am trying to create a database with a table:
I just put this create database in the sql editor and execute it.
Of course I googled first for the error. But I cant find anything for this specific case.
-- Dumping database structure for v2_financieel
CREATE DATABASE IF NOT EXISTS `v2_financieel` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `v2_financieel`;
-- Dumping structure for table v2_financieel.facturen_azie
CREATE TABLE IF NOT EXISTS `facturen_azie` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`kantoor_id` int(10) unsigned NOT NULL,
`factuurnummer` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`factuurdatum` date NOT NULL,
`merknaam_id` int(10) unsigned NOT NULL,
`klant_id` int(10) unsigned NOT NULL,
`omschrijving` varchar(191) COLLATE utf8_unicode_ci NOT NULL,
`aantal_afslagen` decimal(10,1) unsigned NOT NULL,
`bonnummers` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL,
`valuta` char(3) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'EUR',
`koers` decimal(8,4) unsigned NOT NULL,
`koersdatum` date NOT NULL,
`bedrag_euro` decimal(10,2) NOT NULL,
`bedrag_valuta` decimal(10,2) NOT NULL,
`btw` decimal(10,2) NOT NULL,
`factuurbedrag` decimal(10,2) NOT NULL,
`betaald` tinyint(1) NOT NULL DEFAULT '0',
`extra_bedrag` decimal(10,2) NOT NULL,
`extra_omschrijving` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL,
`creditfactuur` tinyint(1) NOT NULL DEFAULT '0',
`credit_id` int(11) NOT NULL DEFAULT '0',
`betalingsdatum` date NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `facturen_azie_factuurnummer_unique` (`factuurnummer`),
KEY `facturen_azie_merknaam_id_foreign` (`merknaam_id`),
KEY `facturen_azie_klant_id_foreign` (`klant_id`),
KEY `facturen_azie_kantoor_id_foreign` (`kantoor_id`),
FULLTEXT KEY `search_azie` (`factuurnummer`,`omschrijving`),
CONSTRAINT `facturen_azie_kantoor_id_foreign` FOREIGN KEY (`kantoor_id`) REFERENCES `kantoren` (`id`),
CONSTRAINT `facturen_azie_klant_id_foreign` FOREIGN KEY (`klant_id`) REFERENCES `klanten` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=496 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
But I get this errror:
12:02:40 CREATE TABLE IF NOT EXISTS `facturen_azie` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `kantoor_id` int(10) unsigned NOT NULL, `factuurnummer` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `factuurdatum` date NOT NULL, `merknaam_id` int(10) unsigned NOT NULL, `klant_id` int(10) unsigned NOT NULL, `omschrijving` varchar(191) COLLATE utf8_unicode_ci NOT NULL, `aantal_afslagen` decimal(10,1) unsigned NOT NULL, `bonnummers` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL, `valuta` char(3) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'EUR', `koers` decimal(8,4) unsigned NOT NULL, `koersdatum` date NOT NULL, `bedrag_euro` decimal(10,2) NOT NULL, `bedrag_valuta` decimal(10,2) NOT NULL, `btw` decimal(10,2) NOT NULL, `factuurbedrag` decimal(10,2) NOT NULL, `betaald` tinyint(1) NOT NULL DEFAULT '0', `extra_bedrag` decimal(10,2) NOT NULL, `extra_omschrijving` varchar(191) COLLATE utf8_unicode_ci DEFAULT NULL, `creditfactuur` tinyint(1) NOT NULL DEFAULT '0', `credit_id` int(11) NOT NULL DEFAULT '0', `betalingsdatum` date NOT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `facturen_azie_factuurnummer_unique` (`factuurnummer`), KEY `facturen_azie_merknaam_id_foreign` (`merknaam_id`), KEY `facturen_azie_klant_id_foreign` (`klant_id`), KEY `facturen_azie_kantoor_id_foreign` (`kantoor_id`), FULLTEXT KEY `search_azie` (`factuurnummer`,`omschrijving`), CONSTRAINT `facturen_azie_kantoor_id_foreign` FOREIGN KEY (`kantoor_id`) REFERENCES `kantoren` (`id`), CONSTRAINT `facturen_azie_klant_id_foreign` FOREIGN KEY (`klant_id`) REFERENCES `klanten` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=496 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci Error Code: 1005. Can't create table `v2_financieel`.`facturen_azie` (errno: 150 "Foreign key constraint is incorrectly formed") 0.359 sec

ERROR 1005 (HY000) errno: 150

I am having this problem, which has been addressed in many posts, but none of them show where I am going wrong. I get the error 1005 with errno 150 when I try the following;
structure of appointments:
CREATE TABLE `appointments` (
`appointmentID` int(11) NOT NULL AUTO_INCREMENT,
`apptNum` tinyint(3) unsigned NOT NULL DEFAULT '1',
`cwaID` int(11) NOT NULL DEFAULT '0',
`siteID` int(11) DEFAULT NULL,
`titleCode` varchar(25) NOT NULL,
`apptPercentage` double DEFAULT '0',
`status` varchar(2) CHARACTER SET utf8 DEFAULT NULL,
`payRate` double DEFAULT '0',
`rateCode` varchar(5) CHARACTER SET utf8 DEFAULT NULL,
`locationInfo` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`apptStartDate` date DEFAULT NULL,
`apptEndDate` date DEFAULT NULL,
`isPrimaryAppointment` varchar(1) CHARACTER SET utf8 NOT NULL
DEFAULT 'y',
`lastModDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE
CURRENT_TIMESTAMP,
PRIMARY KEY (`appointmentID`),
UNIQUE KEY `appointmentID` (`appointmentID`),
UNIQUE KEY `cwaID-apptNum` (`cwaID`,`apptNum`),
KEY `apptNum` (`apptNum`),
KEY `siteID` (`siteID`),
KEY `rateCode` (`rateCode`),
KEY `cwaID` (`cwaID`),
KEY `titleCode` (`titleCode`),
CONSTRAINT `FK_employees_appointments` FOREIGN KEY (`cwaID`) REFERENCES
`employees` (`cwaID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
structure of jobtitles:
CREATE TABLE `jobtitles` (
`titleCode` varchar(25) CHARACTER SET utf8 NOT NULL,
`jobTitle` varchar(255) CHARACTER SET utf8 NOT NULL,
`unitID` varchar(10) CHARACTER SET utf8 NOT NULL,
`scope` varchar(10) CHARACTER SET utf8 NOT NULL,
`payPeriod` char(1) NOT NULL DEFAULT 'M',
`maxPay` decimal(8,2) NOT NULL DEFAULT '0.00',
`minPay` decimal(8,2) NOT NULL DEFAULT '0.00',
PRIMARY KEY (`titleCode`),
KEY `jobTitle` (`jobTitle`),
KEY `scope` (`scope`),
KEY `unitID` (`unitID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I want to add a foreign key to appointments for the titleCode field:
ALTER TABLE appointments ADD CONSTRAINT `FK_jobtitles_appointments`
FOREIGN KEY (`titleCode`) REFERENCES `jobtitles` (`titleCode`);
but this attempt generates the ERROR 1005 with errno 150, which implies a badly formed foreign key.
Any suggestions?

MySQL - Why can't I add a foreign key here?

I'm not sure why I'm not able to add a particular foreign key here. These tables were generated via MySQL Workbench, and based on the MySQL documentation and on searching for other similar problems / solutions, I think everything is in order...yet I can't add one foreign key in particular:
inspection_statuses.inspection_id needs to reference inspection_responses.tpa_result
What am I missing / overlooking?
This is the statement I'm trying to use to add the new foreign key:
ALTER TABLE `vipsouth_app`.`inspection_statuses`
ADD CONSTRAINT `inspection_statuses_ibfk_3`
FOREIGN KEY (`inspection_id`)
REFERENCES `vipsouth_app`.`inspection_responses` (`tpa_result`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
And that produces this error:
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1005: Can't create table vipsouth_app.#sql-1f48_7 (errno: 150 "Foreign key constraint is incorrectly formed")
CREATE TABLE `inspection_responses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inspection_request_id` int(10) unsigned NOT NULL,
`tpa_result` varchar(10) CHARACTER SET utf8 NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(10) unsigned NOT NULL DEFAULT '0',
`updated_at` timestamp NULL DEFAULT NULL,
`updated_by` int(10) unsigned DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`deleted_by` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
UNIQUE KEY `tpa_result_UNIQUE` (`tpa_result`),
KEY `inspection_responses_ibfk_1_idx` (`inspection_request_id`),
CONSTRAINT `inspection_responses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `inspection_statuses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inspection_request_id` int(10) unsigned NOT NULL,
`inspection_response_id` int(10) unsigned NOT NULL,
`tpa_code` varchar(4) COLLATE utf8_unicode_ci NOT NULL,
`user_id` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`inspection_id` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`status` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`note` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`url` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(10) unsigned NOT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`updated_by` int(10) unsigned DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`deleted_by` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `inspection_statuses_ibfk_1_idx` (`inspection_request_id`),
KEY `inspection_statuses_ibfk_2_idx` (`inspection_response_id`),
CONSTRAINT `inspection_statuses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `inspection_statuses_ibfk_2` FOREIGN KEY (`inspection_response_id`) REFERENCES `inspection_responses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
The data types and constraints defined on both the columns should be exactly the same. Except for a foreign key can be NULLable.
This should do it for you. I did not get any error.
SET foreign_key_checks = 0;
CREATE TABLE `inspection_responses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inspection_request_id` int(10) unsigned NOT NULL,
`tpa_result` varchar(10) CHARACTER SET utf8 NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(10) unsigned NOT NULL DEFAULT '0',
`updated_at` timestamp NULL DEFAULT NULL,
`updated_by` int(10) unsigned DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`deleted_by` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
UNIQUE KEY `tpa_result_UNIQUE` (`tpa_result`),
KEY `inspection_responses_ibfk_1_idx` (`inspection_request_id`),
CONSTRAINT `inspection_responses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `inspection_statuses` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`inspection_request_id` int(10) unsigned NOT NULL,
`inspection_response_id` int(10) unsigned NOT NULL,
`tpa_code` varchar(4) COLLATE utf8_unicode_ci NOT NULL,
`user_id` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`inspection_id` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`status` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`note` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
`url` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`created_by` int(10) unsigned NOT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`updated_by` int(10) unsigned DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`deleted_by` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `inspection_statuses_ibfk_1_idx` (`inspection_request_id`),
KEY `inspection_statuses_ibfk_2_idx` (`inspection_response_id`),
CONSTRAINT `inspection_statuses_ibfk_1` FOREIGN KEY (`inspection_request_id`) REFERENCES `inspection_requests` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `inspection_statuses_ibfk_2` FOREIGN KEY (`inspection_response_id`) REFERENCES `inspection_responses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SET foreign_key_checks = 1;

Foreign ID in SQL tables to link one table to another

This is my SQL code which links users to items based on tutorials:
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`user_password_hash` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`user_email` varchar(254) COLLATE utf8_unicode_ci NOT NULL,
`user_access_level` tinyint(3) unsigned NOT NULL DEFAULT '0',
`user_active` tinyint(1) NOT NULL DEFAULT '0',
`user_activation_hash` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_password_reset_hash` char(40) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_password_reset_timestamp` bigint(20) DEFAULT NULL,
`user_failed_logins` tinyint(1) NOT NULL DEFAULT '0',
`user_last_failed_login` int(10) DEFAULT NULL,
`user_registration_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_registration_ip` varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_name` (`user_name`),
UNIQUE KEY `user_email` (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `item` (
`item_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`item_title` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`item_location` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`item_description` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`item_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`item_status` int(1) unsigned NOT NULL,
PRIMARY KEY (`item_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
http://sqlfiddle.com/#!9/fd011
I'm getting a bit confused about how to link the items to the user. It seems like I need something called a foreign key on the items, a bit like this in my item table:
FOREIGN KEY (user_id) REFERENCES user(ID)
I can't seem to get it to compile and query successfully. Can anyone please show me the right way to associate the items with the user.
You need to add the user_id column to the items table along with the constraint:
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`user_password_hash` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`user_email` varchar(254) COLLATE utf8_unicode_ci NOT NULL,
`user_access_level` tinyint(3) unsigned NOT NULL DEFAULT '0',
`user_active` tinyint(1) NOT NULL DEFAULT '0',
`user_activation_hash` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_password_reset_hash` char(40) COLLATE utf8_unicode_ci DEFAULT NULL,
`user_password_reset_timestamp` bigint(20) DEFAULT NULL,
`user_failed_logins` tinyint(1) NOT NULL DEFAULT '0',
`user_last_failed_login` int(10) DEFAULT NULL,
`user_registration_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_registration_ip` varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0',
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_name` (`user_name`),
UNIQUE KEY `user_email` (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `item` (
`item_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
user_id int unsigned,
`item_title` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`item_location` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`item_description` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`item_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`item_status` int(1) unsigned NOT NULL,
PRIMARY KEY (`item_id`),
FOREIGN KEY (user_id) REFERENCES users(user_id)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
The SQL Fiddle is here.
I should point out a few other things:
Pay attention to the database engine you are using. MyISAM doesn't actually enforce the relationship.
Having weird dates as the default value is probably less useful than just using NULL.
I'm not sure if there is a value to having explicit collations for every character definition, unless your database is going to be supporting a wide variety of collations.
Don't use single quotes for numeric constants. So, if a value is declared as a tinyint, set the default ot 0 not '0' (this doesn't affect performance in a CREATE TABLE statement; it is just misleading).
#GordonLindoff's solution is one method, but that assumes that each item belongs to exactly one user, and an item cannot be referenced by multiple users. If you have a many-to-many relationship, where a user can have multiple items and an item can be referenced by multiple users, then you need a third table that links them together:
CREATE TABLE IF NOT EXISTS `user_item` (
`user_id` int(11) unsigned NOT NULL,
`item_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`user_item`,`item_id`),
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (item_id) REFERENCES items(item_id)
)ENGINE=Innodb DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
The Foreign Key constraints enforce that for every row in user_item that the user_id exists in users, and the item_id exists in items. And as was mentioned in a previous comment, that you will need Innodb to have the foreign key constraints enforced.

MySQL Foreign keys - Error 150 while creating tables

I have written some SQL code to create five tables with several relations(foreign keys).
The first foreign key relation works fine, the tables are created without any errors, but when I try to create gasten_url_toegangscodes I get the following error:
#1005 - Can't create table 'dbname.gasten_url_toegangscodes' (errno: 150).
I've read the docs about foreign keys and I still can't find the problem.. The most strange thing about this is that it works in the first three tables..
Can anyone help me please?
My full SQL code is:
CREATE TABLE IF NOT EXISTS `groepen` (
`id` tinyint(3) NOT NULL AUTO_INCREMENT,
`naam` varchar(50) NOT NULL DEFAULT '',
`status` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `gasten` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`bedrijf` varchar(100) NOT NULL DEFAULT '',
`uniek` varchar(64) NOT NULL,
`groepid` tinyint(3) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (groepid) REFERENCES `groepen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `passen` (
`id` tinyint(3) NOT NULL AUTO_INCREMENT,
`naam` varchar(30) NOT NULL DEFAULT '',
`color` varchar(7) NOT NULL DEFAULT '#FFFFFF',
`bekend` tinyint(1) NOT NULL DEFAULT '0',
`welkom` tinyint(1) NOT NULL DEFAULT '0',
`aantal` tinyint(1) NOT NULL DEFAULT '0',
`nummer` int(11) NOT NULL DEFAULT '0',
`begrens` tinyint(1) NOT NULL DEFAULT '0',
`status` tinyint(1) NOT NULL DEFAULT '1',
`priority` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `naam` (`naam`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `gasten_url_toegangscodes` (
`uniek` varchar(64) NOT NULL,
`unieke_url_code` char(64) NOT NULL,
`salt` char(16) NOT NULL,
FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `gasten_tickets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pas_id` tinyint(3) NOT NULL,
`uniek` varchar(64) NOT NULL,
`barcode` char(16) NOT NULL,
`secret_key` char(32) NOT NULL,
`download_count` int(11) NOT NULL DEFAULT '0',
`last_download_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`scanned` enum('N','Y') NOT NULL,
`scanned_datetime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION,
FOREIGN KEY (pas_id) REFERENCES `passen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;
Thanks in advance!
Create an index on gasten.uniek
CREATE TABLE IF NOT EXISTS `gasten` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`bedrijf` varchar(100) NOT NULL DEFAULT '',
`uniek` varchar(64) NOT NULL,
`groepid` tinyint(3) NOT NULL,
PRIMARY KEY (`id`),
INDEX `idx_uniek` (`uniek`),
FOREIGN KEY (groepid) REFERENCES `groepen` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB;
And set the charset of the FK in gasten_url_toegangscodes the same as that of the related column in gasten.
Assuming gasten is UTF-8:
CREATE TABLE IF NOT EXISTS `gasten_url_toegangscodes` (
/* use same charset for this column */
`uniek` varchar(64) NOT NULL CHARSET UTF-8,
`unieke_url_code` char(64) NOT NULL,
`salt` char(16) NOT NULL,
FOREIGN KEY (uniek) REFERENCES `gasten` (`uniek`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;