Insert into a table with forign key within the trigger of the other table in MySQL - mysql

I have a parent table in MySQL as follows:
CREATE TABLE `parentTBL` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`source_name` varchar(255) DEFAULT NULL,
`loc1` smallint(5) unsigned DEFAULT NULL,
`loc2` smallint(5) unsigned DEFAULT NULL,
`extra_info` json DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;
a child table:
CREATE TABLE `childTBL` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`source_names_id` int(10) unsigned NOT NULL,
`locations_id` int(10) unsigned NOT NULL,
`extra_info` json DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_childTBL1_idx` (`source_names_id`),
KEY `fk_childTBL2_idx` (`locations_id`),
CONSTRAINT `fk_childTBL1` FOREIGN KEY (`source_names_id`) REFERENCES `source_names` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_childTBL2` FOREIGN KEY (`locations_id`) REFERENCES `locations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
and these 2 tables:
CREATE TABLE `source_names` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`source_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `source_name_UNIQUE` (`source_name`)
) ENGINE=InnoDB AUTO_INCREMENT=85 DEFAULT CHARSET=latin1;
CREATE TABLE `locations` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`loc1` smallint(5) unsigned DEFAULT NULL,
`loc2` smallint(5) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `loc1` (`loc1`,`loc2`)
) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=latin1;
I have a trigger for parentTBL table before insert like this:
CREATE DEFINER=`root`#`localhost` TRIGGER `myDB`.`parentTBL_BEFORE_INSERT` BEFORE INSERT ON `parentTBL` FOR EACH ROW
BEGIN
INSERT IGNORE INTO source_names (source_name) VALUES (NEW.source_name);
INSERT IGNORE INTO locations (loc1,loc2) VALUES (NEW.loc1,NEW.loc2);
INSERT IGNORE INTO childTBL (source_names_id,locations_id,extra_info) VALUES (????,????,NEW.extra_info);
END
and I want to insert into childTBL and 2 other tables before inserting into parentTBL but I don't know what should I put in ???? fields in above trigger (third INSERT IGNORE. Move scroll to right)?
(It should be noted that I don't want to use SELECT because I have some millions records and fast insertion is important to me and also my problem is not solved with LAST_INSERT_ID() because it is possible that I need the source_names_id which is added before last_inserted in source_names table.)

Related

Mysql on delete cascade not working

i have the tables:
DROP TABLE IF EXISTS `files`;
CREATE TABLE IF NOT EXISTS `files` (
`id` VARCHAR(36) NOT NULL,
`name` VARCHAR(50) NOT NULL,
`extension` VARCHAR(5) NOT NULL,
`version` INT(11) NOT NULL,
`date` DATE NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and TagsFiles:
DROP TABLE If EXISTS `tags_files`;
CREATE TABLE IF NOT EXISTS `tags_files` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`tag_id` INT(11) NOT NULL,
`file_id` VARCHAR(36) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT FOREIGN KEY (`file_id`) REFERENCES `files` (`id`) ON DELETE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
if i now delete one or more entries of the tags_files table, there are no entries in files deleted. Can someone tell me why?
The constrain you have now will delete from tags_files when a referenced id will be deleted on files.
If you need to automatically delete from files when you delete from tags_files, then the constrain must be on files table.
Like this:
DROP TABLE IF EXISTS `files`;
CREATE TABLE IF NOT EXISTS `files` (
`id` VARCHAR(36) NOT NULL,
`name` VARCHAR(50) NOT NULL,
`extension` VARCHAR(5) NOT NULL,
`version` INT(11) NOT NULL,
`date` DATE NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT FOREIGN KEY (`id`) REFERENCES `tags_files` (`file_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and TagsFiles:
DROP TABLE If EXISTS `tags_files`;
CREATE TABLE IF NOT EXISTS `tags_files` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`tag_id` INT(11) NOT NULL,
`file_id` VARCHAR(36) NOT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Well you get an error in above example because MySQL don't allow constraints for varchar type. If you change it to char you can. But you also need to change the sequence of the execution queries due to constrains. Do like this:
DROP TABLE IF EXISTS `files`;
DROP TABLE If EXISTS `tags_files`;
CREATE TABLE IF NOT EXISTS `tags_files` (
`id` INT(11) NOT NULL,
`tag_id` INT(11) NOT NULL,
`file_id` char(36) NOT NULL,
PRIMARY KEY (`file_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `files` (
`id` char(36) NOT NULL,
`name` VARCHAR(50) NOT NULL,
`extension` VARCHAR(5) NOT NULL,
`version` INT(11) NOT NULL,
`date` DATE NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT FOREIGN KEY (`id`) REFERENCES `tags_files` (`file_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

how to get autoincrement ID of one table and insert in another table

I have a mysql table whose description is given below
Create Table
CREATE TABLE `question` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`category` varchar(255) DEFAULT NULL,
`is_deleted` bit(1) NOT NULL,
`question` varchar(255) DEFAULT NULL,
`version` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8
I have another table
Create Table
CREATE TABLE `parent_question` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`is_deleted` bit(1) NOT NULL,
`version` int(11) DEFAULT NULL,
`pid` bigint(20) NOT NULL,
`qid` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK8FEA83DBE860AF9` (`pid`),
KEY `FK8FEA83DBF34C20F6` (`qid`),
CONSTRAINT `FK8FEA83DBE860AF9` FOREIGN KEY (`pid`) REFERENCES `parent` (`id`),
CONSTRAINT `FK8FEA83DBF34C20F6` FOREIGN KEY (`qid`) REFERENCES `question` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8
I have the right to insert one row in the question table.So when I add a new row then id will be generated (because it is auto_increment) and I want to store this id in the qid field parent_question table.
Can anybody please tell me how to do it?
Use the last_insert_id() function:
insert into question values (...);
insert into parent_question values (..., last_insert_id(), ...);

Importing a database schema from a SQL DUMP

I have the following database schema that has a bunch of tables and foreign keys, when i try to import the sql dump i keep getting the following errors.
Can't create table errno 150
I understand that it is trying to create tables with dependencies of tables that are not created yet but i don't understand how to import the schema without butchering out all of the foreign keys and then re-creating them per the answers given on Stack and google.
There has to be an easier way, what do big companies do that have hundreds of tables?
I have the sql statements below and any suggestions would be appreciated. Thanks
#
# Encoding: Unicode (UTF-8)
#
DROP TABLE IF EXISTS `contact_interest`;
DROP TABLE IF EXISTS `contact_seeking`;
DROP TABLE IF EXISTS `interests`;
DROP TABLE IF EXISTS `job_current`;
DROP TABLE IF EXISTS `job_desired`;
DROP TABLE IF EXISTS `job_listings`;
DROP TABLE IF EXISTS `my_contacts`;
DROP TABLE IF EXISTS `profession`;
DROP TABLE IF EXISTS `seeking`;
DROP TABLE IF EXISTS `status`;
DROP TABLE IF EXISTS `zip_code`;
CREATE TABLE `contact_interest` (
`contact_id` int(10) unsigned NOT NULL,
`interest_id` int(10) unsigned NOT NULL,
KEY `mycontacts_contactinterest_fk` (`contact_id`),
KEY `interests_contactinterest_fk` (`interest_id`),
CONSTRAINT `mycontacts_contactinterest_fk` FOREIGN KEY (`contact_id`) REFERENCES `my_contacts` (`contact_id`),
CONSTRAINT `interests_contactinterest_fk` FOREIGN KEY (`interest_id`) REFERENCES `interests` (`interest_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `contact_seeking` (
`contact_id` int(10) unsigned NOT NULL,
`seeking_id` int(10) unsigned NOT NULL,
KEY `contactid_contactseeking_fk` (`contact_id`),
KEY `seeking_contactseeking_fk` (`seeking_id`),
CONSTRAINT `contactid_contactseeking_fk` FOREIGN KEY (`contact_id`) REFERENCES `my_contacts` (`contact_id`),
CONSTRAINT `seeking_contactseeking_fk` FOREIGN KEY (`seeking_id`) REFERENCES `seeking` (`seeking_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `interests` (
`interest_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`interest` varchar(50) DEFAULT NULL,
PRIMARY KEY (`interest_id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=latin1;
CREATE TABLE `job_current` (
`contact_id` int(10) unsigned NOT NULL,
`title` varchar(20) DEFAULT NULL,
`salary` decimal(8,2) DEFAULT NULL,
`start_date` date DEFAULT NULL,
KEY `mycontacts_jobcurrent_fk` (`contact_id`),
CONSTRAINT `mycontacts_jobcurrent_fk` FOREIGN KEY (`contact_id`) REFERENCES `my_contacts` (`contact_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `job_desired` (
`contact_id` int(10) unsigned NOT NULL,
`title` varchar(20) DEFAULT NULL,
`salary_low` decimal(8,2) DEFAULT NULL,
`salary_high` decimal(8,2) DEFAULT NULL,
`available` date DEFAULT NULL,
`years_exp` int(11) DEFAULT NULL,
KEY `mycontacts_jobdesired_fk` (`contact_id`),
CONSTRAINT `mycontacts_jobdesired_fk` FOREIGN KEY (`contact_id`) REFERENCES `my_contacts` (`contact_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `job_listings` (
`job_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(25) DEFAULT NULL,
`salary` decimal(8,2) DEFAULT NULL,
`zip_code` char(5) DEFAULT NULL,
`description` varchar(50) DEFAULT NULL,
PRIMARY KEY (`job_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
CREATE TABLE `my_contacts` (
`contact_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`last_name` varchar(30) DEFAULT NULL,
`first_name` varchar(20) DEFAULT NULL,
`phone` char(10) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`prof_id` int(11) unsigned NOT NULL,
`status_id` int(10) unsigned NOT NULL,
`zip_code` char(5) DEFAULT NULL,
PRIMARY KEY (`contact_id`),
KEY `profession_mycontacts_fk` (`prof_id`),
KEY `zipcode_mycontacts_fk` (`zip_code`),
KEY `status_my_contacts_fk` (`status_id`),
CONSTRAINT `profession_mycontacts_fk` FOREIGN KEY (`prof_id`) REFERENCES `profession` (`prof_id`),
CONSTRAINT `status_my_contacts_fk` FOREIGN KEY (`status_id`) REFERENCES `status` (`status_id`),
CONSTRAINT `zipcode_mycontacts_fk` FOREIGN KEY (`zip_code`) REFERENCES `zip_code` (`zip_code`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1;
CREATE TABLE `profession` (
`prof_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`profession` varchar(30) DEFAULT NULL,
PRIMARY KEY (`prof_id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;
CREATE TABLE `seeking` (
`seeking_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`seeking` varchar(40) DEFAULT NULL,
PRIMARY KEY (`seeking_id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1;
CREATE TABLE `status` (
`status_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`status` varchar(30) DEFAULT NULL,
PRIMARY KEY (`status_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
CREATE TABLE `zip_code` (
`zip_code` char(5) NOT NULL DEFAULT '',
`city` varchar(20) DEFAULT NULL,
`state` char(2) DEFAULT NULL,
PRIMARY KEY (`zip_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I found that I only needed two lines to fix my issue, I added one 0 at the top and 1 at the bottom and I was good. Sorry to waste your time...
SET FOREIGN_KEY_CHECKS = 0;
SET FOREIGN_KEY_CHECKS = 1;
the easiest way would be to do it via commandline like this:
mysql db_name < backup-file.sql
this executes your sql file in one transaction. If you execute your stuff in one transaction then you won't get the foreign key errors.

how to best design the DB in this situation

At a glance, the database schema looks like this:
The schema has to be in 3rd normal form (and I am aware that hotels.average_rating suggests otherwise, try to oversee that, since the database is not fully designed yet). This is for a tourist recommendation system.
The SQL:
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE `activities` (
`activity_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`activity_name` varchar(277) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`activity_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `bookings` (
`from_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`to_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`belong_user` int(10) unsigned NOT NULL,
`belong_hotel` int(10) unsigned NOT NULL,
`rating` int(3) unsigned NOT NULL,
KEY `belong_user` (`belong_user`),
KEY `belong_hotel` (`belong_hotel`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `countries` (
`cuntry_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`country_name` varchar(20) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`cuntry_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `hotels` (
`hotel_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`hotel_name` varchar(128) COLLATE utf8_bin NOT NULL,
`hotel_stars` int(3) NOT NULL,
`hotel_description` text COLLATE utf8_bin NOT NULL,
`average_price` float unsigned NOT NULL,
`average_rating` float unsigned NOT NULL,
`total_rooms` int(10) unsigned NOT NULL,
`free_rooms` int(10) unsigned NOT NULL,
`belong_region` int(10) unsigned NOT NULL,
PRIMARY KEY (`hotel_id`),
KEY `belong_region` (`belong_region`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `hotels_activity_offers` (
`belong_hotel` int(10) unsigned NOT NULL,
`belong_activity` int(10) unsigned NOT NULL,
UNIQUE KEY `belong_hotel_2` (`belong_hotel`,`belong_activity`),
KEY `belong_hotel` (`belong_hotel`),
KEY `belong_activity` (`belong_activity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `regions` (
`region_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`belong_country` int(10) unsigned NOT NULL,
`region_name` varchar(255) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`region_id`),
KEY `belong_country` (`belong_country`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `regions_activity_offers` (
`belong_region` int(10) unsigned NOT NULL,
`belong_activity` int(10) unsigned NOT NULL,
KEY `belong_region` (`belong_region`),
KEY `belong_activity` (`belong_activity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `users` (
`user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(20) COLLATE utf8_bin NOT NULL,
`password` varchar(40) COLLATE utf8_bin NOT NULL COMMENT 'MD5',
`first_name` varchar(20) COLLATE utf8_bin NOT NULL,
`last_name` varchar(20) COLLATE utf8_bin NOT NULL,
`email` varchar(255) COLLATE utf8_bin NOT NULL,
`is_admin` tinyint(1) NOT NULL,
`is_active` tinyint(1) NOT NULL,
PRIMARY KEY (`user_id`),
KEY `is_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `users_favourite_activities` (
`belong_user` int(10) unsigned NOT NULL,
`belong_activity` int(10) unsigned NOT NULL,
UNIQUE KEY `belong_user_2` (`belong_user`,`belong_activity`),
KEY `belong_user` (`belong_user`),
KEY `belong_activity` (`belong_activity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
ALTER TABLE `bookings`
ADD CONSTRAINT `bookings_ibfk_3` FOREIGN KEY (`belong_hotel`) REFERENCES `hotels` (`hotel_id`) ON DELETE CASCADE,
ADD CONSTRAINT `bookings_ibfk_2` FOREIGN KEY (`belong_user`) REFERENCES `users` (`user_id`) ON DELETE CASCADE;
ALTER TABLE `hotels`
ADD CONSTRAINT `hotels_ibfk_1` FOREIGN KEY (`belong_region`) REFERENCES `regions` (`region_id`) ON DELETE CASCADE;
ALTER TABLE `hotels_activity_offers`
ADD CONSTRAINT `hotels_activity_offers_ibfk_2` FOREIGN KEY (`belong_activity`) REFERENCES `activities` (`activity_id`) ON DELETE CASCADE,
ADD CONSTRAINT `hotels_activity_offers_ibfk_1` FOREIGN KEY (`belong_hotel`) REFERENCES `hotels` (`hotel_id`) ON DELETE CASCADE;
ALTER TABLE `regions`
ADD CONSTRAINT `regions_ibfk_1` FOREIGN KEY (`belong_country`) REFERENCES `countries` (`cuntry_id`) ON DELETE CASCADE;
ALTER TABLE `regions_activity_offers`
ADD CONSTRAINT `regions_activity_offers_ibfk_2` FOREIGN KEY (`belong_activity`) REFERENCES `activities` (`activity_id`) ON DELETE CASCADE,
ADD CONSTRAINT `regions_activity_offers_ibfk_1` FOREIGN KEY (`belong_region`) REFERENCES `regions` (`region_id`) ON DELETE CASCADE;
ALTER TABLE `users_favourite_activities`
ADD CONSTRAINT `users_favourite_activities_ibfk_1` FOREIGN KEY (`belong_user`) REFERENCES `users` (`user_id`) ON DELETE CASCADE,
ADD CONSTRAINT `users_favourite_activities_ibfk_2` FOREIGN KEY (`belong_activity`) REFERENCES `activities` (`activity_id`) ON DELETE CASCADE;
The question is: how to best add a "user activity log" feature which stores the activities a user has taken part to? Note that both regions and hotels can have activities, and I need to be able to tell whether that activity has taken place in a region or in a hotel. Referential integrity should be guaranteed.
Present a query (it should use JOIN shouldn't it?) which lists all users and their activities along with the hotel id or region id. (the one which is not applicable can be NULL if required).
Simple solutions are better - so preferably without stored procedures or anything which digs too much in mysql-specific features.
Your database is not normalized - and the way you've done it looks like a poster-child for why normalization is a good idea.
hotels.average_rating?
WTF?
While it can make sense to denormalize your data - this is not how to do it. Think about what you need to do when a user submits a hotel rating - you need to recalculate the value based on all the ratings submitted. If instead you held a sum_of_ratings (or even retained the current average) and a number of ratings then you could calculate the new values based on the hotel record and the new rating without having to look at the other ratings.

MySQL Error 1451

Command:
DELETE FROM `meetings`.`meetingparticipants` WHERE `meetingparticipants`.`idParticipants` =503 AND `meetingparticipants`.`idMeetings` =83
I know I have a simple error in my database. I am attempting to delete a row from the meeting participants table and receiving the following error:
#1451 - Cannot delete or update a parent row: a foreign key constraint fails (`meetings`.`invoices`, CONSTRAINT `fk_meetingid` FOREIGN KEY (`fk_meetingid`) REFERENCES `meetingparticipants` (`idMeetings`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Any ideas? I think I need a foreign key relationship between the participant table and the meeting-participant table with updates, but I rather confirm or see if there are any suggestions??
CREATE TABLE IF NOT EXISTS `invoices` (
`idinvoices` int(11) NOT NULL AUTO_INCREMENT,
`fk_invoiceType` int(11) DEFAULT NULL,
`DocNum` varchar(10) DEFAULT NULL,
`DZ` varchar(9) DEFAULT NULL,
`Amount` decimal(10,2) DEFAULT NULL,
`fk_meetingid` int(11) DEFAULT NULL,
`fk_participantid` int(11) DEFAULT NULL,
`invoiceNotes` varchar(255) DEFAULT NULL,
`invoiceDocuments` blob,
PRIMARY KEY (`idinvoices`),
UNIQUE KEY `DOC_Num` (`DocNum`),
KEY `fk_invoiceType` (`fk_invoiceType`),
KEY `fk_meetingid` (`fk_meetingid`),
KEY `fk_participantid` (`fk_participantid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1014 ;
-- --------------------------------------------------------
--
-- Table structure for table `invoicestatus`
--
CREATE TABLE IF NOT EXISTS `invoicestatus` (
`idinvoiceStatus` int(11) NOT NULL AUTO_INCREMENT,
`invoiceStatus` varchar(45) NOT NULL,
PRIMARY KEY (`idinvoiceStatus`),
UNIQUE KEY `invoiceStatus_UNIQUE` (`invoiceStatus`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;
-- --------------------------------------------------------
--
-- Table structure for table `invoicestatushistory`
--
CREATE TABLE IF NOT EXISTS `invoicestatushistory` (
`fk_invoiceStatus` int(11) DEFAULT NULL,
`statusStamp` datetime DEFAULT NULL,
`fk_idinvoices` int(11) NOT NULL,
`idinvoiceStatusHistory` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`idinvoiceStatusHistory`,`fk_idinvoices`),
KEY `invoiceStatusHistory` (`fk_invoiceStatus`),
KEY `invoiceid` (`fk_idinvoices`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1015 ;
-- --------------------------------------------------------
--
-- Table structure for table `invoicetypes`
--
CREATE TABLE IF NOT EXISTS `invoicetypes` (
`idinvoiceTypes` int(11) NOT NULL AUTO_INCREMENT,
`invoiceType` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idinvoiceTypes`),
UNIQUE KEY `invoiceType_UNIQUE` (`invoiceType`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;
-- --------------------------------------------------------
--
-- Table structure for table `meetingparticipants`
--
CREATE TABLE IF NOT EXISTS `meetingparticipants` (
`idParticipants` int(11) NOT NULL,
`idMeetings` int(11) NOT NULL,
`invited` tinyint(1) DEFAULT NULL,
`attended` tinyint(1) DEFAULT NULL,
`travelCostsReimbursed` tinyint(1) DEFAULT NULL,
`ParticipantFeeEligible` tinyint(1) DEFAULT NULL,
`ParticipantFeeProcessed` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`idParticipants`,`idMeetings`),
KEY `idParticipants` (`idParticipants`),
KEY `idMeetings` (`idMeetings`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `meetings`
--
CREATE TABLE IF NOT EXISTS `meetings` (
`idmeetings` int(11) NOT NULL AUTO_INCREMENT,
`meetingName` varchar(45) NOT NULL,
`beginDate` date DEFAULT NULL,
`endDate` date DEFAULT NULL,
`location` varchar(45) DEFAULT NULL,
`meetingNotes` varchar(255) DEFAULT NULL,
`meetingDocuments` blob,
`fk_programID` int(110) DEFAULT NULL,
PRIMARY KEY (`idmeetings`),
UNIQUE KEY `meetingName_UNIQUE` (`meetingName`),
KEY `programid` (`fk_programID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=85 ;
-- --------------------------------------------------------
--
-- Table structure for table `participants`
--
CREATE TABLE IF NOT EXISTS `participants` (
`idparticipants` int(11) NOT NULL AUTO_INCREMENT,
`firstName` varchar(45) DEFAULT NULL,
`lastName` varchar(45) NOT NULL,
`preferredName` varchar(75) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`affiliation` varchar(45) DEFAULT NULL,
`foreignNational` tinyint(1) DEFAULT NULL,
`grantPaid` tinyint(1) DEFAULT NULL,
`bannerid` varchar(9) DEFAULT NULL,
`participantNotes` varchar(255) DEFAULT NULL,
`participantDocuments` blob,
`visaType` int(11) DEFAULT NULL,
PRIMARY KEY (`idparticipants`),
UNIQUE KEY `bannerid_UNIQUE` (`bannerid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=507 ;
-- --------------------------------------------------------
--
-- Table structure for table `programs`
--
CREATE TABLE IF NOT EXISTS `programs` (
`idprograms` int(11) NOT NULL AUTO_INCREMENT,
`ProgramName` varchar(45) NOT NULL,
PRIMARY KEY (`idprograms`),
UNIQUE KEY `ProgramName_UNIQUE` (`ProgramName`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`userID` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(25) NOT NULL,
`userPassword` varchar(10) NOT NULL DEFAULT 'password',
`userLevel` int(11) NOT NULL,
PRIMARY KEY (`userID`),
UNIQUE KEY `userName` (`userName`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
-- --------------------------------------------------------
--
-- Table structure for table `visatypes`
--
CREATE TABLE IF NOT EXISTS `visatypes` (
`idvisaTypes` int(11) NOT NULL AUTO_INCREMENT,
`visaType` varchar(45) DEFAULT NULL,
`paymentInstructions` varchar(500) DEFAULT NULL,
PRIMARY KEY (`idvisaTypes`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=27 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `invoices`
--
ALTER TABLE `invoices`
ADD CONSTRAINT `fk_invoiceType` FOREIGN KEY (`fk_invoiceType`) REFERENCES `invoicetypes` (`idinvoiceTypes`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_meetingid` FOREIGN KEY (`fk_meetingid`) REFERENCES `meetingparticipants` (`idMeetings`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_participantid` FOREIGN KEY (`fk_participantid`) REFERENCES `meetingparticipants` (`idParticipants`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `invoicestatushistory`
--
ALTER TABLE `invoicestatushistory`
ADD CONSTRAINT `invoiceid` FOREIGN KEY (`fk_idinvoices`) REFERENCES `invoices` (`idinvoices`) ON DELETE NO ACTION ON UPDATE CASCADE,
ADD CONSTRAINT `invoiceStatusHistory` FOREIGN KEY (`fk_invoiceStatus`) REFERENCES `invoicestatus` (`idinvoiceStatus`) ON DELETE NO ACTION ON UPDATE CASCADE;
--
-- Constraints for table `meetings`
--
ALTER TABLE `meetings`
ADD CONSTRAINT `programid` FOREIGN KEY (`fk_programID`) REFERENCES `programs` (`idprograms`) ON DELETE NO ACTION ON UPDATE NO ACTION;
You have a foreign key constraint on the invoices table that is named "fk_meetingid", but references the meetingparticipants table, which is an association table. It would seem to me that if an invoice were linked to a meeting that it should be linked directly to the meetings table.
From the manual:
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
NO ACTION: A keyword from standard SQL. In MySQL, equivalent to
RESTRICT. InnoDB rejects the delete or update operation for the parent
table if there is a related foreign key value in the referenced table.
Some database systems have deferred checks, and NO ACTION is a
deferred check. In MySQL, foreign key constraints are checked
immediately, so NO ACTION is the same as RESTRICT.
If I understand your schema, (which is unlikely after looking at it for 30 seconds), you probably want to use ON DELETE CASCADE so that a participants invoices are deleted when you delete the participant. If you need to keep the invoices but delete the participant then choose ON DELETE SET NULL