Prime Number From a Table

Write a pl/sql source code to check all data from a student_id column of a student table. After check show all student_id which are prime.


Hits: PL/SQL code with Table/View

SQL> create table studentt
2 (
3 student_id number(10),
4 student_name varchar(20)
5 );

Table created.


SQL> insert into studentt
2 values
3 (5,'aaa');

1 row created.


SQL> declare
2 a number(10);
3 b number(10);
4 c number(10):=1;
5 begin
6 select student_id into a from studentt;
7 for b in 2..a/2 loop
8 if (a mod b)=0 then
9 c:=0;
10 exit;
11 end if;
12 end loop;
13 if(c=1)then
14 dbms_output.put_line('student_id is a prime number');
15 else
16 dbms_output.put_line('student_id is not a prime number');
17 end if;
18 end;
19 /

3 comments:

  1. Name Null? Type
    ----------------------------------------- -------- ----------------------------
    STUDENT_ID NOT NULL NUMBER(11)
    NAME VARCHAR2(15)
    BATCH VARCHAR2(5)
    DEPARTMENT VARCHAR2(11)
    ADDRESS VARCHAR2(15)



    STUDENT_ID NAME BATCH DEPARTMENT ADDRESS
    ---------- --------------- ----- ----------- ---------------
    1 shohan 43rd cse uttara
    2 salman 43rd cse uttara
    3 shuvo 43rd cse uttara
    4 tonny 43rd cse uttara
    5 toma 43rd cse uttara
    6 sojib 43rd cse uttara
    7 shuvo 43rd cse dhanmondi




    create or replace procedure check_prime(n number)
    as
    counter number(5):=0;
    begin
    for counter in 2..n-1 loop
    if mod(n,counter)=0 then
    exit;
    else
    dbms_output.put_line(n);
    end if;
    end loop;
    end;
    /

    declare
    cursor hold_id is
    select student_id from student;
    rem_id student.student_id%type;
    begin
    open hold_id;
    loop
    fetch hold_id into rem_id;
    exit when hold_id%notfound;
    check_prime(rem_id);
    end loop;
    end;
    /

    ReplyDelete
  2. id:201330499

    SQL> create table studentt
    2 (
    3 student_id number(10),
    4 student_name varchar(20)
    5 );

    Table created.


    SQL> insert into studentt
    2 values
    3 (5,'aaa');

    1 row created.


    SQL> declare
    2 a number(10);
    3 b number(10);
    4 c number(10):=1;
    5 begin
    6 select student_id into a from studentt;
    7 for b in 2..a/2 loop
    8 if (a mod b)=0 then
    9 c:=0;
    10 exit;
    11 end if;
    12 end loop;
    13 if(c=1)then
    14 dbms_output.put_line('student_id is a prime number');
    15 else
    16 dbms_output.put_line('student_id is not a prime number');
    17 end if;
    18 end;
    19 /
    student_id is a prime number

    ReplyDelete
  3. Name: Sourav Dab Nath Roll : 201430865

    //create table which name employee
    create table employee
    (
    emp_id number(15)primary key,
    name varchar2(30)
    );

    //create table which name car
    create table car
    (
    emp_id number(15),
    car_id number(15),
    car_name varchar2(30),
    constraint car_fk1_emp foreign key(emp_id)references employee(emp_id)
    );

    //insert values in employee table

    insert into employee values(101,'Sourav Dab Nath');
    insert into employee values(102,'Md. Monirul Islam');
    insert into employee values(103,'Iminul haque');

    //insert values in car table

    insert into car values(101,201,'PAGARU');
    insert into car values(102,202,'COROLLA');
    insert into car values(103,203,'PRADO');

    //select employee and car table

    select *from employee;
    select * from car;

    // Show car id and car name which id no. 102

    select car_id,car_name from car where emp_id=102;

    // Show name and car name from imployee and which emp_id and car.emp_id are same that’s id no 203.

    select name,car_name from employee, car where employee.emp_id=car.emp_id and car_id=203;

    ReplyDelete

Popular Posts