第3章 SQL 习题 - 3.13、3.14

3.13 写出对应于图3-18中模式的SQL DDL。在数据类型上做合理的假设,确保声明主码和外码。

可以参考习题3.4.

3.14 考虑图3-18中的保险公司数据库,其中加下划线的是主码。对这个关系数据库构造如下的SQL查询:

a.找出和"John Smith"的车有关的交通事故数量。

在做习题3.4的时候,我们插入自己的数据,基本上是一人一辆车,我们现在找出和“张三”的车有关的交通事故数量吧。

select count(*) from participated where licence in (
	select licence from person natural join owns
	where name = '张三'
);
 count 
-------
     1
(1 row)

b.对事故报告编号为"AR2197"中的车牌是“AABB2000”的车辆损坏保险费用更新到3000美元。

这里事故编号是varchar类型的,我们设计的时候用的是int型的,这样按照我们的数据,把题目修改为更新事故编号为1的车牌为1的费用更新到3000元。

我们先来看看它之前的费用是多少:

select * from participated where (report_number, licence) = (1, 1);
 report_number | licence | driver_id | damage_amount 
---------------+---------+-----------+---------------
             1 |       1 |         1 |          1000
(1 row)

原来费用是1000,现在更新到3000:

update participated set damage_amount = 3000 where (report_number, licence) = (1, 1);

再看看更新后的费用:

 report_number | licence | driver_id | damage_amount 
---------------+---------+-----------+---------------
             1 |       1 |         1 |          3000
(1 row)

猜你喜欢

转载自blog.csdn.net/zhangyingli/article/details/84196381