/* 删除Product表中数据的DELETE语句 (恢复到使用CREATE TABLE创建时数据为空的状态) */ DELETE FROM Product; /* 下面是问题的SELECT语句 */ -- A先生执行(SQL Server、PostgreSQL的情况) BEGIN TRANSACTION; INSERT INTO Product VALUES (‘0001‘, ‘T恤‘, ‘衣服‘, 1000, 500, ‘2009-09-20‘); INSERT INTO Product VALUES (‘0002‘, ‘打孔器‘, ‘办公用品‘, 500, 320, ‘2009-09-11‘); INSERT INTO Product VALUES (‘0003‘, ‘运动T恤‘, ‘衣服‘, 4000, 2800, NULL); -- A先生执行(MySQL的情况) START TRANSACTION; INSERT INTO Product VALUES (‘0001‘, ‘T恤‘, ‘衣服‘, 1000, 500, ‘2009-09-20‘); INSERT INTO Product VALUES (‘0002‘, ‘打孔器‘, ‘办公用品‘, 500, 320, ‘2009-09-11‘); INSERT INTO Product VALUES (‘0003‘, ‘运动T恤‘, ‘衣服‘, 4000, 2800, NULL); -- A先生执行(Oracle、DB2的情况) INSERT INTO Product VALUES (‘0001‘, ‘T恤‘, ‘衣服‘, 1000, 500, ‘2009-09-20‘); INSERT INTO Product VALUES (‘0002‘, ‘打孔器‘, ‘办公用品‘, 500, 320, ‘2009-09-11‘); INSERT INTO Product VALUES (‘0003‘, ‘运动T恤‘, ‘衣服‘, 4000, 2800, NULL); -- B先生执行 SELECT * FROM Product;View Code
/* 删除Product表中数据的DELETE语句 (恢复到使用CREATE TABLE创建时数据为空的状态) */ DELETE FROM Product; /* 以下是问题中添加3行记录的INSERT语句 */ -- 添加3行记录(SQL Server、PostgreSQL的情况) BEGIN TRANSACTION; INSERT INTO Product VALUES (‘0001‘, ‘T恤‘, ‘衣服‘, 1000, 500, ‘2009-09-20‘); INSERT INTO Product VALUES (‘0002‘, ‘打孔器‘, ‘办公用品‘, 500, 320, ‘2009-09-11‘); INSERT INTO Product VALUES (‘0003‘, ‘运动T恤‘, ‘衣服‘, 4000, 2800, NULL); COMMIT; -- 添加3行记录(MySQL的情况) START TRANSACTION; INSERT INTO Product VALUES (‘0001‘, ‘T恤‘, ‘衣服‘, 1000, 500, ‘2009-09-20‘); INSERT INTO Product VALUES (‘0002‘, ‘打孔器‘, ‘办公用品‘, 500, 320, ‘2009-09-11‘); INSERT INTO Product VALUES (‘0003‘, ‘运动T恤‘, ‘衣服‘, 4000, 2800, NULL); COMMIT; -- 添加3行记录(Oracle、DB2的情况) INSERT INTO Product VALUES (‘0001‘, ‘T恤‘, ‘衣服‘, 1000, 500, ‘2009-09-20‘); INSERT INTO Product VALUES (‘0002‘, ‘打孔器‘, ‘办公用品‘, 500, 320, ‘2009-09-11‘); INSERT INTO Product VALUES (‘0003‘, ‘运动T恤‘, ‘衣服‘, 4000, 2800, NULL); COMMIT; /* 下面是问题中的INSERT语句 向Product表中插入Product表(发生错误) */ INSERT INTO Product SELECT * FROM Product;View Code
/* 下面是问题中的SELECT语句 */ -- 商品利润表 CREATE TABLE ProductMargin (product_id CHAR(4) NOT NULL, product_name VARCHAR(100) NOT NULL, sale_price INTEGER, purchase_price INTEGER, margin INTEGER, PRIMARY KEY(product_id)); /* 下面是解答示例 */ -- 将Product表中的数据插入到ProductMargin表中 INSERT INTO ProductMargin (product_id, product_name, sale_price, purchase_price, margin) SELECT product_id, product_name, sale_price, purchase_price, sale_price - purchase_price FROM Product; -- 确认插入数据 SELECT * FROM ProductMargin;View Code
-- 1.下调销售单价 UPDATE ProductMargin SET sale_price = 3000 WHERE product_id = ‘0003‘; -- 2.利润再计算 UPDATE ProductMargin SET margin = sale_price - purchase_price WHERE product_id = ‘0003‘; -- 确认数据更新 SELECT * FROM ProductMargin;View Code