0% found this document useful (0 votes)
17 views4 pages

SQL Data Integration Assignment Guide

The Module 4 assignment focuses on practicing SQL statements for data integration using Oracle and PostgreSQL. It includes various problems that require writing MERGE, INSERT ALL, and INSERT FIRST statements to manipulate data across multiple tables. Students are encouraged to use both database management systems and justify their solutions with execution results where applicable.

Uploaded by

Senti singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

SQL Data Integration Assignment Guide

The Module 4 assignment focuses on practicing SQL statements for data integration using Oracle and PostgreSQL. It includes various problems that require writing MERGE, INSERT ALL, and INSERT FIRST statements to manipulate data across multiple tables. Students are encouraged to use both database management systems and justify their solutions with execution results where applicable.

Uploaded by

Senti singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module 4 Assignment

SQL Statements for Data Integration


The Module 4 assignment provides practice with SQL statements for data integration. A

file in the same website item contains table definitions for the problems. Problems 1 to 9 involve

Oracle SQL for the MERGE and multiple table INSERT statements. Problems 10 to 12 involve

the INSERT … ON CONFLICT statement for PostgreSQL. You are encouraged to use both

DBMSs to complete the assignment. If you only have one DBMS installed, you should provide

your best solution without execution for statements using the other DBMS.

1. Write an Oracle SQL MERGE statement to combine the SSItem dimension table and the

SSItemChanges1 change table. The associated document contains CREATE TABLE and

INSERT statements for both tables. Each matching row of SSItemChanges1 contains values

for both modified and non-modified columns.

2. Write an Oracle SQL MERGE statement to combine the SSItem dimension table and the

SSItemChanges2 change table. The associated document contains CREATE TABLE and

INSERT statements for both tables. Each matching row of SSItemChanges2 contains new

values for modified columns and null values for non-modified columns.

3. Write an Oracle SQL INSERT ALL statement to insert rows of the ProductSale1 table into

four tables (ProductSales2018, ProductSales2019, ProductSales2020, and

ProductSales2021). The associated document contains CREATE TABLE and INSERT

statements for all tables. Note that the target tables lack the SalesYear column. If SalesYear

equals 2018, insert a row into ProductSales2018. The comparisons for the other tables only

differ on the SalesYear value and suffix in the name of the target table. You should compute

the SalesAmt column in each target table as the sum of the quarter sales (Qtr1, Qtr2, Qtr3,

and Qtr4) in the corresponding row of the ProductSale table.


12/10/2022 Module 4 Assignment on SQL Statements for Data Integration Page 2

4. On Problem 3, is the number of the rows in the target tables the same when using INSERT

FIRST versus INSERT ALL? Justify your answer by writing and executing an INSERT

FIRST statement.

5. Write an Oracle SQL INSERT FIRST statement to insert rows of the ProductSale2 table into

three target tables (Year_Low_Sales, Year_Mid_Sales, and Year_High_Sales). The associated

document contains CREATE TABLE statements for all tables. Insert a row into

Year_Low_Sales when annual sales (sum of Qtr1, Qtr2, Qtr3, and Qtr4) are less than 4,000.

Insert a row into Year_Mid_Sales when annual sales are greater than or equal 4,000 and less

than 7,000. Insert remaining rows into Year_High_Sales. You should compute the SalesAmt

column in each target table as the sum of the quarter sales (Qtr1, Qtr2, Qtr3, and Qtr4) in the

corresponding row of the ProductSale table.

6. On Problem 5, is the number of the rows in the target tables the same when using INSERT

FIRST versus INSERT ALL? Justify your answer by writing and executing an INSERT ALL

statement.

7. Write an Oracle MERGE statement to combine the Mobile_Bill table with the Mobile_Usage

table matching on CustId. The associated document contains CREATE TABLE statements

for both tables. The Mobile_Bill table contains the most recent bill with the current amount

(CurrentAmt) and past amount (PastAmt). When a match occurs, update the

Mobile_Bill.CurrentAmt column as minutes used (Mobile_Usage.MinutesUsed) times 0.05

and the Mobile_Bill.PastAmt column as the previous current amount

(Mobile_Bill.CurrentAmt) plus the previous past amount (Mobile_Bill.PastAmt). When a

match does not occur, insert a row into the Mobile_Bill table with the customer identifier
12/10/2022 Module 4 Assignment on SQL Statements for Data Integration Page 3

(Mobile_Usage.CustId), minutes used (Mobile_Usage.MinutesUsed) times 0.05, and 0 for

the past amount (Mobile_Bill.PastAmt).

8. Write an Oracle INSERT FIRST statement to insert rows from a mobile customer table

(Mobile_Customer) into three tables (Mobile_Gold, Mobile_Silver, and Mobile_Bronze)

based on a customer’s current revenue amount (Mobile_Customer.CurrentAmt). The

associated document contains CREATE TABLE statements for all tables. If the current

revenue amount is greater than or equal to 150, insert the mobile customer row into the

Mobile_Gold table. If the current revenue amount is greater than or equal to 100, insert the

mobile customer row into the Mobile_Silver table. Otherwise, insert the mobile customer row

into the Mobile_Bronze table.

9. On Problem 8, is the number of the rows in the target tables the same when using INSERT

FIRST versus INSERT ALL? Justify your answer by writing and executing an INSERT ALL

statement.

10. Write a PostgreSQL INSERT …. CONFLICT ON statement for the requirements in Problem

1.

