You are on page 1of 27

Codes of Oracle & Summary

report on SQL
401: Database Management

Tamanna Tanjum Tisha

029-12-118

Section B

Department of MIS

University of Dhaka
Terms Of Reference:

The report Codes of Oracle & Summary report on SQL is mainly submitted in fulfillment of the
requirement for the course 401: Database Management, Department of Management Information
Systems(MIS), University of Dhaka. I worked & leant Oracle from my course teacher who is
Honorable Professor Dr. M. Helal Uddin Ahmed. I really appreciate his help on this course for
which I learnt SQL.

2
Summary:

An Oracle database is a collection of data treated as a unit. The purpose of a database is to store
and retrieve related information. A database server is the key to solving the problems of
information management. In general, a server reliably manages a large amount of data in a
multiuser environment so that many users can concurrently access the same data. All this is
accomplished while delivering high performance. A database server also prevents unauthorized
access and provides efficient solutions for failure recovery. In fact it is the summary of what we
learn in the class and in the website that my honorable professor gave few days ago.

3
Table of Content

Serial No. Contents Page No.

1 Introduction 4-5

2 Data Types 5-8

3 SQL Keywords 8-12

4 Constraint & Keys 12- 14

5 Auto Increment 14-16

6 Oracle Function 16-17

7 Joining Tables 17-20

8 Exercise 20-25

4
Introduction
SQL stands for Structured Query language, pronounced as "S-Q-L" or sometimes as "See-Quel".
SQL is the standard language for dealing with Relational Databases. SQL can be used to insert,
search, update and delete database records. SQL can do lots of other operations including
optimizing and maintenance of databases. Relational databases like MySQL Database, Oracle, Ms
SQL server, Sybase, etc uses SQL. In fact,

Bit Byte Field Record File Database

\
Bit: Basic unit of information. It's basically a part of binary digit.
Byte: A byte is consists of 8 bytes. Any data that we enter is counted as byte.

Field: Each column is known as field. We enter data under a specific filed.

Record: We enter data for a specific entity in a row wise. This data is generally known as record.

File: Summation of all record is known as file. A number of files create

5
SQL is used for execute queries, retrieve data, insert records, update and delete records, create new
tables and views of a database. I will use Oracle which is a Relational Database Management
System(RDMS). Oracle is a scripted language for applying SQL in a Database. The data in
RDBMS is stored in database objects called tables. A table is a collection of related data entries
and it consists of columns and rows.

6
SQL Data types

Data types in SQL is very important thing that should be known before leaning SQL syntax. When
we create a table using the syntax in Oracle we have to declare the data type of each column
containing in that table. In the Previous page I create a table where 'Roll' field must have 'number'
data type. Similarly, 'Name' filed must have 'varchar' data type. Data types depend on what kind
of data you want to store in a field. You can't avoid data type in Oracle. So, Data types should be
introduced. There are four types of data type. Those are:

String

Numeric

Date & Time

Other types

Those four types are described in the following section.

7
String
Data Ty pe Descrip tion Size

Char(n) Fixed width c haracter 8000 Character


string

Varchar(n) Variabl 8000 character


e width
characte
r string
Varchar2(n Varia
) ble 1,073,741,824
width characters

charact
er
string
Binary(n) Similau 8000 bytes
r to
t
Char
(n) b
store
s
bina
ry
byte
s
Varbinary(n Similar 8000 bytes
) to
varchar
()

8
but
stores
binary
byte
s
Te It also 65,535 bytes
xt( stores
n) string
values
Longtext(n It can 4,294,967,295
) store
more
value characters
than
text

9
Numeric

Sometimes we need to store numeric value that I did in the introduction title. Numeric data
types are :
Data type Description Storage

Bit Stores integer value s 0,1, null value

Tinybit Stores value 0 to 255 1 byte

Int Stores value 4 bytes

-2,147,483,648 to
2,147,483,648

Decimal (p,s) Its value is 1 to 38 5-17 bytes


And can hold precision number

Numeric (p,s) Fixed precision 5-17 bytes

number. Similar to
Decimal(p,s)

Smallmoney Includes m dataonetary 4 bytes

Money Includes m onetary 8 bytes


