创建数据库goods。包括3个表,表字段信息如下:顾客表customers
(1)创建数据库goods。包括3个表,表字段信息如下:
顾客表customers(顾客编号gid,顾客名gname,顾客地址gaddress)
订单表orders(顾客编号gid,商品编号goodsid,订购数量ordercount,订单日期date)
商品表goodsinfo(商品编号goodsid,商品名称goodsname,商品数量goodscount,计量单位units,商品单价goodsprice)
(2)插入以下数据。
在顾客表中插入:
(1,’张三’,’洪山区关山春晓20栋2401’)
(2,’李四’,’洪山区保利时代18栋3001’)
(3,’王五’,’洪山区清江山水1栋504’)
在商品表中插入:
(1,’红薯’,50,’斤’,2)
(2,’动物饼干’,20,’袋’,9.8)
(3,’芹菜’,10,’把’,2)
请写出以下功能对应的sql语句:
顾客张三购买了芹菜3把,需要查询出总共花费多少,以及配送至哪个地址。
(提示:购买芹菜3把,订单表中需要添加记录,商品表中芹菜数量需要更新,在商品表中查询出商品单价*3得到总共花费。在顾客表中可查询出顾客地址。)
--客户表Customers
create table Customers
(
客户ID int primary key,
客户姓名 varchar(20) not null,
客户公司 varchar(20),
库存 varchar(200),
客户电话 varchar(16) check(客户电话 like '123[0-9]{4}')
)
--订单表Orders
create table Orders
(
客户ID int,
产品ID int ,
产品名称 varchar(20)not null,
定货数量 int not null,
总金额 decimal(8,2) not null,
下单时间 datetime not null ,
foreign key (产品ID) references Products(产品ID),
foreign key (客户ID) references Customers(客户ID)
)
--产品表Products
create table Products
(
产品ID int primary key,
产品名称 varchar(20) not null,
产品单价 decimal(5,2),
库存量 int
)
--(1)请根据客户表Customers的结构说明,用T-SQL语言创建该表(已创建)
--(2)查询订单表Orders中下单时间在‘2012-01-01’至’2012-12-31’之间的客户ID,产品名称,定货数量和总金额
select 产品名称,定货数量,总金额 from Orders where 下单时间 like '2012%'
--(3)查询产品表Products中产品单价的最大值、最小值及平均值,并分别使用别名来显示结果
select max(产品单价) as 单价最大值,min(产品单价) as 单价最小值,avg(产品单价) as 单价平均值 from Products
--(4)创建名为v_cus_pro_ord的视图,获取总金额不小于5000的客户ID、客户名称、产品ID、产品名称、产品单价、定货数量、总金额及下单时间
create view v_cus_pro_ord as
select o.客户ID,c.客户姓名,o.产品ID,o.产品名称,p.产品单价,定货数量,总金额,下单时间 from Orders o,Products p,Customers c
where 总金额 >=5000 and o.客户ID=c.客户ID and o.产品ID=p.产品ID