下面是我的表结构和外键:
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。
发生了什么事?非常感谢任何帮助!
答案 0 :(得分:3)
DELETE FROM computer_assignment
WHERE computer_assignment.EmployeeNumber = employee.EmployeeNumber
AND employee.LastName = 'rubble';
您尚未在employee
子句中提及表FROM
,因此未知。
也许你的意思
DELETE FROM computer_assignment
WHERE computer_assignment.EmployeeNumber in
(select employee.EmployeeNumber from employee WHERE
employee.LastName = 'rubble')
答案 1 :(得分: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'
答案 2 :(得分:0)
您只引用FROM中的computer_assignment表,因此此表中的employee表不可用。
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")