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

Dbms 3

The document shows SQL commands used to create a database table called Bank with columns for details of bank customers, insert sample data, retrieve data using queries with filters and sorting, update records, find max balance grouped by branch, use a cursor to retrieve a single customer's data, and handle exceptions.

Uploaded by

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

Dbms 3

The document shows SQL commands used to create a database table called Bank with columns for details of bank customers, insert sample data, retrieve data using queries with filters and sorting, update records, find max balance grouped by branch, use a cursor to retrieve a single customer's data, and handle exceptions.

Uploaded by

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

SQL> CREATE TABLE Bank (

2 S_No INT PRIMARY KEY,


3 Cust_Name VARCHAR(100) NOT NULL,
4 Acc_no INT NOT NULL,
5 Balance DECIMAL(10, 2) NOT NULL CHECK (Balance > 1550),
6 Cus_Branch VARCHAR(100) NOT NULL
7 );

Table created.

SQL> INSERT INTO Bank (S_No, Cust_Name, Acc_no, Balance, Cus_Branch)


2 VALUES (1, 'Ramesh', 12378, 100000, 'Adyar');

1 row created.

SQL>
SQL> INSERT INTO Bank (S_No, Cust_Name, Acc_no, Balance, Cus_Branch)
2 VALUES (2, 'Sam', 12367, 152500, 'Mylapore');

1 row created.

SQL>
SQL> INSERT INTO Bank (S_No, Cust_Name, Acc_no, Balance, Cus_Branch)
2 VALUES (3, 'Harish', 12345, 250000, 'Anna Salai');

1 row created.

SQL>
SQL> INSERT INTO Bank (S_No, Cust_Name, Acc_no, Balance, Cus_Branch)
2 VALUES (4, 'Ranga', 67541, 176544, 'LBNagar');

1 row created.

SQL>
SQL> INSERT INTO Bank (S_No, Cust_Name, Acc_no, Balance, Cus_Branch)
2 VALUES (5, 'Joori', 75238, 238761, 'Adyar');

1 row created.

SQL>
SQL> INSERT INTO Bank (S_No, Cust_Name, Acc_no, Balance, Cus_Branch)
2 VALUES (6, 'Tinu', 21315, 2387, 'Gachibowli');

1 row created.

SQL>
SQL> INSERT INTO Bank (S_No, Cust_Name, Acc_no, Balance, Cus_Branch)
2 VALUES (7, 'Laritek', 56437, 23908, 'Mylapore');

1 row created.

SQL> select Cust_Name from Bank


2 where Balance>1550 and Balance<200000;

CUST_NAME
--------------------------------------------------------------------------------
Ramesh
Sam
Ranga
Tinu
Laritek

SQL> select Acc_no ,Cust_Name from bank


2 ORDER BY DESC Balance;
ORDER BY DESC Balance
*
ERROR at line 2:
ORA-00936: missing expression

SQL> ORDER BY Balance DESC;


SP2-0734: unknown command beginning "ORDER BY B..." - rest of line ignored.
SQL> select Acc_no ,Cust_Name from bank
2 ORDER BY Balance DESC;

ACC_NO
----------
CUST_NAME
--------------------------------------------------------------------------------
12345
Harish

75238
Joori

67541
Ranga

ACC_NO
----------
CUST_NAME
--------------------------------------------------------------------------------
12367
Sam

12378
Ramesh

56437
Laritek

ACC_NO
----------
CUST_NAME
--------------------------------------------------------------------------------
21315
Tinu

7 rows selected.

SQL> UPDATE Bank


2 SET Balance=Balance+(Balance-0.005);

7 rows updated.
SQL> select Cust_Name from Bank where Balance=(select max(Balance) from Bank );

CUST_NAME
--------------------------------------------------------------------------------
Harish

SQL> select Balance from Bank where Cus_Branch=(select max(Balance from bank);
select Balance from Bank where Cus_Branch=(select max(Balance from bank)
*
ERROR at line 1:
ORA-00921: unexpected end of SQL command

SQL> select Balance from Bank where Cus_Branch=(select max(Balance from bank)
2 group by Cus_Branch;
select Balance from Bank where Cus_Branch=(select max(Balance from bank)
*
ERROR at line 1:
ORA-00921: unexpected end of SQL command

SQL> select Balance,max(Cus_Branch) from bank


2 group by Cus_Branch;
select Balance,max(Cus_Branch) from bank
*
ERROR at line 1:
ORA-00979: not a GROUP BY expression

SQL> SELECT Cus_Branch, MAX(Balance) AS Highest_Balance


2 FROM Bank
3 GROUP BY Cus_Branch;

CUS_BRANCH
--------------------------------------------------------------------------------
HIGHEST_BALANCE
---------------
Adyar
477522

Anna Salai
500000

LBNagar
353088

CUS_BRANCH
--------------------------------------------------------------------------------
HIGHEST_BALANCE
---------------
Gachibowli
4774

Mylapore
305000

SQL> select * from Bank


2 where Balance>5000 and Balance<=50000;

S_NO
----------
CUST_NAME
--------------------------------------------------------------------------------
ACC_NO BALANCE
---------- ----------
CUS_BRANCH
--------------------------------------------------------------------------------
7
Laritek
56437 47816
Mylapore

SQL> UPDATE Bank


2 SET Balance=Balance-(Balance*0.005);

7 rows updated.

SQL> select * from Bank


2 where Balance>5000 and Balance<=50000;

S_NO
----------
CUST_NAME
--------------------------------------------------------------------------------
ACC_NO BALANCE
---------- ----------
CUS_BRANCH
--------------------------------------------------------------------------------
7
Laritek
56437 47576.92
Mylapore

SQL> DECLARE
2 v_customer_name VARCHAR2(100) := 'Tinu';
3 v_customer_data Bank%ROWTYPE;
4 BEGIN
5 SELECT *
6 INTO v_customer_data
7 FROM Bank
8 WHERE Cust_Name = v_customer_name;
9
10 -- Displaying the customer data
11 DBMS_OUTPUT.PUT_LINE('Customer Name: ' || v_customer_data.Cust_Name);
12 DBMS_OUTPUT.PUT_LINE('Account Number: ' || v_customer_data.Acc_no);
13 DBMS_OUTPUT.PUT_LINE('Balance: ' || v_customer_data.Balance);
14 DBMS_OUTPUT.PUT_LINE('Branch: ' || v_customer_data.Cus_Branch);
15 EXCEPTION
16 WHEN NO_DATA_FOUND THEN
17 DBMS_OUTPUT.PUT_LINE('Customer with name ''' || v_customer_name || ''' not
found.');
18 WHEN OTHERS THEN
19 DBMS_OUTPUT.PUT_LINE('An error occurred while retrieving customer data.');
20 END;
21 /
Customer Name: Tinu
Account Number: 21315
Balance: 4750.13
Branch: Gachibowli

PL/SQL procedure successfully completed.

SQL> spool off;

You might also like