0% found this document useful (0 votes)
22 views42 pages

Lab 1 To 10 - D2D-09 - Removed

Uploaded by

Meet Savaliya
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)
22 views42 pages

Lab 1 To 10 - D2D-09 - Removed

Uploaded by

Meet Savaliya
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
You are on page 1/ 42

FACULTY OF TECHNOLOGY

Information and communication Technology

Practical 1
Aim: Introduction to RDBMS and APEX Login.

APEX login:
Step 1:https://apex.oracle.com/pls/apex/

Step 2: Click on Request a workspace.

Step 3:Fill all the required details carefully like: First Name: Your name(ex. Mitesh)

Last Name: Your last name (ex. Patel)

Email: Use your institute email address only.

Workspace:name_surname And click on next.

Enrollment no. 92210133009 Batch: TK2/A Page | 3


FACULTY OF TECHNOLOGY
Information and communication Technology

Step 4: Fill survey detail same as below and click on next.

Enrollment no. 92210133009 Batch: TK2/A Page | 4


FACULTY OF TECHNOLOGY
Information and communication Technology

Step 5: Write appropriatetext in textarea and Click on next,

Step 6:Click on checkbox to agree terms and conditions and Click on next.

Enrollment no. 92210133009 Batch: TK2/A Page | 5


FACULTY OF TECHNOLOGY
Information and communication Technology

Step 7:Click on submit request button and Click on next

Step 8: After that you will receive an email on your institute email id.

Enrollment no. 92210133009 Batch: TK2/A Page | 6


FACULTY OF TECHNOLOGY
Information and communication Technology

Step 9: Click on create workspace.

Step 10: Click on Continue to sign in screen.

Enrollment no. 92210133009 Batch: TK2/A Page | 7


FACULTY OF TECHNOLOGY
Information and communication Technology

Step 11: Set your new password and confirm password and click on change password.

Step 12: Click on SQL workshop.

Enrollment no. 92210133009 Batch: TK2/A Page | 8


FACULTY OF TECHNOLOGY
Information and communication Technology

Step 13: Click on SQL Commands.

Step 14: Now you can perform your queries.

Enrollment no. 92210133009 Batch: TK2/A Page | 9


FACULTY OF TECHNOLOGY
Information and communication Technology

Practical 2
Write a program to implement all DDL queries
Create a table ACCOUNT

Column name Data Type Size

acc_no varchar2 5

Name varchar2 30

City varchar2 20

Balance Number 10,2

Loan_taken varchar2 5

Solution :

CREATE TABLE ACCOUNT (


acc_no VARCHAR2(5) PRIMARY KEY,
name VARCHAR2(30) NOT NULL,
city VARCHAR2(20),
balance NUMBER(10, 2),
loan_taken VARCHAR2(5)
);
Output:

Enrollment no. 92210133009 Batch: TK2/A Page | 10


FACULTY OF TECHNOLOGY
Information and communication Technology

Insert the following records.

acc_no Name City Balance loan_taken

A001 Patel Jigar Mehsana 50000 YES

A002 Patel Ramesh Mehsana 50000 YES

A003 Dave Hardik Ahmedabad 75000 NO

A004 Soni Hetal Ahmedabad 100000 NO

A005 Sony Atul Vadodara 100000 YES

Solution :

INSERT INTO ACCOUNT (acc_no, name, city, balance, loan_taken) VALUES


('A001', 'Patel Jigar', 'Mehsana', 50000, 'YES'),
('A002', 'Patel Ramesh', 'Mehsana', 50000, 'YES'),
('A003', 'Dave Hardik', 'Ahmedabad', 75000, 'NO'),
('A004', 'Soni Hetal', 'Ahmedabad', 100000, 'NO'),
('A005', 'Sony Atul', 'Vadodara', 100000, 'YES');
Output

Enrollment no. 92210133009 Batch: TK2/A Page | 11


FACULTY OF TECHNOLOGY
Information and communication Technology

Create a Table LOAN

Column Name Data Type Size

loan_no varchar2 5

acc_no varchar2 5

loan_amt number 10,2

interest_rate number 5,2

loan_date date

remaining_loan number 10,2

Solution :

