题目
--创建数据库
create database Stores
use Stores
go
--创建供应商表
create table T_suppliers(
Supp_code varchar(20) primary key,
Supp_name varchar(50)
)
select *from T_suppliers
--创建商品表
create table T_goods(
Goods_id int identity(1,1) primary key,
Goods_Name varchar(50),
Goods_price decimal(10,2),
Supp_code varchar(20),
Goods_address varchar(50),
foreign key(Supp_code) references T_suppliers(Supp_code)
)
--订单表
create table T_orders(
Order_id int identity(1,1) primary key,
Goods_id int ,
Quantity int ,
Total_price money,
foreign key (Goods_id) references T_goods(Goods_id)
)
--添加数据
insert into T_suppliers values
(00001,'戴尔'),
(00002,'ThinkPad'),
(00003,'华硕')
--商品表
insert into T_goods values
('笔记本电脑',5000, '1','中国北京'),
('无线鼠标',200,'2','中国北京'),
('华硕笔记本',6999,'3','中国上海')
--订单表
insert into T_orders values
(2,2,10000),
(3,3,600),
(4,1,6999)
select *from T_orders
-- 将商品名为“华硕笔记本”的价格上调10%;
update T_goods set Goods_price=Goods_price*1.1 where Goods_Name ='华硕笔记本'
-- 查询所有名称包含“笔记本”的商品编号、商品名称、商品价格;
select Goods_id as 商品编号,Goods_Name as 商品名称,Goods_price as 商品价格 from T_goods where Goods_Name like '%笔记本%'
--查询出订购了供应商名称为“戴尔”的商品名称、订购数量、商品总价;(采用简单多表查询方式)
select Goods_Name as 商品名称,Quantity as 订购数量,Total_price as 商品总价
from T_goods ,T_orders,T_suppliers where T_goods.Goods_id=T_orders.Goods_id
and T_goods.Supp_code=T_suppliers.Supp_code and T_suppliers.Supp_name like '%戴尔%'
--
select Goods_Name as 商品名称,T_orders.Quantity as 订购数量,T_orders.Total_price as 商品总价
from T_goods inner join T_orders on T_goods.Goods_id=T_orders.Goods_id inner join T_suppliers on T_goods.Supp_code=T_suppliers.Supp_code
and T_suppliers.Supp_name like '%戴尔%'
--第三章
--采用子查询获取“供应商为戴尔”的商品信息,按照商品价格升序显示商品编号,商品名称,商品价格
select Goods_id as 商品编号,Goods_Name as 商品名称,Goods_price as 商品价格 from T_goods where Supp_code =
(select Supp_code from T_suppliers where Supp_name like '%戴尔%') order by Goods_price ASC
--采用子查询统计“供应商为华硕”的商品编号,商品名称和订单总价格
select T_goods.Goods_id as 商品编号,Goods_Name as 商品名称,Total_price as 订单总价格 from T_goods join T_orders
on T_goods.Goods_id=T_orders.Goods_id join T_suppliers on T_goods.Supp_code=T_suppliers.Supp_code and Goods_Name like '%华硕%'
--查询生产地在“北京” “上海”地区的商品信息时,要求输出商品名称,商品价格和订购数量
select Goods_Name as 商品名称,Goods_price as 商品价格,Quantity as 订购数量 from
T_goods join T_orders on T_goods.Goods_id=T_orders.Goods_id
and Goods_address in (select Goods_address from T_goods where
Goods_address like '%北京%' or Goods_address like '%上海%' )
-- 采用子查询查询所有供应商供应的产品名称
select Goods_Name as 商品名称 from T_goods where exists
(select Supp_code from T_suppliers where T_goods.Supp_code=T_suppliers.Supp_code)
--第四章
--4.1 使用T-SQL查询供应商是戴尔的所有产品名称
declare @supp_name varchar(50)
set @supp_name='戴尔'
select T_goods.Goods_Name