The document outlines various SQL commands and PL/SQL control statements, categorized into DDL, DML, DCL, TCL, and other experiments. Each section provides the aim, syntax, and examples for executing commands such as creating tables, manipulating data, and implementing loops and conditions. Additionally, it includes nested queries, aggregation operations, and cursor implementations in PL/SQL.
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 ratings0% found this document useful (0 votes)
14 views7 pages
DBMS Practical Final Draft
The document outlines various SQL commands and PL/SQL control statements, categorized into DDL, DML, DCL, TCL, and other experiments. Each section provides the aim, syntax, and examples for executing commands such as creating tables, manipulating data, and implementing loops and conditions. Additionally, it includes nested queries, aggregation operations, and cursor implementations in PL/SQL.
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
You are on page 1/ 7
### **1.
DDL Commands (Experiment 1):**
**Aim:** To execute Data Definition Language (DDL) commands like CREATE, ALTER, DROP, TRUNCATE, and RENAME. #### **Syntax:** 1. **CREATE TABLE:** – CREATE TABLE table_name ( column1 datatype, column2 datatype, ... ); Example: CREATE TABLE customer_details ( cust_id varchar(10), cust_name char(20), cust_street varchar(10), cust_city char(10) ) 2. **ALTER TABLE (ADD):** ALTER TABLE table_name ADD (column_name datatype); Example : ALTER TABLE account_details ADD (branch char(10)) 3. **ALTER TABLE (MODIFY):** ALTER TABLE table_name MODIFY (column_name new_datatype); Example: ALTER TABLE account_details MODIFY (account_no numeric(10)); 4. **DROP TABLE:** DROP TABLE table_name; Example: DROP TABLE account_details; 5. **TRUNCATE TABLE:** TRUNCATE TABLE table_name; Example: TRUNCATE TABLE customer_details; 6. **RENAME TABLE:** RENAME old_table_name TO new_table_name; Example: RENAME account_details TO account;
### **2. DML Commands (Experiment 2):**
**Aim:** To execute Data Manipulation Language (DML) commands like INSERT, SELECT, UPDATE, and DELETE. #### **Syntax:** 1. **INSERT INTO:** INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...); Example: INSERT INTO account_details VALUES ('A-102', 'Perryridge', 400); 2. **SELECT: SELECT column1, column2, ... FROM table_name; Example SELECT * FROM loan_details; 3. **UPDATE:** UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; Example UPDATE account_details SET balance = 500 WHERE account_no = 'A-102'; 4. **DELETE:** DELETE FROM table_name WHERE condition; Example: DELETE FROM account_details WHERE account_no = 'A-102'; ### **3. DCL Commands (Experiment 3):** **Aim:** To execute Data Control Language (DCL) commands like GRANT and REVOKE. 1. **GRANT:** GRANT privilege_name ON object_name TO user_name; Example: GRANT CREATE SESSION TO username; 2. **REVOKE:** REVOKE privilege_name ON object_name FROM user_name; Example: REVOKE CREATE TABLE FROM username;
### **4. TCL Commands (Experiment 4):**
**Aim:** To execute Transaction Control Language (TCL) commands like COMMIT, ROLLBACK, and SAVEPOINT. 1. **COMMIT:* COMMIT; 2. **ROLLBACK:** ROLLBACK; 3. **SAVEPOINT:** SAVEPOINT savepoint_name;
### **5. INSERT Command (Experiment 5):**
**Aim:** To insert records into a table using the `INSERT INTO` command. INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...); Example:INSERT INTO account_details VALUES ('A-102', 'Perryridge', 400);
### **6. Nested Queries (Experiment 6a):**
**Aim:** To perform nested queries using `IN`, `NOT IN`, and set comparisons. 1. **IN:** SELECT column_name FROM table_name WHERE column_name IN (SELECT column_name FROM another_table); Example: SELECT DISTINCT customer_name FROM borrower_details WHERE customer_name IN (SELECT customer_name FROM depositor_details); 2. **NOT IN:** SELECT column_name FROM table_name WHERE column_name NOT IN (SELECT column_name FROM another_table); Example SELECT DISTINCT customer_name FROM borrower_details WHERE customer_name NOT IN (SELECT customer_name FROM depositor_details);
**Aim:** To perform aggregation operations like `AVG`, `COUNT`, `SUM`, `MIN`, and `MAX`. 1. **AVG:* SELECT AVG(column_name) FROM table_name; Example: SELECT AVG(balance) FROM account_details; 2. **COUNT:** SELECT COUNT(column_name) FROM table_name; Example: SELECT COUNT(*) FROM customer_details; 3. **SUM:** SELECT SUM(column_name) FROM table_name; Example: SELECT SUM(balance) FROM account_details; 4. **MIN:** SELECT MIN(column_name) FROM table_name; 5. **MAX:** SELECT MAX(column_name) FROM table_name ### **8. GROUPING & ORDERING (Experiment 6c):** **Aim:** To group and order data using `GROUP BY` and `ORDER BY`. 1. **GROUP BY:* SELECT column_name, aggregate_function(column_name) FROM table_name GROUP BY column_name; Example: SELECT branch_name, AVG(balance) AS avg_balance FROM account_details GROUP BY branch_name; 2. **ORDER BY:** SELECT column_name FROM table_name ORDER BY column_name ASC|DESC; Example: SELECT amount FROM loan_details ORDER BY amount DESC;
### **9. SELECT, FROM, WHERE (Experiment 7):**
**Aim:** To retrieve data using `SELECT`, `FROM`, and `WHERE` clauses. SELECT column1, column2, ... FROM table_name WHERE condition; Example:SELECT customer_name, loan_no, amount FROM loan_details WHERE branch_name = 'Perryridge';
### **10. SET Operations (Experiment 8):**
**Aim:** To perform set operations like `UNION`, `UNION ALL`, `INTERSECT`, and `EXCEPT`. 1. **UNION:** SELECT column_name FROM table1 UNION SELECT column_name FROM table2; 2. **UNION ALL:** SELECT column_name FROM table1 UNION ALL SELECT column_name FROM table2; 3. **INTERSECT:** SELECT column_name FROM table1 INTERSECT SELECT column_name FROM table2; 4. **EXCEPT:** SELECT column_name FROM table1 EXCEPT SELECT column_name FROM table2;
#### **11. Modification of the Database (Experiment 11):**
- **Aim:** To modify the database using `INSERT`, `UPDATE`, and `DELETE` commands. - **Algorithm:** 1. Connect to the database. 2. Execute `INSERT`, `UPDATE`, and `DELETE` queries. 3. Commit the changes. - **Code:** INSERT INTO users (name, email) VALUES ('John', '[email protected]'); UPDATE users SET email = '[email protected]' WHERE id = 2; DELETE FROM users WHERE id = 3; COMMIT;
#### **12. Control Statement (For Loop) (Experiment 12):**
- **Aim:** To implement a `FOR` loop in PL/SQL. - **Algorithm:** 1. Declare a variable. 2. Use a `FOR` loop to iterate and print values. - **Code:** DECLARE i NUMBER; BEGIN FOR i IN 1..10 LOOP DBMS_OUTPUT.PUT_LINE(i); END LOOP; END;
#### **13. Control Statement (While Loop) (Experiment 13):**
- **Aim:** To implement a `WHILE` loop in PL/SQL. - **Algorithm:** 1. Declare a variable. 2. Use a `WHILE` loop to iterate and print values. - **Code:** DECLARE i NUMBER := 1; BEGIN WHILE i <= 10 LOOP DBMS_OUTPUT.PUT_LINE(i); i := i + 1; END LOOP; END;
#### **14. Control Statement (For Reverse Loop) (Experiment 14):**
- **Aim:** To implement a reverse `FOR` loop in PL/SQL. - **Algorithm:** 1. Declare a variable. 2. Use a reverse `FOR` loop to iterate and print values. - **Code:** DECLARE i NUMBER; BEGIN FOR i IN REVERSE 1..10 LOOP DBMS_OUTPUT.PUT_LINE(i); END LOOP; END;
#### **15. Control Statement (Loop End Loop) (Experiment 15):**
- **Aim:** To implement a basic loop with an exit condition. - **Algorithm:** 1. Declare a variable. 2. Use a loop with an exit condition. - **Code:* DECLARE i NUMBER := 1; BEGIN LOOP DBMS_OUTPUT.PUT_LINE(i); i := i + 1; EXIT WHEN i = 10; END LOOP; END; #### **16. Sum of Even Numbers (Experiment 16):** - **Aim:** To calculate the sum of even numbers using a loop. - **Algorithm:** 1. Declare variables for iteration and sum. 2. Use a loop to iterate through even numbers and calculate the sum. - **Code:** DECLARE i NUMBER := 0; s NUMBER := 0; BEGIN WHILE i < 100 LOOP s := s + i; i := i + 2; END LOOP; DBMS_OUTPUT.PUT_LINE('Sum: ' || s); END;
#### **17. Sum of Odd Numbers (Experiment 17):**
- **Aim:** To calculate the sum of odd numbers using a loop. - **Algorithm:** 1. Declare variables for iteration and sum. 2. Use a loop to iterate through odd numbers and calculate the sum. - **Code:** DECLARE i NUMBER := 1; s NUMBER := 0; BEGIN WHILE i < 100 LOOP s := s + i; i := i + 2; END LOOP; DBMS_OUTPUT.PUT_LINE('Sum: ' || s); END;
#### **18. Series Generation (Experiment 18):**
- **Aim:** To generate a series of numbers using a loop. - **Algorithm:** 1. Declare a variable. 2. Use a loop to generate and print the series. - **Code:** DECLARE i NUMBER := 5; BEGIN WHILE i <= 50 LOOP DBMS_OUTPUT.PUT_LINE(i); i := i + 5; END LOOP; END;
#### **19. Subprogram (Experiment 19):**
- **Aim:** To implement a subprogram (procedure) in PL/SQL. - **Algorithm:** 1. Create a procedure to insert data into a table. 2. Call the procedure. - **Code:** CREATE OR REPLACE PROCEDURE pro1 IS no NUMBER := 215; name VARCHAR2(20) := 'Priya'; tsal NUMBER := 20000; BEGIN INSERT INTO emp1 (eno, ename, tsal) VALUES (no, name, tsal); END; BEGIN pro1; END;
#### **20. Cursor in PL/SQL (Experiment 20):**
- **Aim:** To implement a cursor in PL/SQL to fetch and display data. - **Algorithm:** 1. Declare a cursor to fetch data from a table. 2. Use a loop to fetch and display data. - **Code:** DECLARE CURSOR c1 IS SELECT eno, ename, sal FROM emp1173; no emp1173.eno%TYPE; name emp1173.ename%TYPE; tsal emp1173.sal%TYPE; BEGIN OPEN c1; LOOP FETCH c1 INTO no, name, tsal; EXIT WHEN c1%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Number: ' || no || ', Name: ' || name || ', Salary: ' || tsal); END LOOP; CLOSE c1; END; #### **21. Control Statement (If-Else-End If) (Experiment 21):** - **Aim:** To implement conditional logic using `IF-ELSE` in PL/SQL. - **Algorithm:** 1. Declare a variable. 2. Use `IF-ELSE` to check a condition and perform actions. - **Code:** DECLARE temp NUMBER := 456; BEGIN IF temp < 20000 THEN temp := temp + 200; ELSE temp := temp + 100; END IF; DBMS_OUTPUT.PUT_LINE('New Salary: ' || temp); END;