CREATE TABLE LOAN (

loan_no VARCHAR2(5) PRIMARY KEY,


acc_no VARCHAR2(5) REFERENCES ACCOUNT(acc_no),
loan_amt NUMBER(10, 2),
interest_rate NUMBER(5, 2),
loan_date DATE,
remaining_loan NUMBER(10, 2)
);
Insert the following Records.

Loan_no Acc_no Loan_amt Interest_rate Loan_date Remaining_loan

L001 A001 100000 7 1-jan-04 75000

L002 A002 300000 9 18-may-04 150000

Enrollment no. 92210133009 Batch: TK2/A Page | 12


FACULTY OF TECHNOLOGY
Information and communication Technology

L003 A005 500000 11 15-june-04 300000

Solution :

INSERT INTO LOAN (loan_no, acc_no, loan_amt, interest_rate, loan_date, remaining_loan)


VALUES ('L001', 'A001', 100000, 7, TO_DATE('01-JAN-2004', 'DD-MON-YYYY'), 75000);
INSERT INTO LOAN (loan_no, acc_no, loan_amt, interest_rate, loan_date, remaining_loan)
VALUES ('L002', 'A002', 300000, 9, TO_DATE('18-MAY-2004', 'DD-MON-YYYY'), 150000);
INSERT INTO LOAN (loan_no, acc_no, loan_amt, interest_rate, loan_date, remaining_loan)
VALUES ('L003', 'A005', 500000, 11, TO_DATE('15-JUN-2004', 'DD-MON-YYYY'), 300000);

Output

Create a table INSTALLMENT

Column Name Data Type Size

loan_no varchar2 5

inst_no varchar2 5

inst_Date Date

Amount Number 10,2

Enrollment no. 92210133009 Batch: TK2/A Page | 13


FACULTY OF TECHNOLOGY
Information and communication Technology

Solution :

CREATE TABLE INSTALLMENT (


loan_no VARCHAR2(5),
inst_no VARCHAR2(5),
inst_Date DATE,
Amount NUMBER(10, 2)
);
Insert following Records

Loan_no Inst_no Date Amount

L001 I001 2-Feb-04 15000

L002 I002 18-June-04 20000

L003 I003 15-July-04 20000

Solution :

INSERT INTO INSTALLMENT (loan_no, inst_no, inst_Date, Amount)


VALUES ('L001', 'I001', TO_DATE('02-FEB-2004', 'DD-MON-YYYY'), 15000);
INSERT INTO INSTALLMENT (loan_no, inst_no, inst_Date, Amount)
VALUES ('L002', 'I002', TO_DATE('18-JUN-2004', 'DD-MON-YYYY'), 20000);
INSERT INTO INSTALLMENT (loan_no, inst_no, inst_Date, Amount)
VALUES ('L003', 'I003', TO_DATE('15-JUL-2004', 'DD-MON-YYYY'), 20000);
Output

Enrollment no. 92210133009 Batch: TK2/A Page | 14


FACULTY OF TECHNOLOGY
Information and communication Technology

Create a Table TRANSACTION

Column Name Data Type Size

acc_no Varchar2 5

tr_Date Date

Amt Number 10,2

type_of_tr Char 1

mode_of_pay Varchar2 10

Solution :

CREATE TABLE TRANSACTION (


acc_no VARCHAR2(5),
tr_Date DATE,
Amt NUMBER(10, 2),
type_of_tr CHAR(1),
mode_of_pay VARCHAR2(10)
);

Insert a Following Records

Acc_no Date Amt Type_of_tr Mode_of_pay

A001 3-may-04 10000 D Cash

A002 5-july-04 5000 W Cheque

A003 12-Aug-04 25000 D Cheque

A004 15-may-04 30000 D Cheque

A005 22-oct-04 15000 W Cash

Solution :

INSERT INTO TRANSACTION (acc_no, tr_date, amt, type_of_tr, mode_of_pay) VALUES


