MySQL foreign key failure on create but not alter statement - mysql

The below SQL fails. with "#1005 - Can't create table 'scheduler_appointmentLog' (errno: 150)"
CREATE TABLE IF NOT EXISTS `scheduler_appointment` (
`id` INT NOT NULL AUTO_INCREMENT,
`timeAdded` INT NOT NULL,
`timeModified` INT NOT NULL,
`core_office_id` INT NOT NULL,
`core_patrparty_id` INT NOT NULL,
`core_procedure_id` INT NOT NULL,
`core_staff_id_dr` INT NOT NULL,
`scheduler_appointmentStatus_id` INT NOT NULL,
`scheduler_chair_id` INT NOT NULL,
`apptDate` DATE NULL,
`startTime` TIME NULL,
`endTime` TIME NULL,
`arrivedTime` TIME NULL,
`seatTime` TIME NULL,
`readyForDoctor` TIME NULL,
`frontDeskTime` TIME NULL,
`departTime` TIME NULL,
`comment` LONGTEXT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_scheduler_appointment_scheduler_appointmentRequest1`
FOREIGN KEY (`id`)
REFERENCES `scheduler_appointmentRequest` (`scheduler_appointment_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_scheduler_appointment_financial_payschedQ1`
FOREIGN KEY (`id`)
REFERENCES `financial_payschedQ` (`scheduler_appointment_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_scheduler_appointment_financial_chargeLog1`
FOREIGN KEY (`id`)
REFERENCES `financial_chargeLog` (`scheduler_appointment_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_scheduler_appointment_scheduler_appointmentLog1`
FOREIGN KEY (`id`)
REFERENCES `scheduler_appointmentLog` (`scheduler_appointment_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
HOWEVER if i turn it into 2 statements it works. The Create table in one statement and the constraints in the other.
This will add the table with no errors.
CREATE TABLE IF NOT EXISTS `scheduler_appointment` (
`id` INT NOT NULL AUTO_INCREMENT,
`timeAdded` INT NOT NULL,
`timeModified` INT NOT NULL,
`core_office_id` INT NOT NULL,
`core_patrparty_id` INT NOT NULL,
`core_procedure_id` INT NOT NULL,
`core_staff_id_dr` INT NOT NULL,
`scheduler_appointmentStatus_id` INT NOT NULL,
`scheduler_chair_id` INT NOT NULL,
`apptDate` DATE NULL,
`startTime` TIME NULL,
`endTime` TIME NULL,
`arrivedTime` TIME NULL,
`seatTime` TIME NULL,
`readyForDoctor` TIME NULL,
`frontDeskTime` TIME NULL,
`departTime` TIME NULL,
`comment` LONGTEXT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
And this will add the constraint with no errors.
ALTER TABLE `scheduler_appointment` ADD
CONSTRAINT `fk_scheduler_appointment_scheduler_appointmentRequest1`
FOREIGN KEY (`id`)
REFERENCES `scheduler_appointmentRequest` (`scheduler_appointment_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
EDIT...
Working export from phpMyAdmin to show mine is not backwards.
ALTER TABLE `scheduler_appointment`
ADD CONSTRAINT `fk_scheduler_appt_financial_payschedQ1` FOREIGN KEY (`id`) REFERENCES `financial_payschedQ` (`scheduler_appointment_id`),
ADD CONSTRAINT `fk_scheduler_appointment_financial_payschedQ1` FOREIGN KEY (`id`) REFERENCES `financial_payschedQ` (`scheduler_appointment_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_scheduler_appointment_scheduler_appointmentLog1` FOREIGN KEY (`id`) REFERENCES `scheduler_appointmentLog` (`scheduler_appointment_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_scheduler_appointment_scheduler_appointmentRequest1` FOREIGN KEY (`id`) REFERENCES `scheduler_appointmentRequest` (`scheduler_appointment_id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
I guess i need help to understand why the ALTER table will add it to the database but it can not be done in the create table statement. If they both failed i would not bother asking, but one works and one doesnt. If MySQL Workbench would export as ALTER statements (like phpMyAdmin does) instead of inline statements then i also wouldn't care to know the answer. However i am stuck needing to know to make the exported file from workbench to load into phpMyAdmin.

Related

Proper use of ON CASCADE UPDATE?

While trying to run the following query:
UPDATE Flight
SET FLNO = '1001'
WHERE FLNO = '1000';
I receive the following error:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`user/FlightLegInstance`, CONSTRAINT `FlightLegInstance_ibfk_1` FOREIGN KEY (`FLNO`) REFERENCES `FlightLeg` (`FLNO`) ON UPDATE CASCADE)
The following are my creation queries:
Flight:
CREATE TABLE Flight (
FLNO INTEGER NOT NULL,
Meal varchar(50) NOT NULL,
Smoking char(1) NOT NULL,
PRIMARY KEY (FLNO)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
FlightLeg:
CREATE TABLE FlightLeg (
FLNO INTEGER NOT NULL,
Seq char(25) NOT NULL,
FromA char(3) NOT NULL,
ToA char(3) NOT NULL,
DeptTime DATETIME NOT NULL,
ArrTime DATETIME NOT NULL,
Plane INTEGER NOT NULL,
PRIMARY KEY (FLNO, Seq),
FOREIGN KEY (FLNO) REFERENCES Flight(FLNO) ON UPDATE CASCADE,
FOREIGN KEY (FromA) REFERENCES Airport(Code),
FOREIGN KEY (ToA) REFERENCES Airport(Code)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
FlightLegInstance:
CREATE TABLE FlightLegInstance (
Seq char(25) NOT NULL,
FLNO INTEGER NOT NULL,
FDate DATE NOT NULL,
ActDept DATETIME NOT NULL,
ActArr DATETIME NOT NULL,
Pilot INTEGER NOT NULL,
PRIMARY KEY (Seq, FLNO, FDate),
FOREIGN KEY (FLNO) REFERENCES FlightLeg(FLNO) ON UPDATE CASCADE,
FOREIGN KEY (FLNO, FDate) REFERENCES FlightInstance(FLNO, FDate) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I would assume that the error is in one of the two FK definitions of FlightLegInstance, but I'm not sure. Can anyone help me with this?
Thanks.
this is not working just because you have defind [flno] column in second table as foriegn key
the solution of this problem is you have to update it at both places
thanks

Error 1215: Cannot add foreign key constraint SQL Statement

Not sure why I am still encountering the issue "Error 1215" wherein they have the same data type and parent table is in primary key.
child table:
CREATE TABLE `customer_notice_type` (
`CUSTOMER_NOTICE_TYPE_ID` int(11) NOT NULL AUTO_INCREMENT,
`CUSTOMER_ID` int(11) NOT NULL,
`CUSTOMER_NOTICE_TYPE_NAME` varchar(50) NOT NULL,
`SYSTEM_NOTICE_TYPE_ID` int(11) NOT NULL,
`STATUS` char(1) NOT NULL,
`CREATED_BY` varchar(50) NOT NULL,
`CREATED_DATE` datetime NOT NULL,
`MODIFIED_BY` varchar(50) DEFAULT NULL,
`MODIFIED_DATE` datetime DEFAULT NULL,
PRIMARY KEY (`CUSTOMER_NOTICE_TYPE_ID`),
KEY `fk_customer_id_customer_notice_type_idx` (`CUSTOMER_ID`),
CONSTRAINT `fk_customer_id_customer_notice_type` FOREIGN KEY (`CUSTOMER_ID`) REFERENCES `customer` (`CUSTOMER_ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=494 DEFAULT CHARSET=latin1;
parent table:
CREATE TABLE `system_notice_type` (
`SYSTEM_NOTICE_TYPE_ID` int(11) NOT NULL,
`SYSTEM_NOTICE_TYPE_NAME` varchar(45) NOT NULL,
`LINE_OF_BUSINESS_ID` int(11) NOT NULL,
`STATUS` char(1) NOT NULL,
PRIMARY KEY (`SYSTEM_NOTICE_TYPE_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;
SQL script to create Foreign Key:
ALTER TABLE `fexpress`.`customer_notice_type`
ADD CONSTRAINT `fk_system_notice_type_customer_notice_type`
FOREIGN KEY (`SYSTEM_NOTICE_TYPE_ID`)
REFERENCES `fexpress`.`system_notice_type` (`SYSTEM_NOTICE_TYPE_ID`)
ON DELETE CASCADE ON UPDATE CASCADE;
You have two potential problems. First, the alter table statement references fexpress. This may or may not be the correct schema for the table. So, that is one potential problem.
The second real problem is the constraint defined in the child table:
CONSTRAINT `fk_customer_id_customer_notice_type` FOREIGN KEY (`CUSTOMER_ID`) REFERENCES `customer`(`CUSTOMER_ID`) ON DELETE CASCADE ON UPDATE CASCADE
The parent table is not yet defined, so it generates an error.
Removing this row and adjusting the schema name results in working code, as in this SQL Fiddle.

SQL design for a navigation menu with one of two columns

I have navigation menus which have links either to an external url, or to a local page identified by the pages PK pages_id.
To implement, I created the following tables.
pages stores the HTML for the center part in each page.
menus stores the name of the menu.
pages_has_menus adds the menu to the page, and specified whether it is a main menu, sub menu, etc as specified by menu_types.
menus_has_pages specifies the actual items in the menu.
How can I ensure that menus_has_pages doesn't contain both a pages_id and a external_url, and also always contains one or the other?
Should the schema be changed by adding addition tables for each scenario?
CREATE TABLE IF NOT EXISTS `pages` (
`id` INT UNSIGNED NOT NULL,
`sites_id` INT UNSIGNED NOT NULL,
`html` TEXT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_pages_sites_idx` (`sites_id` ASC),
CONSTRAINT `fk_pages_sites`
FOREIGN KEY (`sites_id`)
REFERENCES `sites` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `menu_types` (
`type` CHAR(4) NOT NULL,
PRIMARY KEY (`type`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `menus` (
`id` INT UNSIGNED NOT NULL,
`name` VARCHAR(45) NULL,
`sites_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_menus_sites1_idx` (`sites_id` ASC),
CONSTRAINT `fk_menus_sites1`
FOREIGN KEY (`sites_id`)
REFERENCES `sites` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `pages_has_menus` (
`pages_id` INT UNSIGNED NOT NULL,
`menus_id` INT UNSIGNED NOT NULL,
`menu_types_type` CHAR(4) NOT NULL,
PRIMARY KEY (`pages_id`, `menus_id`),
INDEX `fk_pages_has_menus_menus1_idx` (`menus_id` ASC),
INDEX `fk_pages_has_menus_pages1_idx` (`pages_id` ASC),
INDEX `fk_pages_has_menus_menu_types1_idx` (`menu_types_type` ASC),
UNIQUE INDEX `unique_menu` (`pages_id` ASC, `menu_types_type` ASC),
CONSTRAINT `fk_pages_has_menus_pages1`
FOREIGN KEY (`pages_id`)
REFERENCES `pages` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_pages_has_menus_menus1`
FOREIGN KEY (`menus_id`)
REFERENCES `menus` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_pages_has_menus_menu_types1`
FOREIGN KEY (`menu_types_type`)
REFERENCES `menu_types` (`type`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `menus_has_pages` (
`menus_id` INT UNSIGNED NOT NULL,
`pages_id` INT UNSIGNED NULL,
`external_url` VARCHAR(45) NULL,
`order` TINYINT NULL,
`name` VARCHAR(45) NULL,
`new_window` TINYINT NULL,
PRIMARY KEY (`menus_id`, `pages_id`),
INDEX `fk_menus_has_pages_pages1_idx` (`pages_id` ASC),
INDEX `fk_menus_has_pages_menus1_idx` (`menus_id` ASC),
CONSTRAINT `fk_menus_has_pages_menus1`
FOREIGN KEY (`menus_id`)
REFERENCES `menus` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_menus_has_pages_pages1`
FOREIGN KEY (`pages_id`)
REFERENCES `pages` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I guess I would use two tables instead of yours:
one with:
pages_id INT UNSIGNED NOT NULL,
and one with :
external_url VARCHAR(45) NOT NULL,
And use 'UNION' when you need to merge two 'SELECT' queries.

MySQL duplicate key error

If I run this code all work well, but if uncomment last constraint, I got the following error:
Error Code: 1022. Can't write; duplicate key in table 'transfer'
but there no another key 'fk_component_id', what wrong with this code?
-- -----------------------------------------------------
-- Table `pcdb`.`transfer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `pcdb`.`transfer` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`component_id` INT UNSIGNED NOT NULL,
`type` INT NULL,
`parent_id` INT UNSIGNED NULL,
`source_id` INT UNSIGNED NULL,
`contractor_id` INT UNSIGNED NULL,
`src_department_id` INT UNSIGNED NULL,
`dest_department_id` INT UNSIGNED NULL,
`session_id` INT UNSIGNED NOT NULL,
`count` INT UNSIGNED NOT NULL,
`comment` TEXT(65535) NULL,
`active` TINYINT(1) NOT NULL DEFAULT 1,
PRIMARY KEY (`id`),
CONSTRAINT `fk_src_department_id`
FOREIGN KEY (`src_department_id`)
REFERENCES `pcdb`.`department` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_income_id`
FOREIGN KEY (`source_id`)
REFERENCES `pcdb`.`transfer` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_contractor_id`
FOREIGN KEY (`contractor_id`)
REFERENCES `pcdb`.`contractor` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_session_id`
FOREIGN KEY (`session_id`)
REFERENCES `pcdb`.`session` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_dest_department_id`
FOREIGN KEY (`dest_department_id`)
REFERENCES `pcdb`.`department` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_parent_id`
FOREIGN KEY (`parent_id`)
REFERENCES `pcdb`.`transfer` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION/*,
CONSTRAINT `fk_component_id`
FOREIGN KEY (`component_id`)
REFERENCES `pcdb`.`component` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION*/);
It sounds like you may already have a constraint with that name on a different table in the database. Try changing the name of that constraint to something like "fk_transfer_component_id" and see if you still get the error.

Why can't I add a foreign key constraint this way?

Tables:
CREATE TABLE `relation` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
`gender` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_relation` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `invite` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`date_sent` date NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`relation_id` int(10) unsigned NOT NULL,
`email` varchar(255) NOT NULL,
`code` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_user` (`user_id`),
CONSTRAINT `fk_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
The SQL statement executed was:
ALTER TABLE `invite`
ADD CONSTRAINT `fk_relation`
FOREIGN KEY (`relation_id`)
REFERENCES `relation` (`id`)
ON DELETE CASCADE ON UPDATE RESTRICT
Mysql Error:
SQLSTATE[HY000]: General error: 1005 Can't create table 'dbtest.#sql-d00_39' (errno: 121).
The relation.id and invite.relation_id columns are of the same type int(10) unsigned
UPDATE
The table invite is empty while adding this key.
The table relation has 3 rows.
try this :
ALTER TABLE invite
ADD CONSTRAINT fk_relation
FOREIGN KEY (relation_id)
REFERENCES relation(id)
According to the doc syntax is correct SQL FOREIGN KEY Constraint
The DDL for Foreign Key creation now automatically includes statements to specify actions on "Delete" and "Update". However, for "Delete", it includes the statement "ON DELETE RESTRICT", which does not appear to be a valid T-SQL statement.
TRY THIS :
ALTER TABLE invite WITH CHECK ADD CONSTRAINT fk_relation
FOREIGN KEY (relation_id) REFERENCES relation (id)