Codes of Oracle & Summary Report On SQL: 401: Database Management
Codes of Oracle & Summary Report On SQL: 401: Database Management
report on SQL
401: Database Management
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
1 Introduction 4-5
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: 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.
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
Other types
7
String
Data Ty pe Descrip tion Size
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
-2,147,483,648 to
2,147,483,648
number. Similar to
Decimal(p,s)
10
Date & Time
This is another data type of Oracle SQL. Date & Time data types are given below:
Data Type Description Storage
Timestamp 3
11
modified.
There are another kinds of data type that are not included on the previous three type. Those
are:
Data Ty Description
p
e
Maximum 2GB
Cursor Stores a reference to a cursor used for database operations
Tabl e
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:
...........................................
...........................................
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;
Deleting a table:
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>
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.
Rename Table:
rename to Stu_Per;
15
Comment Keyword
Comment is helpful to the code writer & gives a mark in SQL script. The Syntax is:
/*.....................*/
1. Primary key: the attribute that is used for specially identify a row in a table or making
relation. In fact,
* 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.
1. Not Null: This constraint helps to complementary include 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 <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.
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);
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:
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:
Oracle Functions
SQL also provides Functions for Oracle Database. One can make calculation easily using it.
The syntax are given below:
Average: select avg(3,4) "Average" from Dual; (Dual is built in dummy table in Oracle).
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:
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:
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:
Example:
select
Stu_Per.Roll, Stu_Per.Name, Stu_Marks.Grade from Stu_Per full outer join Stu_Marks on
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'.
Name varchar2(20),
Address varchar2(30),
Input:
25
Title varchar2(25),
Author varchar2(20),
Price float(5),
Available varchar2(4),
input:
insert into Books values(2912, 'About earth', 'Durjoy', 12.30, 'Yes', 036,
1202);
insert into Books values(2914, 'COVID19', 'Emon', 11.50, 'Yes', 038, 1206);
Selecting tables:
26
27