当前位置:网站首页>Database Experiment four View experiment
Database Experiment four View experiment
2022-04-23 21:45:00 【Hebuter-Rui】
1. The experiment purpose
be familiar with SQL Language about view operation , Be able to use SQL Statement to create the desired view , Define the schema outside the database , And can use the created view to realize data management .
2. The contents and requirements of the experiment
in the light of tpch Database or self built database test Create views and bands with check option The view of , And verify with check option Validity of options . Understand and master the implementation principle of view resolution , Master the difference between updatable view and non updatable view .
3. The experimental steps
(1) Create a “ Hebei Huaxin group ” Supplier supplied parts view Viewpart1, Supplier number is required 、 Name of supplier 、 Part number 、 Part name 、 Quantity available 、 Retail price and supply price .
Source code :
CREATE VIEW Viewpart1 (
suppkey,
sname,
partkey,
pname,
availqty,
retailprice,
supplycost
) AS SELECT
supplier.suppkey,
supplier. NAME,
part.partkey,
part. NAME,
partsupp.availqty,
part.retailprice,
partsupp.supplycost
FROM
supplier,
part,
partsupp
WHERE
supplier. NAME = ' Hebei Huaxin group ' WITH CHECK OPTION;Results screenshots :

(2) Create a view ViewCust1, According to customer Statistics 2020 Total purchase amount of annual orders and total quantity of parts , The customer number is required to be output 、 full name 、 Total purchase amount and total quantity of purchased parts .
Source code :
CREATE VIEW viewcust1 (
custkey,
NAME,
price_sum,
num_sum
) AS SELECT
customer.custkey,
customer. NAME,
SUM(totalprice),
SUM(quantity)
FROM
customer,
lineitem,
orders
WHERE
YEAR (orderdate) = '2020'
AND orders.orderkey = lineitem.orderkey
AND orders.custkey = customer.custkey
GROUP BY
Custkey;Results screenshots :

1) For the view just created , Insert a record , See if it can pass the verification , And explain why .
Source code :
INSERT INTO viewcust1
VALUES
(
'131209',
' The blue sky ',
201312.09,
1111
);result : Can't pass validation , Part of the reason for this function to come from the view , Can't update
2) For the view just created , Query that the total purchase amount exceeds 10 Million customer number 、 full name 、 Total purchase amount and total quantity of purchased parts .
Source code :
SELECT *
FROM viewcust1
WHERE price_sum > 100000;Query results :

3) Create a “ Hebei Iron and Steel Group Co., Ltd ” Supplier supplied parts view Viewpart2, It is required to list the number of supplied parts 、 Supplier No 、 Available quantity and supply price .
Source code :
CREATE VIEW viewpart2 AS SELECT
partkey,
suppkey,
availqty,
supplycost
FROM
partsupp
WHERE
suppkey IN (
SELECT
suppkey
FROM
supplier
WHERE
NAME = ' Hebei Iron and Steel Group Co., Ltd '
);Results screenshots :

Then add... Respectively through this view 、 Delete and modify a “ Hebei Iron and Steel Group Co., Ltd ” Parts supply record .
Add source code :
INSERT INTO viewpart2
VALUES(1209,24706,129,1209);Results screenshots :

Modify the source code :
UPDATE viewpart2
SET supplycost = 1312.09
WHERE partkey = 1209;Results screenshots :

Remove source code :
DELETE
FROM viewpart2
WHERE partkey = 1209;Results screenshots :

(4) Use with check option Create a “ Hebei Iron and Steel Group Co., Ltd ” Supplier supplied parts view Viewpart3, It is required to list the number of supplied parts 、 Supplier No 、 Available quantity and supply price .
Then add... Respectively through this view 、 Delete and modify a “ Hebei Iron and Steel Group Co., Ltd ” Parts supply record , Verify that the view is updatable , And compare with “(3)” Are there any similarities and differences in the experimental results .
Source code :
CREATE VIEW viewpart3 AS SELECT
partkey,
suppkey,
availqty,
supplycost
FROM
partsupp
WHERE
suppkey IN (
SELECT
suppkey
FROM
supplier
WHERE
NAME = ' Hebei Iron and Steel Group Co., Ltd '
) WITH CHECK OPTION;Results screenshots :

