2013-03-26 94 views
-2

如何刪除不製造PC的製造商生產的所有筆記本電腦?基於查詢的SQL刪除

目前,我有以下表格:

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',''), 
('TOSHIBA','JAPAN','1235553560'); 
+0

什麼掙扎?你確定你試圖刪除一個'索引'? – LittleBobbyTables 2013-03-26 13:33:52

+0

你想刪除基於查詢的索引?什麼查詢? – 2013-03-26 13:34:03

+0

該查詢應該是「刪除製造商製造的不製造PC的所有筆記本電腦」 – Aaron 2013-03-26 13:39:28

回答

1

DELETE語句是這樣的:

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

這裏是一個[demo](http://sqlfiddle.com/#!9/e3d90/1)刪除是在模式創建端,顯示設備不見了 – 2013-03-26 14:48:20