You are on page 1of 8

7.

CREATING DATABASE

AIM:

To create database and also to create relationship among database.

COMMAND OVERVIEW:

SYNTAX:
CREATE DATABASE [ database ]
{ USER SYS IDENTIFIED BY password
| USER SYSTEM IDENTIFIED BY password
| CONTROLFILE REUSE
| MAXDATAFILES integer
| MAXINSTANCES integer
| CHARACTER SET charset
| NATIONAL CHARACTER SET charset
| SET DEFAULT
{ BIGFILE | SMALLFILE } TABLESPACE
| database_logging_clauses
| tablespace_clauses
| set_time_zone_clause
};
PROBLEM STATEMENT:

1. Create a new database by the name student_db with required features.

SQL> CREATE DATABASE STUDENT_DB


USER SYS IDENTIFIED BY ORACLE
USER SYSTEM IDENTIFIED BY ORACLE
DATAFILE ‘C:\oracle\oradata\ORA11\SYSTEM01.DBF’ SIZE 325M REUSE AUTOEXTEND ON NEXT
10240K MAXSIZE UNLIMITED
SYSAUX DATAFILE ‘C:\oracle\oradata\ORA11\SYSAUX01.DAT’ SIZE 120M REUSE AUTOEXTEND
ON NEXT 5M MAXSIZE 2048M
DEFAULT TABLESPACE USERS DATAFILE ‘C:\oracle\oradata\ORA11\USERS01.DBF’ SIZE 50M
REUSE AUTOEXTEND ON MAXSIZE UNLIMITED;

RESULT: Thus the database was created with the specified parameters.
8. HIGH LEVEL PROGRAMMING LANGUAGE EXTENSION (PL/SQL BLOCKS)
AIM:
To solve the given problem statements using PL/SQL blocks.

PL/SQL BLOCK:
PL/SQL is a block-structured language, meaning that PL/SQL programs are divided and written in
logical blocks of code. Each block consists of three sub-parts:

1. Declarations: This section starts with the keyword DECLARE. It is an optional section and defines all
variables, cursors, subprograms, and other elements to be used in the program.
2: Executable Commands: This section is enclosed between the keywords BEGIN and END and it is a
mandatory section. It consists of the executable PL/SQL statements of the program. It should have at
least one executable line of code, which may be just a NULL command to indicate that nothing should be
executed.
3: Exception Handling: This section starts with the keyword EXCEPTION. This section is again optional and
contains exception(s) that handle errors in the program.

Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within other PL/SQL
blocks using BEGIN and END.

SYNTAX:
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;

PROBLEM STATEMENTS:
REFERED TABLES:
Employe (emp_id,emp_name,dept_id,basic,hra,da,pf,net);

1. Create a PL/SQL block for inserting values in the Employee table. Only emp_id, emp_name,
department & basic should be received as input while executing the block and for the rest of the fields
the values need to be calculated as given below.

1
Calculations:
HRA=50% OF BASIC
DA=20% OF BASIC
PF=7% OF BASIC
NETPAY=BASIC+DA+HRA-PF

INPUT:

OUTPUT:

2. Create a PL/SQL block for updating records in Employe table where the user should provide the
emp_id and the new basic salary and thus the HRA,DA, PF and NETPAY should get calculated and
updated accordingly.

INPUT:

OUTPUT:
3. Create a PL/SQL block for showing the new netpay after getting new basic salary for a particular
employee. Display the new netpay along the old netpay without updating in the table. Also If no
customer found with the given customer id then show appropriate error message.
INPUT:
4 Create a PL/SQL block to find whether the given number is prime or not
Input:
declare
       num number;
       i number:=1;
       c number:=0;
  begin
        num:=&num;
       for i in 1..num
       loop
          if((mod(num,i))=0)
           then
              c:=c+1;
         end if;
      end loop;
     if(c>2)
     then
         dbms_output.put_line(num||' not a prime');
     else
        dbms_output.put_line(num||' is prime');
     end if;

2
  end;
   /
5. Create a PL/SQL block to find the factorial of a given number
Input:
declare
    n number;
    fac number:=1;
    i number;
 
begin
    n:=&n;
 
    for i in 1..n
    loop
        fac:=fac*i;
    end loop;
 
    dbms_output.put_line('factorial='||fac);
end;
/
6. Create a PL/SQL block to find sum of n numbers
declare
a number (5)=0;
b number (5);
c number (5);
begin
c:=&c;
for b in 1...c loop
a:=a+b;
end loop;
dbms_output.put_line(a);
end ;
/

RESULT:
Thus PL/SQL statements were written for inserting & updating the rows in the EMPLOYE table.

9. PROCEDURES

AIM:

3
To solve the given problem statements using procedures.

PL/SQL PROCEDURES:

An Oracle stored procedure is a program stored in an Oracle database. The following are the
advantages of using procedures.

Better performance: Oracle stored procedures load once into the shared pool and remain there
unless they become paged out. Subsequent executions of the Oracle stored procedure are far faster than
executions of external code.

Coupling of data with behaviour: DBAs can use naming conventions to couple relational tables
with the behaviors associated with a table by using Oracle stored procedures as "methods". If all
behaviors associated with the employee table are prefixed with the table name--employee.hire,
employee.give_raise, for example--the data dictionary can be queries to list all behaviors associated with
a table (select * from dba_objects where owner = 'EMPLOYEE'), and it's easy to identify and reuse code
via stored procedures.

Isolation of code: Since all SQL is moved out of the external programs and into the Oracle stored
procedures, the application programs become nothing more than calls to Oracle stored procedures. As
such, it becomes very simple to swap out one database and swap in another one.

SYNTAX:

CREATE [OR REPLACE] PROCEDURE procedure_name (parameters list)


IS
<declaration_section>

BEGIN
<executable_section>

EXCEPTION
<exception_section>

END;

PROBLEM STATEMENTS:
REFERRED TABLES:

Borrow(acc_no , rollno, date_issue);

1. Write a procedure to insert a record in borrower relation. Before inserting check whether the book
is available or not.

4
OUTPUT:

2. Write a procedure to insert a record in borrower relation with the above constraints and also
ensure that the member has not borrowed more than three books.

OUTPUT:

Result:
Thus PL/SQL Procedures were created to solve the given problem statements.

10.FUNCTIONS

AIM:

5
To solve the given problem statements using PL/SQL functions.

PL/SQL FUNCTION:

A PL/SQL function is same as a procedure except that it returns a value. A standalone function is
created using the CREATE FUNCTION statement.

SYNTAX

CREATE [OR REPLACE] FUNCTION function_name (parameter_name [IN | OUT | IN OUT] type [, ...])

RETURN return_datatype

{IS | AS}

BEGIN

< function_body >

END;

PROBLEM STATEMENT:

REFERRED TABLES:

Transaction (accno number(5), amount number(7,2), trans_type varchar(5),dot date);

1. Create a function to insert the records into the transaction table, after performing each transaction
in the transaction table show the net balance of the particular account.

OUTPUT:

2. Create a function to find the factorial of a given number

3. Develop a function named Customer_Loan which accepts Loanid as input and displays Custid,
CustName and loan_amount.
Customer (Custid, Custname, Age, phno)
Loan (Loanid, Amount, Custid, Emi)

RESULT:
Thus PL/SQL functions were created to solve the given problem statements.

6
7

You might also like