MariaDB - Foreign Key problem when reference SMALLINT - sql

I have a problem with MariaDB's foreign keys...
These are the tables:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` binary(20) NOT NULL,
`role` smallint(5) unsigned NOT NULL DEFAULT 2,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_UN` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
CREATE TABLE `roles` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `roles_name_UN` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
I can't understand why when I try to update "users" with this command:
ALTER TABLE unito_tweb.users ADD CONSTRAINT users_role_FK FOREIGN KEY (`role`) REFERENCES unito_tweb.roles(id) ON DELETE SET DEFAULT ON UPDATE CASCADE;
I get this error:
Can't create table `unito_tweb`.`users` (errno: 150 "Foreign key constraint is incorrectly formed")

Related

Having trouble adding a foreign key in HeidiSQL (Error 1215)

I've tried just about everything but I'm getting error 1215 when trying to create a foreign key in a child table I have. Here are my tables:
CREATE TABLE `Con` (
`ConID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(250) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`Website` varchar(500) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`FirstYear` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`ConID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `ConEvent` (
`EventID` int(11) NOT NULL AUTO_INCREMENT,
`ConID` int(11) NOT NULL,
`DateStart` date DEFAULT NULL,
`DateEnd` date DEFAULT NULL,
`Year` tinyint(4) DEFAULT NULL,
`Venue` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`Address` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
`City` tinytext COLLATE utf8_unicode_ci,
`StateProvince` tinytext COLLATE utf8_unicode_ci,
`Country` tinytext COLLATE utf8_unicode_ci,
PRIMARY KEY (`EventID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Here is my syntax:
ALTER TABLE ConEvent
ADD FOREIGN KEY (ConID) REFERENCES Con(ConID);
I can't SHOW ENGINE INNODB STATUS; because I'm not a super user (error 1227). I tried to make myself one but was unable to.

SQL - Can't add new foreign key constraint on SQL Fiddle

I'm working on SQL Fiddle and code so far looks like this:
CREATE TABLE IF NOT EXISTS `orders` (
`id` int(6) unsigned NOT NULL,
`date` DATE NOT NULL,
`customerID` int(6) unsigned NOT NULL,
`paymentAmmount` float(6) unsigned NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_customers_customerID` FOREIGN KEY (`customerID`)
REFERENCES `customers` (`customerID`) );
CREATE TABLE IF NOT EXISTS `items` (
`id` int(6) unsigned NOT NULL,
`name` varchar(200) NOT NULL,
PRIMARY KEY (`id`) ) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `customers` (
`customerID` int(6) unsigned NOT NULL,
`firstName` varchar(200) NOT NULL,
`lastName` varchar(200) NOT NULL,
`address` varchar(200) NOT NULL,
#`accountId` int(6) unsigned NOT NULL,
PRIMARY KEY (`customerID`) ) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `items-orders` (
`id` int(6) unsigned NOT NULL,
`itemId` int(6) unsigned NOT NULL,
`orderId` int(6) unsigned NOT NULL,
`itemQuantity` int(1) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_items_itemId` (`itemId`),
CONSTRAINT `FK_items_itemId` FOREIGN KEY (`itemId`) REFERENCES `items` (`id`) ) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(6) unsigned NOT NULL,
`email` varchar(200) NOT NULL,
`passwordHash` varchar(200) NOT NULL,
PRIMARY KEY (`id`) ) DEFAULT CHARSET=utf8;
There are two lines that don't work:
KEY `FK_customers_customerID` (`customerID`),
CONSTRAINT `FK_customers_customerID` FOREIGN KEY (`customerID`) REFERENCES `customers` (`id`) ) DEFAULT CHARSET=utf8;
I'm trying to link the 'customerID' var in the 'orders' table to the same var in the 'customers' table.
It worked with the 'items-orders' and 'items' table, but now I keep getting this message:
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 'KEY `FK_customers_customerID` (`customerID`), CONSTRAINT `FK_customers_customer' at line 1
I really don't know how to fix this, any help would be appriciated.
The primary key in customers is CustomerId, so you need to use that:
CONSTRAINT `FK_customers_customerID` FOREIGN KEY (`customerID`)
REFERENCES `customers` (`customerID`)

How to resolve 'Foreign key constraint is incorrectly formed' issue

I am trying to create a database table called wp_tokens with a foreign key relationship to another table called wp_users. However, every time I attempt to run the create table SQL, I get the error "Foreign key constraint is incorrectly formed". I have tried multiple re-edits of the same code, but I just cannot figure out what is going on.
This is the code for wp_users
CREATE TABLE `wp_users` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_login` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_pass` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_nicename` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_url` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_activation_key` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_status` int(11) NOT NULL DEFAULT '0',
`display_name` varchar(250) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY `user_login_key` (`user_login`),
KEY `user_nicename` (`user_nicename`),
KEY `user_email` (`user_email`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci
This is the SQL for wp_tokens
CREATE TABLE `wp_tokens` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`token` varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES wp_users (`ID`)
)
Any help would be greatly appreciated!
The unsigned is important -- the types need to be identical. Try this:
CREATE TABLE `wp_tokens` (
id mediumint(9) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) unsigned NOT NULL,
`token` varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (user_id) REFERENCES wp_users (`ID`)
)
Here is a rextester.

Can't create table (errno 150) Openshift's phpmyadmin [duplicate]

This question already has answers here:
MySQL Creating tables with Foreign Keys giving errno: 150
(20 answers)
Closed 8 years ago.
Here's my schema:
-- Table 'users'
CREATE TABLE IF NOT EXISTS `users` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_bin NOT NULL,
`password` varchar(100) COLLATE utf8_bin NOT NULL,
`registrationDate` datetime NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS tasks (
id bigint(20) NOT NULL AUTO_INCREMENT,
description TEXT,
title TEXT,
type TEXT,
createdDate DATETIME NOT NULL,
finishedDate DATETIME,
user_id bigint(20) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT user_fk FOREIGN KEY (user_id) REFERENCES USERS(ID) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1;
I keep getting errno 150 when I try creating "tasks" table.
Looks like phpmyadmin on openshift is case sensitive, if you dig into the error you get this:
LATEST FOREIGN KEY ERROR
------------------------
140827 10:52:57 Error in foreign key constraint of table testing/tasks:
FOREIGN KEY (user_id) REFERENCES USERS(ID) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1:
Cannot resolve table name close to:
(ID) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1
In the below code, notice that i changed USERS to users in the second table creation code after REFERENCES.
Looks like this code works:
CREATE TABLE IF NOT EXISTS `users` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_bin NOT NULL,
`password` varchar(100) COLLATE utf8_bin NOT NULL,
`registrationDate` datetime NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS tasks (
id bigint(20) NOT NULL AUTO_INCREMENT,
description TEXT,
title TEXT,
type TEXT,
createdDate DATETIME NOT NULL,
finishedDate DATETIME,
user_id bigint(20) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT user_fk FOREIGN KEY (user_id) REFERENCES users(ID) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1;

Cannot add or update a child row: a foreign key constraint fails (Clubs and Users)

User Table
CREATE TABLE IF NOT EXISTS `users` (
`userId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`firstname` varchar(25) NOT NULL,
`lastname` varchar(25) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(32) NOT NULL,
`addressLine1` varchar(80) NOT NULL,
`addressLine2` varchar(80) NOT NULL,
`town` varchar(30) NOT NULL,
`county` varchar(30) NOT NULL,
PRIMARY KEY (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=55 ;
Club Table
CREATE TABLE IF NOT EXISTS `clubs` (
`clubId` int(11) NOT NULL AUTO_INCREMENT,
`clubName` varchar(100) NOT NULL,
`startTime` varchar(5) NOT NULL,
`finishTime` varchar(5) NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`clubId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
memberid Table
CREATE TABLE IF NOT EXISTS `membersid` (
`memberId` int(11) NOT NULL AUTO_INCREMENT,
`clubId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
PRIMARY KEY (`memberId`,`clubId`,`userId`),
UNIQUE KEY `memberId` (`memberId`),
KEY `clubId` (`clubId`),
KEY `userId` (`userId`),
KEY `clubId_2` (`clubId`),
KEY `clubId_3` (`clubId`),
KEY `userId_2` (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
DAO
String query = "INSERT INTO membersid( userId, clubId ) VALUES ( ?,? )";
Keep getting error Cannot add or update a child row: a foreign key constraint fails
If someone could be that would be great:)
Your keys in the membersid table (please change that name to members) are messed up. Try
CREATE TABLE IF NOT EXISTS members
(
`memberId` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`clubId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
UNIQUE KEY (`clubId`,`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;