0% found this document useful (0 votes)
10 views

Group by Clause in Oracle

The document discusses using merge statements in SQL to update existing records and insert new records into tables. It shows creating tables, inserting initial data, and then using merge to update some product records based on a source query and insert new product records if they do not already exist.

Uploaded by

wajeeha4804
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Group by Clause in Oracle

The document discusses using merge statements in SQL to update existing records and insert new records into tables. It shows creating tables, inserting initial data, and then using merge to update some product records based on a source query and insert new product records if they do not already exist.

Uploaded by

wajeeha4804
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

UNIVERSITY OF ENGINNERING AND

TECHNOLOGY, TAXILA

Subject: DBS LAB


Assignment No # 02
Dated:22 /02/24 (Thursday)

Submitted By:
Wajeeha Islam
22-SE-18
(OMEGA)

Submitted To:

Miss. Rabia Arshad

Department of Software Engineering,


UET TAXILA
Question # 01
(a) Insert following records by creating corresponding tables in your database.
(b) Apply merge and update some records which exist. Also insert some new record using
Merge.

Answer:01:
1. Creating Table of Customer_T, Order_Line_T, PRODUCT_T and Inserting Values.

CREATE TABLE
customer_T(
custoner_id NUMBER(02),
Customer_Name
VARCHAR(15),
Customer_Address
VARCHAR(15),
City Char(20),
State Char(20),
Postal_code Integer
);

ALTER TABLE customer_T


Modify(Customer_Name VARCHAR(50));

-- multiple insertion

INSERT INTO customer_t(custoner_id,customer_name,customer_address,city,state,postal_code)


VALUES (&Customer_ID, '&Customer_Name', '&Customer_Address', '&City', '&State',
&Postal_Code);

CREATE TABLE Order_Line_T(


order_id number,
product_id number,
order_quantity number);
-- multiple insertion
insert into
Order_Line_T ( order_id,product_id,order_quantity)
values (&order_id,&product_id,&order_quantity);

select * from Order_Line_T;

CREATE TABLE PRODUCT_T(


product_id number,
product_description varchar(20),
product_finish char(10),
standard_price number,
product_line_id number);

insert into PRODUCT_T (product_id,


product_description, product_finish, standard_price, product_line_id )
values (&product_id,'&product_description','&product_finish',&standard_price,&product_line_id);
select * from PRODUCT_T;

2. Updating existing record of Product_T


MERGE INTO PRODUCT_T p
USING (
SELECT 4 as product_id,
'Entertainment Center' as
product_description,
'White Ash' as product_finish,
620 as standard_price,
3 as product_line_id
FROM dual
UNION ALL
SELECT 5,
'Writer''s Desk',
'Cherry',
320,
1
FROM dual
) new_data ON (p.product_id = new_data.product_id)
WHEN MATCHED THEN
UPDATE SET p.product_description = new_data.product_description,
p.product_finish = new_data.product_finish,
p.standard_price = new_data.standard_price,
p.product_line_id = new_data.product_line_id;

3. Inserting new record of Product_T


MERGE INTO PRODUCT_T p
USING (
SELECT 7 as product_id, 'Bookshelf' as product_description, 'Oak' as product_finish, 200 as
standard_price, 2 as product_line_id FROM dual
UNION ALL
SELECT 8, 'Dining Table', 'Mahogany', 800, 3 FROM dual
) new_data
ON (p.product_id = new_data.product_id)
WHEN NOT MATCHED THEN
INSERT (product_id, product_description, product_finish, standard_price, product_line_id)
VALUES (new_data.product_id, new_data.product_description, new_data.product_finish,
new_data.standard_price, new_data.product_line_id);

Explanation:

1. Merge Operation on Product_T:


We're conducting a merge operation on the table ‘Product_T’ which involves updating existing
rows or inserting new ones. The operation is guided by new data (`new_data`).
2. Specifying Source Data:
The `new_data` originates from a subquery, which selects values from a source, such as
another table or predefined values.
3. Matching Condition:
We specify how to match rows from the source data (`new_data`) with rows in the
‘Product_T’ table, using the `product_id` column.
4. Update Action (When Matced):
When a match is found based on the ` product_id `, we update the existing row in
‘product_id’ with the values from `new_data`.
5. Insert Action (When Not Matched):
If no match is found based on the ‘product_id’ , we insert a new row into the ‘Product_T’ table
using the values from `new_data`.
Each operation updates or inserts rows into the `PRODUCT_T` table based on matching or
non-matching rows with the source data (`new_data`). Each merge operation operates
independently on its respective table.

You might also like