实例:数据库sales
1.客户表(Customer)
客户编号(CusNo) | 姓名(CusName) | 地址(Address) | 电话(Tel) |
C001 | 杨婷 | 北京 | 010-5328953 |
C002 | 李和平 | 上海 | 021-62359651 |
C003 | 叶新 | 成都 | 024-3222781 |
C004 | 冯辰诚 | 上海 | 021-87235965 |
2.产品表(Product)
产品编号(ProNo) | 品名(ProName) | 单价(price) | 库存数量(Stocks) |
P0001 | 液晶电视 | 5600.00 | 800 |
P0002 | 空调 | 2390.00 | 460 |
P0003 | 洗衣机 | 3700.00 | 600 |
P0004 | 电热水器 | 890.00 | 120 |
3.销售表(ProOut)
销售日期(SaleDate) | 客户编号(CusNo) | 产品编号(ProNo) | 销售数量(Quantity) |
2007-10-27 | C001 | P0001 | 3 |
2007-11-06 | C004 | P0003 | 40 |
2007-12-27 | C001 | P0003 | 5 |
2008-3-15 | C002 | P0002 | 12 |
2008-05-02 | C003 | P0002 | 21 |
2008-05-02 | C003 | P0001 | 9 |
2008-09-21 | C004 | P0001 | 30 |
2008-11-21 | C004 | P0001 | 73 |
一、创建一自定义函数sumMoney,要求能够利用该函数计算出销售金额,并进行测试,利用该函数计算出每种产品(ProNo)的销售金额。
1 DELIMITER $$
2 CREATE FUNCTION sumMoney( pno VARCHAR(10)) -- 输入产品编号
3 RETURNS DOUBLE(10,2) -- 返回金额数据类型
4 BEGIN -- 函数体(返回销售金额=产品单价*销售数)
5 RETURN
6 (SELECT SUM(quantity) FROM proout po,product pr WHERE po.prono=pr.prono AND pr.prono=pno GROUP BY po.prono) --销售数
7 *(SELECT pr.price FROM product pr WHERE pr.prono=pno ); --单价
8 END$$
9 DELIMITER ;
10
11 测试:SELECT sumMoney('P0001');
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKsAAAAlCAIAAACyMI7mAAAF9UlEQVR4nO2a3UscVxjG3z+lgc0HhIKr64GALAgiiFBaSFaTZXDExgtvvJEElSyapY6QEPAiEXSqZS666A7FqyWKWxHbRE1ckzRhs1mQBSl0e1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1qpDqsVbfHpbtDOImdS7/ouUeYDZwTrEEFN8V/Igi4ORYkT1PYVnLnxznT47zJ1ovdtqnRmQASPbK1qE20wbQNuIMAABIKifH+dUkPrg6s5MvTF11OtuntJNjx+lddVZMKt6F7F57A8f5mTZrLiGt15pYU54z8o7cGWkHd4dnNWfavN9WxD5ZrSY9P0l/4hDwvvjaj2gCfv2pYTXT9sUqx38oA9dvHNVzhxwCPrz/xY8oAv74bb/xNDfg3GzSHDvg4P7n7fd/+L83WVvff1XH7XEIKH9450c0Ab+/DtXI4hDgP8L/DjdXEATUJSCMZouQgKBHnb8FcNJqGM0QNAF1KTsJCWiioAmgCki+Fgi27CQkoImCIYAsIBEigCk7CSYB6Rg+71h61zV30zEAKUOOZM2MhOee0+SG4HSaAKqABF/j0xqBe9mykwASkI6RV96KjBRLp2PU1aLN3XTMuiIZyUkibnJDfDpDAFlAIkQAU3YSOAJqXI/ddEzKVCkCWNPTzkix9FlNbohPpwmgCkhECGDLToJGwG5akqQY/XjdTcdi6WqVJIBnctviJjfEpzMEkAUkQgQwZSdBIyAjOZc+I9kMOM8F78+dazYWAVQBiQgBbNlJAAlwvgU819jzBxf3Ocw1G+lbgCogESKAKTsJGgHOs71arWYk+tbk3qzkm6Bk8eO2zmBWrV8uiJd+8ekMAWQBiRABTNlJ4Aiwr0HNX76I+5Ux3cmeF0pxk0eA+HSaAKqARIQAtuwkgAQ0bzAEkAUkIn8RYstOQgKaKGgCqAISEQLYspOQgCYKhoB6lJ2EBDRRMG+C9fvvcBhNFKdhhUjAwyKg1jt/GEGIvwFJ8Bv+q1K3dAAAAABJRU5ErkJggg==" alt="" />
二、创建视图viewPro,要求显示每种产品的销售量和销售金额。
1 DELIMITER $$
2 CREATE FUNCTION sumMoney( pno VARCHAR(10)) -- 输入产品编号
3 RETURNS DOUBLE(10,2) -- 返回金额数据类型
4 BEGIN -- 函数体(返回销售金额=产品单价*销售数)
5 RETURN
6 (SELECT SUM(quantity) FROM proout po,product pr WHERE po.prono=pr.prono AND pr.prono=pno GROUP BY po.prono) --销售数
7 *(SELECT pr.price FROM product pr WHERE pr.prono=pno ); --单价
8 END$$
9 DELIMITER ;
10
11 测试:SELECT sumMoney('P0001');
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAT4AAABcCAIAAAB4EU6CAAAQvElEQVR4nO1d72sbRxre/+RauPaO1A5JfF6oKYGDUBApRc7lWic2je1eJchx1ZlLUpImRUHFsttzUY40hTa6a5wv7toWpg0iF+KU/HCCnViOVRsnURtCTOC4QCz7/N33YXdnZ2bf1Y6kXc2uZh5ezPrVzO68M8+zMzuSXinlcvlbCQmJkGDLhKJLt+wF9FN7cqrmg7CdI2zgfgCW7uzty/VYGRuk2duXN+9+LA0Z1TnimLCB+9eZgHTn7lypx6hB+t/8adJ6+vb12JykTSUWSc/iVMKlin7m7ZFFh1cXpxK5fQqBfb8b3tfjVN4nY2ZwqlOJZFxeSnVGU85nyE6OxFsVGq3R1CQ/tjUkcKsHBlqcTmVYJup6rSCao3Tvzl6tx2jpLgzRdlZVtr+1aP371vD3VJm+Phvn+s5C51HUHO75/q2+o0eBKy4MLR79FX2Gs6rqUNg/gxk8ElEMoiC2mewciSgtcVxsk4ltnSPWcWsia54kRVItO9Bi60RFUZhI7xfbGhI4dvJoxE3kqU79KiMRspsimdupzEiFioGU7vzctXqMGqSN4omhHWS37P9g41JUtf6NDu14faL4xYZlH/Tt/wD794StwBcbxRND+6ML+vG5141T7YhOHHvJkPo5ovzCsZcoz8a519VjJzbo0/prIIMzUYIxnfrfaGpWi7dSzNPirS3xAaz8QGKbqclIJ8H17EA0bptgGeerBknXn8Ct8oYmgZDRfS2S0eKt1h0tO5Dg0z/eSLdw73o9Rkt36UvL8vvVPx62jnfsX8BftexwHyq29OXGUnJoxxsTZJmFj94Yyn+5sXS4T3l56KM3bJOLvfzL9hlI/SgJXd1HAxisxTvRBHL78qSWRRqbHElNalmLlFq8tWWbfQ2MsR9kJ4FW7FqcpetX4JhudbPmdqtz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pUtlvUB2dp/1VYvtfxtzKmaXLnntXL86jH4y28MIwzLv8mUwx7FoPuly70xaulTWC8LOvq0oyo6j31UoY5fusIpf2miffn/yOELQHzDwZTDHsWg+6XLvTFq6VNYLy3746w4leu7+nWPbW4/94FAGy5JROUL9/lRFhNbzOuiU0mUCx7EQRLqN7ExaulTWi2qNacFsHqHnemtRUMQCrDZCuWB2A8exaD7pcu9MWrpU1otqrfI2FbZHru+IY/chBXuJ3C5HL4BO5wCDiOBsUzV4LJpPutw7k5YulfWiWnN7c8gnhEW53BnMbSx4B+4HOHcmLV0q60W1VvkjGf4FGIp3hjYDwGBeY8E9cD/AtzNp6VJZL6o1+UHIymhKBrNA2MD9ACxdr7Jk8I4uoBCWwcIG7gfkl/44QFgGCxu4H/Dxq/Yu3/CXkJCoD4R0tyQkJMIGKV0JiVDi/7pYtAwT9A4zAAAAAElFTkSuQmCC" alt="" />
三、创建存储过程p_Pro, 要求能够根据指定的客户编号,统计该客户购买每种产品的产品号、数量。
1 DELIMITER $$
2 CREATE
3 PROCEDURE p_Pro(cno VARCHAR(10)) -- 创建存储过程PROCEDURE,名称 p_Pro,参数名称及参数类型(cno VARCHAR(10))
4 BEGIN
5 SELECT po.prono AS'产品号',SUM(po.quantity)AS'数量'
6 FROM proout po WHERE po.cusno=cno GROUP BY po.prono; -- 存储的内容
7 END$$
8 DELIMITER ;
9
10 测试:CALL p_Pro('C004'); -- 使用CALL关键字
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMQAAAA3CAIAAADcwnKLAAAH7klEQVR4nO2c3WvbVhjGz3+yDcY2XHtkTSIojMKgFEzLsEPWryTEcdcaclFj6AfN2HARJDGdhwv9gDbe1OQirRKbMELwRrOStknjtI1rLyVtAyE0FJZdJEqWe+9CsnS+lBxXsiNbengujl/JUv2eX885Ut8eIDhyZFjFYrFYLAJBECQzJF/UlEvVtGyYBxym7EzGiCUoidmZzPazH+xpLA92MAWmuad/GjGWxP9eXEHd3tXSTgRRj4Xz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0mdc1EGW8+FCZZzshUFanz2IfABG9QyQITdU9LyjRHubexaY5KmJVUHzDF4K2Pd+saHCZ4g0oWmKh7Wu4AUyEWVFZsWos9mA6qD3HpIKARZh3VB0yq0AU4pb+2SZjgDSpZYKLuaVnEXg3ASGlzH/QcYDBoSdURTNrbGCWg0ws4TPAGlSwwUfe0rJckGlIdwcQqHCZ4g0qWl5bUPS3tlkSqHJiQPS1ZYKLuaWm3JFLlwGTOnpZ2SyJVDkxOCYppsjtMZslouZ6jmpUCk95TmyNH5ep/3gupJ/R0ZzwAAAAASUVORK5CYII=" alt="" />
四、创建一个触发器t_Stocks,要求当插入销售表(ProOut)的销售记录时,根据销售数量(Quantity)的变化,能更新产品表(Product)中相应的库存数量 (Stocks)。
这里需要注意的是new和old的用在after和before时有不同,如下:
1 DELIMITER $$
2 CREATE TRIGGER t_Stocks AFTER INSERT
3 ON proout FOR EACH ROW
4 BEGIN
5 DECLARE num INT ; -- 定义变量,关键字DECLARE
6 SET num =
7 (SELECT stocks FROM product WHERE prono = new.prono);
8 IF num < new.quantity
9 THEN SET new.quantity = num ;
10 END IF ; -- IF 条件表达式 THEN 执行语句 END IF;
11 /*假设给的销售数大于库存数,那新的库存数将为负数,这与实际不相符,所以对销售数进行判断,如果大于库存数,重新赋值销售数=库存数*/
12 UPDATE product SET stocks = stocks - new.quantity WHERE prono = new.prono ;
13 END $$
14 DELIMITER ;
运行结果:出现错误 Error Code :1362
Updating of NEW row is not allowed in after trigger
原因是什么呢?是因为:
AFTER是先完成数据的INSERT/UPDATE/DELETE,再触发,触发的语句晚于监视的增删改操作,无法影响前面的INSERT/UPDATE/DELETE动作。
也就是说在AFTER中对new数据进行重新赋值不能影响前面的INSERT/UPDATE/DELETE动作,也就变得没有意义,因此在AFTER中不能对new数据进行 赋值,只能读取。
BEFORE是先完成触发,再进行INSERT/UPDATE/DELETE,触发的语句先于监视的INSERT/UPDATE/DELETE,也就是有机会判断、修改INSERT /UPDATE/DELETE操作,因此对new数据赋值要放在BEFORE中。
修改后语句:
1 DELIMITER $$
2 CREATE TRIGGER t_Stocks BEFORE INSERT
3 ON proout FOR EACH ROW
4 BEGIN
5 DECLARE num INT ;
6 SET num =
7 (SELECT stocks FROM product WHERE prono = new.prono);
8 IF num < new.quantity
9 THEN SET new.quantity = num ;
10 END IF ;
11 UPDATE product SET stocks = stocks - new.quantity WHERE prono = new.prono ;
12 END $$
13 DELIMITER ;
14
15 测试:INSERT INTO proout VALUES('2009-02-35','C002','P0001',900); -- 原来Stocks是800
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUoAAAAkCAIAAABgwuTMAAALQ0lEQVR4nO2c/VMTSRrH+w+5rdu6cr09DFVHcoln3R613rmuWd0quPMFFhdSoqCuZyqr7oIEDMISed3giiiCTkBcCIFFjQhIEiG8CIJKVpdlUY/C486SVcLd/X73w7z19PSECTNAEqbr+0N88kxPP939mX6mgw0IggjIUQiCkKuqKChR0xtKIJFbCIIAZMzDg51SFIC6b3iw87/3c9aykN4IE+0+7vyrqQVR4tFrB4yWA0ZL17mPnjZseF7/7nP7u0+J30w07+h0FCuBRK44eI8MdUsR0n3/GctfywrPyfSrjy+/te0Sol9sqTR8utPw6c6Z7s2vfO//6/bG2Ru/m72pnWnXtZwzKIFErjh43x/ukSIU74dn1rLCczIdKfXc8PrN1X3Xvf6yq0OlDYNZVd4KorPEvKvEvOtn35/mfJtftMXNNMfO3tT+o13dWJKgBBK54uA9NuKRIqT7/u2vEC/nTgAAAGCTM7RLQvBfYYXnZCKpyKnu+87jv9A60jM0ed37Q3fv/SsVGVcqMuZ8W+ZHt72+t2Wu//1X3vjZjk1NtqSoDqQgkZx3IMbkWOY2OzJjgd62sr3EwfvBaK8UoXg/rgpVZ377nlNG/46dul2Hl9AMWRTOVJw4e/d8y72yq0Ou3snTF/vNZ91Wq9VqtfqdH7/0/PHngc2vB+P/2b3x++YPSr7KjuJATCqQWM6wJxPhjszYhAKZPZcqDt6PxnxShHTfwkTtQleSjno6xuv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ZQigfZVaJZiDr2I8rGqmoevPBibSSNP+rzJDwVYLGd5Vnd7YyGrZ/BxPSPc0AVj1JGprmsVmmXrpQHG+HjBMMxuxqgjU13XKjTL1ksDjPHxgmGY3YxRR6a6rlVolq2XBhjj4wXDMLsZo07xn1gjYYyPFwzD7GaMOgLVdWG3QIEC2YN+qkdN2ucLzDQFpoJhdjNGHabIPwDaV/ERuuXPvQAAAABJRU5ErkJggg==" alt="" />
五、在查询的基础上创建一张新表Cus,要求显示客户“C003”在2008年购买的产品号、数量。
MySQL提供的方法和SQL Server的 select (查询) into [新表] from [源表]方法不同,使用的是Create table [表名] as (查询)的方法。
复制整个表为 CREATE TABLE [新表] SELECT * FEOM [源表];
DROP TABLE IF EXISTS cus;
CREATE TABLE cus AS
SELECT po.prono,SUM(po.quantity)
FROM proout po
WHERE po.cusno='c003' AND YEAR(po.saledate)= GROUP BY prono;
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOYAAABKCAIAAAAg1r95AAAKCUlEQVR4nO2d/2sb5x3Hnz8ma4cqhcae9YN+GARKQQyG1MFqNyUR3pChpfXEskKWuDioRHNbjevoNmit4nowEjkxgRrhhtqj7ergNHESLcF11QYTYxg12FLn328/PHfPl889J518z+lOyufN+4fTc4/u7tHndY+ee+55eEgTheorkTkUqn9kmiaZm5vTwj49opZDhauBKcjgSUJ2fW3Zj5tCpNfXlg+/Pt+nBgVBR8cQ2Vs3b/gxiPT/7lzsUyOykTVE9uv1z/wYInv3T31qRDayhsjeubXqxyDSP9b/3KdWIFudSBCqWCIeK1SXC3HrY6Fs74rHsoSQ+AT7fQtx6eP62vK1yVhishJ64Lt2dSJB0sbRvltOaywyRHbj9ud+DJF98Nc+tRPZQjxWqLLg0e1iVqIzbdDwxGPZsh0qtm25mD1y4HuGZqaoOafWUkNk79350o9BpEsnCDkxevXXVu2U+2C2tTnb2pzMEULIsdIHo0lCCPn51c3Z1uZs61P6kRByrPTpbGtz9u65Y4QcS56QEpU5tbsdsmIkl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