create table 人员表(人员编号,社会保险号,姓名,地址)  --对人员编号做主键、社会保险号做唯一索引
create table 驾驶表(人员编号,汽车编号)              --双主键双外键
create table 汽车表(汽车编号,牌照,生产年月,型号)    --对汽车编号做主键,牌照做唯一索引create table 事故表(事故编号,日期,驾驶员编号,汽车编号,事故损失量)  --对事故编号做主键,驾驶员编号,汽车编号做索引
3、 select count(*) 人员总数 from 事故表 where year(日期)=1998 group by 驾驶员编号
4、 select 姓名 from 人员表 where 人员编号 not in (select 人员编号 from  事故表)
5、 select a.型号,count(*) 总的数量  from 汽车表 a join 事故表 b on a.汽车编号=b.汽车编号  group by a.型号
6、 
    delete 事故表 where 驾驶员编号 in (select 驾驶员编号 from  人员表 where 姓名='John Smith') 
    delete 驾驶表 where 人员编号 in (select 驾驶员编号 from  人员表 where 姓名='John Smith') 
    delete 人员表 where 姓名='John Smith'

解决方案 »

  1.   

    如果你是"每个人可以有多辆汽车"不会出现一辆车多个人的话!
    create table 驾驶表(人员编号,汽车编号) 可以不要!
    其他修改:
    create table 人员表(人员编号,社会保险号,姓名,地址)  --对人员编号做主键、社会保险号做唯一索引
    create table 汽车表(汽车编号,牌照,生产年月,型号,人员编号)    --对汽车编号做主键,牌照做唯一索引create table 事故表(事故编号,日期,驾驶员编号,汽车编号,事故损失量)  --对事故编号做主键,驾驶员编号,汽车编号做索引
    3、 select count(*) 人员总数 from 事故表 where year(日期)=1998 group by 驾驶员编号  ---如果是人次,而不是人数的话要去掉group by
    4、 select 姓名 from 人员表 where 人员编号 not in (select 人员编号 from  事故表)    
    5、 select a.型号,count(*) 总的数量  from 汽车表 a join 事故表 b on a.汽车编号=b.汽车编号  group by a.型号
    6、 
        delete 事故表 where 驾驶员编号 in (select 驾驶员编号 from  人员表 where 姓名='John Smith') 
        update 汽车表 set 人员编号=null where 人员编号 in (select 驾驶员编号 from  人员表 where 姓名='John Smith')
        delete 人员表 where 姓名='John Smith'
      

  2.   

    create table person
    (card varchar(4)not null, ---他这里没用编号而是直接用card当业务主键是可以的只要可以唯一标识一字段就可!但用varchar确不敢恭维
     pname varchar(10)not null,
     address varchar(20),
     primary key(card))
     
    create table car
    (cno varchar(4)not null, --同上
     years datetime,
     levels varchar(2),
     primary key(cno))create table insident --用双业务主键是可以的,
    (times datetime,
     cno varchar(4),
     card varchar(4),
     driver varchar(10), ----driver是画蛇添足 除非可以有多个驾驶人,具体要看业务,
     loss int,
     primary key(times,card),
     foreign key(card) references person(card),
     foreign key(cno) references car(cno))print'找出在1998年其车辆出过车祸的人员总数'
    select distinct count(*) as 车辆出过车祸的人员总数
    from person
    where card in
      (select card
       from  insident
       where times<'1999/01/01'and times>'1998/01/01') ---这样写不是太麻烦了,且好象是times>=...吧!