Related
I am still a beginner in mySQL. I have been trying to figure out a trigger for my project but can't come up with a solution. Here is what I am trying to do:
When an order is made I want a trigger to update the inventory. The bump here is that orders can have many products and those products are composed of different elements of the inventory. Here is how I tried it maybe it can help you understand the idea.
create trigger subtract_inv
after insert on order_details for each row
begin
update inventory
set Inv_qty = Inv_qty - (order_details.Order_details_qty * recipes.Requiered_qty)
where Products_Prod_ID = NEW.Products_Prod_ID and inventory.Inv_ID = recipes.Inventory_Inv_ID;
end$$
delimiter ;
Here are the 3 tables
-- -----------------------------------------------------
-- Table `McDo`.`Order_details`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `McDo`.`Order_details` (
`orders_Order_id` INT NOT NULL,
`Products_Prod_ID` INT NOT NULL,
`Order_details_qty` INT NULL,
PRIMARY KEY (`orders_Order_id`, `Products_Prod_ID`),
INDEX `fk_orders_has_Products_Products1_idx` (`Products_Prod_ID`
ASC) VISIBLE,
INDEX `fk_Order_details_orders1_idx` (`orders_Order_id` ASC)
VISIBLE,
CONSTRAINT `fk_orders_has_Products_Products1`
FOREIGN KEY (`Products_Prod_ID`)
REFERENCES `McDo`.`Products` (`Prod_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Order_details_orders1`
FOREIGN KEY (`orders_Order_id`)
REFERENCES `McDo`.`Orders` (`Order_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `McDo`.`Inventory`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `McDo`.`Inventory` (
`Inv_ID` INT NOT NULL,
`Inv_type` VARCHAR(1) NOT NULL,
`Inv_name` VARCHAR(45) NULL,
`Inv_qty` INT NULL,
`Inv_price_unit` DECIMAL(20,2) NULL,
PRIMARY KEY (`Inv_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `McDo`.`Recipes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `McDo`.`Recipes` (
`Products_Prod_ID` INT NOT NULL,
`Inventory_Inv_ID` INT NOT NULL,
`Requiered_qty` INT NULL,
PRIMARY KEY (`Products_Prod_ID`, `Inventory_Inv_ID`),
INDEX `fk_Products_has_Inventory_Products1_idx` (`Products_Prod_ID` ASC)
VISIBLE,
INDEX `fk_Products_has_Inventory_Inventory1_idx` (`Inventory_Inv_ID`
ASC) VISIBLE,
CONSTRAINT `fk_Products_has_Inventory_Products1`
FOREIGN KEY (`Products_Prod_ID`)
REFERENCES `McDo`.`Products` (`Prod_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Products_has_Inventory_Inventory1`
FOREIGN KEY (`Inventory_Inv_ID`)
REFERENCES `McDo`.`Inventory` (`Inv_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
and here a sample of the data
-- -----------------------------------------------------
-- Data for table `McDo`.`Recipes`
-- -----------------------------------------------------
START TRANSACTION;
USE `McDo`;
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 102, 3);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 103, 2);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 112, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 110, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 111, 2);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 114, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 113, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 108, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 101, 2);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 111, 2);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 103, 2);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 107, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 113, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 114, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 108, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 109, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (301, 204, 1);
INSERT INTO `McDo`.`Recipes` (`Products_Prod_ID`, `Inventory_Inv_ID`, `Requiered_qty`) VALUES (302, 202, 1);
-- -----------------------------------------------------
-- Data for table `McDo`.`Inventory`
-- -----------------------------------------------------
START TRANSACTION;
USE `McDo`;
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (101, 'C', 'regular bun', 1000, 0.07);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (102, 'C', 'sesame bun', 1000, 0.12);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (103, 'C', 'beef patty', 1000, 0.35);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (107, 'C', 'ketchup sachet', 5000, 0.02);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (108, 'C', 'seasoning sachet', 5000, 0.01);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (109, 'C', 'mustard sachet', 5000, 0.01);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (110, 'C', 'big mac sauce sachet', 5000, 0.04);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (111, 'C', 'cheese slice', 2000, 0.05);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (112, 'C', 'precut letuce', 1500, 0.02);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (113, 'C', 'precut onion', 1500, 0.01);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (114, 'C', 'precut pickle', 1500, 0.01);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (202, 'N', 'mc double wrapping', 1000, 0.01);
INSERT INTO `McDo`.`Inventory` (`Inv_ID`, `Inv_type`, `Inv_name`, `Inv_qty`, `Inv_price_unit`) VALUES (204, 'N', 'big mac box', 1000, 0.02);
and orders can look like:
1000,301,2;
1000,302,1;
1001,301,2;
1002,302,3;
You are allowed to use joins in an update query so perhaps this
delimiter $$
CREATE TRIGGER `subtract_inv` AFTER INSERT ON `order_details` FOR EACH ROW
begin
update inventory join
(select r.inventory_inv_id rinvid,new.order_details_qty * r.Requiered_qty adjustment
from recipes r join inventory i on i.inv_id = r.inventory_inv_id
where r.products_prod_id = new.products_prod_id) s
set inv_qty = inv_qty - s.adjustment
where inventory.Inv_ID = s.rinvid;
end $$
delimiter ;
I'm really frusterated with these codes and I can't find where the errors are..
SSH secure shell .txt file with the error codes:
Table created.
SQL>
SQL> CREATE TABLE Order_mys (
2 OrderID NUMBER(3) NOT NULL,
3 OrderDate DATE NOT NULL,
4 CustID CHAR(5) NOT NULL,
5 PRIMARY KEY (OrderID),
6 FOREIGN KEY (CustID) REFERENCES Customer_mys
7 ) ;
CREATE TABLE Order_mys (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
SQL> CREATE TABLE Product_mys (
2 ProductID NUMBER(3) NOT NULL,
3 ProductName VARCHAR(30) NOT NULL,
4 CatID Number(3) NOT NULL,
5 PRIMARY KEY (ProductID),
6 FOREIGN KEY (CatID) REFERENCES Category_mys
7 ) ;
CREATE TABLE Product_mys (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
SQL> CREATE TABLE OrderDetail_mys (
2 OrderID NUMBER(3) NOT NULL,
3 ProductID NUMBER(3) NOT NULL,
4 ProductQty NUMBER(4) NOT NULL,
5 ProdcutPrice NUMBER(6,2) NOT NULL,
6 PRIMARY KEY (OrderID, ProductID),
7 FOREIGN KEY (OrderID) REFERENCES Order_mys,
8 FOREIGN KEY (ProductID) REFERENCES Product_mys
9 ) ;
CREATE TABLE OrderDetail_mys (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
SQL>
SQL> DESCRIBE Dept_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
DEPTID NOT NULL NUMBER(3)
DEPTNAME NOT NULL VARCHAR2(20)
SQL> DESCRIBE Commission_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
COMMCLASS NOT NULL CHAR(1)
COMMRATE NOT NULL NUMBER(2,2)
SQL> DESCRIBE Category_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
CATID NOT NULL NUMBER(3)
CATNAME NOT NULL VARCHAR2(20)
SQL> DESCRIBE SalesRep_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
SALESREPID NOT NULL NUMBER(4)
SALESREPFNAME NOT NULL VARCHAR2(20)
SALESREPLNAME NOT NULL VARCHAR2(20)
DEPTID NOT NULL NUMBER(3)
COMMCLASS NOT NULL CHAR(1)
SQL> DESCRIBE Customer_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
CUSTID NOT NULL VARCHAR2(5)
CUSTFNAME NOT NULL VARCHAR2(20)
CUSTLNAME NOT NULL VARCHAR2(20)
CUSTPHONE CHAR(10)
SALESREPID NOT NULL NUMBER(4)
SQL> DESCRIBE Order_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
ORDERID NOT NULL NUMBER(3)
ORDERDATE NOT NULL DATE
CUSTID NOT NULL CHAR(5)
SQL> DESCRIBE Product_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
PRODUCTID NOT NULL NUMBER(3)
PRODUCTNAME NOT NULL VARCHAR2(30)
CATID NOT NULL NUMBER(3)
SQL> DESCRIBE OrderDetail_mys ;
Name Null? Type
----------------------------------------- -------- ----------------------------
ORDERID NOT NULL NUMBER(3)
PRODUCTID NOT NULL NUMBER(3)
PRODUCTQTY NOT NULL NUMBER(4)
PRODCUTPRICE NOT NULL NUMBER(6,2)
SQL>
SQL> --Part II
SQL>
SQL> INSERT INTO Dept_mys
2 VALUES (10, 'Store Sales') ;
1 row created.
SQL>
SQL> INSERT INTO Dept_mys
2 VALUES (14, 'Corp Sales') ;
1 row created.
SQL>
SQL> INSERT INTO Dept_mys
2 VALUES (16, 'Web Sales') ;
1 row created.
SQL>
SQL>
SQL>
SQL> INSERT INTO Commission_mys
2 VALUES (A, .1) ;
VALUES (A, .1)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL> INSERT INTO Commission_mys
2 VALUES (B, .08) ;
VALUES (B, .08)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL> INSERT INTO Commission_mys
2 VALUES (Z, 0) ;
VALUES (Z, 0)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL> INSERT INTO Commission_mys
2 VALUES (C, .05) ;
VALUES (C, .05)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL>
SQL> INSERT INTO Category_mys
2 VALUES (1, 'Hand Tools') ;
1 row created.
SQL>
SQL> INSERT INTO Category_mys
2 VALUES (2, 'power Tools') ;
1 row created.
SQL>
SQL> INSERT INTO Category_mys
2 VALUES (4, 'Fasteners') ;
1 row created.
SQL>
SQL> INSERT INTO Category_mys
2 VALUES (6, 'Misc') ;
1 row created.
SQL>
SQL> INSERT INTO Category_mys
2 VALUES (3, 'Measuring Tools') ;
1 row created.
SQL>
SQL> INSERT INTO Category_mys
2 VALUES (5, 'Hardware') ;
1 row created.
SQL>
SQL>
SQL>
SQL> INSERT INTO SalesRep_mys
2 VALUES (10, 'Alice', 'Jones', 10, 'A') ;
INSERT INTO SalesRep_mys
*
ERROR at line 1:
ORA-02291: integrity constraint (MYS2306.SYS_C00966867) violated - parent key
not found
SQL>
SQL> INSERT INTO SalesRep_mys
2 VALUES (12, 'Greg', 'Taylor', 14, 'B') ;
INSERT INTO SalesRep_mys
*
ERROR at line 1:
ORA-02291: integrity constraint (MYS2306.SYS_C00966867) violated - parent key
not found
SQL>
SQL> INSERT INTO SalesRep_mys
2 VALUES (14, 'Sara', 'Day', 10, 'Z') ;
INSERT INTO SalesRep_mys
*
ERROR at line 1:
ORA-02291: integrity constraint (MYS2306.SYS_C00966867) violated - parent key
not found
SQL>
SQL> INSERT INTO SalesRep_mys
2 VALUES (8, 'Kay', 'Price', 14, 'C') ;
INSERT INTO SalesRep_mys
*
ERROR at line 1:
ORA-02291: integrity constraint (MYS2306.SYS_C00966867) violated - parent key
not found
SQL>
SQL> INSERT INTO SalesRep_mys
2 VALUES (20, 'Bob', 'Jackson', 10, 'B') ;
INSERT INTO SalesRep_mys
*
ERROR at line 1:
ORA-02291: integrity constraint (MYS2306.SYS_C00966867) violated - parent key
not found
SQL>
SQL> INSERT INTO SalesRep_mys
2 VALUES (22, 'Micah', 'Moore', 16, 'Z') ;
INSERT INTO SalesRep_mys
*
ERROR at line 1:
ORA-02291: integrity constraint (MYS2306.SYS_C00966867) violated - parent key
not found
SQL>
SQL>
SQL>
SQL> INSERT INTO Customer_mys
2 VALUES (S100, 'John', 'Smith', '5551212', 10) ;
VALUES (S100, 'John', 'Smith', '5551212', 10)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL> INSERT INTO Customer_mys
2 VALUES (A120, 'Jane', 'Adams', '817555', 14) ;
VALUES (A120, 'Jane', 'Adams', '817555', 14)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL> INSERT INTO Customer_mys
2 VALUES (J090, 'Tim', 'Jones', NULL, 10) ;
VALUES (J090, 'Tim', 'Jones', NULL, 10)
*
ERROR at line 2:
ORA-00984: column not allowed here
SQL>
SQL> INSERT INTO Customer_mys
2 VALUES (B200, 'Ann', 'Brown', '972555', 14) ;
VALUES (B200, 'Ann', 'Brown', '972555', 14)
*
ERROR at line 2:
ORA-00984: column not allowed here
Here's the tables that I did with the relationships, foreign key, inserts
DROP TABLE Customer_mys ;
DROP TABLE SalesRep_mys ;
DROP TABLE Category_mys ;
DROP TABLE Commission_mys ;
DROP TABLE Dept_mys ;
--part I
CREATE TABLE Dept_mys (
DeptID Number(3) NOT NULL,
DeptName VARCHAR(20) NOT NULL,
PRIMARY KEY (DeptID)
) ;
CREATE TABLE Commission_mys (
CommClass CHAR(1) NOT NULL,
CommRate Number(2,2) NOT NULL,
PRIMARY KEY (CommClass)
) ;
CREATE TABLE Category_mys (
CatID Number(3) NOT NULL,
catName VARCHAR(20) NOT NULL,
PRIMARY KEY (CatID)
) ;
CREATE TABLE SalesRep_mys (
SalesRepID NUMBER(4) NOT NULL,
SalesRepFName VARCHAR(20) NOT NULL,
SalesRepLName VARCHAR(20) NOT NULL,
DeptID NUMBER(3) NOT NULL,
CommClass CHAR(1) NOT NULL,
PRIMARY KEY (SalesRepID),
FOREIGN KEY (DeptID) REFERENCES Dept_mys,
FOREIGN KEY (CommClass) REFERENCES Commission_mys
) ;
CREATE TABLE Customer_mys (
CustID VARCHAR(5) NOT NULL,
CustFName VARCHAR(20) NOT NULL,
CustLName VARCHAR(20) NOT NULL,
CustPhone CHAR(10),
SalesRepID NUMBER(4) NOT NULL,
PRIMARY KEY (CustID),
FOREIGN KEY (SalesRepID) REFERENCES Customer_mys
) ;
CREATE TABLE Order_mys (
OrderID NUMBER(3) NOT NULL,
OrderDate DATE NOT NULL,
CustID CHAR(5) NOT NULL,
PRIMARY KEY (OrderID),
FOREIGN KEY (CustID) REFERENCES Customer_mys
) ;
CREATE TABLE Product_mys (
ProductID NUMBER(3) NOT NULL,
ProductName VARCHAR(30) NOT NULL,
CatID Number(3) NOT NULL,
PRIMARY KEY (ProductID),
FOREIGN KEY (CatID) REFERENCES Category_mys
) ;
CREATE TABLE OrderDetail_mys (
OrderID NUMBER(3) NOT NULL,
ProductID NUMBER(3) NOT NULL,
ProductQty NUMBER(4) NOT NULL,
ProdcutPrice NUMBER(6,2) NOT NULL,
PRIMARY KEY (OrderID, ProductID),
FOREIGN KEY (OrderID) REFERENCES Order_mys,
FOREIGN KEY (ProductID) REFERENCES Product_mys
) ;
DESCRIBE Dept_mys ;
DESCRIBE Commission_mys ;
DESCRIBE Category_mys ;
DESCRIBE SalesRep_mys ;
DESCRIBE Customer_mys ;
DESCRIBE Order_mys ;
DESCRIBE Product_mys ;
DESCRIBE OrderDetail_mys ;
--Part II
INSERT INTO Dept_mys
VALUES (10, 'Store Sales') ;
INSERT INTO Dept_mys
VALUES (14, 'Corp Sales') ;
INSERT INTO Dept_mys
VALUES (16, 'Web Sales') ;
INSERT INTO Commission_mys
VALUES (A, .1) ;
INSERT INTO Commission_mys
VALUES (B, .08) ;
INSERT INTO Commission_mys
VALUES (Z, 0) ;
INSERT INTO Commission_mys
VALUES (C, .05) ;
INSERT INTO Category_mys
VALUES (1, 'Hand Tools') ;
INSERT INTO Category_mys
VALUES (2, 'power Tools') ;
INSERT INTO Category_mys
VALUES (4, 'Fasteners') ;
INSERT INTO Category_mys
VALUES (6, 'Misc') ;
INSERT INTO Category_mys
VALUES (3, 'Measuring Tools') ;
INSERT INTO Category_mys
VALUES (5, 'Hardware') ;
INSERT INTO SalesRep_mys
VALUES (10, 'Alice', 'Jones', 10, A) ;
INSERT INTO SalesRep_mys
VALUES (12, 'Greg', 'Taylor', 14, B) ;
INSERT INTO SalesRep_mys
VALUES (14, 'Sara', 'Day', 10, Z) ;
INSERT INTO SalesRep_mys
VALUES (8, 'Kay', 'Price', 14, C) ;
INSERT INTO SalesRep_mys
VALUES (20, 'Bob', 'Jackson', 10, B) ;
INSERT INTO SalesRep_mys
VALUES (22, 'Micah', 'Moore', 16, Z) ;
INSERT INTO Customer_mys
VALUES (S100, 'John', 'Smith', '5551212', 10) ;
INSERT INTO Customer_mys
VALUES (A120, 'Jane', 'Adams', '817555', 14) ;
INSERT INTO Customer_mys
VALUES (J090, 'Tim', 'Jones', NULL, 10) ;
INSERT INTO Customer_mys
VALUES (B200, 'Ann', 'Brown', '972555', 14) ;
INSERT INTO Customer_mys
VALUES (G070, 'Kate', 'Green', NULL, 10) ;
INSERT INTO Customer_mys
VALUES (S120, 'Nicole', 'Sims', NULL, 16) ;
INSERT INTO Order_mys
VALUES (100, '24-Jan-15', S100) ;
INSERT INTO Order_mys
VALUES (101, '25-Jan-15', A120) ;
INSERT INTO Order_mys
VALUES (102, '26-Jan-15', J090) ;
INSERT INTO Order_mys
VALUES (105, '26-Jan-15', B200) ;
INSERT INTO Order_mys
VALUES (106, '27-Jan-15', G070) ;
INSERT INTO Order_mys
VALUES (108, '27-Jan-15', S120) ;
INSERT INTO Product_mys
VALUES (121, 'BD Hammer', 1) ;
INSERT INTO Product_mys
VALUES (228, 'Makita Power Drill', 2) ;
INSERT INTO Product_mys
VALUES (480, '1 # BD Nails', 4) ;
INSERT INTO Product_mys
VALUES (610, '3M Duct Tape', 6) ;
INSERT INTO Product_mys
VALUES (618, '3M Masking Tape', 6) ;
INSERT INTO Product_mys
VALUES (380, 'Acme Yard Stick', 3) ;
INSERT INTO Product_mys
VALUES (535, 'Schlage Door Knob', 5) ;
INSERT INTO Product_mys
VALUES (123, 'Acme Pry Bar', 1) ;
INSERT INTO Product_mys
VALUES (229, 'BD Power Drill', 2) ;
INSERT INTO Product_mys
VALUES (124, 'Acme Hammer', 1) ;
INSERT INTO Product_mys
VALUES (235, 'Makita Power Drill', 2) ;
INSERT INTO OrderDetail_mys
VALUES (100, 121, 2, 8.00) ;
INSERT INTO OrderDetail_mys
VALUES (100, 228, 2, 65.00) ;
INSERT INTO OrderDetail_mys
VALUES (100, 480, 2, 3.00) ;
INSERT INTO OrderDetail_mys
VALUES (100, 407, 1, 4.25) ;
INSERT INTO OrderDetail_mys
VALUES (101, 610, 200, 1.75) ;
INSERT INTO OrderDetail_mys
VALUES (101, 618, 100, 1.25) ;
INSERT INTO OrderDetail_mys
VALUES (101, 121, 2, 8.00) ;
INSERT INTO OrderDetail_mys
VALUES (102, 380, 2, 1.25) ;
INSERT INTO OrderDetail_mys
VALUES (102, 121, 1, 7.00) ;
INSERT INTO OrderDetail_mys
VALUES (102, 535, 4, 7.50) ;
INSERT INTO OrderDetail_mys
VALUES (103, 121, 50, 7.00) ;
INSERT INTO OrderDetail_mys
VALUES (103, 123, 20, 6.25) ;
INSERT INTO OrderDetail_mys
VALUES (101, 121, 2, 8.00) ;
INSERT INTO OrderDetail_mys
VALUES (104, 229, 1, 50.00) ;
INSERT INTO OrderDetail_mys
VALUES (104, 610, 200, 1.75) ;
INSERT INTO OrderDetail_mys
VALUES (104, 380, 2, 1.25) ;
INSERT INTO OrderDetail_mys
VALUES (104, 535, 4, 7.50) ;
INSERT INTO OrderDetail_mys
VALUES (101, 121, 2, 8.00) ;
INSERT INTO OrderDetail_mys
VALUES (105, 610, 200, 1.75) ;
INSERT INTO OrderDetail_mys
VALUES (105, 123, 40, 5.00) ;
INSERT INTO OrderDetail_mys
VALUES (106, 124, 1, 6.50) ;
INSERT INTO OrderDetail_mys
VALUES (107, 229, 1, 59.00) ;
INSERT INTO OrderDetail_mys
VALUES (108, 235, 1, 65.00) ;
COMMIT ;
SELECT * FROM Dept_mys ;
SELECT * FROM Commission_mys ;
SELECT * FROM SalesRep_mys ;
SELECT * FROM Customer_mys ;
SELECT * FROM Category_mys ;
SELECT * FROM Order_mys ;
SELECT * FROM Product_mys ;
SELECT * FROM OrderDetail_mys ;
--PART III
UPDATE Customer_mys
SET CustLName = 'Thompson'
WHERE CustID = 'A120' ;
UPDATE Customer_mys
SET CustPhone = '2146881234'
WHERE CustID = 'J090' ;
UPDATE Product_mys
SET ProductPrice = 7.50
WHERE ProductID = '121' ;
UPDATE OrderDetail_mys
SET ProductName = 'BD Claw Hammer' ;
INSERT INTO Product_mys
VALUES (122, 'BD Rubber Mallet', 1) ;
INSERT INTO Product_mys
VALUES (125, 'Stanley Chisel', 1) ;
INSERT INTO Product_mys
VALUES (126, 'BD Chisel', 1) ;
INSERT INTO Product_mys
VALUES (381, 'BD Measuring Tape', 3) ;
INSERT INTO Product_mys
VALUES (382, 'Stanley Meausring Tape', 3) ;
INSERT INTO Commission_mys
VALUES (D, .03) ;
INSERT INTO SalesRep_mys
VALUES (21, 'Mark', 'Anderson', 14, C) ;
INSERT INTO SalesRep_mys
VALUES (23, 'Jennie', 'Hayes', 10, D) ;
INSERT INTO OrderDetail_mys
VALUES (109, 228, 1, 62.50) ;
INSERT INTO OrderDetail_mys
VALUES (109, 407, 1, 4.00) ;
INSERT INTO OrderDetail_mys
VALUES (109, 381, 2, 7.75) ;
INSERT INTO OrderDetail_mys
VALUES (110, 122, 2, 7.00) ;
INSERT INTO OrderDetail_mys
VALUES (110, 124, 1, 6.75) ;
INSERT INTO OrderDetail_mys
VALUES (110, 480, 2, 3.25) ;
COMMIT ;
SELECT * FROM Dept_mys ;
SELECT * FROM Commission_mys ;
SELECT * FROM SalesRep_mys ;
SELECT * FROM Customer_mys ;
SELECT * FROM Category_mys ;
SELECT * FROM Order_mys ;
SELECT * FROM Product_mys ;
SELECT * FROM OrderDetail_mys ;
I believe you're getting this error because A and B are being interpreted as some kind of variable. Try modifying your query to
INSERT INTO SalesRep_mys
VALUES (10, 'Alice', 'Jones', 10, 'A') ;
INSERT INTO SalesRep_mys
VALUES (12, 'Greg', 'Taylor', 14, 'B') ;
I have a MySQL DB that needs to provide attributes associated with a taxonomy. I desire to have a list of all attributes, including "inherited attributes" for a given element in the taxonomy. I'm not an SQL expert & have learned (and plagiarized) much from this site. One thing I learned here is to use a Closure Table to represent my Hierarchy. What I need to do for my database is associate a large number of attributes to elements within hierarchy. However, I can't seem to figure out how to grab all of the attributes associated with a given node and all of its parents. I created the following example database for the purpose of this question (feel free to comment on anything about this schema, the fake data is just noise).
My example MySQL database structure looks like this:
-- Simple Sample
SET FOREIGN_KEY_CHECKS=0;
DROP TRIGGER IF EXISTS inheritance_tree_insert;
DROP TRIGGER IF EXISTS inheritance_tree_update;
DROP TABLE IF EXISTS inheritance_paths;
DROP TABLE IF EXISTS inheritance_tree;
DROP TABLE IF EXISTS attributes;
SET FOREIGN_KEY_CHECKS=1;
CREATE TABLE `inheritance_tree` (
`it_id` INT NOT NULL, -- PK
`parent` INT, -- Parent id & FK
`child_order` INT, -- Oder of siblings
`name` VARCHAR(500) NOT NULL, -- Name for the entry
PRIMARY KEY (`it_id`),
FOREIGN KEY (`parent`) REFERENCES inheritance_tree(`it_id`) ON DELETE CASCADE,
INDEX(`name`)
) ENGINE = INNODB;
-- Trigger to update the paths table for new entries
DELIMITER //
CREATE TRIGGER `inheritance_tree_insert` AFTER INSERT ON `inheritance_tree` FOR EACH ROW
BEGIN
INSERT INTO `inheritance_paths` (`ancestor`, `descendant`, `len`)
SELECT `ancestor`, NEW.`it_id`, len + 1 FROM `inheritance_paths`
WHERE `descendant` = NEW.`parent`
UNION ALL SELECT NEW.`it_id`, NEW.`it_id`, 0;
END; //
DELIMITER ;
DELIMITER //
CREATE TRIGGER `inheritance_tree_update` BEFORE UPDATE ON `inheritance_tree` FOR EACH ROW
BEGIN
-- From http://www.mysqlperformanceblog.com/2011/02/14/moving-subtrees-in-closure-table/
IF OLD.`parent` != NEW.`parent` THEN
-- Remove the node from its current parent
DELETE a FROM `inheritance_paths` AS a
JOIN `inheritance_paths` AS d ON a.`descendant` = d.`descendant`
LEFT JOIN `inheritance_paths` AS x
ON x.`ancestor` = d.`ancestor` AND x.`descendant` = a.`ancestor`
WHERE d.`ancestor` = OLD.`it_id` AND x.`ancestor` IS NULL;
-- Add the node to its new parent
INSERT `inheritance_paths` (`ancestor`, `descendant`, `len`)
SELECT supertree.`ancestor`, subtree.`descendant`, supertree.`len`+subtree.`len`+1
FROM `inheritance_paths` AS supertree JOIN `inheritance_paths` AS subtree
WHERE subtree.`ancestor` = OLD.`it_id`
AND supertree.`descendant` = NEW.`parent`;
END IF;
END; //
DELIMITER ;
CREATE TABLE `inheritance_paths` (
`ancestor` INT NOT NULL,
`descendant` INT NOT NULL,
`len` INT NOT NULL,
PRIMARY KEY (`ancestor`, `descendant`),
FOREIGN KEY (`ancestor`) REFERENCES inheritance_tree(`it_id`) ON DELETE CASCADE,
FOREIGN KEY (`descendant`) REFERENCES inheritance_tree(`it_id`) ON DELETE CASCADE
) ENGINE = INNODB;
INSERT INTO `inheritance_tree` VALUES(1, NULL, NULL, 'Animal');
INSERT INTO `inheritance_tree` VALUES(2, 1, 1, 'Mammal');
INSERT INTO `inheritance_tree` VALUES(3, 1, 2, 'Bird');
INSERT INTO `inheritance_tree` VALUES(4, 1, 3, 'Reptile');
INSERT INTO `inheritance_tree` VALUES(5, 2, 2, 'Feline');
INSERT INTO `inheritance_tree` VALUES(6, 2, 1, 'Bovine');
INSERT INTO `inheritance_tree` VALUES(7, 1, 3, 'Fish');
INSERT INTO `inheritance_tree` VALUES(8, 4, 1, 'Snake');
INSERT INTO `inheritance_tree` VALUES(9, 4, 2, 'Lizard');
INSERT INTO `inheritance_tree` VALUES(10, NULL, NULL, 'Machine');
INSERT INTO `inheritance_tree` VALUES(11, 10, 1, 'Automobile');
INSERT INTO `inheritance_tree` VALUES(12, 10, 2, 'OfficeMachine');
INSERT INTO `inheritance_tree` VALUES(13, 10, 3, 'Robot');
DELIMITER ;
CREATE TABLE `attributes` (
`a_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'the unique identifier',
`attribute_name` varchar(255) DEFAULT NULL,
`attribute_type` int(11) NOT NULL,
PRIMARY KEY (`a_id`),
KEY `fk_attributes_attribute_type1_idx` (`attribute_type`),
CONSTRAINT `fk_attributes_attribute_type1_idx` FOREIGN KEY (`attribute_type`) REFERENCES `inheritance_tree` (`it_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;
INSERT INTO `attributes` VALUES(1, 'IsAlive', 1); -- Animal
INSERT INTO `attributes` VALUES(2, 'HasMaximumLifeSpan', 1); -- Animal
INSERT INTO `attributes` VALUES(3, 'IsNotAlive', 10); -- Machine
INSERT INTO `attributes` VALUES(4, 'HasIndeterminantLifeSpan', 10); -- Machine
INSERT INTO `attributes` VALUES(5, 'BreathesAir', 2); -- Mammal
INSERT INTO `attributes` VALUES(6, 'CanFly', 3); -- Bird
INSERT INTO `attributes` VALUES(7, 'HasFeathers', 3); -- Bird
INSERT INTO `attributes` VALUES(8, 'ColdBlooded', 4); -- Reptile
INSERT INTO `attributes` VALUES(9, 'HasFourLegs', 5); -- Feline
INSERT INTO `attributes` VALUES(10, 'HasFourLegs', 6); -- Bovine
INSERT INTO `attributes` VALUES(11, 'ColdBlooded', 7); -- Fish
INSERT INTO `attributes` VALUES(12, 'HasNoLegs', 8); -- Snake
INSERT INTO `attributes` VALUES(13, 'HasFourLegs', 9); -- Lizard
INSERT INTO `attributes` VALUES(14, 'ConsumesGasoline', 11); -- Automobile
INSERT INTO `attributes` VALUES(15, 'ConsumesElectricity', 12); -- OfficeMachine
INSERT INTO `attributes` VALUES(16, 'ConsumesElectricity', 13); -- Robot
INSERT INTO `attributes` VALUES(17, 'WarmBlooded', 2); -- Mammal
INSERT INTO `attributes` VALUES(18, 'WarmBlooded', 3); -- Bird
INSERT INTO `attributes` VALUES(19, 'BreathesWater', 7); -- Fish
INSERT INTO `attributes` VALUES(20, 'HasScales', 8); -- Snake
INSERT INTO `attributes` VALUES(21, 'HasSkin', 9); -- Lizard
Assumptions:
1.All entities in the taxonomy are uniquely named. This means there is one and only one sub-type called “Fish” anywhere in any of the taxonomy trees.
2.Attributes are not unique and may repeat
GOAL:
Given the input type “Lizard” I would like a single query that returns the following list of attribute records:
1, IsAlive, 1
2, HasMaximumLifeSpan, 1
5, BreathesAir, 2
8, ColdBlooded, 4
13, HasFourLegs, 9
21, HasSkin, 9
SELECT a.*
FROM inheritance_tree t
JOIN inheritance_paths p ON p.descendant = t.it_id
JOIN attributes a ON a.attribute_type = p.ancestor
WHERE t.name = 'Lizard'
See it on sqlfiddle.
Note that your example output includes a_id = 5 (BreathesAir), which is a property of it_id = 2 (Mammal), which is not in the Lizard's ancestry.
I need to update some rows in my table, for simplicity called "three".
I select the columns to update with this query:
SELECT one.id
FROM one
JOIN `two` ON ( one.id = two.page )
JOIN `three` ON ( one.id = three.item )
WHERE two.level = 1
AND two.item = (SELECT item FROM two WHERE page = 5 AND level = 1 )
AND three.position > (SELECT position FROM three WHERE item = 5 )
ORDER BY three.position
Now I call an update query with id's I get.
Is there any chance to eliminate the subqueries?
Edit (after Melanie's comment):
Table "one":
|id|text|
Table "two":
|id|item|page|level|
Table "three":
|item|position|
So when I run the query
SELECT item FROM two WHERE page = 5 AND level = 1
It will return f.ex 1 and the final WHERE clause will be:
two.item = 1 AND two.level = 1
Which is not the same as:
two.level = 1 and two.page = 5
I have the table one - some text with some one.id. I need to update all items from table three which has higher position than my item (f.ex. id = 5) have. But those items should also have the same two.item in table two, where two.page = one.id and level = 1
I am sorry for a poor description.
You should be able to replace those subqueries by joins:
SELECT one.id
FROM one
JOIN `two2` ON (two2.page = 5 AND two2.level = 1)
JOIN `two` ON ( one.id = two.page AND two.item = two2.item )
JOIN `three2` ON ( three.item = 5)
JOIN `three` ON ( one.id = three.item AND three.position > three2.position)
WHERE two.level = 1
ORDER BY three.position
#TheVedge is interesting solution but does not produce the same result as your query
I suggest to avoid duplicate the same table also with a view so a little correction
Another correction is three2.item=5
I suggest to use in subquery limit 0,1 so never return more then one element
SELECT one.id
FROM one
JOIN `two` AS TWO2 ON (two2.page = 5 AND two2.level = 1)
JOIN `two` ON ( one.id = two.page AND two.item = two2.item )
JOIN `three` AS THREE2 ON ( three2.item = 5)
JOIN `three` ON ( one.id = three.item AND three.position > three2.position)
WHERE two.level = 1
ORDER BY three.position
Remember that you are not doing the same thing with this query.
Try this
CREATE TABLE `one` (
`id` INT(10) NULL DEFAULT NULL,
`text` VARCHAR(50) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
_
CREATE TABLE `three` (
`item` INT(10) NULL DEFAULT NULL,
`position` INT(10) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
_
CREATE TABLE `two` (
`id` INT(10) NULL DEFAULT NULL,
`item` INT(10) NULL DEFAULT NULL,
`page` INT(10) NULL DEFAULT NULL,
`level` INT(10) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
_
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (1, 1, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (3, 3, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (4, 4, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (5, 5, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (6, 6, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (7, 7, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (8, 8, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (9, 9, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (2, 2, 5, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (10, 2, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (11, 1, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (13, 3, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (14, 4, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (15, 5, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (16, 6, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (17, 7, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (18, 8, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (19, 9, 1, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (20, 2, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (21, 1, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (23, 3, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (24, 4, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (25, 5, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (26, 6, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (27, 7, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (28, 8, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (29, 9, 2, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (30, 2, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (31, 1, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (33, 3, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (34, 4, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (35, 5, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (36, 6, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (37, 7, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (38, 8, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (39, 9, 3, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (40, 2, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (41, 1, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (42, 3, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (43, 4, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (44, 5, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (45, 6, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (46, 7, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (47, 8, 4, 1);
INSERT INTO `two` (`id`, `item`, `page`, `level`) VALUES (48, 9, 4, 1);
-
INSERT INTO `three` (`item`, `position`) VALUES (1, 1);
INSERT INTO `three` (`item`, `position`) VALUES (2, 1);
INSERT INTO `three` (`item`, `position`) VALUES (3, 1);
INSERT INTO `three` (`item`, `position`) VALUES (4, 1);
INSERT INTO `three` (`item`, `position`) VALUES (5, 0);
INSERT INTO `three` (`item`, `position`) VALUES (6, 1);
INSERT INTO `three` (`item`, `position`) VALUES (7, 1);
INSERT INTO `three` (`item`, `position`) VALUES (8, 1);
INSERT INTO `three` (`item`, `position`) VALUES (9, 1);
INSERT INTO `three` (`item`, `position`) VALUES (10, 1);
INSERT INTO `three` (`item`, `position`) VALUES (11, 1);
INSERT INTO `three` (`item`, `position`) VALUES (12, 1);
_
INSERT INTO `one` (`id`, `text`) VALUES (1, 'A');
INSERT INTO `one` (`id`, `text`) VALUES (2, 'B');
INSERT INTO `one` (`id`, `text`) VALUES (3, 'C');
INSERT INTO `one` (`id`, `text`) VALUES (4, 'D');
INSERT INTO `one` (`id`, `text`) VALUES (5, 'E');
INSERT INTO `one` (`id`, `text`) VALUES (6, 'F');
INSERT INTO `one` (`id`, `text`) VALUES (7, 'G');
_
SELECT
one.id, one.text
,two.id,two.item,two.page,two.level
,three.item,three.position
FROM one
JOIN `two` ON ( one.id = two.page )
JOIN `three` ON ( one.id = three.item )
WHERE two.level = 1
AND two.item = (SELECT item FROM two WHERE page = 5 AND level = 1 limit 0,1 )
AND three.position > (SELECT position FROM three WHERE item = 5 limit 0,1 )
ORDER BY three.position
SELECT
one.id, one.text
,two.id,two.item,two.page,two.level
,three.item,three.position
FROM one
JOIN `two` AS TWO2 ON (two2.page = 5 AND two2.level = 1)
JOIN `two` ON ( one.id = two.page AND two.item = two2.item )
JOIN `three` AS THREE2 ON ( three2.item = 5)
JOIN `three` ON ( one.id = three.item AND three.position > three2.position)
WHERE two.level = 1
ORDER BY three.position
With original query you made a select of specific element in TheVedge solution you are joining more data
So result depend on what you select
Another useful analysis is http://dev.mysql.com/doc/refman/5.0/en/show-profile.html and Explain
Show Profile show that at the first run
your original query does
Status Duration
starting 0.000039
checking query cache for query 0.000144
Opening tables 0.000032
System lock 0.000007
Table lock 0.000061
init 0.000054
optimizing 0.000314
statistics 0.000021
preparing 0.000051
Creating tmp table 0.000084
executing 0.000004
Copying to tmp table 0.000063
optimizing 0.000008
statistics 0.000019
preparing 0.000009
executing 0.000004
Sending data 0.000054
optimizing 0.000008
statistics 0.000007
preparing 0.000009
executing 0.000003
Sending data 0.000126
Sorting result 0.000030
Sending data 0.000025
end 0.000004
removing tmp table 0.000011
end 0.000005
query end 0.000004
freeing items 0.000101
storing result in query cache 0.000008
logging slow query 0.000003
cleaning up 0.000006
Proposed query does
Status Duration
starting 0.000036
checking query cache for query 0.000122
Opening tables 0.000030
System lock 0.000008
Table lock 0.000064
init 0.000046
optimizing 0.000028
statistics 0.000026
preparing 0.000072
Creating tmp table 0.000086
executing 0.000005
Copying to tmp table 0.001081
Sorting result 0.000040
Sending data 0.000056
end 0.000005
removing tmp table 0.000010
end 0.000005
query end 0.000004
freeing items 0.000108
storing result in query cache 0.000007
logging slow query 0.000003
cleaning up 0.000005
So when you have full data you can try to evalute better the response of both query
This question is similar to but different from other SO questions involving removal of duplicate rows in MySQL.
Chosen solutions in the referenced questions(below) fail where one or more of the columns contain NULL values. I'm including schema as well as two chosen answers in a sqlfiddle below, to illustrate where the previous solutions are not working.
Source schema:
CREATE TABLE IF NOT EXISTS `deldup` (
`or_id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` varchar(5) NOT NULL,
`txt_value` varchar(20) NOT NULL,
`date_of_revision` date NOT NULL,
`status` int(3) DEFAULT NULL,
PRIMARY KEY (`or_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
INSERT INTO `deldup` (`or_id`, `order_id`, `txt_value`, `date_of_revision`, `status`) VALUES
(1, '10001', 'ABC', '2003-03-06', NULL),
(2, '10001', 'RFE', '2003-03-11', NULL),
(3, '10002', 'ASE', '2009-08-05', NULL),
(4, '10003', 'PEF', '2001-11-03', NULL),
(5, '10004', 'OIU', '1999-10-29', NULL),
(6, '10005', 'FOO', '2002-03-01', NULL),
(7, '10006', 'RTY', '2005-08-19', NULL),
(8, '10001', 'NND', '2003-03-20', NULL),
(9, '10005', 'VBN', '2002-02-19', NULL),
(10, '10002', 'AAQ', '2009-08-13', NULL),
(11, '10002', 'EEW', '2009-08-07', NULL),
(12, '10001', 'ABC', '2003-03-06', 3),
(13, '10001', 'ABC', '2003-03-06', 3),
(14, '10001', 'ABC', '2003-03-06', NULL),
(15, '10001', 'ABC', '2003-03-06', NULL);
Solution Example 1:
http://sqlfiddle.com/#!2/983f3/1
create temporary table tmpTable (or_id int);
insert tmpTable
(or_id)
select or_id
from deldup yt
where exists
(
select *
from deldup yt2
where yt2.txt_value = yt.txt_value
and yt2.order_id = yt.order_id
and yt2.date_of_revision = yt.date_of_revision
and yt2.status = yt.status
and yt2.or_id > yt.or_id
);
delete
from deldup
where or_id in (select or_id from tmpTable);
Note that the rows containing non-null row values are successfully deleted, however rows containing null values are not removed (See row 14 and 15 in the resulting SELECT query).
Solution Example 2:
http://sqlfiddle.com/#!2/8a4f8/1
DELETE
n1
FROM
deldup n1,
deldup n2
WHERE
n1.or_id < n2.or_id AND
n1.order_id = n2.order_id AND
n1.txt_value = n2.txt_value AND
n1.date_of_revision = n2.date_of_revision AND
n1.status = n2.status
This solution involves less code and works just the same as Example 1, including the exclusion of rows containing null values.
How can I remove the duplicate rows where one of the column values contains NULL values?
References:
Delete all Duplicate Rows except for One in MySQL?
Remove duplicate rows in MySQL
How to remove duplicate entries from a mysql db?
How about simply adjusting the second solution with something like this:
WHERE
...
(n1.status IS NULL AND n2.status IS NULL
OR n1.status = n2.status)
I assume you consider the record a duplicate only if all values are repeated.