data

Float(n) Includes float data 4-8 bytes


The n par
ameter
indicates wh fieldether the hold
should 4 or es.
8 byt

10
Date & Time

This is another data type of Oracle SQL. Date & Time data types are given below:
Data Type Description Storage

Date It stores only date 3


bytes
from January 1, 0001

to December 31, 9999

Time Store a time only to an 3-5


bytes
accuracy of 100
nanoseconds

Datetime From January 1, 1753 8


bytes

to December 31, 9999


with an accuracy of
3.33 milliseconds

Datetime2 From January 1, 0001 to December 31, 9999 6-8


with an accuracy of bytes
100 nanoseconds

Timestamp 3

Stores a unique bytes

number that gets

updated every time a


row gets created or

11
modified.

Other Data Types

There are another kinds of data type that are not included on the previous three type. Those
are:
Data Ty Description
p
e

Xml Stores XML formatted data.

Maximum 2GB
Cursor Stores a reference to a cursor used for database operations

Tabl e

Stores a result-set for later


processing

12
SQL Keywords
SQL keywords for Oracle database are easy and most of those keywords similar to the
other database. SQL keywords are given below: Creating Table:

create table <table name>

(<column name 1> data type,

<column name 2> data type,

...........................................

...........................................

<column name n> data type );

Example: Now I will create a table which name is 'Student_Personal' crate table
Student_Personal
(Roll numeric(3),

Name varchar2(10),

Address varchar2(20),

DOB date);

Inserting Data:

Insert into <table name> values (data 1, data 2, ........ data n); Example: Now I will insert data
into 'Student_Personal'
insert into Student_Personal values (01, 'DM', 'Keraniganj, Dhaka',
to_date('02/04/1998','dd/mm/yyyy'));

Selecting field:

13
select * from <table name>; or select <column name1 .... column name n> from <table
name>; Example: select * from Student_Personal; or select Roll, Name, DOB from
Student_Personal;
Using order by: select * from <table name> order by <column name> asc or desc;
Example: select * from Student_Personal order by Roll asc;

Clone Table Creation: create table


<table name>
as (selecting fields);

Example: create table Student_Personal2 as (select * from


Student_Personal);

Deleting a specific row of a table:

delete from <table name> where column name = condition;

Example: delete from Student_Personal2 where roll = 3; Deleting all rows of


a table: delete <table name>; or truncate table <table name>;
Example: delete Student_Personal2;

Deleting a table:

drop table <table name>;

Example: drop table Student_Personal2;

14
'Update' keyword

'Update' is generally used when we need to update any data in the table. Remember we can
change the record of a table not the field of a table using 'Update'. The syntax is: Update <table
name>

set column name 1 = value, .......column name n = value where = condition ;

Example: update
Student_Personal
set Name = 'Durjay Mondol' where Roll = 1;

Alter Table

'Alter table' keyword is used for adding a new column, modifying the existing column,
dropping a column, rename a column or table.

Adding a new column: alter table


Student_Personal add result
number(1); Modifying existing
column: alter table Student_Personal
modify result varchar(2); Dropping
column: alter table Student_Personal
drop column result;
Rename Column:

alter table Student_Personal

rename column Name to Stu_name;

Rename Table:

alter table Student_Personal

rename to Stu_Per;

15
Comment Keyword

Comment is helpful to the code writer & gives a mark in SQL script. The Syntax is:

/*.....................*/

Oracle console avoid anything that is between comment sign.

Constraint & Keys


Key is a set of attributes of an entity to uniquely identify or making relation. Keys are also
considered as constraint in SQL. keys help to connect and work as a filter in entering data.
There are three kinds of keys:

1. Primary key: the attribute that is used for specially identify a row in a table or making
relation. In fact,

* It deploy the duplication.

* Helps to create relation.

* It can't be empty.

* Maintaining uniqueness.

2. Foreign Key: A key that provides a link between data in two tables . It references the
primary key of the parent table. For making or joining tables foreign Key should be declared.

3. Composite Key: When a key is made of two attributes is known as Composite Key.

Other Constraint are :

1. Not Null: This constraint helps to complementary include data in a specific field.