Add source code :
INSERT INTO viewpart2
VALUES(1209,24706,129,1209);Results screenshots :

Modify the source code :
UPDATE viewpart2
SET supplycost = 1312.09
WHERE partkey = 1209;Results screenshots :

Remove source code :
DELETE
FROM viewpart2
WHERE partkey = 1209;Results screenshots :

(5) Create customer ordered parts detail view Viewcust2, Customer number is required 、 full name 、 Number of parts purchased 、 amount of money ;
Source code :
CREATE VIEW viewcust2 (
custkey,
NAME,
price_sum,
num_sum
) AS SELECT
customer.custkey,
customer. NAME,
SUM(totalprice),
SUM(quantity)
FROM
customer,
lineitem,
orders
WHERE
orders.orderkey = lineitem.orderkey
AND orders.custkey = customer.custkey
GROUP BY
custkey;Results screenshots :

1) Then create a view based on the view Viewcust3, List the average number of parts purchased by each customer and the average amount , Display customer number 、 full name 、 Average number of parts purchased 、 Average amount ;
Source code :
CREATE VIEW viewcust3 (
custkey,
NAME,
price_avg,
num_avg
) AS SELECT
viewcust2.custkey,
viewcust2. NAME,
(
viewcust2.price_sum / COUNT(orders.orderkey)
),
(
viewcust2.num_sum / COUNT(lineitem.orderkey)
)
FROM
viewcust2,
orders,
lineitem
WHERE
viewcust2.custkey = orders.custkey
AND orders.orderkey = lineitem.orderkey
GROUP BY
viewcust2.custkey;Results screenshots :

2) Delete view Viewcust2.
Source code :
DROP VIEW viewcust2;Results screenshots :

版权声明
本文为[Hebuter-Rui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204200618536915.html
边栏推荐
- Pycharm Chinese plug-in
- Preliminary analysis of Airbase
- How to use the project that created SVN for the first time
- Getting started with detectron2
- Google tries to use rust in Chrome
- Thinkphp5 + data large screen display effect
- This paper solves the cross domain problem of browser
- Opencv reports an error. Expected PTR < CV:: UMAT > for argument '% s'‘
- Online Excel to CSV tool
- CUDA, NVIDIA driver, cudnn download address and version correspondence
猜你喜欢

Preliminary analysis of Airbase

1. Finishing huazi Mianjing -- 1

Problem brushing plan -- dynamic programming (IV)

Arm architecture assembly instructions, registers and some problems

Amazon and epic will be settled, and the Microsoft application mall will be opened to third parties

Deep analysis of C language function

Plato Farm元宇宙IEO上线四大,链上交易颇高

Is rust more suitable for less experienced programmers?

Express③(使用Express编写接口、跨域有关问题)
![[leetcode refers to the two numbers of offer 57. And S (simple)]](/img/c2/7c1434e3c0bbcc6d6364361c3a3ab4.png)
[leetcode refers to the two numbers of offer 57. And S (simple)]
随机推荐
Some thoughts on super in pytorch, combined with code
Express ③ (use express to write interface and cross domain related issues)
Graph traversal - BFS, DFS
JS merge duplicate data in array object
小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
1. Finishing huazi Mianjing -- 1
Sequential state
NVM introduction, NVM download, installation and use (node version management)
Deep analysis of C language function
Introduce structured concurrency and release swift 5.5!
2.整理华子面经--2
Alibaba cloud responded to the disclosure of user registration information
Tensorflow realizes gradient accumulation, and then returns
Assertionerror: invalid device ID and runtimeerror: CUDA error: invalid device ordinal
Online Excel to CSV tool
CUDA, NVIDIA driver, cudnn download address and version correspondence
Arm architecture assembly instructions, registers and some problems
Database experiment I database definition and data import
Oracle intercepts special characters
wait、waitpid