GaussDB Cloud Database SQL Application Series - View Management

I. Introduction

GaussDB is a high-performance relational database based on cloud computing technology that supports multiple data models and distributed architectures. In GaussDB, view management is a very important function, which can help users manage and query data more conveniently.

Database view management refers to the process of creating, modifying, deleting, querying and other operations on views in the database.

2. Preparatory conditions

Refer to the previous article "GaussDB Cloud Database SQL Application Series - Basic Use"

1. Log in to Huawei cloud database GaussDB

2. Select the corresponding instance and enter the SQL execution interface

3. View syntax and parameters

1. CREATE VIEW syntax format

CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] VIEW view_name [ ( column_name [, ...] ) ]

[ WITH ( {view_option_name [= view_option_value]} [, ... ] ) ]

AS query;

Parameter description

  • OR REPLACE: If the view already exists, redefine it.
  • TEMP | TEMPORARY: Create a temporary view.
  • view_name: The name of the view to be created. Can be decorated with patterns.
  • column_name: An optional list of names to use as the field names for the view. If not given, the field names are taken from the field names in the query.
  • view_option_name[=view_option_value]: This clause specifies an optional parameter for the view. Currently the only parameter supported by view_option_name is security_barrier, which should be used when VIEW tries to provide row-level security.
  • Query: A SELECT or VALUES statement that provides rows and columns for a view.

2. DROP VIEW syntax format

DROP VIEW [ IF EXISTS ] view_name [, ...] [ CASCADE | RESTRICT ];

【Parameter Description】

  • IF EXISTS: Issues a notice instead of throwing an error if the specified view does not exist.
  • view_name: The name of the view to delete.
  • CASCADE | RESTRICT: CASCADE: Cascade delete objects that depend on this view (such as other views). RESTRICT: Refuse to drop this view if any dependent objects exist. This option is the default.

4. Operation example

In the database, view management usually requires the use of SQL statements to operate

1. Create the basic table

1) Create an order table, which contains fields: order number, order date, supplier number, product number, product name, product origin, product quantity, and product purchase price.

--Delete table order_info

DROP TABLE IF EXISTS order_info;

--Create table order_info

CREATE TABLE order_info

(

order_id int PRIMARY KEY

,order_date date not null

,supplier_id int not null

,goods_id char(20) not null

,goods_name char(20) not null

,goods_home varchar(100) not null

,goods_number int not null

,goods_amount int not null

);

2) Create a sales information table, which contains fields: sales number, sales date, product number, product name, product quantity, and product price.

--Delete table sell_list_info

DROP TABLE IF EXISTS sell_list_info;

--Create table sell_list_info

CREATE TABLE sell_list_info

(

sell_id int PRIMARY KEY

,sell_date date not null

,goods_id char(20) not null

,goods_name char(20) not null

,goods_number int not null

,sell_goods_amount int not null

);

3) Create a commodity information management table, where the fields include: commodity number, commodity barcode, commodity name, commodity origin, commodity inventory, commodity purchase price, commodity selling price, supplier number, and warehouse number.

--Delete table goods_info

DROP TABLE IF EXISTS goods_info;

--Create table goods_info

CREATE TABLE goods_info

(

goods_id char(20) PRIMARY KEY

,goods_code varchar(50) not null

,goods_name char(20) not null

,goods_home varchar(100) not null

,goods_number int not null

,purchase_goods_amount int not null

,sell_goods_amount int not null

,supplier_id int not null

,warehouse_id int not null

);

2. Set the view application scenario

Example 1, to facilitate data analysis

Views can be used to aggregate, group, and filter data to facilitate data analysis and report generation.

Scenario: According to the order table, count the total number of products, total price, and average price by product name and order date.

-- delete view

DROP VIEW IF EXISTS order_info_view;

--According to the order table, count the total number of products, total price, and average price according to the product name and order date.

CREATE VIEW order_info_view