2. Unique: It helps to include unique data in a specific field.

16
3. Check: It helps to include a pattern on a specific field.

Key structure: (Primary Key) alter table <table name> add constraint <constraint
name> primary key(column name);
Example:

alter table Stu_Per add constraint pk_stuper primary key


(Roll);
(Foreign key)

alter table <table2> add constraint <constraint name> foreign key (column name)
references <table1>(column name);

We need to create a new table for declaring a foreign key. Assume a new Table name
Stu_Marks that includes Roll, Grade & Rank.

create table Stu_Marks (Roll


number(3),
Grade varchar2(3),

Rank varchar2(4)); input of new table - insert into Stu_Marks values (01, 'A+', '1st');
Alter table Stu_Marks
add constraint fk_stumarks foreign key(Roll) references Stu_Per(Roll);

(Other constrain Structure)

Let's remake the Stu_Per table again with some constrains

create table Stu_Per1


(Roll number (3) primary key not null,

Name varchar2(10) check(Name like '_M%'),

Address varchar2(15) unique,

DOB date);

17
Like Keyword

Like keyword can also be used as constrain. Some examples of like are given below:

Select * from Stu_Per where Name like 'D%'; (Here, 'D%' means the name that starts from D).

Select * from Stu_Per where Name like '%M'; (Here, '%M' means the name that ends with m).

Auto Increment

Auto increment use for updating an auto value each time with a unique value that is declared.
By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for
each new record. The auto_increment structure is given below:

create table stu_per3


(Roll number(3) primary key auto_increment,
Name varchar2(10) not null,
Address varchar2(15) unique, Grade
varchar2(3)); alter table stu_per3
auto_increment = 100;

insert into stu_per3(Name, Address, Grade) values ('DM', 'Dhaka', A+);

OR,

Creating Sequence: Sometimes auto increment becomes a hard task to input data. We can use
sequence & it works like auto increment.

The structure:

create sequence <sequence name> minvalue <n> maxvalue <n> start with <n> increment by
<n>; insert into <table name> values(sequence name.nextval, data .........);

18
Example:

create sequence seq_roll


minvalue 1 maxvalue 25
start with 1 increment
by 1;

insert into Stu_Per values (seq_roll.nextval, 'DM', 'Dhaka', 'A+');

Oracle Functions
SQL also provides Functions for Oracle Database. One can make calculation easily using it.
The syntax are given below:

select <Function name>(m, n) "Title name" from <table name>;

Average: select avg(3,4) "Average" from Dual; (Dual is built in dummy table in Oracle).

Minimum: select min(3,4) "Minimum" from Dual;

Maximum: select max(3,4) "Maximum" from dual;

Summation: select sum(4, 5) "Summation" from dual;

Absolute value: select abs(-145) "Absolute" from dual;

Power: select power(4, 5) "Power" from dual;

Round: select round(14.59, 1) "Round Number" from dual;

Square Root: select sqrt(25) "Square Root" from dual;

Exponential: select exp(5) "Exponential value" from dual;

Extract: select extract(year from sysdate) "Year" from dual;

Greatest: select greatest(17,20, 18) "Greatest" from dual;

Least: select least(17,20,18) "Least" from dual;


19
There are more functions are available than we mentioned in the example section of Oracle
Function.

Joining Table
For joining table we need two keys that I mentioned earlier. One is primary key another is
foreign key. There are Four kinds of join.
those are given below:

1. Inner Join
2. Left Join
3. Right Join
4. Full Join
Inner Join: It is used when
two or more tables need a
specific data for common
record. In
fact, Inner join is used when
common record are
inspected. The formula is
given below:
select column_names, From table_1,
inner join table_2 on (condition); Example:
select
Stu_Per.Roll, Stu_Per.Name, Stu_Marks.Grade from Stu_Per inner join Stu_Marks on
(Stu_Per.Roll = Stu_Marks.Roll);

Left Join: It is used for making relation between table 1 and table 2 where all the record of table
1 will be shown and the common record of table 2 will also be shown. But if there is no
common record in table 2, only table 1 records will be shown. In fact,

20
left join only used to show A joining with the common records of B. The syntax are given
below:

select column_names, From table_1,


