Foreign Key Failure #1452 MariaDB PhpMyAdmin Mysql - mysql

got a slight problem with my constraint I wanna add to my tables:
so first at all my tables:
DROP TABLE IF EXISTS `Sales`;
CREATE TABLE IF NOT EXISTS `Sales` (
`ItemNo` int(11) NOT NULL,
`Model` varchar(100) NOT NULL unique,
`PurchasingPrice` decimal(11,2) NOT NULL,
`ManufNo` int(11) Not Null,
`LocNo` int(11) NOT NULL,
`SuppNo` int(11) NOT NULL,
`CatNo`int(11) NOT NULL,
`UnitPrice`decimal (11,2),
PRIMARY KEY (`ItemNo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `Sales` (`ItemNo`, `Model`, `PurchasingPrice`,`ManufNo`, `LocNo`,`SuppNo`,`CatNo`,`Unitprice`) VALUES
(1,'Tieflader 18t',6969.84,4,1,6,2,9582.56),
(2,'Betonmischer-3m³',47829.00,3,2,3,3,82457.23),
(3,'Lastenkran 800kg',4129.00,2,2,2,3,8466.43),
(4,'Hubwagen,10m',9478.00,1,2,3,3,18457.84);
DROP TABLE IF EXISTS `Rent`;
CREATE TABLE IF NOT EXISTS `Rent` (
`ItemNo` int(11) NOT NULL, ( Different Number-Area like 1000+, very small project)
`Model` varchar(100) NOT NULL unique,
`PurchasingPrice` decimal(11,2) NOT NULL,
`ManufNo` int(11) Not Null,
`LocNo` int(11) NOT NULL,
`SuppNo` int(11) NOT NULL,
`CatNo`int(11) NOT NULL,
`PricePerDay`decimal (11,2),
`RentDateNo`int(11),
`AdressNo` int(11),
PRIMARY KEY (`ItemNo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `RENT` (`ItemNo`, `Model`, `PurchasingPrice`,`ManufNo`, `LocNo`,`SuppNo`,`CatNo`,`PricePerDay`,`RentDateNo`,`AdressNo`) VALUES
(1000,'Betonmischer 50l',123.45,4,1,6,2,42.56,2,1),
(1001,'Winkelschleifer 800 Watt³',29.00,3,2,3,3,17.23,3,1),
(1002,'Akkuschrauber 12V',129.00,2,2,2,3,16.43,1,2),
(1003,'Akkuschrauber 18V',178.00,1,2,3,3,21.84,1,2);
DROP TABLE IF EXISTS `Stock`;
CREATE TABLE IF NOT EXISTS `Stock` (
`ItemNo` int(11) NOT NULL,
`Min-Stock` int(11) Not Null,
`Current-Stock` int(11) Not Null,
`Max-Stock` int(11) Not Null,
PRIMARY KEY (`ItemNo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `Stock` (`ItemNo`,`Min-Stock`,`Current-Stock`,`Max-Stock`) VALUES
(1000,0,1,1),
(1001,0,1,1),
(1002,0,0,1),
(1003,0,1,1),
(1,2,5,8),
(2,1,2,2),
(3,3,6,9),
(4,1,3,4);
When I try to add an constraint FK with:
ALTER TABLE `Stock`
ADD CONSTRAINT `sales_stock_fk` FOREIGN KEY (`ItemNo`) REFERENCES `sales`(`ItemNo`) ON DELETE RESTRICT ON UPDATE RESTRICT,
ADD CONSTRAINT `rent_stock_fk` FOREIGN KEY (`ItemNo`) REFERENCES `rent`(`ItemNo`) ON DELETE RESTRICT ON UPDATE RESTRICT;
it does Fail. When I delete all Numbers which aren't from the sales Table I can use this one:
ALTER TABLE `Stock`
ADD CONSTRAINT `sales_stock_fk` FOREIGN KEY (`ItemNo`) REFERENCES `sales`(`ItemNo`) ON DELETE RESTRICT ON UPDATE RESTRICT;
and can also add new items in the sales table as wanted and the new in stock. But I can't add Stocks for rent items and also the combined Add Constraint with both tables at once don't work. I am not sure how to get it work that I can add my Items to the table. I always get the #1452 child table failure.
Maybe someone can help me an know how to handle it.
The result should be: I got to the table stock and hover over an ItemNo and when I click it I should be forwarded to table sales if ItemNo is from sales or rent if from rent. I am using Xampp phpmyadmin, MariaDB

So the error is Cannot add or update a child row: a foreign key constraint fails you should change values the Sales table on ItemNo change it before 1 to 1000 and more
your value is
INSERT INTO `Sales` (`ItemNo`, `Model`, `PurchasingPrice`,`ManufNo`, `LocNo`,`SuppNo`,`CatNo`,`Unitprice`) VALUES
(1,'Tieflader 18t',6969.84,4,1,6,2,9582.56),
(2,'Betonmischer-3m³',47829.00,3,2,3,3,82457.23),
(3,'Lastenkran 800kg',4129.00,2,2,2,3,8466.43),
(4,'Hubwagen,10m',9478.00,1,2,3,3,18457.84);
Change it to :
INSERT INTO `Sales` (`ItemNo`, `Model`, `PurchasingPrice`,`ManufNo`, `LocNo`,`SuppNo`,`CatNo`,`Unitprice`) VALUES
(1000,'Tieflader 18t',6969.84,4,1,6,2,9582.56),
(1001,'Betonmischer-3m³',47829.00,3,2,3,3,82457.23),
(1002,'Lastenkran 800kg',4129.00,2,2,2,3,8466.43),
(1003,'Hubwagen,10m',9478.00,1,2,3,3,18457.84);

Related

mysql 8 (innodb) foreign key constraints on newly created indexes

Suppose I have a table items
with columns id (PRIMARY), name(VARCHAR), section_id (BIGINT), updated_at (DATETIME),
and a table sections with id (PRIMARY).
Naturally, items.section_id is a foreign key that refers to sections.id.
Suppose there is an index on items of the columns (section_id, name). I believe that if you tried to drop this index, you would get an error that it is needed in a foreign key constraint. I can accept this.
Now, I want to create a new index, like create index ix_section_id_id_updated_at on items (section_id, id, updated_at). MySQL lets me do this, but if I go to drop this table, I get that same error: it fails, because it is needed in a foreign key constraint.
Why should this be? It already has one index that can be used for this foreign key check. Further, the error does NOT go away with set FOREIGN_KEY_CHECKS=0;. Is there a way to force MySQL to not associate the new index with the foreign key, so that it is quick to drop? This is necessary because I will be running the migration on a production server with temporary downtime, and need to be able to quickly revert the migration in case of anything going wrong afterwards.
I can reproduce your issue if I don't create an index on section_id and allow mysql to do so on the creation of a foreign key(as described in the manual). Adding a new index drops the auto generated key and if you then drop the new index an error is generated because of the requirement to have a key , and mysql does not auto generate one on a drop.. . If you manually generate a key on section_id this problem does not happen..and the newly created compound index drops successfully.
drop table if exists items;
drop table if exists sections;
create table items(id int PRIMARY key, name varchar(3), section_id BIGINT, updated_at DATETIME);
create table sections(id bigint primary key);
alter table items
add foreign key fk1(section_id) references sections(id);
show create table items;
CREATE TABLE `items` ( `id` int(11) NOT NULL,
`name` varchar(3) DEFAULT NULL,
`section_id` bigint(20) DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk1` (`section_id`),
CONSTRAINT `fk1` FOREIGN KEY (`section_id`) REFERENCES `sections` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;
alter table items
add key key1(section_id, name);
show create table items;
CREATE TABLE `items` (
`id` int(11) NOT NULL,
`name` varchar(3) DEFAULT NULL,
`section_id` bigint(20) DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `key1` (`section_id`,`name`),
CONSTRAINT `fk1` FOREIGN KEY (`section_id`) REFERENCES `sections` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
and with manually generated key
drop table if exists items;
drop table if exists sections;
create table items(id int PRIMARY key, name varchar(3), section_id BIGINT, updated_at DATETIME);
create table sections(id bigint primary key);
alter table items
add key sid(section_id);
alter table items
add foreign key fk1(section_id) references sections(id);
show create table items;
CREATE TABLE `items` (
`id` int(11) NOT NULL,
`name` varchar(3) DEFAULT NULL,
`section_id` bigint(20) DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `sid` (`section_id`),
CONSTRAINT `fk1` FOREIGN KEY (`section_id`) REFERENCES `sections` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
alter table items
add key key1(section_id, name);
show create table items;
CREATE TABLE `items` (
`id` int(11) NOT NULL,
`name` varchar(3) DEFAULT NULL,
`section_id` bigint(20) DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `sid` (`section_id`),
KEY `key1` (`section_id`,`name`),
CONSTRAINT `fk1` FOREIGN KEY (`section_id`) REFERENCES `sections` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Why I am having a this error on phpmyadmin: Error creating foreign key on revision (check data types)?

I´m trying to create Foreign Keys in phpmyadmin, but I get this error:
Error creating foreign key on revision (check data types)
I don´t understand it because the data types are equal. So, what I want is to create a Foreign Key from 'acoustictreatment' to 'filterspecifications' which contains tag, offerid and revision. But I get the error that I mentioned.
This are my tables:
CREATE TABLE `offer` (
`projectid` varchar(20) NOT NULL,
`customer` varchar(255) NOT NULL,
`creator` varchar(255) NOT NULL,
`date` date NOT NULL,
`revision` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `offer`
ADD PRIMARY KEY (`projectid`,`revision`);
CREATE TABLE `filterspecifications` (
`tag` varchar(100) NOT NULL,
`gasFlow` double NOT NULL,
`dustToHandle` double NOT NULL,
`offerid` varchar(20) NOT NULL,
`selectedFilter` varchar(20) NOT NULL,
`revision` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `filterspecifications`
ADD PRIMARY KEY (`tag`,`offerid`,`revision`),
ADD KEY `offerid` (`offerid`,`revision`);
ALTER TABLE `filterspecifications`
ADD CONSTRAINT `filterspecifications_ibfk_1`
FOREIGN KEY (`offerid`,`revision`) REFERENCES `offer` (`projectid`, `revision`) ON DELETE CASCADE ON UPDATE CASCADE;
CREATE TABLE `acoustictreatment` (
`tag` varchar(100) NOT NULL,
`offerid` varchar(20) NOT NULL,
`outputFanSilencer` tinyint(1) NOT NULL,
`fanAcousticInsulation` tinyint(1) NOT NULL,
`revision` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `acoustictreatment`
ADD PRIMARY KEY (`tag`,`offerid`,`revision`);
The solution that I found is to delete the 'acoustictreatment' table and create it again with the foreign key from the beginning. Because what I was trying to do was to create all the tables and then create the foreign keys, but that didn´t work for me

Cannot add or update a child row: a foreign key constraint fails phpmyadmin

This is my db
CREATE DATABASE IF NOT EXISTS `project` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `project`;
checks Table
CREATE TABLE IF NOT EXISTS `checks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`issue` varchar(10) NOT NULL,
`content` varchar(100) NOT NULL,
`contact` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `content` (`content`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
Issue table
CREATE TABLE IF NOT EXISTS `issues` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`createtime` date NOT NULL,
`title` varchar(20) NOT NULL,
`text` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `text` (`text`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
The code i try to insert
ALTER TABLE `checks` ADD CONSTRAINT `context` FOREIGN KEY ( `content` ) REFERENCES `project`.`issues` (
`text`
) ON DELETE RESTRICT ON UPDATE CASCADE ;
Error message:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`project`.`#sql-1d5c_130`, CONSTRAINT `context` FOREIGN KEY (`content`) REFERENCES `issues` (`text`) ON UPDATE CASCADE)
Anyone can help ??? I've seen the other posts on this topic, but no luck. Am I overseeing something or any idea what to do?
You need to first create an index on `issues`.`text` column.
Make sure there is no data in both tables. If there is some data, then make sure for every entry in `checks`.`content` the same entry exists in `issues`.`text`.

Only allow one unique foreign key for primary

I have 3 tables in mysql, One is a Company table, the other is a license table and the last is a joining table between both primary keys, When a person adds a company id to the license id in the joining table, it allows multiple companies to exist for one license, this cannot happen, so I need to do something that will only allow one company id for one license id
heres the tables
Table license
CREATE TABLE `License` (
`license_id` int(11) NOT NULL AUTO_INCREMENT,
`license_number` varchar(45) NOT NULL,
`start_date` date NOT NULL,
`end_date` date NOT NULL,
`duration` int(11) NOT NULL,
`expiry_date` date NOT NULL,
`product_id` int(11) DEFAULT NULL,
PRIMARY KEY (`license_id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=latin1;
;
Company Table
CREATE TABLE `Company` (
`company_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`physical_address` varchar(255) DEFAULT NULL,
`postal_address` varchar(255) DEFAULT NULL,
`reseller_id` int(11) DEFAULT NULL,
PRIMARY KEY (`company_id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1;
and Joining table
CREATE TABLE `CompanyLicense` (
`license_id` int(11) NOT NULL,
`company_id` int(11) NOT NULL,
PRIMARY KEY (`license_id`,`company_id`),
KEY `companlicence_company_fk_idx` (`company_id`),
CONSTRAINT `companylicense_company_fk` FOREIGN KEY (`company_id`) REFERENCES `Company` (`company_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `companylicense_license_fk` FOREIGN KEY (`license_id`) REFERENCES `License` (`license_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
So far i have this
INSERT INTO CompanyLicense (license_id, company_id) VALUES
('2','6') on duplicate key update license_id = '2';
doesnt seem to do the job
You need to make company unique in companylicense:
ALTER TABLE companylicense ADD UNIQUE KEY (company)
or better yet, make company a field in license instead of having a link table.

mysql prevent deleting records that is in used

i got 2 tables. product and order_items which contain all the products that were bought.
so how do i create a relationship in mysql whereby if a product exists in order_items, restrict users from deleting it from product table??
thanks
You can do this with Foreign keys with the InnoDB Engine.
ALTER TABLE order_items ADD FOREIGN KEY (`p_id`) REFERENCES `products` (`p_id`);
The ID on products must be a key (it probably already is the primary key).
If you are not using InnoDB, you cannot enforce this with MySQL, but it must be enforced with your application (check whether a record exists in orders first for example).
So with your tables, you run:
ALTER TABLE `order_item` ADD FOREIGN KEY (`bookid`) REFERENCES `book` (`id`);
You're looking for a foreign key. Specifically look at the "Restrict" option.
http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
my table structure:
CREATE TABLE `book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`title` varchar(100) NOT NULL,
`description` text NOT NULL,
`author` varchar(100) NOT NULL,
`publisher` varchar(100) NOT NULL,
`edition` int(11) NOT NULL,
`isbn` varchar(13) NOT NULL,
`category` varchar(11) NOT NULL,
`datesubmitted` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8;
CREATE TABLE `order_item` (
`orderid` int(11) NOT NULL,
`bookid` int(11) NOT NULL,
KEY `Foreign` (`bookid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;