google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: RDBMS example for one to many relationship

Wednesday, April 1, 2015

RDBMS example for one to many relationship

Q.).
         Customer (cno,cname,city)
         Account (ano,acc_type,balance)

          Relationships between  customer and account  is one-to-many.
 
         Constraints :-primary key,
                                 Balance should be>100

SQL> create table customer2
  2  ( cno number(5)primary key,
  3    cname varchar2(30),
  4    city varchar2(40)
  5  );
Table created.

SQL> insert into customer2
  2  values('&cno','&cname','&city');
Enter value for cno: 101
Enter value for cname: mahendra
Enter value for city: pali
old   2: values('&cno','&cname','&city')
new   2: values('101','mahendra','pali')
1 row created.
SQL> select * from customer2;

  CNO   CNAME   CITY
--------   -----------  ------------------
  101 mahendra       pali
  102 babulal          pune
 103 amit              mumbai

 CNO   CNAME   CITY
--------   -----------  ------------------
 104        raju                 pune
105          akash          Ajmer

SQL> create table account
  2  ( ano number(5)primary key,
  3    acc_type varchar2(20),
  4    balance number(5) check(balance>100),
  5    cno number(5)references customer2(cno)
  6  );
Table created.

SQL> insert into account
2 values('&ano','&acc_type','&balance','&cno');
SQL> select * from account;

       ANO ACC_TYPE                          BALANCE        CNO
---------- ------------------------------ ---------- ----------
      1201 saving                              13000        101
      1202 current                             20000        102
      1203 saving                              14000        103
      1204 saving                              50000        104
      1205 current                             14000        105
      1206 saving                              25000        104
      1207 current                             19000        105


B). wirte a cursor to add interest of 3% to the balance of all account whose balance is greater than 10000.

  1  declare
  2     cursor c1 is select ano from account
  3     where balance>10000;
  4  begin
  5     for x in c1 loop
  6       update account
  7       set balance=balance+balance*3/100
  8       where ano=x.ano;
  9       commit;
 10     end loop;
 11* end;
SQL> /
PL/SQL procedure successfully completed.

SQL> select * from account;

       ANO ACC_TYPE                          BALANCE        CNO
---------- ------------------------------ ---------- ----------
      1201 saving                              13390        101
      1202 current                             20600        102
      1203 saving                              14420        103
      1204 saving                              51500        104
      1205 current                             14420        105
      1206 saving                              25750        104
      1207 current                             19570        105

7 rows selected.


a). create  or replace a PL/ SQL procedure to find total balance of all customers of pune city.
  1  create or replace procedure dis(x in varchar)
  2  is
  3      b number(20);
  4  begin
  5      select sum(a.balance) into b from customer2 c,
  6      account a where c.cno=a.cno and
  7      c.city=x;
  8        dbms_output.put_line('total balance of all customers of pune city:-'||b);
  9* end;
 10  /

Procedure created.

SQL> declare
  2
  3  begin
  4    dis('pune');
  5  end;
  6  /
total balance of all customers of pune city:-97850

PL/SQL procedure successfully completed.




अच्छे विचार करे विचार

  पहचान की नुमाईश, जरा कम करें... जहाँ भी "मैं" लिखा है, उसे "हम" करें... हमारी "इच्छाओं" से ज़्यादा "सुन...