left join table_2 on (condition);

Example:

select
Stu_Per.Roll, Stu_Per.Name, Stu_Marks.Grade from Stu_Per left join
Stu_Marks on (Stu_Per.Roll = Stu_Marks.Roll);
Right Join: It is used for returning all the records of table 2 with the common records of table
1. If there is no common record in table 2, it provides null value. The diagram is given below:

21
The syntax of right join is given below:

select column_names, From table_1,


right join table_2 on (condition);

Example:
select
Stu_Per.Roll, Stu_Per.Name, Stu_Marks.Grade from Stu_Per right join Stu_Marks on
(Stu_Per.Roll = Stu_Marks.Roll);

Full Join: It is used for joining two table. It shows records of both tables and their common
records also. It fact, it will return records of both diagrams that are selected to be linked. So,
the diagram is given below:

22
The syntax of full join is given below:

select column_names, From table_1,


full outer join table_2 on (condition);

Example:

select
Stu_Per.Roll, Stu_Per.Name, Stu_Marks.Grade from Stu_Per full outer join Stu_Marks on

(Stu_Per.Roll = Stu_Marks.Roll); Exercise

Recently I have studied about inventory management system of a library. I think this example
will be perfect for the exercise. The example is given below:

23
24
From The E-R Diagram it is clear that there are three entities. Those are: Books, Publisher,
Members. 'Books' entity has seven attributes in which 'Book_ID' is the primary key and 'Memb_ID'
& 'Pub_ID' are foreign key for making relation. Similarly, 'Publisher' entity has three attributes on
which 'Pub_ID' is primary key. 'Members' entity has five attributes on which 'Memb_ID' is primary
key. 'Books' and 'Publisher' both are in relation which has 'Many to Many' cardinalities and the
relation is called 'Published' . Again, 'Books' and 'Members' both are in relation that is called
'Borrowed' and the cardinalities is 'Many to One'.

Code: create table Publisher(Pub_ID number(5),


Name Varchar2(20), Address varchar2(30),
constraint pk_tblpub primary key(Pub_ID)); Input:
insert into Publisher values (036, 'Durjoy', 'Keranigonj'); insert into Publisher
values (037, 'Tonmoy', 'Dhanmondi'); insert into Publisher values (038, 'Emon',
'Jatrabari'); create table Members(Memb_ID number(5),

Name varchar2(20),

Address varchar2(30),

Memb_date date, Exp_date


date,
constraint pk_tblmem primary key(Memb_ID));

Input:

insert into Members values (1202, 'Papri', 'Chittagong', to_date('12-05-


2019','dd-mm-yyyy'), to_date('12-05-2020', 'dd-mm-yyyy'));

insert into Members values (1204, 'Supriya', 'Chittagong', to_date('2106-2019','dd-mm-yyyy'),


to_date('21-06-2020', 'dd-mm-yyyy'));

insert into Members values (1206, 'Khadija', 'Mirpur', to_date('10-122019','dd-mm-yyyy'),


to_date('10-12-2020', 'dd-mm-yyyy')); create table Books(Book_id number(5),

25
Title varchar2(25),

Author varchar2(20),

Price float(5),

Available varchar2(4),

Pub_ID number(5), Memb_ID number(5),


constraint pk_tblbk primary key(Book_id), constraint
fk_tblbk1 foreign key(Pub_ID) references Publisher(Pub_ID),

constraint fk_tblbk2 foreign key(Memb_ID) references


Members(Memb_ID));

input:

insert into Books values(2912, 'About earth', 'Durjoy', 12.30, 'Yes', 036,
1202);

insert into Books values(2913, 'Smoking', 'Tonmoy', 10.50, 'NO', 037,


1204);

insert into Books values(2914, 'COVID19', 'Emon', 11.50, 'Yes', 038, 1206);

Selecting tables:

select Books.Book_ID, Books.Title, Books.Memb_ID, Members.Name, Members.Address,


Members.Memb_date, Publisher.Pub_ID,
Publisher.Name from Members join Books on (Members.Memb_ID =

Books.Memb_ID) join Publisher on (Publisher.Pub_ID = Books.Pub_ID);

26
27

You might also like