11. Write a PostgreSQL INSERT …. CONFLICT ON statement for the requirements in Problem

2.

12. Modify the solution to Problem 11 to work with NOT NULL constraints on all columns of

SSItem except for ItemId. INSERT statements have been modified in the change table in the

associated document. Text columns (VARCHAR and CHAR) have a value of “NV” for no

value. Numeric columns have a value of 0 for no value. For this problem, you should study

the extended example for the last PostgreSQL slide (slide 11). The extended example is only
12/10/2022 Module 4 Assignment on SQL Statements for Data Integration Page 4

contained the file with Lesson 7 examples. This slide document does not contain this

example.

Common questions

Powered by AI

Understanding table schematics is essential in creating MERGE or INSERT statements in SQL because it ensures that data is accurately aligned with the table structure, preserving data integrity and query correctness. Knowledge of the schematics allows developers to correctly map source columns to target columns, apply appropriate data transformations, and implement conditional logic based on field requirements. It directly affects query outcomes by preventing data type mismatches, missing columns, or redundant data entries, leading to efficient and accurate data integration processes, minimizing the risk of errors during execution .

The primary differences in handling data integration between Oracle SQL and PostgreSQL lie in their strategies for conflict resolution and data synchronization capabilities. Oracle SQL extensively uses MERGE statements that facilitate conditional insert or update operations within a single transaction block, efficiently merging data with existing records. It focuses on matching conditions and updating existing data where applicable. Conversely, PostgreSQL employs the INSERT ... ON CONFLICT statement to resolve unique or primary key constraint issues, allowing for explicit actions like DO UPDATE or DO NOTHING. This provides a flexible mechanism for handling duplicate conflicts during data insertion while maintaining database integrity .

An SQL developer might choose to use the INSERT FIRST statement over INSERT ALL for distributing customer data into tiered tables to efficiently handle hierarchical conditions where each row should only fall into one category. INSERT FIRST allows defining a sequence of conditional logic to place data into the first applicable category, thus preventing redundancy and ensuring each entry is categorized accurately into one tier based on revenue. This approach minimizes database storage usage and ensures clarity in data distribution by allowing each customer to belong to only one of the specified categories like Gold, Silver, or Bronze .

A PostgreSQL user might choose to use a NOT NULL constraint alongside "NV" values in text columns during data integration when there is a need to maintain mandatory data integrity while signaling the absence of meaningful values. This approach is useful in datasets requiring all fields to be populated to fulfill schema requirements while still indicating fields where the actual data is missing or unavailable. Using "NV" can help clear audits and compliance checks by showing complete column population with placeholders that denote explicit absence of real data, allowing for seamless processing without violating nullability constraints .

The primary function of the MERGE statement in Oracle SQL is to perform data integration by synchronizing rows in a target table with rows from a source table based on specific conditions. This allows for conditional INSERT, UPDATE, or DELETE operations in a single SQL statement, effectively merging changes from one table (e.g., change table) into a dimension or fact table. It efficiently handles cases where data needs to be updated if matches occur or new rows have to be inserted if no matches are found .

The calculation for SalesAmt in an Oracle SQL MERGE statement involves updating existing records by combining current values with those from a source table when matches occur. In contrast, an INSERT statement calculates SalesAmt as part of the data insertion process for new entries, often by summing relevant input columns such as quarterly sales amounts before inserting into the target table. The primary difference lies in the update versus initial insertion operation, with MERGE statements often used for integrating and updating existing records based on certain conditions .

The INSERT ALL statement in Oracle SQL allows for the distribution of rows into multiple target tables without evaluating any conditions beyond the basic requirements for insertion. This means every row is considered for insertion into each specified table, based purely on criteria like the sales year. On the other hand, the INSERT FIRST statement evaluates specified conditions in sequence, and once a condition is met, the current row is inserted into the respective table, with no further consideration of subsequent conditions. This means that if a row fits multiple conditions, it will only be inserted into the first applicable table, creating different row distributions compared to INSERT ALL .

When using the INSERT ... ON CONFLICT statement in PostgreSQL, it is crucial to consider the handling of unique constraints that may result in conflicts during data insertion. This statement allows specifying an alternative action (such as DO UPDATE) when a conflict arises. Considerations include determining whether to update existing records or ignore duplicates, understanding which columns will participate in conflict resolution (typically unique or primary key constraints), and ensuring data consistency across tables. Strategic use of this statement can avoid duplicate entries while updating essential data seamlessly, allowing for robust data integration .

When using SQL MERGE statements in Oracle to combine tables with mismatched columns, challenges include handling cases where columns do not align or data types do not match between source and target tables. To address these issues, developers can utilize conversion functions to reconcile data types, establish default values for missing columns, and ensure proper use of conditional logic to direct updates and inserts accurately. Thoroughly understanding the schema and utilizing subscripts or aliases can further mitigate potential discrepancies and allow for a smooth integration process .

Conditional logic plays a pivotal role in differentiating data handling between INSERT FIRST and INSERT ALL in Oracle SQL. In INSERT FIRST, conditions are evaluated sequentially, and rows are inserted into the first table where a condition is met, ensuring each row is processed uniquely according to priorities set by the developer. In contrast, INSERT ALL does not rely on condition ordering; each condition is independently evaluated, and rows may be inserted into multiple targets if criteria are met. This approach allows for multiple table insertions without strict sequential logic impacts, emphasizing both inclusion and exclusion principles .

You might also like