基于查询的SQL删除

时间:2013-03-26 13:32:31

标签: sql sql-delete

如何删除不制造PC的制造商生产的所有笔记本电脑?

我目前有以下表格:

CREATE TABLE `Device` (
`model` varchar(50) NOT NULL DEFAULT '',
`speed` double DEFAULT NULL,
`RAM` int(11) DEFAULT NULL,
`HD` int(11) DEFAULT NULL,
`list_price` double DEFAULT NULL,
`type` varchar(45) NOT NULL,
`screen` varchar(45) DEFAULT NULL,
PRIMARY KEY (`model`,`type`)
);

INSERT INTO `Device` VALUES 
('GATE TOP',2.5,2000,500,1000,'LAPTOP','12'),
('GATE TOWER',2.5,2000,500,1000,'DESKTOP',NULL),
('HELLO TOWER',3.5,8000,1000,1299,'DESKTOP',NULL),
('MACBOOK AIR',2.5,2048,500,599,'LAPTOP','11'),
('MACBOOK PRO',3.5,8000,1000,1299,'LAPTOP','19'),
('PAAP',2.5,2048,500,599,'DESKTOP',NULL),
('TOWER',3,3100,400,2499,'DESKTOP',NULL),
('ULTRA TOWER',6,5000,1000,8999,'DESKTOP',NULL),
('VAIO',2.5,2000,500,569,'LAPTOP','12'),
('VAIO TOWER',2.5,2000,500,569,'DESKTOP',NULL);

CREATE TABLE `Product` (
`manu_Name` varchar(50) NOT NULL DEFAULT '',
`model` varchar(50) NOT NULL DEFAULT '',
 PRIMARY KEY (`manu_Name`,`model`),
 KEY `Product` (`model`),
 CONSTRAINT `Product_ibfk_1` FOREIGN KEY (`manu_Name`) 
 REFERENCES `Manufacturer` (`name`),
 CONSTRAINT `Product_ibfk_2` FOREIGN KEY (`model`) 
 REFERENCES `Device` (`model`)
);

INSERT INTO `Product` VALUES 
('GATEWAY','GATE TOP'),('GATEWAY','GATE TOWER'),
('ACER','HELLO TOWER'),('APPLE','MACBOOK AIR'),
('APPLE','MACBOOK PRO'),('ACER','PAAP'),
('DELL','TOWER'),('SONY','VAIO'),
('SONY','VAIO TOWER');

CREATE TABLE `Manufacturer` (
`name` varchar(50) NOT NULL,
`country` varchar(50) DEFAULT NULL,
`phone` varchar(10) DEFAULT NULL,
PRIMARY KEY (`name`)
);

INSERT INTO `Manufacturer` VALUES 
('ACER','TAIWAN','9024801111'),
('APPLE','UNITED STATES','9028189125'),
('DELL','UNITED STATES','9025551234'),
('GATEWAY','UNITED STATES','8705551698'),
('SONY','JAPAN','0123456789'),
('TOSHIBA','JAPAN','1235553560');

1 个答案:

答案 0 :(得分:1)

删除语句将是这样的:

delete from `Device` where `model` in (
    select `model` from `Product` where `manu_Name` in ('APPLE')
);