AS

SELECT goods_name

,order_date

,SUM(goods_number) AS total_number

,SUM(goods_number * goods_amount) AS total_amount

,AVG(goods_amount) AS avg_amount

FROM order_info

GROUP BY goods_name, order_date;

Example 2, generate monthly and annual reports

Scenario: Based on the sales table, calculate the total sales quantity and total sales of all sales items by year, as well as their corresponding warehouse numbers and supplier numbers.

-- delete view

DROP VIEW IF EXISTS sell_list_sum_view;

--According to the sales table, count the total sales quantity and total sales of all sold commodities by year, as well as their corresponding warehouse numbers and supplier numbers.

CREATE VIEW sell_list_sum_view

AS

SELECT t1.goods_id

,t1.goods_name

,t1.s_year

,t1.total_number

,t1.total_amount

,t2.supplier_id

,t2.warehouse_id

FROM

(SELECT goods_id

,EXTRACT(YEAR FROM sell_date) AS s_year

,goods_name

,SUM(goods_number) AS total_number

,SUM(goods_number * sell_goods_amount) AS total_amount

FROM sell_list_info

GROUP BY goods_id

,EXTRACT(YEAR FROM sell_date)

,goods_name

) t1

LEFT JOIN

(SELECT goods_id

,supplier_id

,warehouse_id

FROM goods_info

) t2

ON t1.goods_id =t2.goods_id

Example 3, to achieve data security

In some cases, applications require access control over data, such as allowing only specific users or roles to access certain data. This kind of control is easily achieved using views.

Scenario: Only a certain user or role is allowed to access certain data (specified columns), for example, only give user zhangsan access to the fields "commodity name, commodity origin, commodity inventory, commodity price".

By creating a view and giving zhangsan access to this view

-- delete view

DROP VIEW IF EXISTS goods_info_view;

--Only allow a certain user or role to access certain data (specify columns)

--For example, give the user zhangsan access to the fields "commodity name, commodity inventory, and commodity price" of the "commodity information management table (without access rights to this table)".

--Give zhangsan permission to access this view

CREATE VIEW goods_info_view

AS

SELECT goods_name

,goods_number

,sell_goods_amount

FROM goods_info;

-- Grant zhangsan query permission

GRANT SELECT ON goods_info_VIEW TO zhangsan;

Example four, simplifying complex queries

When you need to perform a joint query on multiple tables, you can use views to combine these tables into a single query result set, thereby simplifying the query process.

Scenario: Based on the student's course grade table in the previous article, generate a view: obtain the summary data of the student's name, course, grade, and substitute teacher information

-- delete view

DROP VIEW IF EXISTS student_c_view;

--Generate a view based on the student's course grade table in the previous article: get the student's name, course, grade, and substitute teacher's information summary data

CREATE VIEW student_c_view

AS

SELECT t2.sname

,t3.cname

,t1.scgrade

,t3. cteacher

FROM

(SELECT sno

,cno

,scgrade

FROM sc

) t1

LEFT JOIN students t2

ON t1.sno=t2.sno

left join course t3

ON t1.cno=t3.cno;

--View Results

SELECT * FROM student_c_view;

V. Summary

A database view is a virtual table defined by a query statement. A view can be seen as a logical reorganization of a data collection, and users can access the data through the view without having to care about the storage method and location of the data.

GaussDB provides flexible view management functions, including operations such as creating (CREATE), modifying (OR REPLACE), deleting (DROP) and querying (SELEC) views. A view is a virtual table, which is obtained after logical operation of data in one or more basic tables. It can simplify complex data structures, improve query efficiency, and hide the details of basic tables. Users can choose different view types and define view columns according to their own needs to meet different data analysis and query requirements. At the same time, GaussDB also supports permission control on views to ensure data security and privacy.

--Finish

Guess you like

Origin blog.csdn.net/GaussDB/article/details/130941278