how to change CONSTRAINT in this table for my project - mysql

Hi i make a table from this tutorial MySQL Foreign Key On Delete and this work good for my project
user table :
CREATE TABLE `User` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
UserStaff Table :
CREATE TABLE `UserStaff` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`UserId` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`Id`),
KEY `UserId` (`UserId`),
CONSTRAINT `UserStaff_ibfk_1`
FOREIGN KEY (`UserId`)
REFERENCES `User` (`Id`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
my question is : whate is UserStaff_ibfk_1 ?
can i change it to example_name or not
thanks

Related

mySQL: Cannot add foreign key constraint when creation a table

I want to create this table
CREATE TABLE `t_plan` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `t_plan_x_currency` (
`plan_id` bigint(20) NOT NULL ,
`currency_symbol` varchar(20) NOT NULL DEFAULT 'EUR',
`price` decimal(12,6) NOT NULL DEFAULT '0',
PRIMARY KEY (`plan_id`,`currency_symbol`),
CONSTRAINT `t_plan_x_currency_ibfk_1` FOREIGN KEY (`plan_id`) REFERENCES `t_plan` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
but I got this error:
Cannot add foreign key constraint
The columns involved in a foreign key relationship have to be of the same type. You have different types, int(11) for t_plan.id and bigint(20) for t_plan_x_currency.plan_id.
Make t_plan.id also a bigint(20).
CREATE TABLE `t_plan` (
`id` bigint(20) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `t_plan_x_currency` (
`plan_id` bigint(20) NOT NULL ,
`currency_symbol` varchar(20) NOT NULL DEFAULT 'EUR',
`price` decimal(12,6) NOT NULL DEFAULT '0',
PRIMARY KEY (`plan_id`,`currency_symbol`),
CONSTRAINT `t_plan_x_currency_ibfk_1` FOREIGN KEY (`plan_id`) REFERENCES `t_plan` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Or make t_plan_x_currency.plan_id an int(11), what ever is more desirable for you.

Foreign key constraint fails when I want delete table

When I am trying
drop tables users;
It shows error: Cannot delete or update a parent row: a foreign key constraint fails
Here are my tables:
CREATE TABLE `events` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(128) NOT NULL,
`users_id` int(11) NOT NULL,
`capacity` int(64) NOT NULL,
`event_date` datetime NOT NULL,
`text` text CHARACTER SET utf8 COLLATE utf8_czech_ci,
`last_edit` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `users_id` (`users_id`),
CONSTRAINT `events_ibfk_5` FOREIGN KEY (`users_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`role` enum('admin','member','guest','registered') NOT NULL DEFAULT 'registered',
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `users_data` (
`id` int(120) NOT NULL AUTO_INCREMENT,
`users_id` int(11) DEFAULT NULL,
`name` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`phone_number` varchar(25) NOT NULL,
PRIMARY KEY (`id`),
KEY `users_id` (`users_id`),
CONSTRAINT `users_data_ibfk_5` FOREIGN KEY (`users_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `user_matches` (
`user_id` int(120) NOT NULL,
`match_id` int(11) NOT NULL,
KEY `user_id` (`user_id`),
KEY `match_id` (`match_id`),
CONSTRAINT `user_matches_ibfk_4` FOREIGN KEY (`user_id`) REFERENCES `users_data` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `user_matches_ibfk_5` FOREIGN KEY (`match_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I think I did wrong foreign key but I am not sure how to change it correctly. Thank you for your any advice.
When you submit drop table users MySQL checks for referential integrity constraints violation when the deletion is about to be committed. This check fails because you have defined the following constraint of events:
CONSTRAINTevents_ibfk_5FOREIGN KEY (users_id) REFERENCESusers(id) ON DELETE CASCADE ON UPDATE CASCADE
and at the same time on the events table the users_id field cannot be NULL. Therefore, upon dropping the table, the users_id is set to NULL which fails during commit. One possible way to work around this problem is to remove the NOT NULL constraint on the users_id field on the events table.

(errno: 150) Error Driving Me Nuts

I might be missing something here, I have given up after 2 weeks of trying, Can anyone maybe see something blindingly obvious why this will not import please ?
I have tried to check everything that I can think of but when I try to insert it I just get the 150 error message.
SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
DROP TABLE IF EXISTS `classifieds_announcements`;
CREATE TABLE `classifieds_announcements` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author` int(11) NOT NULL,
`category` int(11) NOT NULL,
`title` tinytext NOT NULL,
`text` text NOT NULL,
`date` int(11) NOT NULL COMMENT 'timestamp',
`expire` int(11) NOT NULL COMMENT 'timestamp',
`photo` varchar(255) NOT NULL,
`approved` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `author` (`author`),
KEY `category` (`category`),
CONSTRAINT `classifieds_announcements_ibfk_1` FOREIGN KEY (`author`) REFERENCES `users` (`idu`) ON DELETE CASCADE,
CONSTRAINT `classifieds_announcements_ibfk_2` FOREIGN KEY (`category`) REFERENCES `classifieds_categories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_categories`;
CREATE TABLE `classifieds_categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` tinytext NOT NULL,
`parent` int(11) DEFAULT NULL,
`allow_posting` int(11) DEFAULT '1' COMMENT 'may be 0 or 1 only if parent=0, else will be 1',
`order` int(11) DEFAULT '50',
PRIMARY KEY (`id`),
KEY `parent` (`parent`),
CONSTRAINT `classifieds_categories_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `classifieds_categories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_config`;
CREATE TABLE `classifieds_config` (
`key` varchar(255) NOT NULL,
`value` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `classifieds_config` (`key`, `value`) VALUES
('allow_emoticons', '1'),
('post_on_user_wall', '1'),
('approve_new_posts', '1'),
('index_columns', '4'),
('time_between_announces', '300'),
('time_between_reviews', '90');
DROP TABLE IF EXISTS `classifieds_inputs`;
CREATE TABLE `classifieds_inputs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category` int(11) NOT NULL,
`name` text NOT NULL,
`type` varchar(255) NOT NULL,
`options` text,
`required` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `category` (`category`),
CONSTRAINT `classifieds_inputs_ibfk_1` FOREIGN KEY (`category`) REFERENCES `classifieds_categories` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_inputs_answers`;
CREATE TABLE `classifieds_inputs_answers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`input` int(11) NOT NULL,
`announce` int(11) NOT NULL,
`answer` text NOT NULL,
PRIMARY KEY (`id`),
KEY `input` (`input`),
KEY `announce` (`announce`),
CONSTRAINT `classifieds_inputs_answers_ibfk_1` FOREIGN KEY (`input`) REFERENCES `classifieds_inputs` (`id`) ON DELETE CASCADE,
CONSTRAINT `classifieds_inputs_answers_ibfk_2` FOREIGN KEY (`input`) REFERENCES `classifieds_inputs` (`id`) ON DELETE CASCADE,
CONSTRAINT `classifieds_inputs_answers_ibfk_3` FOREIGN KEY (`announce`) REFERENCES `classifieds_announcements` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_reports`;
CREATE TABLE `classifieds_reports` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`reported_by` int(11) NOT NULL,
`reported_announce` int(11) NOT NULL,
`read` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `reported_by` (`reported_by`),
KEY `reported_announce` (`reported_announce`),
CONSTRAINT `classifieds_reports_ibfk_1` FOREIGN KEY (`reported_by`) REFERENCES `users` (`idu`) ON DELETE CASCADE,
CONSTRAINT `classifieds_reports_ibfk_2` FOREIGN KEY (`reported_announce`) REFERENCES `classifieds_announcements` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `classifieds_reviews`;
CREATE TABLE `classifieds_reviews` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`announce` int(11) NOT NULL,
`score` tinyint(4) NOT NULL,
`user` int(11) NOT NULL,
`text` text NOT NULL,
PRIMARY KEY (`id`),
KEY `announce` (`announce`),
KEY `user` (`user`),
CONSTRAINT `classifieds_reviews_ibfk_1` FOREIGN KEY (`announce`) REFERENCES `classifieds_announcements` (`id`) ON DELETE CASCADE,
CONSTRAINT `classifieds_reviews_ibfk_2` FOREIGN KEY (`user`) REFERENCES `users` (`idu`) ON DELETE CASCADE,
CONSTRAINT `classifieds_reviews_ibfk_3` FOREIGN KEY (`announce`) REFERENCES `classifieds_announcements` (`id`) ON DELETE CASCADE,
CONSTRAINT `classifieds_reviews_ibfk_4` FOREIGN KEY (`user`) REFERENCES `users` (`idu`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Mysql cross-database foreign key

I am trying to create a cross database foreign key. When I run the following code on the same database
CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`int_id` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `int_id` (`int_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `t2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ext_id` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `ext_id` (`ext_id`),
CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`ext_id`) REFERENCES `t1` (`int_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
However, when create t1 on one databse (d1) and then run the following code on a second databse (d2), I receive the generic error: #1005 - Can't create table 'userdata.t2' (errno: 150)
CREATE TABLE `t2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ext_id` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `ext_id` (`ext_id`),
CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`ext_id`) REFERENCES `d1.t1` (`int_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Any help will be appreciated. Thanks!
Your syntax is not correct, try this
CREATE TABLE `t2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ext_id` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `ext_id` (`ext_id`),
CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`ext_id`) REFERENCES `d1`.`t1` (`int_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Foreign Key Constraint Fails - Probably misunderstanding something about my relationships

I'm having a little trouble with some MySQL relationships. I think I'm missing something obvious in my structure. Here's my SQL:
DROP TABLE IF EXISTS `parentlist_comments`;
CREATE TABLE `parentlist_comments` (
`id` char(36) NOT NULL,
`parentlist_id` char(36) NOT NULL,
`user_id` char(36) NOT NULL,
`comment` char(50) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `fk_parentlist_comments_parentlist` (`parentlist_id`),
KEY `fk_parentlist_comment_user` (`user_id`),
CONSTRAINT `fk_parentlist_comments_parentlist` FOREIGN KEY (`parentlist_id`) REFERENCES `parentlists` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_comment_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `parentlist_submissions`;
CREATE TABLE `parentlist_submissions` (
`id` char(36) NOT NULL,
`parentlist_id` char(36) NOT NULL,
`type_id` char(36) NOT NULL,
`name` char(25) NOT NULL,
`user_id` char(36) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`votes` int(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_parentlist_submissions_user` (`user_id`),
KEY `fk_parentlist_submissions_list` (`parentlist_id`),
KEY `fk_parentlist_submissions_type` (`type_id`),
CONSTRAINT `fk_parentlist_submissions_list` FOREIGN KEY (`parentlist_id`) REFERENCES `parentlists` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_submissions_type` FOREIGN KEY (`type_id`) REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_parentlist_submissions_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `parentlists`;
CREATE TABLE `parentlists` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`description` char(50) NOT NULL,
`user_id` char(36) NOT NULL,
`max_comments` int(3) NOT NULL DEFAULT '0',
`max_submissions` int(3) NOT NULL DEFAULT '10',
`max_votes` int(3) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_list_user` (`user_id`),
CONSTRAINT `fk_list_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED;
DROP TABLE IF EXISTS `submissions`;
CREATE TABLE `submissions` (
`id` char(36) NOT NULL,
`type_id` char(36) NOT NULL,
`name` char(30) NOT NULL,
`description` char(50) NOT NULL,
`embed` char(200) DEFAULT NULL,
`user_id` char(36) NOT NULL,
`accepted` tinyint(1) NOT NULL DEFAULT '0',
`submitted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`votes` int(5) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `fk_submission_user` (`user_id`),
KEY `fk_submission_type` (`type_id`),
CONSTRAINT `fk_submission_type` FOREIGN KEY (`type_id`) REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_submission_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `types`;
CREATE TABLE `types` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`description` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` char(36) NOT NULL,
`name` char(20) NOT NULL,
`password` char(20) NOT NULL,
`email` char(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I created an column called submission_id in parentlist_submissions. I'm trying to create a foreign key relationship between parentlist_submissions.submission_id and submissions.id, when I attempt to do this I get the error: Foriegn key constraint fails. For whatever reason my query browser won't let me copy this.
Any help here is greatly appreciated!
That error is usually caused by the tables already being populated with data that violate the constraint. (Note that nulls may be a problem if you've just added the column.)
I'm guessing, because I don't see that you've posted the statement where you create the submission_index column or where you create the foreign key constraint.
You seem to be missing the "parentlist_submissions.submission_id" column.