数据库实验

create database company

go

use company

/*员工人事表*/

create table employee

( emp_no char(5) not null primary key,

 emp_name varchar(10) not null,

 sex char(2) not null,

 dept varchar(10) not null,

 title varchar(10)  not null,

 date_hired datetime  not null,

 birthday datetime  null,

 salary int not null,

 telephone varchar(20) null,

 addr varchar(50) null

)


go



/*客户表*/

create table customer

(cust_id char(5) Not null primary key,

cust_name varchar(20) Not null,

addr varchar(40) Not null,

tel_no varchar(20) Not null,

zip char(6) null

)

go

/*销售主表*/

create table sales

(

order_no int Not null primary key,

cust_id char(5) Not null,

sale_id char(5) Not null,

tot_amt numeric(9,2) Not null,

order_date datetime Not null,

invoice_no char(10) not null

)

go


/*销货明细表*/

create table sale_item

(order_no int Not null,

prod_id char(5) Not null,

constraint PK_sale_item  primary key(order_no,prod_id),

qty int Not null,

unit_price numeric(7,2) Not null,

order_date datetime null

)

go


/*产品名称表*/

create table product

(prod_id char(5) Not null primary key,

prod_name varchar(20) Not null

)

go

use company

/*人事表(employee)数据:*/

insert employee values('E0001','王大华','男','业务','经理','1976-10-13','1951-08-01',80000,'0218120440','上海市')

insert employee values('E0003','陈自强','男','会计','科长','1986-09-15','1963-06-09',48000,'0255344441','南京市')

insert employee values('E0014','周小梅','女','业务','职员','1996-03-01','1970-03-28',32000,'0218128079','上海市')

insert employee values('E0009','陈建国','男','管理','科长','1987-04-15','1967-09-01',45000,'0224507863','天津市')

insert employee values('E0017','林光华','男','业务','职员','1995-10-13','1973-08-17',30000,'0218344560','上海市')

insert employee values('E0006','李珠珠','女','管理','经理','1988-01-01','1961-07-12',60000,'0106750321','北京市')

insert employee values('E0002','李庄敬','男','人事','科长','1980-09-15','1958-05-13',80000,'0207180787','广州市')

insert employee values('E0010','王成功','男','信息','职员','1993-02-15','1969-04-15',45000,'0106543475','北京市')

insert employee values('E0013','陈中华','男','业务','职员','1993-02-15','1966-07-01',43000,'0224506541','天津市')

insert employee values('E0008','刘  刚','男','业务','职员','1994-11-01','1968-08-01',40000,'0218128727','上海市')

insert employee values('E0005','李珊珊','女','会计','职员','1990-03-20','1967-04-25',38000,'0218344787','上海市')

insert employee values('E0011','李小蓉','女','人事','职员','1994-11-01','1970-11-18',30000,'0811545412','重庆市')

insert employee values('E0012','蔡文钦','男','制造','厂长','1984-08-15','1960-07-21',50000,'0218120636','上海市')

insert employee values('E0015','张大山','男','制造','职员','1993-12-15','1968-09-23',35000,'0218344472','上海市')

insert employee values('E0007','吴铁雄','男','信息','科长','1989-10-01','1965-04-18',50000,'0277758521','武汉市')

insert employee values('E0016','方美美','女','业务','职员','1992-05-20','1966-06-23',40000,'0218507470','上海市')

insert employee values('E0004','刘中兴','男','制造','经理','1984-05-01','1960-05-23',60000,'0218506110','上海市')

insert employee values('E0019','王仁华','男','信息','经理','1985-09-15','1959-03-24',60000,'0218128091','上海市')

insert employee values('E0020','陈火旺','男','业务','职员','1992-08-01','1964-05-12',40000,'0224120477','天津市')

insert employee values('E0018','林中正','男','管理','总经理','1974-10-01','1953-05-04',100000,'0218120564','上海市')



/*  客户(customer)表数据:*/

insert customer values('C0001','ABC股份有限公司','上海市','电话甲','100')