('A001', TO_DATE('03-MAY-2004', 'DD-MON-YYYY'), 10000, 'D', 'Cash'),
('A002', TO_DATE('05-JUL-2004', 'DD-MON-YYYY'), 5000, 'W', 'Cheque'),
('A003', TO_DATE('12-AUG-2004', 'DD-MON-YYYY'), 25000, 'D', 'Cheque'),
('A004', TO_DATE('15-MAY-2004', 'DD-MON-YYYY'), 30000, 'D', 'Cheque'),
('A005', TO_DATE('22-OCT-2004', 'DD-MON-YYYY'), 15000, 'W', 'Cash');

Enrollment no. 92210133009 Batch: TK2/A Page | 15


FACULTY OF TECHNOLOGY
Information and communication Technology

List of queries

1. Display all rows and all columns of table Transaction.


2. Display all rows and selected columns of table Installment.
3. Display selected rows and selected columns of table Account.
4. Display selected rows and all columns of table loan.
5. Show the structure of the table loan, account and transaction.

Output
1.

2.

3.

Enrollment no. 92210133009 Batch: TK2/A Page | 16


FACULTY OF TECHNOLOGY
Information and communication Technology

4.

5.

Enrollment no. 92210133009 Batch: TK2/A Page | 17


FACULTY OF TECHNOLOGY
Information and communication Technology

Practical 3
Write a program to implement all DML queries
Table: ACCOUNT.

Insert the following records if you have not inserted in PRACTIAL - 1

Acc_no Name City Balance Loan_taken

A001 Patel Jigar Mehsana 50000 YES

A002 Patel Ramesh Mehsana 50000 Yes

A003 Dave Hardik Ahmedabad 75000 NO

A004 Soni Hetal Ahmedabad 100000 NO

A005 Soni Atul Vadodara 100000 YES

1. Change the name ‘patel jigar’ to ‘patel hiren’.

2. Change the name and city where account number is A005. (new name = ‘kothari nehal’ and
new city = ‘patan’).

Enrollment no. 92210133009 Batch: TK2/A Page | 18


FACULTY OF TECHNOLOGY
Information and communication Technology

3. Display only those records where loan taken status is ‘YES’.

4. Add the new column (address varchar2 (20)) into table ACCOUNT.

5. Create another table ACCOUNT_TEMP (acc_no, name, balance) from table ACCOUNT.

6. Rename the table ACCOUNT to ACCOUNT_MASTER.

Enrollment no. 92210133009 Batch: TK2/A Page | 19


FACULTY OF TECHNOLOGY
Information and communication Technology

