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);
I have three tables :- category ,employee and user_access_permission . I want to create a trigger that will fire when new category is added , insert values in user_access_permission for each employee id .default value for user_access is 0.
Category table :-
CREATE TABLE IF NOT EXISTS `categories` (
`category_id` int(11) NOT NULL AUTO_INCREMENT,
`category_name` varchar(100) NOT NULL,
`section_id` int(11) NOT NULL,
UNIQUE KEY `category_id` (`category_id`),
UNIQUE KEY `category_name` (`category_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Employee table :-
CREATE TABLE IF NOT EXISTS `employee` (
`employee_id` int(11) NOT NULL AUTO_INCREMENT,
`emp_pass` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`employee_id`),
UNIQUE KEY `email_UNIQUE` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' ' AUTO_INCREMENT=1;
User Access Permission Table :-
CREATE TABLE IF NOT EXISTS `user_access_permission` (
`uap_id` int(10) NOT NULL AUTO_INCREMENT,
`section_id` int(10) NOT NULL,
`category_id` int(10) NOT NULL,
`employee_id` int(10) NOT NULL,
`access_level` int(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`uap_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
I am writing trigger like this :-
CREATE TRIGGER update_permssion_on_add_category
AFTER INSERT
ON categories FOR EACH ROW
insert into user_access_permission(section_id,category_id,employee_id,access_level) select(new.section_id,new.category_id,employee_id,0) from employee
But I don't get things right. Please Help
You have
select(new.section_id,new.category_id,employee_id,0) from employee
but section_id and category_id come from the category table.
I would suggest changing it to be 'from category' but the problem then is where do you get the employee_id number from? It's not anywhere in the original insert statement for the category table. Unless you decide to have it be some default value every time the trigger is fired.
I have two tables:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`username` varchar(45) NOT NULL,
`password` varchar(200) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `users_info` (
`id` int(8) NOT NULL,
`name` varchar(35) NOT NULL,
`address` varchar(35) NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I want that when an user is inserted in table users, to automatically insert a new row in table users_info that would contain the same id in the id column, as the users table.
When I do my inserts, I do them like this: INSERT INTO users (username,password) VALUES ('user1','pass1');
I believe that I should look at MySQL triggers. I did take a look but I don't know how to retrieve the key id from the users table for every last row that is inserted, supposing that other rows could be inserted in parallel in other threads.
If you create AFTER INSERT trigger, you can use NEW object:
CREATE
TRIGGER `aftInsert_users` AFTER INSERT ON `users`
FOR EACH ROW BEGIN
INSERT INTO users_info (itemID) VALUES (NEW.id);
END;
A trigger on after insert should work if you define default values for the columns defined as not null:
CREATE TRIGGER insert_users_info
AFTER INSERT ON `users`
FOR EACH ROW INSERT INTO `users_info` (id) VALUES (NEW.id);
See the MySQL documentation on triggers for more information.
I have two MySQL INNODB tables 'student' and 'instructor'. They are analogous in almost every single way. I can add arbitrary records into the 'student' table, but not the 'instructor' table. Both tables have the same structure with ID as the primary key.
(ID varchar, name varchar, dept_name, varchar, field4 int)
I have tried queries such as
INSERT INTO instructor VALUES ('12345', 'name', 'computer science', 50000);
INSERT INTO instructor SET ID = 67890, name = 'Polson', dept_name = 'Bioinformatics', salary = 100 ON DUPLICATE KEY SET name = 'Polson', dept_name = 'Bioinformatics', salary = 100;
INSERT INTO instructor (ID) VALUES ('12345');
INSERT INTO instructor (ID) VALUE ('12345');
while all of these queries work with the 'student' table. What is going on?
Here are my create table statements. My user has full permissions. Is there any status query that I should use to look for issues? Thank you!
CREATE TABLE `instructor` (
`ID` varchar(5) NOT NULL DEFAULT '',
`name` varchar(20) DEFAULT NULL,
`dept_name` varchar(25) DEFAULT NULL,
`salary` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `dept_name` (`dept_name`),
CONSTRAINT `instructor_ibfk_1` FOREIGN KEY (`dept_name`) REFERENCES `department` (`dept_name`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `student` (
`ID` varchar(10) NOT NULL DEFAULT '',
`name` varchar(20) DEFAULT NULL,
`dept_name` varchar(25) DEFAULT NULL,
`total_credits` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `dept_name` (`dept_name`),
CONSTRAINT `student_ibfk_1` FOREIGN KEY (`dept_name`) REFERENCES `department` (`dept_name`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE department (
dept_name varchar(25) NOT NULL DEFAULT '',
building varchar(15) DEFAULT NULL,
budget float(12,2) DEFAULT NULL,
PRIMARY KEY (dept_name)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
which contains a record Bioinformatics | DBI | 1000000.00