I have got a problem with two tables:
CREATE TABLE IF NOT EXISTS `addresses` (
`adr_id` int(11) NOT NULL auto_increment,
`per_id` int(11) NOT NULL,
`adr_street` varchar(50) NOT NULL,
`adr_houseno` int(11) default NULL,
`adr_housenoadd` varchar(10) default NULL,
`adr_postalcode` varchar(25) NOT NULL,
`adr_city` varchar(20) NOT NULL,
`adr_type` varchar(45) default NULL,
`cnt_id` int(11) NOT NULL,
`adr_date` date default NULL,
`sys-mut-dt` timestamp NULL default NULL,
`sys-mut-user` varchar(20) default NULL,
`sys-mut-id` int(11) NOT NULL default '0',
PRIMARY KEY (`adr_id`),
KEY `per_id` (`per_id`),
KEY `cnt_id` (`cnt_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
--
-- RELATIES VOOR TABEL `addresses`:
-- `cnt_id`
-- `countries` -> `cnt_id`
-- `per_id`
-- `persons` -> `per_id`
--
CREATE TABLE `events` (
`evt_id` int(11) NOT NULL auto_increment,
`evt_name` varchar(50) NOT NULL,
`evt_description` varchar(100) default NULL,
`evt_startdate` date NOT NULL,
`evt_enddate` date default NULL,
`evt_starttime` time default NULL,
`evt_endtime` time default NULL,
`evt_amtpersons` int(11) default NULL,
`sts_id` int(11) NOT NULL,
`adr_id` int(11) default NULL,
`evt_amtPersonsSubs` tinyint(4) NOT NULL default '0',
`evt_photo` varchar(50) default NULL,
`sys-mut-dt` timestamp NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`sys-mut-user` varchar(20) default NULL,
`sys-mut-id` int(11) NOT NULL default '0',
PRIMARY KEY (`evt_id`),
KEY `sts_id` (`sts_id`),
KEY `adr_id` (`adr_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The problem is, when i try to delete a record from address table I get the following error
DELETE
FROM addresses
WHERE per_id = 45
1451 - Cannot delete or update a parent row: a foreign key constraint fails (`site/events`, CONSTRAINT `adr_id` FOREIGN KEY (`adr_id`) REFERENCES `addresses` (`adr_id`) ON DELETE NO ACTION ON UPDATE NO ACTION) ;
The weird thing is that there is no record with the same per_id or adr_id in the events table.
So what's going on over here?
How Can I solve this? ;-)
/* I think it means "How can I solve this?" */
It may sound stupid, but are you absolutely sure there are no child rows?
Could you please run this:
SELECT e.*
FROM addresses a
JOIN events e
ON e.adr_id = a.adr_id
WHERE a.per_id = 45
Related
The result of "SHOW CREATE TABLE order_products" are
CREATE TABLE `order_products` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`quantity` int(11) NOT NULL,
`price` double DEFAULT NULL,
`amount` double(8,2) NOT NULL,
`product_id` int(10) unsigned NOT NULL,
`tax_id` int(10) unsigned DEFAULT NULL,
`discount_id` int(10) unsigned DEFAULT NULL,
`order_id` int(10) unsigned DEFAULT NULL,
`username` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`shop_name` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `order_products_product_id_foreign` (`product_id`),
KEY `order_products_tax_id_foreign` (`tax_id`),
KEY `order_products_discount_id_foreign` (`discount_id`),
KEY `order_products_order_id_foreign` (`order_id`)
) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci
And i try to delete my key "order_products_product_id_foreign" by
ALTER TABLE order_products DROP FOREIGN KEY order_products_product_id_foreign
But it gives me error
#1091 - Can't DROP 'order_products_product_id_foreign'; check that column/key exists
You don't need to specify the FOREIGN KEY keyword here.
alter table order_products drop key order_products_product_id_foreign;
It's not a foreign key. so execute the line.
ALTER TABLE order_products DROP KEY order_products_product_id_foreign
bank_api_uat_user Table
CREATE TABLE IF NOT EXISTS `bank_api_uat_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bank_name` varchar(255) DEFAULT NULL,
`role` varchar(10) NOT NULL,
`bank_code` char(10) NOT NULL,
`user_name` varchar(255) NOT NULL,
`password` varchar(20) NOT NULL,
`api_key` varchar(255) NOT NULL,
`Client_Secret` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_name` (`user_name`),
KEY `user_name_2` (`user_name`),
KEY `id` (`id`,`user_name`),
KEY `role` (`role`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=36 ;
create_role Table
CREATE TABLE IF NOT EXISTS `create_role` (
`date` datetime NOT NULL,
`Role_name` varchar(50) NOT NULL,
`Role_code` varchar(5) NOT NULL,
PRIMARY KEY (`Role_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I want to assign referential integrity to bank_api_uat_user table each time i add constraint it gives below error
MySQL said: Documentation
1215 - Cannot add foreign key constraint
Below query used to create foreign key.
ALTER TABLE `bank_api_uat_user` ADD CONSTRAINT `const_file_role` FOREIGN KEY (`role`) REFERENCES `test`.`create_role`(`Role_code`) ON DELETE SET NULL ON UPDATE SET NULL;
Its important to make a child column Null for setting null reference option.
the above queries work if role is declared as NULL
CREATE TABLE IF NOT EXISTS `bank_api_uat_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bank_name` varchar(255) DEFAULT NULL,
`role` varchar(10) NULL,
`bank_code` char(10) NOT NULL,
`user_name` varchar(255) NOT NULL,
`password` varchar(20) NOT NULL,
`api_key` varchar(255) NOT NULL,
`Client_Secret` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_name` (`user_name`),
KEY `user_name_2` (`user_name`),
KEY `id` (`id`,`user_name`),
KEY `role` (`role`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=36 ;
Full explanation can be find here : https://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html
I'm working a project with Yiiframwork and I have this table in my data base project
CREATE TABLE IF NOT EXISTS `tbl_annonce` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`idEntreprise` tinyint(3) unsigned NOT NULL
COMMENT 'CONSTRAINT FOREIGN KEY (idEntreprise) REFERENCES tbl_entreprise(id)',
`titre` varchar(100) NOT NULL,
`detailleDiscription` varchar(5000) NOT NULL,
`categorie` varchar(100) DEFAULT NULL,
`typePoste` varchar(100) NOT NULL,
`salaireMin` varchar(80) NOT NULL,
`salaireMax` varchar(80) NOT NULL,
`niveauEtude` varchar(80) NOT NULL,
`niveauExperience` varchar(80) NOT NULL,
`langue` varchar(50) DEFAULT NULL,
`poste` varchar(50) NOT NULL,
`pays` varchar(50) NOT NULL,
`ville` varchar(50) NOT NULL,
`adresse` varchar(80) NOT NULL,
`datePublication` timestamp NOT NULL
DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`etat` varchar(50) NOT NULL,
`photo` varchar(255) NULL,
`nometr` text NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_idEntrepriseAnn`
FOREIGN KEY (idEntreprise)
REFERENCES tbl_entreprise(id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
I only use 'comment' because I found it in a tutorial for a yii framework project.
I have got the attribute 'idEntreprise' as FK in my DB but when I entred other 'idEntreprise' that does note exist in my table entreprise it was not problem, instead it must be a problem.
So I added 'identreprise' as Fk and that have this problem now.
Hope you understand my problem :/
can any one helep me plz !!
To define a foreign key on a column, it must fulfil some conditions.
Child and parent column must have same column definition by type,
signed.
Parent column must have an index defined on it.
Make sure that
The column tbl_entreprise.id is indexed.
The column definition idEntreprise tinyint(3) unsigned in tbl_annonce matches
with that of tbl_entreprise.id
Refer to: MySQL: Foregin Key Constaints
I keep on getting the below error message when I try to enter some important records I accentually deleted.
Duplicate entry 'EMIR2023 ' for key 'flightNo'
Is there a way in the phpMyAdmin environment I can disable the UNIQUE KEY then activate it when I am done with inserting the records?
Find below the structure of my table, I hope it helps
--
-- Table structure for table `flightSched`
--
CREATE TABLE IF NOT EXISTS `flightSched` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`timePeriod` time DEFAULT '00:00:00',
`depOrArriv` varchar(9) DEFAULT NULL,
`flightNo` varchar(9) NOT NULL,
`airline` varchar(20) DEFAULT NULL,
`dest` varchar(30) DEFAULT NULL,
`origin` varchar(30) DEFAULT NULL,
`depature` time DEFAULT '00:00:00',
`don` set('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday') DEFAULT NULL,
`arrivalTime` datetime DEFAULT '0000-00-00 00:00:00',
`arrivalTimeSec` varchar(28) DEFAULT NULL,
`status` varchar(15) NOT NULL,
`image_type` varchar(25) DEFAULT NULL,
`image` blob NOT NULL,
`image_size` varchar(25) DEFAULT NULL,
`image_name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `flightNo` (`flightNo`),
UNIQUE KEY `arrivalTime_2` (`arrivalTime`),
KEY `arrivalTime` (`arrivalTime`),
KEY `arrivalTime_3` (`arrivalTime`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=537 ;
Looking forward to your reply :-)
Try unique_checks
SET unique_checks=0;
... import operation ...
SET unique_checks=1;
check reference
i have these 3 tables
CREATE TABLE IF NOT EXISTS `enrollment` (
`STUDENT_NUM` varchar(10) NOT NULL,
`SUBJECT_NUM` varchar(10) NOT NULL,
`UNITS` int(10) NOT NULL,
`DAYS` varchar(50) NOT NULL,
`TIME_START` time NOT NULL,
`TIME_END` time NOT NULL,
`ROOM_ID` int(11) DEFAULT NULL,
`PRELIM` float(10,2) DEFAULT NULL,
`MIDTERM` float(10,2) DEFAULT NULL,
`FINALS` float(10,2) DEFAULT NULL,
`FINAL_GRADE` float(10,2) DEFAULT NULL,
`SEMESTER` varchar(50) NOT NULL,
`SCHOOL_YEAR` varchar(50) NOT NULL,
`DATE_ADDED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`STUDENT_NUM`,`SUBJECT_NUM`),
KEY `SUBJECT_NUM` (`SUBJECT_NUM`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `subjects` (
`SUBJECT_NUM` varchar(10) NOT NULL,
`EMPLOYEE_NUM` varchar(10) NOT NULL,
`SUBJECT_TITLE` varchar(100) DEFAULT NULL,
`DEPARTMENT` varchar(100) DEFAULT NULL,
`UNITS` int(10) NOT NULL,
`DAYS` varchar(50) NOT NULL,
`TIME_START` time NOT NULL,
`TIME_END` time NOT NULL,
`room_id` int(11) DEFAULT NULL,
`SEMESTER` varchar(50) NOT NULL,
`SCHOOL_YEAR` varchar(50) NOT NULL,
`COUNT` int(10) DEFAULT NULL,
`STATUS` varchar(50) DEFAULT NULL,
`FLAG` varchar(50) NOT NULL,
`DATE_ADDED` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`SUBJECT_NUM`),
UNIQUE KEY `SUBJECT_NUM` (`SUBJECT_NUM`),
KEY `EMPLOYEE_NUM` (`EMPLOYEE_NUM`),
KEY `EMPLOYEE_NUM_2` (`EMPLOYEE_NUM`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `room` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`room` varchar(255) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `room` (`room`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
what i am trying to do is make the field ROOM_ID from enrollment and subjects foreign key and the reference be the ID from room.. the ROOM_ID must not be unique...
i am getting this error
#1452 - Cannot add or update a child row: a foreign key constraint fails (`enrollmentdb`.`#sql-277_164`, CONSTRAINT `#sql-277_164_ibfk_3` FOREIGN KEY (`ROOM_ID`) REFERENCES `room` (`ID`))
when i am using this SQL command:
ALTER TABLE enrollment
ADD FOREIGN KEY (room_id)
REFERENCES room(ID)
Try below code if its working :
ALTER TABLE enrollment
ADD CONSTRAINT ROOM_ID_fk
FOREIGN KEY(ROOM_ID)
REFERENCES room(ID);
Thanks!
room_id is capitalised in your table. Try:
ALTER TABLE enrollment
ADD FOREIGN KEY (ROOM_ID)
REFERENCES room(ID)
replace 'room_id' with 'ROOM_ID'