0% found this document useful (0 votes)
46 views3 pages

PL/SQL Programs for Employee Data Management

Program 3 is not described, as the document does not provide any details about what this program is intended to do.

Uploaded by

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

PL/SQL Programs for Employee Data Management

Program 3 is not described, as the document does not provide any details about what this program is intended to do.

Uploaded by

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

PROGRAM 1: Write a PL/SQL program using for loop to insert ten rows into a data base table

SQL> -- create demo table


SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL primary key,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /

Table created.

SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /

no rows selected

SQL>
SQL>
SQL>
SQL>
SQL> BEGIN
2 FOR v_LoopCounter IN 1..10 LOOP
3 INSERT INTO employee (id)
4 VALUES (v_LoopCounter);
5 END LOOP;
6 END;
7 /

PL/SQL procedure successfully completed.

SQL>
SQL> select * from employee;
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY
DESCRIPTION
---- -------------------- -------------------- --------- --------- ---------- ----------
---------------
1
2
3
4
5
6
7
8
9
10

10 rows selected.

SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /

Table dropped.

SQL>
SQL>

PROGRAM 2: GIVEN A TABLE EMPLOYEE ( EMPNO, NAME, SALARY,


DESIGNATION, DEPTID), WRITE A CURSOR TO SELECT FIVE HIGHEST PAID
EMPLOYEES FROM THE TABLE

SQL> SELECT ename, empno, sal FROM emp ORDER BY sal DESC;

ENAME EMPNO SAL


---------- ----------- --------
KING 7839 5000
SCOTT 7788 3000
FORD 7902 3000
JONES 7566 2975
BLAKE 7698 2850
CLARK 7782 2450
ALLEN 7499 1600
TURNER 7844 1500
MILLER 7934 1300
WARD 7521 1250
MARTIN 7654 1250
ADAMS 7876 1100
JAMES 7900 950
SMITH 7369 800

DECLARE
CURSOR c1 is
SELECT ename, empno, sal FROM emp
ORDER BY sal DESC; -- start with highest paid employee
my_ename CHAR(10);
my_empno NUMBER(4);
my_sal NUMBER(7,2);
BEGIN
OPEN c1;
FOR i IN 1..5 LOOP
FETCH c1 INTO my_ename, my_empno, my_sal;
EXIT WHEN c1%NOTFOUND; /* in case the number requested */
/* is more than the total */
/* number of employees */
INSERT INTO temp VALUES (my_sal, my_empno, my_ename);
COMMIT;
END LOOP;
CLOSE c1;
END;

Output Table
SQL> SELECT * FROM temp ORDER BY col1 DESC;

COL1 COL2 MESSAGE


--------- -------- -------
5000 7839 KING
3000 7902 FORD
3000 7788 SCOTT
2975 7566 JONES
2850 7698 BLAKE

PROGRAM 3:

Common questions

Powered by AI

In Source 1, the cursor is used to retrieve data in a controlled manner, allowing the program to process each row sequentially until the top five highest salaries are fetched. The cursor defines a SELECT query that orders employees by their salaries in descending order. This ensures that only the highest-paid employees are processed by iterating over the first five rows. The use of a cursor for this purpose helps maintain efficiency by minimizing data retrieval and processing overhead, as the query filtering happens at the database level, reducing the amount of data transferred to the PL/SQL block for further processing .

Primary key constraints ensure data integrity by enforcing uniqueness for each row in the database, which prevents duplicate data entries. In the PL/SQL programs from Source 1, defining 'id' as the primary key guarantees that each insertion operation contributes a unique identifier to the Employee table. This, in turn, supports reliable data retrieval and prevents anomalies that can arise from duplicate rows, such as redundancy or inaccurate query results. Additionally, primary keys enhance referential enforcement in relational databases, supporting constrained operations across different tables .

