数据库系统概论第三章课后习题第四题数据库表及习题答案
下面是题目里所用到的数据库表导入代码
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `J`;
CREATE TABLE `J` (
`JNO` varchar(255) NOT NULL,
`JNAME` varchar(255) DEFAULT NULL,
`CITY` varchar(255) DEFAULT NULL,
PRIMARY KEY (`JNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
BEGIN;
INSERT INTO `J` VALUES ('J1', '三建', '北京');
INSERT INTO `J` VALUES ('J2', '一汽', '长春');
INSERT INTO `J` VALUES ('J3', '弹簧厂', '天津');
INSERT INTO `J` VALUES ('J4', '造船厂', '天津');
INSERT INTO `J` VALUES ('J5', '机车厂', '唐山');
INSERT INTO `J` VALUES ('J6', '无线电厂', '常州');
INSERT INTO `J` VALUES ('J7', '半导体厂', '南京');
COMMIT;
DROP TABLE IF EXISTS `P`;
CREATE TABLE `P` (
`PNO` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`PNAME` varchar(255) DEFAULT NULL,
`COLOR` varchar(255) DEFAULT NULL,
`WEIGHT` varchar(255) DEFAULT NULL,
PRIMARY KEY (`PNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
BEGIN;
INSERT INTO `P` VALUES ('P1', '螺母', '红', '12');
INSERT INTO `P` VALUES ('P2', '螺栓', '绿', '17');
INSERT INTO `P` VALUES ('P3', '螺丝刀', '蓝', '14');
INSERT INTO `P` VALUES ('P4', '螺丝刀', '红', '14');
INSERT INTO `P` VALUES ('P5', '凸轮', '蓝', '40');
INSERT INTO `P` VALUES ('P6', '齿轮', '红', '30');
COMMIT;
DROP TABLE IF EXISTS `S`;
CREATE TABLE `S` (
`SNO` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`SNAME` varchar(255) DEFAULT NULL,
`STATUS` varchar(255) DEFAULT NULL,
`CITY` varchar(255) DEFAULT NULL,
PRIMARY KEY (`SNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
BEGIN;
INSERT INTO `S` VALUES ('S1', '精益', '20', '天津');
INSERT INTO `S` VALUES ('S2', '盛锡', '10', '北京');
INSERT INTO `S` VALUES ('S3', '东方红', '30', '北京');
INSERT INTO `S` VALUES ('S4', '丰泰盛', '20', '天津');
INSERT INTO `S` VALUES ('S5', '为民', '30', '上海');
COMMIT;
DROP TABLE IF EXISTS `SPJ`;
CREATE TABLE `SPJ` (
`SNO` varchar(255) NOT NULL,
`PNO` varchar(255) NOT NULL,
`JNO` varchar(255) NOT NULL,
`QTY` varchar(255) DEFAULT NULL,
PRIMARY KEY (`SNO`,`PNO`,`JNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
BEGIN;
INSERT INTO `SPJ` VALUES ('S1', 'P1', 'J1', '200');
INSERT INTO `SPJ` VALUES ('S1', 'P1', 'J3', '100');
INSERT INTO `SPJ` VALUES ('S1', 'P1', 'J4', '700');
INSERT INTO `SPJ` VALUES ('S1', 'P2', 'J2', '100');
INSERT INTO `SPJ` VALUES ('S2', 'P3', 'J1', '400');
INSERT INTO `SPJ` VALUES ('S2', 'P3', 'J2', '200');
INSERT INTO `SPJ` VALUES ('S2', 'P3', 'J4', '500');
INSERT INTO `SPJ` VALUES ('S2', 'P3', 'J5', '400');
INSERT INTO `SPJ` VALUES ('S2', 'P5', 'J1', '400');
INSERT INTO `SPJ` VALUES ('S2', 'P5', 'J2', '100');
INSERT INTO `SPJ` VALUES ('S3', 'P1', 'J1', '200');
INSERT INTO `SPJ` VALUES ('S3', 'P3', 'J1', '200');
INSERT INTO `SPJ` VALUES ('S4', 'P5', 'J1', '100');
INSERT INTO `SPJ` VALUES ('S4', 'P6', 'J3', '300');
INSERT INTO `SPJ` VALUES ('S4', 'P6', 'J4', '200');
INSERT INTO `SPJ` VALUES ('S5', 'P2', 'J4', '100');
INSERT INTO `SPJ` VALUES ('S5', 'P3', 'J1', '200');
INSERT INTO `SPJ` VALUES ('S5', 'P6', 'J2', '200');
INSERT INTO `SPJ` VALUES ('S5', 'P6', 'J4', '500');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
我用的是MacOS下的navicat,只要建一个查询,复制上述代码运行就可以自动建表、录入数据了
下面是题目的答案:
SELECT DISTINCT SNO FROM SPJ WHERE JNO='J1';
SELECT DISTINCT SNO FROM SPJ WHERE PNO='P1' AND jno='j1';
SELECT DISTINCT SNO FROM SPJ,P WHERE COLOR='红' AND JNO='J1' AND SPJ.PNO=P.PNO;
SELECT DISTINCT J.JNO FROM J WHERE J.JNO NOT IN (SELECT DISTINCT JNO FROM S,SPJ,P WHERE COLOR='红' AND CITY='天津' AND S.SNO=SPJ.SNO AND P.PNO=SPJ.PNO);
SELECT DISTINCT JNO FROM SPJ WHERE NOT EXISTS (SELECT*FROM SPJ AS SPJ1 WHERE SPJ1.SNO='S1' AND NOT EXISTS (SELECT*FROM SPJ AS SPJ2 WHERE SPJ2.JNO=SPJ.JNO AND SPJ2.PNO=SPJ1.PNO));
第五题有点奇怪,运行出来是J4,我认为按照正常的想法应该是J1,J3,J4,J2或者一个都不满足,但是答案给出的结果就是这个,所以也不再做深究了,可能题目有问题,也可能我理解的有问题吧。
|