数据库实验五

---(1)-------------------
alter table 结算金额表
add  备注 char(20) ;

select *
from 结算金额表
--(2)-----------------------------------------------
alter table 结算金额表
alter column 施工单位 char(20) not null;
alter table 结算金额表
alter column 年月 varchar(7) not null;
alter table 结算金额表
add primary key(施工单位 ,年月);

TRUNCATE TABLE 结算金额表
insert 
into 结算金额表(施工单位 ,年月, 结算金额)
select 施工单位,convert(varchar(7),结算日期,111),sum(结算金额)
from 作业项目表
group by 施工单位,convert(varchar(7),结算日期,111);
---------------------------------------------------------------
delete 
from 结算金额表;

delete  结算金额表;
--2---------------------------------------------------
--(1)-----------------------------------------------
 alter table 材料费表
alter column 单据号 char(20) not null;

 alter table 材料费表
alter column 物码 char(20) not null;

alter table 材料费表
add   primary key (单据号,物码);
-----------------------------------------------------------------------------------------------------------------
alter table 作业项目表
alter column 单据号 char(20) not null;

alter table 作业项目表
add primary key (单据号);
--------------------------------------------------------------------------------------------------
alter table 单位代码表
alter column 单位代码 char(20) not null;

alter table 单位代码表
add primary key (单位代码);
----------------------------------------------------
alter table 施工单位表
alter column 施工单位名称 char(20) not null;

alter table 施工单位表
add primary key (施工单位名称);
-------------------------------------------------------
alter table 物码表
alter column 物码 char(20) not null;

alter table 物码表
add primary key (物码);
-------------------------------------------------------------
alter table 油水井表
alter column 井号 char(20) not NUll;

alter table 油水井表
add primary key (井号);
---------------------------------------------------------------------
 begin tran
 insert  into  材料费表  values('zy2016001','wm004',100,10);

 insert  into  材料费表  values('zy2016002',NULL,200,10);
 rollback
--(2)------------------------------------------------------------------
alter table 材料费表 
add constraint a1 foreign key (单据号) references 作业项目表(单据号);

alter table 材料费表 
add constraint a2 foreign key (物码) references 物码表(物码);

alter table 作业项目表
add constraint b1 foreign key(预算单位) references 单位代码表(单位代码);


alter table 作业项目表
add constraint b2 foreign key(井号) references 油水井表(井号);


alter table 作业项目表
add constraint b3 foreign key(施工单位) references 施工单位表(施工单位名称);

-----------------------------------------------------------------------------------------
insert into 油水井表
values ('y007','油井','112203002');

insert  into  材料费表  
values('zy2016007','wm006',100,10);

begin tran 

update 作业项目表
set 施工单位 = '作业公司作业五队'
where 单据号 = 'zy2016001';

delete 
from  单位代码表
where 单位代码 = '112202002';

update 物码表
set 物码 ='wm04'
where 名称规格 = '材料四';
rollback tran
--(3)-------------------------------
alter table 单位代码表
add constraint aa1 check (单位名称  is not null );

alter table 单位代码表
add constraint uni unique(单位代码);

alter table 油水井表
add constraint aa2 check(井别 in ('油井','水井'));

alter table 油水井表
alter column 单位代码 char(20) not null;

alter table 物码表
alter  column 名称规格 char(20) not null;

alter table 物码表
add constraint uni2 unique(名称规格);

alter table 物码表
alter  column 计量单位 char(20) not null;

alter table 材料费表
alter column 消耗数量 int not null ;

alter table 材料费表
alter column 单价 int not null ;

alter table 作业项目表
add constraint asdaf check (材料费+人工费+设备费+其他费用 = 结算金额);

--3------------------------------------------------------------------------------------
--(1)--------------------------------------------
create view 视图一
as
select 作业项目表.*,物码,消耗数量,单价
from 作业项目表,材料费表
where 作业项目表.单据号  = 材料费表.单据号;
--(2)------------
select 结算金额,消耗数量
from 视图一
where 物码 = 'wm003';

select 预算单位,施工内容
from 视图一
where 单据号 =  'zy2016001' and 物码 = 'wm001';
--(3)-------------------------
create view 预算状态
as
select 单据号,预算单位,井号,预算金额,预算人,预算日期
from 作业项目表

begin tran
insert
into 预算状态
values ('zy2016008','112202002','y005',¥10000,'张三', '2016-07-02');
rollback tran

猜你喜欢

转载自blog.csdn.net/qq_38717135/article/details/80048558