Like the title says, I need to query a self referencing table to find all items that are NOT the parent of any item.
Here is my table:
itemNumber | quantity | cost | parentItem | level
------------|----------|------|------------|-------
1 | 1 | 31 | NULL | 0
2 | 2 | 4 | 1 | 1
3 | 1 | 2 | 2 | 2
4 | 2 | 1 | 3 | 3
5 | 1 | 2 | 2 | 2
6 | 1 | 10 | 1 | 1
7 | 2 | 5 | 6 | 2
8 | 1 | 1 | 7 | 3
9 | 1 | 3 | 7 | 3
10 | 1 | 1 | 7 | 3
11 | 1 | 6 | 1 | 1
12 | 3 | 2 | 11 | 2
13 | 1 | 7 | 1 | 1
Here is my schema:
CREATE TABLE IF NOT EXISTS `recursivebom`.`level` (
`level` INT NOT NULL,
PRIMARY KEY (`level`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `recursivebom`.`item`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `recursivebom`.`item` (
`itemNumber` INT NOT NULL,
`quantity` DECIMAL NULL,
`cost` DECIMAL NULL,
`parentItem` INT NULL,
`level` INT NOT NULL,
PRIMARY KEY (`itemNumber`, `level`),
INDEX `fk_item_item1_idx` (`parentItem` ASC),
INDEX `fk_item_level1_idx` (`level` ASC),
CONSTRAINT `fk_item_item1`
FOREIGN KEY (`parentItem`)
REFERENCES `recursivebom`.`item` (`itemNumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_item_level1`
FOREIGN KEY (`level`)
REFERENCES `recursivebom`.`level` (`level`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Here is my table:
itemNumber | quantity | cost | parentItem | level
------------|----------|------|------------|-------
1 | 1 | 31 | NULL | 0
2 | 2 | 4 | 1 | 1
3 | 1 | 2 | 2 | 2
4 | 2 | 1 | 3 | 3
5 | 1 | 2 | 2 | 2
6 | 1 | 10 | 1 | 1
7 | 2 | 5 | 6 | 2
8 | 1 | 1 | 7 | 3
9 | 1 | 3 | 7 | 3
10 | 1 | 1 | 7 | 3
11 | 1 | 6 | 1 | 1
12 | 3 | 2 | 11 | 2
13 | 1 | 7 | 1 | 1
Here is my schema:
CREATE TABLE IF NOT EXISTS `recursivebom`.`level` (
`level` INT NOT NULL,
PRIMARY KEY (`level`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `recursivebom`.`item`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `recursivebom`.`item` (
`itemNumber` INT NOT NULL,
`quantity` DECIMAL NULL,
`cost` DECIMAL NULL,
`parentItem` INT NULL,
`level` INT NOT NULL,
PRIMARY KEY (`itemNumber`, `level`),
INDEX `fk_item_item1_idx` (`parentItem` ASC),
INDEX `fk_item_level1_idx` (`level` ASC),
CONSTRAINT `fk_item_item1`
FOREIGN KEY (`parentItem`)
REFERENCES `recursivebom`.`item` (`itemNumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_item_level1`
FOREIGN KEY (`level`)
REFERENCES `recursivebom`.`level` (`level`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;