Loop constructs in PL/SQL, such as the FOR loop in Source 1, provide significant flexibility and power in database manipulation by allowing repeated execution of code blocks, thus enabling bulk operations like inserting multiple rows. This reduces manual coding effort and minimizes procedural overhead when compared to singular, repetitive SQL execution. Furthermore, loops enable complex logic to be embedded within each iteration, facilitating sophisticated data manipulation like conditional checks or computations before insertion. The iterative nature of loops streamlines the process of handling large datasets effectively in both data management and automation within transactional applications .

The execution of the PL/SQL block in Source 1 demonstrates transactional consistency and integrity by ensuring that each iteration of the loop inserts a unique 'id' into the Employee table. PL/SQL transactions are atomic, meaning that each INSERT operation is an all-or-nothing execution, preserving the integrity of the database across multiple operations. Through the use of primary keys, potential issues such as duplicate entries are avoided, maintaining a state where database rules—like uniqueness constraints—are constantly upheld. Moreover, any failure in the transaction cycle will prevent partial data modifications, reinforcing consistent states across the database .

The temporary table in the cursor operation serves as a staging area for processing and storing the fetched results, which can then be utilized or manipulated further in the application. In a real-world scenario, such a table can be invaluable for interim data manipulation, aggregation, or transformation before committing to permanent tables. For instance, after processing and inserting the top five highest salaried employees into this table, further analytics could be performed, such as calculating average salaries or generating reports based on the sorted data. This intermediate step allows for more sophisticated operations without directly impacting the original dataset, maintaining overall data integrity .

The PL/SQL program uses a FOR loop to iteratively insert ten rows into the Employee table by assigning integer values 1 through 10 to the 'id' field. Each iteration of the loop executes an INSERT statement adding a new row with the loop variable as the ID. This implies that the table accepts integer values for the 'id' field and is structurally designed to have 'id' as a unique primary key, ensuring no duplicate entries for this field. Any other fields, if not constrained to be non-null, remain NULL unless specified, preserving database integrity by enforcing primary key constraints .

For the successful execution of the FOR loop in the PL/SQL program, the following conditions must be met with respect to the table schema: the 'id' column in the Employee table must accept integer values as its data type or have an implicit conversion from string to integer; the 'id' column must be defined as a primary key to ensure uniqueness across inserted values; and the table must be devoid of any constraints that would violate the integrity of operations performed by the loop (such as NOT NULL constraints on unspecified columns unless they have default values). Additionally, there should be no existing rows with the same 'id' values in the table to avoid primary key constraint violations .

The cursor-based operation offers several benefits over a direct SQL query. It allows more granular control over the data manipulation process by fetching and processing one row at a time, which can be advantageous when handling large datasets or performing complex logic on a per-row basis. Moreover, the cursor enables conditional logic, such as breaking the loop early when the desired number of employees is processed, thus potentially reducing computational resources. This sequential approach also simplifies handling of errors and exceptions during data processing, providing a robust mechanism for safeguarded transactions .

Using the SQL ORDER BY clause with PL/SQL cursors allows for sorting data efficiently at the database level, which optimizes query performance and retrieval time. This ensures that when the cursor fetches rows, they are already in the desired order, which minimizes the need for additional sorting operations in the PL/SQL code. However, a potential pitfall is increased resource utilization on the database server, as sorting large datasets can be computationally intensive. Additionally, if the ORDER BY clause is not supported by proper indexing, it may lead to slower query performance. Therefore, while beneficial in terms of streamlined PL/SQL logic, careful consideration of database performance and indexing strategies must be made .

If the transaction in the cursor operation is not properly committed, the newly inserted rows in the temporary table may not be permanently saved, leading to data that is not retrievable in subsequent operations, ultimately affecting data integrity by causing loss of data updates. Conversely, if an error occurs and the changes are not rolled back, it may leave the database in an inconsistent state, with partial inserts that do not reflect the true dataset. This necessitates proper handling of transaction control commands (COMMIT and ROLLBACK) to ensure data consistency and reflect accurate outcomes of transaction operations as intended .

You might also like