v In SQL, a view is a virtual table based on the dynamic
results returned in response to an SQL statement. Every time a user queries an
SQL view, the database uses the view's SQL statement to recreate the data
specified in the SQL view. The data displayed in an SQL view can be extracted
from one or more database tables.
v An SQL view contains rows and columns, just like a
real database table; the fields in an SQL view are fields from one or more real
tables in the database. You can add SQL functions, for example, WHERE or JOIN
statements, to a view and present the resulting data as if it were coming from
one, single table.
Example: Simple View
Code:
create view "KABIL_PRACTICE"."SUPERSTORE_VIEW"
as
select
"Order_ID",
"Customer_Name",
"Sales"
from
"KABIL_PRACTICE"."SUPERSTORE_SALES";
(OR)
create view "KABIL_PRACTICE"."SUPERSTORE_VIEW" ("O_ID","C_NAME","SALES_PRICE")
as
select
"Order_ID",
"Customer_Name",
"Sales"
from
"KABIL_PRACTICE"."SUPERSTORE_SALES";
Note:
Here I just change the column name
of the view as (O_ID","C_NAME","SALES_PRICE"). Both
are Same.
To Call the View:
Right click the view from the view folder and click
open Data Preview or Open Data Content.
To Drop the View:
Drop view "KABIL_PRACTICE"."SUPERSTORE_VIEW";
Result:
Example: View with Joins
create view "KABIL_PRACTICE"."PAYMENT_DETAILS" as
(
select
c."CUSTOMERNUMBER",
c."CUSTOMERNAME",
p."PAYMENTDATE",
p."AMOUNT"
from
"KABIL_PRACTICE"."CUSTOMER_SAP_STUDENT" as c
inner join
"KABIL_PRACTICE"."PAYMENTS_SAP_STUDENT" as p
on
c."CUSTOMERNUMBER" = p."CUSTOMERNUMBER");
RESULT:
ok
ReplyDelete