3

以下是我的表结构和外键:

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

CREATE SCHEMA IF NOT EXISTS `homework9` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `homework9` ;

-- -----------------------------------------------------
-- Table `homework9`.`employee`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `homework9`.`employee` ;

CREATE  TABLE IF NOT EXISTS `homework9`.`employee` (
  `EmployeeNumber` INT NOT NULL ,
  `FirstName` VARCHAR(15) NULL ,
  `LastName` VARCHAR(15) NULL ,
  `Department` VARCHAR(15) NULL ,
  `Phone` VARCHAR(15) NULL ,
  `Email` VARCHAR(25) NULL ,
  PRIMARY KEY (`EmployeeNumber`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `homework9`.`computer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `homework9`.`computer` ;

CREATE  TABLE IF NOT EXISTS `homework9`.`computer` (
  `SerialNumber` INT NOT NULL ,
  `Make` VARCHAR(12) NOT NULL ,
  `Model` VARCHAR(24) NOT NULL ,
  `ProcessorType` VARCHAR(24) NULL ,
  `ProcessorSpeed` DECIMAL(3,2) NOT NULL ,
  `MainMemory` VARCHAR(15) NOT NULL ,
  `DiskSize` VARCHAR(15) NOT NULL ,
  PRIMARY KEY (`SerialNumber`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `homework9`.`computer_assignment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `homework9`.`computer_assignment` ;

CREATE  TABLE IF NOT EXISTS `homework9`.`computer_assignment` (
  `EmployeeNumber` INT NOT NULL ,
  `SerialNumber` INT NOT NULL ,
  `DateAssigned` DATETIME NOT NULL ,
  `DateReassigned` DATETIME NULL ,
  PRIMARY KEY (`EmployeeNumber`, `SerialNumber`) ,
  INDEX `fk_computer_assignment_computer1` (`SerialNumber` ASC) ,
  CONSTRAINT `fk_computer_assignment_employee`
    FOREIGN KEY (`EmployeeNumber` )
    REFERENCES `homework9`.`employee` (`EmployeeNumber` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_computer_assignment_computer1`
    FOREIGN KEY (`SerialNumber` )
    REFERENCES `homework9`.`computer` (`SerialNumber` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

我尝试运行的更新导致我遇到的错误...

USE homework9;
set sql_safe_updates=0; 

UPDATE employee SET Department = 'marketing' WHERE EmployeeNumber = 9;
UPDATE employee SET phone = '315-999-3344' WHERE LastName = 'rubble';
UPDATE computer SET make = 'Dell', model = 'OptiPlex 980', processortype = 'Intel i3-650',
    processorspeed = 3.20, mainmemory = '4.0 GBytyes', DiskSize = '1.0 TBytes' 
    WHERE SerialNumber = 9871278;
UPDATE computer SET processorspeed = processorspeed + 0.50;
DELETE FROM computer_assignment WHERE EmployeeNumber = 11;
DELETE FROM employee WHERE EmployeeNumber = 11;
DELETE FROM computer_assignment WHERE computer_assignment.EmployeeNumber = employee.EmployeeNumber AND employee.LastName = 'rubble';

set sql_safe_updates=1; 

select * from employee;
select * from computer;
select * from computer_assignment;

导致此错误:where 子句中的未知列employee.EmployeeNumber。

这是怎么回事?任何帮助是极大的赞赏!

4

3 回答 3

3
DELETE FROM computer_assignment 
WHERE computer_assignment.EmployeeNumber = employee.EmployeeNumber 
AND employee.LastName = 'rubble';

您没有employeeFROM子句中提到表,所以它是未知的。

也许你的意思是

DELETE FROM computer_assignment 
WHERE computer_assignment.EmployeeNumber in 
  (select employee.EmployeeNumber from employee WHERE
   employee.LastName = 'rubble')
于 2012-04-18T05:38:10.573 回答
2
DELETE FROM computer_assignment 
WHERE computer_assignment.EmployeeNumber = employee.EmployeeNumber
  AND employee.LastName = 'rubble'

问题是employee在此查询中的任何地方都没有定义。

DELETE FROM computer_assignment
INNER JOIN employee ON computer_assignment.EmployeeNumber = employee.EmployeeNumber
WHERE computer_assignment.EmployeeNumber = employee.EmployeeNumber
  AND employee.LastName = 'rubble'
于 2012-04-18T05:36:51.657 回答
0

您仅引用 FROM 中的 computer_assignment 表,因此此处不提供员工表。

DELETE FROM computer_assignment WHERE computer_assignment.EmployeeNumber = employee.EmployeeNumber AND employee.LastName = 'rubble';

试试这个:

DELETE FROM computer_assignment WHERE computer_assignment.EmployeeNumber IN (SELECT EmployeeNumber FROM employee WHERE LastName = "Rubble")
于 2012-04-18T05:45:55.347 回答