7. update the column balance for all the account holders. (Multiply the balance by 2 for each
account holders

8. Describe the structure of table ACCOUNT.

9. Delete the records whose account no is A004.

Enrollment no. 92210133009 Batch: TK2/A Page | 20


FACULTY OF TECHNOLOGY
Information and communication Technology

Table: LOAN.

Insert the following Records if you have not inserted in PRACTICAL-1

Loan_no Acc_no Loan_amt Interest_rate Loan_date Remaining_loan

L001 A001 100000 7 1-jan-04 75000

L002 A002 300000 9 18-may-04 150000

L003 A005 500000 11 15-june-04 300000

1. For each loan holders Add 100000 Rs. Amount into the column loan_amt.

2. for each loan holders Increase the interest rate 2%.

3. Create another table LOAN_TEMP (loan_no, Acc_no, loan_amt, loan_date) from The
table LOAN.

Enrollment no. 92210133009 Batch: TK2/A Page | 21


FACULTY OF TECHNOLOGY
Information and communication Technology

4. Display only those records where loan holder taken a loan in month of January.

5. Modify the structure of table LOAN by adding one column credit_no varchar2

6. Display the Loan amount*2 of table LOAN.

Enrollment no. 92210133009 Batch: TK2/A Page | 22


FACULTY OF TECHNOLOGY
Information and communication Technology

7. Display the records of table LOAN by date wise in ascending order.

8. Display the records of table LOAN by account number wise in descending Order.

Table: INSTALLMENT.

1. Insert following Records if you have not inserted in PRACTICAL-1.


Loan_no Inst_no Inst_Date Amount

L001 I001 2-Feb-04 15000

L002 I002 18-June-04 20000

L003 I003 15-July-04 20000

1. Change the Inst_Date ‘2-Feb-04’ to ’3-Mar-04’.

Enrollment no. 92210133009 Batch: TK2/A Page | 23


FACULTY OF TECHNOLOGY
Information and communication Technology

2. Reduce 5000 amount from all Installment holders.

3. Add the amount 5000 where loan no is ‘L003’ and ‘L002’.

4. Change the column size of 5 to 7 where column name is Loan_no.

5. Decrease the column size 5 to 4 where column name Inst_no.

6. Show the structure of the Table.

Enrollment no. 92210133009 Batch: TK2/A Page | 24


FACULTY OF TECHNOLOGY
Information and communication Technology

7. Change the amount 15000 to 5000 where loan number is L001

8. Perform delete operation. (Delete only particular one record)

9. Only create a structure of table installment1 from table installment.

Table: TRANSACTION.

1. Insert a Following Records if you have not inserted in PRACTICAL-1.

Acc_no Trans_Date Amt Type_of_tr Mode_of_pay

A001 3-may-04 10000 D Cash

Enrollment no. 92210133009 Batch: TK2/A Page | 25


FACULTY OF TECHNOLOGY
Information and communication Technology

A002 5-july-04 5000 W Check

A003 12-Aug-04 25000 D Check

A004 15-may-04 30000 D Check

A005 22-oct-04 15000 W Cash

2. Insert any duplicate value and display all the records without any duplicate
rows.

3. Select all the records in descending order(account number wise).

4. Display amt, date, and type of transaction by date wise.

5. Create another table TRANSACTION_TEMP from this table.

Enrollment no. 92210133009 Batch: TK2/A Page | 26


FACULTY OF TECHNOLOGY
Information and communication Technology

6. Create a another table TRANS_TEMP by change the column name acc_no to


account_no.

7. Delete a table TRANSACTION_TEMP.

8. Rename the table TRANSACTION to TRANS.

9. Only create a structure of table transaction1 from table transaction.

Enrollment no. 92210133009 Batch: TK2/A Page | 27


FACULTY OF TECHNOLOGY
Information and communication Technology

10. Display account number where type of transaction is ‘D’.

Enrollment no. 92210133009 Batch: TK2/A Page | 28


FACULTY OF TECHNOLOGY
Information and communication Technology

Practical 4
Write a program to implement all DCL queries.

-- Create a new user


CREATE USER demo_user IDENTIFIED BY demo_password;

-- Grant permissions to the user


GRANT CONNECT, CREATE SESSION TO demo_user;

-- Grant specific privileges to the user on a table (e.g., EMPLOYEES)


GRANT SELECT, INSERT, UPDATE ON employees TO demo_user;

-- Revoke specific privileges from the user


REVOKE INSERT ON employees FROM demo_user;

-- Grant ALL privileges to the user on a table


GRANT ALL ON employees TO demo_user;

-- Revoke all privileges from the user on a table


REVOKE ALL ON employees FROM demo_user;

-- Drop the user after operations (clean up)


DROP USER demo_user CASCADE;

Enrollment no. 92210133009 Batch: TK2/A Page | 29


FACULTY OF TECHNOLOGY
Information and communication Technology

Practical 5

Write a program to implement all constraints


⚫ NOT NULL
⚫ UNIQUE
⚫ PRIMARY KEY
⚫ FOREIGN KEY
⚫ CHECK
⚫ DEFAULT
⚫ CREATE INDEX

Enrollment no. 92210133009 Batch: TK2/A Page | 30


FACULTY OF TECHNOLOGY
Information and communication Technology

Enrollment no. 92210133009 Batch: TK2/A Page | 31


FACULTY OF TECHNOLOGY
Information and communication Technology

Enrollment no. 92210133009 Batch: TK2/A Page | 32


FACULTY OF TECHNOLOGY
Information and communication Technology
Practical 6
Write a program to implement all aggregate functions. Sample table:
orders
ord_no purch_amt ord_date customer_id salesman_id
---------- ---------- ---------- ----------- -----------
70001 150.5 2012-10-05 3005 5002
70009 270.65 2012-09-10 3001 5005
70002 65.26 2012-10-05 3002 5001
70004 110.5 2012-08-17 3009 5003
70007 948.5 2012-09-10 3005 5002
70005 2400.6 2012-07-27 3007 5001
70008 5760 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.4 2012-10-10 3009 5003
70012 250.45 2012-06-27 3008 5002
70011 75.29 2012-08-17 3003 5007
70013 3045.6 2012-04-25 3002 5001

1. write a SQL query to calculate total purchase amount of all orders.


Return total purchase amount.

2. write a SQL query to calculate the average purchase amount of all


orders. Return average purchase amount.

Enrollment no. 92210133009 Batch: TK2/A Page | 33


FACULTY OF TECHNOLOGY
Information and communication Technology
3. write a SQL query that counts the number of unique salespeople.
Return number of salespeople.

4. write a SQL query to find the maximum purchase amount.

5. write a SQL query to find the minimum purchase amount.

6. write a SQL query to find the highest purchase amount ordered by


each customer. Return customer ID, maximum purchase amount.

Enrollment no. 92210133009 Batch: TK2/A Page | 34


FACULTY OF TECHNOLOGY
Information and communication Technology
7. write a SQL query to determine the highest purchase amount made by
each salesperson on '2012-08-17'. Return salesperson ID, purchase
amount

Practical 7
Write a program to implement all inbuilt functions of SQL

CREATE TABLE sample_data (


id NUMBER PRIMARY KEY,
name VARCHAR2(50),
birth_date DATE,
salary NUMBER(10, 2),
department VARCHAR2(20)
);

INSERT INTO sample_data (id, name, birth_date, salary, department) VALUES


(1, 'John Doe', TO_DATE('1985-05-15', 'YYYY-MM-DD'), 3500.00, 'HR'),
(2, 'Jane Smith', TO_DATE('1990-08-25', 'YYYY-MM-DD'), 4200.00, 'Finance'),
(3, 'Bob Brown', TO_DATE('1978-11-30', 'YYYY-MM-DD'), 5000.00, 'HR'),
(4, 'Alice Green', TO_DATE('1995-01-10', 'YYYY-MM-DD'), 6000.00, 'Marketing');

Enrollment no. 92210133009 Batch: TK2/A Page | 35


FACULTY OF TECHNOLOGY
Information and communication Technology

SELECT
UPPER(name) AS uppercase_name,
LOWER(name) AS lowercase_name,
INITCAP(name) AS initcap_name,
LENGTH(name) AS name_length,
SUBSTR(name, 1, 4) AS substring_name,
CONCAT(name, ' - Employee') AS concat_name
FROM sample_data;

SELECT
salary,
ROUND(salary, 0) AS rounded_salary,
TRUNC(salary, 0) AS truncated_salary,
MOD(salary, 1000) AS mod_salary,
ABS(salary - 5000) AS absolute_diff
FROM sample_data;

Enrollment no. 92210133009 Batch: TK2/A Page | 36


FACULTY OF TECHNOLOGY
Information and communication Technology
SELECT
COUNT(*) AS total_count,
SUM(salary) AS total_salary,
AVG(salary) AS average_salary,
MAX(salary) AS max_salary,
MIN(salary) AS min_salary
FROM sample_data;

Enrollment no. 92210133009 Batch: TK2/A Page | 37


FACULTY OF TECHNOLOGY
Information and communication Technology

Practical 8
Write a program to implement Triggers in SQL.

Create trigger which executed before insert on student table and check value for
email Id whether it is already exist or not if exist then prevent insertion and
display error message.

CREATE TABLE student1 (


student_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY
KEY,
name VARCHAR2(50),
email VARCHAR2(100) UNIQUE,
course VARCHAR2(50)
);

CREATE OR REPLACE TRIGGER trg_check_duplicate_email


BEFORE INSERT ON student
FOR EACH ROW
DECLARE
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count
FROM student
WHERE email = :NEW.email;

IF v_count > 0 THEN


RAISE_APPLICATION_ERROR(-20001, 'Error: Email ID already exists.
Please use a different email ID.');
END IF;
END;

Enrollment no. 92210133009 Batch: TK2/A Page | 38


FACULTY OF TECHNOLOGY
Information and communication Technology

INSERT INTO student (name, email, course) VALUES ('John Doe',


'[email protected]', 'Computer Science');

INSERT INTO student (name, email, course) VALUES ('Jane Smith',


'[email protected]', 'Mathematics');

Enrollment no. 92210133009 Batch: TK2/A Page | 39


FACULTY OF TECHNOLOGY
Information and communication Technology

Practical 9
Write a program to implement Procedures in SQL.

Create procedure which accept two parameter account number and withdraw
amount from account table. Display available balance before and after
withdraw.

CREATE TABLE account (


acc_no VARCHAR2(10) PRIMARY KEY,
balance NUMBER(10, 2)
);

INSERT INTO account (acc_no, balance) VALUES ('A001', 5000.00);


INSERT INTO account (acc_no, balance) VALUES ('A002', 10000.00);
INSERT INTO account (acc_no, balance) VALUES ('A003', 7500.00);

CREATE OR REPLACE PROCEDURE withdraw_amount (


p_acc_no IN account.acc_no%TYPE,
p_withdraw_amt IN NUMBER
)
AS
v_balance_before NUMBER(10, 2);
v_balance_after NUMBER(10, 2);
BEGIN
SELECT balance INTO v_balance_before
FROM account
WHERE acc_no = p_acc_no;

DBMS_OUTPUT.PUT_LINE('Available Balance Before Withdrawal: ' ||


v_balance_before);

Enrollment no. 92210133009 Batch: TK2/A Page | 40


FACULTY OF TECHNOLOGY
Information and communication Technology
IF v_balance_before >= p_withdraw_amt THEN
UPDATE account
SET balance = balance - p_withdraw_amt
WHERE acc_no = p_acc_no;

SELECT balance INTO v_balance_after


FROM account
WHERE acc_no = p_acc_no;

DBMS_OUTPUT.PUT_LINE('Available Balance After Withdrawal: ' ||


v_balance_after);
ELSE
RAISE_APPLICATION_ERROR(-20002, 'Insufficient Balance for
Withdrawal.');
END IF;
END;
/

-- Test the procedure by withdrawing 1500 from account 'A001'


BEGIN
withdraw_amount('A001', 1500);
END;
/

Enrollment no. 92210133009 Batch: TK2/A Page | 41


FACULTY OF TECHNOLOGY
Information and communication Technology

-- Test the procedure by attempting to withdraw 8000 from account 'A003' (should
fail due to insufficient balance)
BEGIN
withdraw_amount('A003', 8000);
END;

Enrollment no. 92210133009 Batch: TK2/A Page | 42


FACULTY OF TECHNOLOGY
Information and communication Technology

Practical 10
Write a program to implement View in SQL.

1)create view on employee table which includes all records of only “marketing”
department.

2)create view on employee table which includes column employee name,salary


and bonus as (10% of salary)

CREATE TABLE employee (


employee_id NUMBER PRIMARY KEY,
employee_name VARCHAR2(50),
department VARCHAR2(50),
salary NUMBER(10, 2)
);

INSERT INTO employee (employee_id, employee_name, department, salary)


VALUES (1, 'Alice Johnson', 'Marketing', 5000.00);
INSERT INTO employee (employee_id, employee_name, department, salary)
VALUES (2, 'Bob Smith', 'Finance', 6000.00);
INSERT INTO employee (employee_id, employee_name, department, salary)
VALUES (3, 'Charlie Brown', 'Marketing', 7000.00);
INSERT INTO employee (employee_id, employee_name, department, salary)
VALUES (4, 'David Wilson', 'IT', 8000.00);
INSERT INTO employee (employee_id, employee_name, department, salary)
VALUES (5, 'Eva Green', 'Marketing', 5500.00);

select * from employee

Enrollment no. 92210133009 Batch: TK2/A Page | 43


FACULTY OF TECHNOLOGY
Information and communication Technology

CREATE OR REPLACE VIEW marketing_employees AS


SELECT *
FROM employee
WHERE department = 'Marketing';

CREATE OR REPLACE VIEW employee_salary_bonus AS


SELECT
employee_name,
salary,
salary * 0.10 AS bonus
FROM employee;

-- Select all records from the marketing_employees view


SELECT * FROM marketing_employees;

-- Select employee name, salary, and bonus from the employee_salary_bonus view
SELECT * FROM employee_salary_bonus;

Enrollment no. 92210133009 Batch: TK2/A Page | 44

You might also like