insert customer values('C0002','客户乙','天津市','电话乙','400')

insert customer values('C0003','客户丙','北京市','电话丙','800')

insert customer values('C0004','客户丁','上海市','电话丁','104')

insert customer values('C0005','客户戊','北京市','电话戊','803')

insert customer values('C0006','客户己','上海市','电话己','103')

insert customer values('C0007','客户庚','上海市','电话庚','102')

select * from customer


/*销售(sales)主表数据:*/

insert sales values (10002,'C0002','E0013',22700.00,'1996-11-10','I000000002')

insert sales values(10003,'C0003','E0014',13960.00,'1996-10-15','I000000003')

insert sales values(10004,'C0003','E0014',33000.00,'1996-11-10','I000000004')

insert sales values (10001,'C0001','E0008',60000.00,'1996-11-10','I000000001')

insert sales values (10007,'C0008','E0008',20000.00,'1996-11-10','I000000008')


/*销售明细表(sale_item)数据:*/

insert sale_item values (10001,'P0001',5 , 2500.00,'1996-10-22')

insert sale_item values (10001,'P0002',3 , 6500.00,'1996-10-22')

insert sale_item values (10001,'P0003',2 , 5300.00,'1996-10-22')

insert sale_item values (10001,'P0004',2 , 1600.00,'1996-10-22')

insert sale_item values (10002,'P0001',3 , 2600.00,'1996-11-10')

insert sale_item values (10002,'P0003',1 , 5300.00,'1996-11-10')

insert sale_item values (10002,'P0008',2 , 4800.00,'1996-11-10')

insert sale_item values (10003,'P0001',4 , 2700.00,'1996-10-15')

insert sale_item values (10003,'P0004',2 , 1580.00,'1996-10-15')




/*产品名称(product)表数据:*/

insert product values ('P0001','16M DRAM')

insert product values ('P0002','14寸显示器')

insert product values ('P0003','1.2GB硬盘')

insert product values ('P0004','3.5寸软驱')

insert product values ('P0005','键盘')

insert product values ('P0006','VGA显示卡')

insert product values ('P0007','网卡')

insert product values ('P0008','Pentium100CPU')

insert product values ('P0009','激光打印机')

insert product values ('P0010','8倍速光驱')

insert product values ('P0011','计算机字典')

insert product values('P0012','9600bits-s调制解调器')

insert product values('P0013','Pentium主板')


/*1、创建一个视图cust_view,该视图只含上海客户信息,即客户号、客户姓名、住址。*/

create view cust_view as select cust_id,cust_name,addr from customer;

/*2、利用cust_view视图添加一条记录数据。(注意:分别查看customer表和该视图的结果。)*/

insert customer values('C1111','Aaaaaaa','上海市','电话甲','100');  

select * from customer ;

select * from cust_view;

/*3、修改cust_view视图,该视图只含上海客户信息,即客户号、客户姓名、住址、电。*/

drop view cust_view

go

create view cust_view as select cust_id,cust_name,addr from customer where addr = '上海市';

/*4、删除视图中所有姓“王”的客户数据。*/

delete from cust_view where cust_name like '王%';

/*5、通过视图修改表内某一客户的姓名。*/

update cust_view set cust_name = '王某某' where cust_name = '客户丁';

/*6、有两个基本表employee和sales,创建一个视图,该视图包含相同业务员的编号、姓名、订单号、销售总金额。*/

create view cust_all as select employee.emp_no,employee.emp_name,sales.order_no,sales.tot_amt from employee,sales where sales.sale_id = employee.emp_no;

/*7、将上述视图中订单号为10001的记录的销售金额改为60000。*/

update cust_all set tot_amt = '6000000' where order_no = '10001';

/*8、给上述视图添加一条记录数据。*/


insert into cust_all(emp_no,emp_name,order_no,tot_amt) values('EEEEEE','QQQQQQQ','10003','3960.00');


/*9、删除上述视图。*/

drop view cust_all;

drop view cust_view;



你可能感兴趣的:(数据库,视图)