You are on page 1of 72

6.1.

Oracle XE and SQL Developer download and


setup
Download Oracle Express Edition and SQL Developer
For this topic, we need to have Oracle database, as there are various topics with content on SQL
queries to create table, perform insert, update, delete, select etc. on those tables and work with
Java Database Connectivity (JDBC).
We can install Oracle Express Edition Database Server (also known as Oracle XE) on our local
systems / laptops after reviewing the license agreement as our purpose is learning and training
ourselves.
Once the database server is configured, we need client software to connect with the database
and execute our queries. We will download SQL Developer for this purpose. SQL Developer can
be downloaded and configured with any running database server. In our case, we will configure it
to work with installed Oracle Express Edition server in our local machine.
Below video will take us through the steps on how to download both the software (Oracle
Express Edition and SQL Developer):
Download links visible in the videos are as below:
Oracle Express Edition:
http://www.oracle.com/technetwork/database/database-technologies/expressedition/downloads/index.html
SQL Developer:
http://www.oracle.com/technetwork/developer-tools/sql-developer/downloads/index.html
If these links are not accessible, we can search with google for Download Oracle Express
Edition and Download SQL Developer and select relevant link referring oracle.com official site.
Installing Oracle Express Edition
After download, we need to install Oracle Express Edition server to start with. Refer below video
which will explain all steps on installation and other features to ensure that installation is
successful and database server is running fine.
Configuring SQL Developer and connecting with installed Oracle Express Edition
As discussed before, once the database engine is configured, we need to configure the client
software to connect with the database. Below video will take you through the steps for
configuring SQL Developer and connecting with installed Oracle XE. Video will also show how to
create user in Oracle and grant specific rights.
Configuring JDBC for XE installed in local machine
You can revisit this section after the current course is completed and you have gone through
basics of Java Database Connectivity JDBC in next chapter. The content of next chapter has
examples where Oracle database is connected using Java code, where we specify the server
details, with user name and password. However, you may refer it at this time based on interest.

Below video has a simple example which demonstrates how to use installed Oracle XE with
JDBC code, while running the code either from local machine OR from other machine in the
same network.

6.2. Introduction to Database


Data : It is unprocessed raw facts which has no meaning. For example, 300 is data.
Information : It is processed data which helps in decision making and planning. When we add
meaning to data, it becomes information.
For example, 300 is the total strength of associates present today at Ahmadabad ILP center .
Information = Data + context , in which data is 300 and context is ' total strength of associates
present today at Ahmadabad ILP center'.
We need to manage data because of following problems with data management:
- Redundancy
Data existing at multiple places
- Inconsistency
Stale data, corrupted data
- Unavailability
Data doesn't exist
Quality information needs data management. Data management means managing creation,
reading, update & deletion of data.
Approaches to data management:

File

Database

Advantage of File System :


-Cost effective for smaller data size
-Data capturing is simple
Disadvantage of File System :
-Unable to manage complex data
- Data searching may be difficult
- Large data can not be managed
Database System :
A database is an organized collection of data. Using database, we can input, store, retrieve and
manage the large data. The data are typically organized to find relevant information. For
example, modeling the availability of rooms in hotels in a way that supports finding a hotel with
vacancies.

-It has a Database Management System (DBMS) - collection of interrelated data and programs
to access the data.
-It has a data model the manner in which data is stored.
-Uses a programming language to design and manage data - (Data definition language-DDL,
Data Manipulation language-DML)
Advantage of Database System :
-It organize, store and retrieve large amounts of data
-Data quality is ensured
-Data structure can be maintained
Disadvantage of Database System :
-Need a DBMS
-Start up effort required
-Training effort required
Database is collection of tables and each table contains rows and columns .
For example, below mentioned 'Customers' table contains 3 rows and 3 columns (CustomerID,
CustomerName, City)

Types of Database and its architecture


Types of databases include analytic, management, operational, flat-file and hierarchical
databases. Other databases include end-user, network, distributed and rational databases. The
difference in the databases is based on how it works and what it is used for.
There are many types of databases that vary by function and data model.
One way to classify different types of databases is by their function:
Analytical databases, sometimes called On-line Analytical Processes (OLAP) are designed to
assist in decision-making. Typically, an OLAP database contains statistical information that is
read by users, but not modified.
Operational databases, also known as On-line Transaction Processing (OLTP) can be
manipulated by users. Records can be added, deleted or modified by users with the proper level
of access.
The second way to classify different types of databases is by their 'data model'.
A data model describes the structure of data and how it is accessed. There are several common
data models:
Flat-file. Flat file databases most closely resemble paper files and file cabinets. Flat file

databases, while simple to create and access, contain lots of redundant information. These
redundancies slow down the search process, making the flat-file type of database inefficient.
Hierarchical databases relate tables in a parent/child format. Although this reduces redundancy
and increases organization, it is still a limiting way to handle data. The fact that child tables at the
same level don't have links to each other makes searching problematic.
Network databases contain simple data that exist with or without links to other data. The
database is separated into records, which may be sliced into columns or fields.
Relational. Relational databases link tables with common "key" fields allowing for sophisticated
relationships between tables. Table links can be "indexed" or stored for future use, resulting in
quicker searches in this type of database.
Object-oriented. Object oriented databases are structurally similar to relational databases.
While most other types of databases are designed to hold text and numbers, object-oriented
databases are designed to store audio, video and other mixed media objects.
A database management system (DBMS) is an aggregate of data, hardware, software, and
users that helps an enterprise manage its operational data. DBMS is responsible for maintaining
the integrity and security of stored data, and for recovering information if the system fails. . It can
follow network, hierarchical or other data model. In DBMS, we need to open each and every
table whenever we want to use it.
Advantages of DBMS :

Reduced data redundancy

Greater data integrity and consistency

Improved data security

Reduced data entry, storage and retrieval cost

Improved data access to users through use of host and query language

Disadvantages of DBMS :

Database systems are complex, difficult and time consuming to design

Damage to databases affects virtually to all applications programs

Substantial hardware and software start up costs

Initial training is required for all programmers and users

RDBMS stands for Relational Database Management System. In case of RDBMS, we can
establish relation between tables and it is based on the relational model. In RDBMS, if we open
database,all the tables would be accessible.
It is basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2,
Oracle, MySQL, and Microsoft Access. RDBMS allows the data to be queried based on any
column in any table and there is no need to create an index in order to query data so relational
data is easier to query than hierarchical.
Structure Of Database

When we use a database, we are not usually concerned with where each piece of data is stored,
or what size it is. We just want to be sure that when you refer to a name, for example, the correct
value is returned.
Physical database structure
The physical database describes how the structures in the logical database and the search paths
between them are implemented. The term database means the logical database, unless
indicated otherwise.
One or more data files, two are more redo log files, and one or more control files are components
of logical database structure of Oracle database.
It specifies the physical configuration of the database on the storage media. At a physical level,
the data is stored in data files on disk . The data in the data files is stored in operating system
blocks. It is the detailed design of a system that includes modules & the database's hardware &
software specifications of the system.
One characteristic of an RDBMS is the independence of logical data structures such as tables,
views, and indexes from physical storage structures. Because physical and logical structures are
separate, you can manage physical storage of data without affecting access to logical structures.
For example, renaming a database file does not rename the tables stored in it.
Data files and temp files
A data file is a physical file on disk that was created by Oracle Database and contains data
structures such as tables and indexes. A temp file is a data file that belongs to a temporary table
space. The data is written to these files in an Oracle proprietary format that cannot be read by
other programs.
Control files
A control file is a root file that tracks the physical components of the database.
Online redo log files
The online redo log is a set of files containing records of changes made to data.
A database instance is a set of memory structures that manage database files.
Logical database structure
The logical database is the structure of the data and the relationships between different pieces of
information. There is no information about how these structures and relations are implemented.
Tablespaces and database's schema objects are components of logical database structure of
Oracle database.
Once the relationships and dependencies amongst the various pieces of information have been
determined, it is possible to arrange the data into a logical structure which can then be mapped
into the storage objects supported by the database management system. In the case of relational
databases the storage objects are tables which store data in rows and columns.
Oracle Database allocates logical space for all data in the database. The logical units of
database space allocation are data blocks, extents, segments, and table spaces.

At the finest level of granularity, Oracle Database stores data in data blocks. One logical data
block corresponds to a specific number of bytes of physical disk space, for example, 2 KB. Data
blocks are the smallest units of storage that Oracle Database can use or allocate.
An extent is a set of logically contiguous data blocks allocated for storing a specific type of
information.
A segment is a set of extents allocated for a specific database object, such as a table. For
example, the data for the employees table is stored in its own data segment, whereas each index
for employees is stored in its own index segment. Every database object that consumes storage
consists of a single segment.
Database server and its instances
A database server is a computer program that provides database services to other computer
programs or computers, as defined by the clientserver model. The term may also refer to a
computer dedicated to running such a program.
Most of the Database servers works with the base of Query Language. Each Database
understands its query language and converts it to Server readable form and executes it to
retrieve the results.
Some examples of proprietary database servers are Oracle, DB2, Informix, and Microsoft SQL
Server.
Every server uses its own query logic and structure. The SQL query language is more or less the
same in all relational database servers
An instance of the Database Engine is a copy of the sqlservr.exe executable that runs as an
operating system service. Each instance manages several system databases and one or more
user databases. Each computer can run multiple instances of the Database Engine. Applications
connect to the instance in order to perform work in a database managed by the instance.
A database instance is a set of memory structures that manage database files. A database is a
set of physical files on disk created by the CREATE DATABASE statement. The instance
manages its associated data and serves the users of the database.
Every running Oracle database is associated with at least one Oracle database instance.
Because an instance exists in memory and a database exists on disk, an instance can exist
without a database and a database can exist without an instance.
Database Instance Structure
When an instance is started, Oracle Database allocates a memory area called the system global
area (SGA) and starts one or more background processes. The SGA serves various purposes,
including the following:
Maintaining internal data structures that are accessed by many processes and threads
concurrently
Caching data blocks read from disk
Buffering redo data before writing it to the online redo log files
Storing SQL execution plans

The SGA is shared by the Oracle processes, which include server processes and background
processes, running on a single computer. The way in which Oracle processes are associated
with the SGA varies according to operating system.
A database instance includes background processes. Server processes, and the process
memory allocated in these processes, also exist in the instance. The instance continues to
function when server processes terminate.
To execute any query, we have to start oracle instance using STARTUP command.

6.3. Normalization
Normalization is the process of organizing the fields and tables of a relational database to
minimize redundancy and dependency.
To free the database from Insertion,Update and Deletion anomalies.
Normalization is used for mainly two purpose,
1. Eliminating redundant(useless) data.
2. Ensuring data dependencies make sense i.e data is logically stored.
3. Without Normalization,it becomes difficult to handle and update the database, without facing
data loss.
4. Insertion, Updation and Deletion anomalies are very frequent.
A table which is not in Normalized form:

Types of Anomalies
Updation Anomaly :
To update address of a student who occurs twice or more than twice in a table, we will have to
update S_Address column in all the rows, else data will become inconsistent.
Table showing Updation Anomaly:

Insertion Anomaly :
Suppose for a new admission, we have a Student id(S_id), name and address of a student but if
student has not opted for any subjects yet then we have to insert NULL there, leading to Insertion
anomaly. If null values are not allowed in the table for the column Subject _Opted , with insertion
anomaly the problem will become worse, leading to not being able to add a new student to the
DB unless and until the student has opted for a subject.
Table showing Insertion Anomaly:

Deletion Anomaly :
A deletion anomaly is the unintended loss of data due to deletion of other data. For example, if
we delete a student belonging to city of Dharwad then we lose the information about that city. Or
if (S_id) 403 has only one subject and temporarily he drops it, when we delete that row entire
student record will be deleted along with it.
Table showing Deletion Anomaly:

First Normal Form (1NF)


As per First Normal Form, no two rows of data must contain repeating group of information. i.e.
each set of column must have a unique value, such that multiple columns cannot be used to
fetch the same row and each row should have a primary key to distinguish it as unique.
A table which is not in First normal form:

Student name 'nandish' is used twice in the table and subject maths is also repeated.
To reduce above table to First Normal form break the table into two different tables.
Student Table:

Subject Table:

In Student table concatenation of subject_id and student_id is the Primary key.

Now both the Student table and Subject table are normalized to first normal form.

Second Normal Form (2NF)


Should meet all the needs of First Normal Form. There must not be any partial dependency.
A table which is not in Second normal form:

Customer_Name is only dependent on customer_id, Order_name is dependent on Order_id and


there is no link between sale_detail and Customer_name.
To reduce above table to Second Normal form break the table into three different tables
Customer_Detail Table :

Order_Detail Table :

Sale_Detail Table :

Third Normal Form (3NF)


Every non-prime attribute of table must be dependent on primary key. The transitive functional
dependency should be removed from the table.
Student_id

Student_name

DOB

Street

City

State

Street, city and state depends upon Zip. The dependency between zip and other fields is called
transitive dependency. To apply 3NF, move the street, city and state to new table,with Zip as
primary key.
Student_id

Student_name

DOB

Zip

Zip

Zip

Street

City

State

BCNF(Boyce and Codd Normal Form)

It is a higher version of the Third Normal Form.

This form deals with certain type of anomalies that is not handled by 3NF.

A 3NF table which does not have multiple overlapping candidate keys is said to be in
BCNF.

6.4. Data Types and SQL Concepts


A data type defines what kind of value a column can contain. Database has many data types as
shown below :
Float, Data and Time, Number, String, Boolean, BLOB, CLOB etc..
Each column in a database table is required to have a name and a data type. SQL developers
have to decide what types of data will be stored inside each and every table column when
creating a SQL table.
However, different databases offer different choices for the data type definition.
The following table shows some of the common names of data types between the various
database platforms:

The following table lists the general data types in SQL:

SQL Concepts
SQL is a standard language for accessing and manipulating databases. It stands for Structured
Query Language. Users interact with database systems through query languages.
DDL : It is Data Definition Language which consist of 'CREATE, DROP, TRUNCATE, ALTER'
command.
DML : It is Data Manipulation Language which consist of 'INSERT, DELETE, UPDATE'
command.
DRL : It is Data Retrieval Language which consist of 'SELECT' command.
CREATE TABLE Statement
The CREATE TABLE statement is used to create a table in a database. Tables are organized into
rows and columns; and each table must have a name.
Syntax
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
The column_name parameters specify the names of the columns of the table. The data_type
parameter specifies what type of data the column can hold (e.g. varchar, integer, date, etc.). The
size parameter specifies the maximum length of the column of the table.
Try it yourself and verify the output:
Now we want to create a table called "Persons" that contains five columns: PersonID, LastName,
FirstName, Address, and City.
We use the following CREATE TABLE statement:
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),

FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
DROP TABLE Statement
The DROP TABLE statement is used to delete a table.
DROP TABLE table_name
TRUNCATE TABLE Statement
What if we only want to delete the data inside the table, and not the table itself?
Then, use the TRUNCATE TABLE statement:
TRUNCATE TABLE table_name
The ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
Syntax
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype
To delete a column in a table, use the following syntax (notice that some database systems don't
allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name
Try it yourself and verify the output:
Look at the "Persons" table:

Now we want to add a column named "DateOfBirth" in the "Persons" table.


We use the following SQL statement:
ALTER TABLE Persons
ADD DateOfBirth date
INSERT Statement
The INSERT INTO statement is used to insert new records in a table.
It is possible to write the INSERT INTO statement in two forms.
The first form does not specify the column names where the data will be inserted, only their
values:

INSERT INTO table_name


VALUES (value1,value2,value3,...);
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
UPDATE Statement
The UPDATE statement is used to update existing records in a table.
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';
DELETE Statement
The delete statement is used to delete existing records in a table.
DELETE FROM table_name
WHERE some_column=some_value;
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders';
Delete All Data
It is possible to delete all rows in a table without deleting the table. This means that the table
structure,
attributes, and indexes will be intact:
DELETE FROM table_name;
The DELETE statement is used to delete records in a table.
SQL Constraints
SQL constraints are used to specify rules for the data in a table. It helps to ensure the data type
and range that goes into the database entity.
If there is any violation between the constraint and the data action, the action is aborted by the
constraint.
Constraints can be specified when the table is created (inside the CREATE TABLE statement) or
after the table is created (inside the ALTER TABLE statement).
SQL CREATE TABLE + CONSTRAINT Syntax
CREATE TABLE table_name
(

column_name1 data_type(size) constraint_name,


column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
....
);
In SQL, we have the following constraints:
NOT NULL - Indicates that a column cannot store NULL value
UNIQUE - Ensures that each row for a column must have a unique value
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or
combination of two or more columns) have an unique identity which helps to find a particular
record in a table more easily and quickly.
FOREIGN KEY - It points to a primary key in another table ensure the referential integrity of the
data in one table to match values in another table
CHECK - Ensures that the value in a column meets a specific condition
DEFAULT - Specifies a default value when specified none for this column
NOT NULL Constraint
The NOT NULL constraint enforces a column to NOT accept NULL values.
The NOT NULL constraint enforces a field to always contain a value. This means that you cannot
insert a new record, or update a record without adding a value to this field.
The following SQL enforces the "P_Id" column as PRIMARY KEY and the "LastName" column to
not accept NULL values:
CREATE TABLE Persons
(
P_Id int PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
UNIQUE Constraint
The UNIQUE constraint uniquely identifies each record in a database table.
The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a
column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint
defined on it.
Note : We may have many UNIQUE constraints per table, but only one PRIMARY KEY
constraint per table.
The following SQL creates a UNIQUE constraint on the "P_Id" column when the "Persons" table
is created:

CREATE TABLE Persons


(
P_Id int UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
SQL UNIQUE Constraint on ALTER TABLE
To create a UNIQUE constraint on the "P_Id" column when the table is already created, use the
following SQL:
ALTER TABLE Persons
ADD UNIQUE (P_Id)
or
ALTER TABLE Persons
ADD CONSTRAINT uc_0 UNIQUE(P_Id)
To DROP a UNIQUE Constraint
ALTER TABLE Persons
DROP CONSTRAINT uc_0
PRIMARY KEY Constraint
The PRIMARY KEY constraint uniquely identifies each record in a database table.
Primary keys must contain unique values. A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have only ONE primary key.
SQL PRIMARY KEY Constraint on CREATE TABLE
The following SQL creates a PRIMARY KEY on the "P_Id" column when the "Persons" table is
created:
CREATE TABLE Persons
(
P_Id int ,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
or
CREATE TABLE Persons
(
P_Id int PRIMARY KEY,
LastName varchar(255) NOT NULL,

FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
SQL PRIMARY KEY Constraint on ALTER TABLE
To create a PRIMARY KEY constraint on the "P_Id" column when the table is already created,
use the following SQL:
ALTER TABLE Persons
ADD PRIMARY KEY (P_Id)
or
ALTER TABLE Persons
ADD CONSTRAINT pk_0 PRIMARY KEY(P_Id)
To DROP a PRIMARY KEY Constraint
ALTER TABLE Persons
DROP CONSTRAINT pk_0
FOREIGN KEY Constraint
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
Let's illustrate the foreign key with an example. Look at the following two tables:
The "Persons" table:

The "Orders" table:

Note that the "P_Id" column in the "Orders" table points to the "P_Id" column in the "Persons"
table.
The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links between
tables.
The FOREIGN KEY constraint also prevents invalid data from being inserted into the foreign key
column, because it has to be one of the values contained in the table it points to.
CREATE TABLE Orders
(
O_Id int PRIMARY KEY,

OrderNo int ,
P_Id INT,
CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
)
SQL FOREIGN KEY Constraint on ALTER TABLE
To create a FOREIGN KEY constraint on the "P_Id" column when the "Orders" table is already
created, use the following SQL:
ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
or
ALTER TABLE Orders
ADD CONSTRAINT fk_0 FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
To DROP a FOREIGN KEY Constraint
ALTER TABLE Persons
DROP CONSTRAINT fk_0
CHECK Constraint
The CHECK constraint is used to limit the value range that can be placed in a column.
If you define a CHECK constraint on a single column it allows only certain values for this column.
If you define a CHECK constraint on a table it can limit the values in certain columns based on
values in other columns in the row.
SQL CHECK Constraint on CREATE TABLE
The following SQL creates a CHECK constraint on the "P_Id" column when the "Persons" table is
created. The CHECK constraint specifies that the column "P_Id" must only include integers
greater than 0.
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CHECK (P_Id>0)
)
or
CREATE TABLE Persons
(

P_Id int NOT NULL CHECK (P_Id>0),


LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple
columns, use the following SQL syntax:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Bangalore')
)
SQL CHECK Constraint on ALTER TABLE
To create a CHECK constraint on the "P_Id" column when the table is already created, use the
following SQL:
ALTER TABLE Persons
ADD CHECK (P_Id>0)
To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple
columns, use the following SQL syntax:
ALTER TABLE Persons
ADD CONSTRAINT chk_Person CHECK (P_Id>0 AND City='Bangalore')
To DROP a CHECK Constraint
To drop a CHECK constraint, use the following SQL:
ALTER TABLE Persons
DROP CONSTRAINT chk_Person
DEFAULT Constraint
The DEFAULT constraint is used to insert a default value into a column.
The default value will be added to all new records, if no other value is specified.
SQL DEFAULT Constraint on CREATE TABLE
The following SQL creates a DEFAULT constraint on the "City" column when the "Persons" table
is created:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,

FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Bangalore'
)
Points to remember
Database
Data : It is unprocessed raw facts
Information : It is processed data which helps in decision making and planning.
A database is an organized collection of data. Using database, we can input, store, retrieve and
manage the large data.
Types of Database
DBMS : It is an aggregate of data, hardware, software, and users that helps an enterprise
manage its operational data. In case of DBMS, we can not establish relation between tables . It
can follow network, hierarchical or other data model.
RDBMS : It stands for Relational Database Management System. In case of RDBMS, we can
establish relation between tables and it is based on the relational model
Normalization
It is the process of organizing the fields and tables of a relational database to minimize
redundancy and dependency.
It has three forms : INF , 2 NF and 3 NF
Structure Of Database

Physical

Logical

Connecting to Server from Client through


Putty
SqlDbxPersonal tool (SqlDbx)
Data Types
Float, Data and Time, Number, String, Boolean, BLOB, CLOB etc..
SQL Concepts
It is a standard language for accessing and manipulating databases. It stands for Structured
Query Language.
DDL : CREATE, DROP, TRUNCATE, ALTER
DML : INSERT, DELETE, UPDATE
DRL : SELECT

6.5. DRL -Data Retrieval Language


The Data Retrieval Language consists of Select command and its clauses.

The most commonly used SQL command is SELECT statement. The SQL SELECT statement is
used to query or retrieve data from a table in the database. The query may retrieve information
from specified columns or from all of the columns in the table. To create a simple SQL SELECT
Statement, you must specify the column(s) name and the table name. The whole query is called
SQL SELECT Statement. The result is stored in a result table called result-set.
Syntax of SQL SELECT Statement:

'table-name' is the name of the table from which the information is retrieved.

'column_list' includes one or more columns from which data is retrieved.

The code within the brackets is optional.

For example, consider the table student_details:

To select the first name of all the students the query will be:

NOTE:
The commands are not case sensitive. The above SELECT statement can also be written as
"select first_name from students_details;"
Try it yourself and verify the output:

You can also retrieve data from more than one column. For example, to select first name and last
name of all the students.
NOTE:
In a SQL SELECT statement only SELECT and FROM statements are mandatory. Other clauses
like WHERE, ORDER BY, GROUP BY, HAVING are optional.
To retrieve data from more than all the columns, we use '*' :

Use of expressions in SQL Select Statement


Expressions combine many arithmetic operators, they can be used in SELECT, WHERE and
ORDER BY Clauses of the SQL SELECT Statement.
The operators are evaluated in a specific order of precedence, when more than one arithmetic
operator is used in an expression. The order of evaluation is: parentheses, division,
multiplication, addition, and subtraction. The evaluation is performed from the left to the right of
the expression.
For example: If we want to display the first and last name of students combined together, we
need to use the concat operator.
The SQL Select Statement would be like:

Also, an alias name can be provided using 'AS'

This can be called as alias for column. Similarly, we have alias for tables.
Try it yourself and verify the output:

In the above query, alias 's' is defined for the table student_details and the column first_name is
selected from the table.
Aliases is more useful when

There are more than one tables involved in a query,

Functions are used in the query,

The column names are big or not readable,

More than one columns are combined together

6.6. SQL WHERE Clause


The WHERE Clause is used when we want to retrieve specific information from a table excluding
other irrelevant data.
For example, when we want to see the information about students with Branch as JAVA then
information of students in other branch becomes irrelevant. Retrieving information about all the
students would increase the processing time for the query.

So SQL offers a feature called WHERE clause, which we can use to restrict the data that is
retrieved. The condition provided in the WHERE clause filters the rows retrieved from the table
and gives only those rows which are expected.
WHERE clause can be used along with SELECT, DELETE, UPDATE statements.
Syntax of SQL WHERE Clause:
WHERE {column or expression} comparison-operator value
Syntax for a WHERE clause with Select statement is:
SELECT column_list FROM table-name WHERE condition;
column or expression - Is the column of a table or a expression
comparison-operator - operators like = < > etc.
value - Any user value or a column name for comparison
Try it yourself and verify the output: To find the name of a student with branch as JAVA, the
query would be like:

Comparison Operators and Logical Operators are used in WHERE Clause. These operators are
discussed in the later sections.
NOTE: Aliases defined for the columns in the SELECT statement cannot be used in the WHERE
clause to set conditions. Only aliases created for tables can be used to reference the columns in
the table.
How to use expressions in the WHERE Clause?
Expressions can also be used in the WHERE clause of the SELECT statement.
For example: Lets consider the Student_Details table. If we want to display the details of
students whose age is less than 22 then the query will be:

NOTE:
Aliases defined in the SELECT Statement can be used in WHERE Clause.

6.7. Comparison Operators


Comparison operators are used to compare the column data with specific values in a condition.
Comparison Operators are also used along with the SELECT statement to filter data based on
specific conditions.
The below table describes each comparison operator.

Try it yourself and verify the output:

Logical Operators:
There are three Logical Operators namely, AND, OR, and NOT. These operators compare two
conditions at a time to determine whether a row can be selected for the output. When retrieving
data using a SELECT statement, we can use logical operators in the WHERE clause, which
allows us to combine more than one condition.

Try it yourself and verify the output:

Multiple logical operators can be used in an SQL statement. When we combine the logical
operators in a SELECT statement, the order in which the statement is processed is
AND
OR
NOT
Try it yourself and verify the output:
Example for nested conditions can be:

SQL Comparison Operators


The comparison keywords available in sql are used to enhance the search capabilities of a sql
query. They are "IN", "BETWEEN...AND", "IS NULL", "LIKE".

SQL LIKE Operator


The LIKE operator is used to display all rows in a table whose column values match a specified
pattern. It is useful when we want to search rows to match a specific pattern, or when we do not
know the entire value. For this purpose we use wildcard characters '%' (Percentage) and '_'
(Underscore).

Try it yourself and verify the output:


If we want to fetch the details of student whose name starts with 'J', the query can be as follows:

To fetch the records where we know some pattern of a field, the query can be as follows:

Note:
The data inside quotes is case sensitive. The following query will not fetch any data since 'A' is
in Upper Case:

Hence no records are fetched.


To match a one character, we use '_' as follows:

Hence, it matches 'T'om and displays that.


NOTE:
Each underscore acts as a placeholder for only one character. So we can use more than one
underscore.
For Example:

SQL BETWEEN ... AND Operator


The operator BETWEEN and AND, are used to compare data for a range of values.
For Example:

NOTE:
The BETWEEN...AND operator also includes the first and last values along with the middle
values as in the given example, 20 and 22 are also included.
SQL IN Operator
The IN operator is used to compare a column with more than one value. It is similar to an OR
condition.
Try it yourself and verify the output:
To display the details of students with Branch as 'Java' or 'Unix', the query can be:

NOTE:.
The data used to compare is case sensitive.
SQL IS NULL Operator
A column value is NULL if it does not exist. The IS NULL operator is used to display all the rows
for columns that do not have a value.
For Example:
If we want to display the details of student who has not registered for any course then the query
will be:

There would be no output if every student has registered in a course in the table student_details,
else the names of the students who have not registered would be displayed.

6.8. SQL Order By and SQL Group By


SQL Order By
The ORDER BY clause is used in a SELECT statement to sort results either in ascending or
descending order. Oracle sorts query results in ascending order by default.
Syntax for using SQL ORDER BY clause to sort data is:
SELECT column-list
FROM table_name [WHERE condition]
[ORDER BY column1 [, column 2, .. [column N]] [ASC | DESC]];
The database records are as follows:

Try it yourself and verify the output:: To fetch the records of students in ascending order of the
age then the query will be:

By default the data is sorted in ascending order, hence if we want to display it in descending
order then the query will be:

The data can also be sorted by multiple columns as shown in below query:

Hence, the data will be sorted by 'Age' first and then by 'Id'.
We can represent the columns in the ORDER BY clause by specifying the position of a column in
the SELECT list, instead of writing the column name.
The above query can also be written as given below:

How to use expressions in the ORDER BY Clause?


Expressions in the ORDER BY clause of a SELECT statement. Suppose we have to add one
year to age of all the students, and then sort in order of the new age then the query would be

SQL Distinct Keyword


This keyword is used to select the distinct rows.
For Example: To select all distinct department names from employee table, the query would be

Hence, this keyword can be used to fetch the non redundant data from the table.
SQL Aggregate Functions
Group functions are built-in SQL functions that operate on groups of rows and return one value
for the entire group. These functions are: COUNT, MAX, MIN, AVG, SUM
SQL COUNT ()
This function returns the number of rows in the table that satisfies the condition specified in the
WHERE condition. If the WHERE condition is not specified, then the query returns the total
number of rows in the table.
For Example: If you want the number of students in a particular department, the query would be:

If you want the total number of students in all the department, the query would be

SQL MAX()
This function is used to get the maximum value from a column.
To get the maximum age of a student, the query would be:

SQL MIN()
This function is used to get the minimum value from a column.
To get the minimum age of a student, the query would be:

SQL AVG()
This function is used to get the average value of a numeric column.
To get the average age of students, the query would be

SQL SUM()
This function is used to get the sum of a numeric column
To get the total age of students,

SQL Group By
The GROUP BY statement is used in conjunction with the aggregate functions to group the
result-set
by one or more columns.
SQL GROUP BY Syntax:
SELECT column_name, aggregate_function(column_name)
FROM table_name

WHERE column_name operator value


GROUP BY column_name;
To understand the group by clause, we have added one more column as fees into the table
Student_details as follows:

Now if we want to display the total fees of all the branches then, the query would be

The expressions must be also included in the group by clause else it will not execute. Also there
must be at least one table included in the from clause. Where clause can be used before group
by if there are any condition/s to be met.
Also remember the sequence that we discussed in the beginning of DRL that needs to be
followed.
SELECT column_list
FROM table-name
[WHERE clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause];
From clause is mandatory and rest are optional
SQL Having Clause
The SQL HAVING Clause is used in combination with the GROUP BY Clause to restrict the
groups of returned rows to only those whose the condition is TRUE.
The HAVING clause was added to SQL because the WHERE keyword could not be used with
aggregate functions.

SQL GROUP BY Syntax:


SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;
Now if we want to display the total fees of all the branches where the fee is greater than 4000
then, the query would be

Hence, 'Having clause' does the work of a 'where clause' for Grouping functions as shown in the
example as only the fee less than 4000 is displayed.
Also, we can give the alias for the aggregate function in the following manner:

When WHERE, GROUP BY and HAVING clauses are used together in a SELECT statement, the
WHERE clause is processed first, then the rows that are returned after the WHERE clause is
executed are grouped based on the GROUP BY clause.
Finally, any conditions on the group functions in the HAVING clause are applied to the grouped
rows before the final output is displayed.

6.9. SQL Joins

The SQL Joins clause is used to combine records from two or more tables in a database by
using values common to each.
SQL Join Types:
There are different types of joins available in SQL:
INNER JOIN: returns rows when there is a match in both tables.
LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table.
RIGHT JOIN: returns all rows from the right table, even if there are no matches in the left table.
FULL JOIN: returns rows when there is a match in one of the tables.
SELF JOIN: is used to join a table to itself as if the table were two tables, temporarily renaming
at least one table in the SQL statement.
To understand joins, we will add one more table as STUDENT_JOINING as shown below. Hence
we will perform all the joins operations on these tables.

INNER JOIN
The most frequently used and important of the joins is the INNER JOIN. They are also referred to
as an EQUIJOIN.
The INNER JOIN creates a new result table by combining column values of two tables (table1
and table2) based upon the join-predicate. The query compares each row of table1 with each
row of table2 to find all pairs of rows which satisfy the join-predicate. When the join-predicate is
satisfied, column values for each matched pair of rows of A and B are combined into a result row.
The basic syntax is as follows:
SELECT table1.column1, table2.column2...
FROM table1
INNER JOIN table2
ON table1.common_field = table2.common_field;
For example, lets join the Student_Details and Student_Joining table and identify the joining date
of each student. The query would be:

It is also important to visualize how different joins work. Hence we will see a diagrammatic
representation of all the joins.
The SQL INNER JOIN would return the records where table1 and table2 intersect.

Also, if we want to display Id that is the common field between the tables, then

LEFT JOIN
The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the right
table. This means that if the ON clause matches 0 (zero) records in right table, the join will still
return a row in the result, but with NULL in each column from right table.
This means that a left join returns all the values from the left table, plus matched values from the
right table or NULL in case of no matching join predicate.

The left join is also referred as left outer join


The basic syntax is as follows:
SELECT table1.column1, table2.column2...
FROM table1LEFT JOIN table2
ON table1.common_field = table2.common_field;
For Example:

The diagrammatic representation of all left join:

RIGHT JOIN
The SQL RIGHT JOIN returns all rows from the right table, even if there are no matches in the
left table. This means that if the ON clause matches 0 (zero) records in left table, the join will still
return a row in the result, but with NULL in each column from left table.
This means that a right join returns all the values from the right table, plus matched values from
the left table or NULL in case of no matching join predicate.
The right join is also referred as right outer join
The basic syntax is as follows:

SELECT table1.column1, table2.column2...


FROM table1
RIGHT JOIN table2
ON table1.common_field = table2.common_field;
For Example:

The diagrammatic representation of all right join:

FULL JOIN
The SQL FULL JOIN combines the results of both left and right outer joins.
The joined table will contain all records from both tables, and fill in NULL for missing matches on
either side.
The basic syntax is as follows:
SELECT table1.column1, table2.column2...
FROM table1
FULL JOIN table2
ON table1.common_field = table2.common_field;
Try it yourself and verify the output::

The diagrammatic representation of all full join:

SELF JOIN
A Self Join is a type of sql join which is used to join a table to itself, particularly when the table
has a FOREIGN KEY that references its own PRIMARY KEY. It is necessary to ensure that the
join statement defines an alias for both copies of the table to avoid column ambiguity.
A self join is basically when a table is joined to itself. The way you should visualize a self join for
a given table is by imagining that a join is performed between two identical copies of that table.
And that is exactly why it is called a self join because of the fact that its just the same table
being joined to another copy of itself rather than being joined with a different table.
A self join must have aliases:
In a self join we are joining the same table to itself by essentially creating two copies of that table.
But, how do we distinguish between the two different copies of the table because there is only
one table name after all? Well, when we do a self join, the table names absolutely must use
aliases otherwise the column names would be ambiguous. In other words, we would not know
which tables columns are being referenced without using aliases for the two copies of the table.
Try it yourself and verify the output:

6.10. SQL Subquery


A Subquery or Inner query or Nested query is a query within another SQL query and embedded
within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition to further
restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN etc.
Certain Rules for subquery:
Subqueries must be enclosed within parentheses.
A subquery can have only one column in the SELECT clause, unless multiple columns are in the
main query for the subquery to compare its selected columns.
An ORDER BY cannot be used in a subquery, although the main query can use an ORDER BY.
The GROUP BY can be used to perform the same function as the ORDER BY in a subquery.
Subqueries that return more than one row can only be used with multiple value operators, such
as the IN operator.
The SELECT list cannot include any references to values that evaluate to a BLOB, ARRAY,
CLOB, or NCLOB.

A subquery cannot be immediately enclosed in a set function.


The BETWEEN operator cannot be used with a subquery; however, the BETWEEN operator can
be used within the subquery.
Subqueries with the SELECT Statement
Subqueries are most frequently used with the SELECT statement.
The basic syntax is as follows:
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
Try it yourself and verify the output: consider the Student_details table:

Now we have learn about max() -aggregate function that it fetches the maximum value. What if
we want to fetch the record with the second largest value??
Here, sub query can be used as follows:
Let us fetch the second highest ID from Student_Details table :

Initially, the subquery will fetch the ID as 1007. Hence the query becomes max(ID) excluding
1007.
Thus, we get the ID as 1006 that is the second highest.
Subqueries with the INSERT Statement

Subqueries also can be used with INSERT statements. The INSERT statement uses the data
returned from the subquery to insert into another table. The selected data in the subquery can be
modified with any of the character, date or number functions.
The basic syntax is as follows:
INSERT INTO table_name [ (column1 [, column2 ]) ]
SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ]
Try it yourself and verify the output:
Consider a table STUDENT_DETAILS_2 with similar structure as STUDENT_DETAILS table.
Now to copy complete STUDENT_DETAILS table into STUDENT_DETAILS_2, following is the
syntax:

Hence, the same details are inserted as are present in the Student_Details table.
Subqueries with the UPDATE Statement
Subquery can be used in conjunction with the UPDATE statement. Either single or multiple
columns in a table can be updated when using a subquery with the UPDATE statement.
The basic syntax is as follows:
UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]

Assuming, we have STUDENT_DETAILS_2 table available which is backup of


STUDENT_DETAILS table.
Try it yourself and verify the output:
Following example updates Price by 0.25 times in STUDENT_DETAILS table for all the students
whose AGE is greater than or equal to 21:

Subqueries with the DELETE Statement


The subquery can be used in conjunction with the DELETE statement like with any other
statements mentioned above.
The basic syntax is as follows:
DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
Try it yourself and verify the output:
Assuming, we have STUDENT_DETAILS_2 table available which is backup of
STUDENT_DETAILS table.
Following example deletes records from CUSTOMERS table for all the customers whose AGE is
greater than or equal to 23:

6.11. TCL and DCL


TCL- Transaction Control Language
Transaction Control Language(TCL) commands are used to manage transactions in
database.These are used to manage the changes made by DML statements. It also allows
statements to be grouped together into logical transactions.
A transaction is a unit of work that is performed against a database. Transactions are units or
sequences of work accomplished in a logical order, whether in a manual fashion by a user or
automatically by some sort of a database program.
A transaction is the propagation of one or more changes to the database. For example, if you are
creating a record or updating a record or deleting a record from the table, then you are
performing transaction on the table. It is important to control transactions to ensure data integrity
and to handle database errors.
Properties of Transactions:
Transactions have the following four standard properties, usually referred to by the acronym
ACID:
Atomicity: ensures that all operations within the work unit are completed successfully;
otherwise, the transaction is aborted at the point of failure, and previous operations are rolled
back to their former state.
Consistency: ensures that the database properly changes states upon a successfully committed
transaction.
Isolation: enables transactions to operate independently of and transparent to each other.
Durability: ensures that the result or effect of a committed transaction persists in case of a
system failure.
The TCL commands are as follows:
The COMMIT Command
COMMIT command is used to permanently save any transaction into database.
The COMMIT command saves all transactions to the database since the last COMMIT or
ROLLBACK command.
The syntax for commit is:

commit;
Observations of Data State when COMMIT is Pending:
Consider a scenario where you have not issued commit and also the auto commit is off. Now
since we work in distributed environment, there might be multiple database client/s.
Now you performed an insert operation and never issued a commit command after that. Hence
the transaction is locked.
Now some other database client tried to insert another data.
At this point of time, the query will continue to execute and execution will never be completed
because it is waiting for the other client's log to get completed and hence the connection will be
timed out.
The query inserted first is at the position where the second query is trying to get inserted.

The sessions at other client usually gets the data that is commit and hence the transactions are
never locked.
The ROLLBACK Command
This command restores the database to last commited state. It is also use with savepoint
command to jump to a savepoint in a transaction.
The syntax for rollback is:
rollback;
or
rollback to savepoint-name;
The SAVEPOINT Command
Savepoint command is used to temporarily save a transaction so that you can rollback to that
point whenever necessary.
The syntax for savepoint is:
savepoint savepoint-name;
Try it yourself and verify the output:
Let us consider the Student_details table for explaining TCL
The default table is as follows:

Lets use some SQL queries on the above table and see the results

The Output is as follows:

Now let's execute and understand the use of rollback and savepoint:

Hence, the changes made after S2 are removed from the database.

Similarly, let's execute the following query:

Hence, the changes made after S1 are removed from the database.

DCL- Data Control Language


Data Control Language(DCL) is used to control privilege in Database. To perform any operation
in the database, such as for creating tables, sequences or views we need administrative
privileges.
Also, these activities are performed by Database Administrator (DBA) as it requires the
knowledge of Database Architecture. Hence, as developers we need to have understanding on
DCL but need not to execute the commands.

Privileges are of two types:


System :creating session, table etc are all types of system privilege.
Object : any command or query to work on tables comes under object privilege.
DCL defines two commands

Grant : Gives user access privileges to database.

Revoke : Take back permissions from user.

To Allow a User to create Session


grant create session to username;
To Allow a User to create Table
grant create table to username;
To provide User with some Space on Tablespace to store Table
alter user username quota unlimited on system;
To Grant all privilege to a User
grant sysdba to username;
To Grant permission to Create any Table
grant create any table to username;
To Grant permission to Drop any Table
grant drop any table to username;
To take back Permissions
revoke create table from username;

6.12. Primary Key Generation Techniques


Understanding Oracle Sequences
Sequence is a Oracle database object from which multiple users may generate unique
integers. You can use sequences to automatically generate primary key values.
Creating sequences:
Sequence can be created in Oracle using following syntax:
CREATE SEQUENCE [schema_name].sequence_name
[MINVALUE number]
[MAXVALUE number]
[START WITH number]
[INCREMENT BY number]
[NOCACHE]
[NOCYCLE];

The sequence name is composed of an optional schema name. If you omit schema, sequence
will be created in the current schema.
Attributes of sequences:
Sequence can be created using below attributes.
INCREMENT BY

Specify the interval between sequence numbers.

This integer value can be any positive or negative integer, but it cannot be 0.

This value can have 28 or fewer digits.

The absolute of this value must be less than the difference of MAXVALUE and
MINVALUE.

If this value is negative, then the sequence descends. If the value is positive, then the
sequence ascends.

If you omit this clause, then the interval defaults to 1.

START WITH

Specify the first sequence number to be generated.

Use this clause to start an ascending sequence at a value greater than its minimum or to
start a descending sequence at a value less than its maximum.

For ascending sequences, the default value is the minimum value of the sequence.

For descending sequences, the default value is the maximum value of the sequence.

This integer value can have 28 or fewer digits.

MAXVALUE

Specify the maximum value the sequence can generate.

This integer value can have 28 or fewer digits.

MAXVALUE must be equal to or greater than START WITH and must be greater than
MINVALUE.

If specified, MAXVALUE may not be greater than the largest positive integer that fits in a
DataType value.

If MAXVALUE is not specified, or if NO MAXVALUE is specified,MAXVALUE defaults to


the largest positive integer which fits in a DataType value.

NOMAXVALUE

This is the default.

Specify NOMAXVALUE to indicate a maximum value of 1027 for an ascending sequence


or -1 for a descending sequence.

MINVALUE

Specify the minimum value of the sequence.

This integer value can have 28 or fewer digits.

MINVALUE must be less than or equal to START WITH and must be less than
MAXVALUE.

If specified, MINVALUE must be an integer which fits in a DataType value.

If MINVALUE is not specified, or if NO MINVALUE is specified, MINVALUE defaults to the


smallest negative number which fits in a DataType value.

NOMINVALUE

This is the default.

Specify NOMINVALUE to indicate a minimum value of 1 for an ascending sequence or


-1026 for a descending sequence.

CYCLE

Specify CYCLE to indicate that the sequence continues to generate values after reaching
either its maximum or minimum value.

After an ascending sequence reaches its maximum value, it generates its minimum
value. After a descending sequence reaches its minimum, it generates its maximum
value.

NOCYCLE

This is the default.

Specify NOCYCLE to indicate that the sequence cannot generate more values after
reaching its maximum or minimum value.

CACHE

Specify how many values of the sequence the database preallocates and keeps in
memory for faster access.

The default is 20. So, if the sequence is starting at 1, the database will cache 1..20 and
sets the sequence pointer at 21.

Database will answer NEXTVAL request from the numbers in memory.

This integer value can have 28 or fewer digits.

The minimum value for this parameter is 2.

For sequences that cycle, this value must be less than the number of values in the cycle.

You cannot cache more values than will fit in a given cycle of sequence numbers.

Therefore, the maximum value allowed for CACHE must be less than the value
determined by the following formula:

(CEIL (MAXVALUE - MINVALUE)) / ABS (INCREMENT)

If a system failure occurs or database is shutdown, then all cached sequence values that
have not been used in committed DML statements are lost. The potential number of lost
values is equal to the value of the CACHE parameter.

When the database restarts, it will look at the current number and loads the next 20 from
the memory, setting the sequence forward by 20. This improves the response but will
lose some numbers.

NOCACHE

Specify NOCACHE to indicate that values of the sequence are not pre-allocated.

If you omit both CACHE and NOCACHE, then the database caches 20 sequence
numbers by default.

Example The following statement creates the sequence customers_seq. This sequence could
be used to provide customer ID numbers when rows are added to the customers table.
CREATE SEQUENCE customers_seq
START WITH 1000
INCREMENT BY 1
NOCACHE
NOCYCLE;
Altering sequences:
Use the ALTER SEQUENCE statement to change the sequence attributes like increment,
minimum and maximum values, cached numbers, and behavior of an existing sequence.
Note that, any alteration in the existing sequence affects only future sequence numbers and will
not affect the existing values which are generated using sequence.
There are certain rules to alter the existing sequence:
To restart the sequence at a different number, you must drop and re-create it.
If you change the INCREMENT BY value before the first invocation of NEXTVAL, then some
sequence numbers will be skipped. Therefore, if you want to retain the original START WITH
value, you must drop the sequence and re-create it with the original START WITH value and the
new INCREMENT BY value.
While altering, Oracle Database performs internal validations to maintain the integrity of
sequence
For ex., a new MAXVALUE cannot be imposed that is less than the current sequence number.
Example This statement sets a new maximum value for the customers_seq sequence, which
was created in the earlier section.
Try it yourself and verify the output:
ALTER SEQUENCE customers_seq
MAXVALUE 1500;
This statement turns on CYCLE and CACHE for the customers_seq sequence:

Try it yourself and verify the output:


ALTER SEQUENCE customers_seq
CYCLE
CACHE 5;
This statement alters the sequence to skip specific values. For example, if the last value used by
the sequence was 100, and you would like to reset the sequence to serve 225 as the next value,
you could execute below set of statements.
ALTER SEQUENCE customers_seq
INCREMENT BY 124;
SELECT customers_seq.nextval FROM dual;
ALTER SEQUENCE customers_seq
INCREMENT BY 1;
Now, the next value which will get generated would be 225 onwards.
Dropping sequences:
Use the DROP SEQUENCE statement to drop/remove the existing sequence from the database.
You can also use DROP SEQUENCE statement to restart a sequence by dropping and then recreating it.
For example, if you have a sequence with a current value of 150 and you would like to restart the
sequence with a value of 27, then you can first drop the sequence and then re-create it with the
same name and using START WITH attribute value of 27.
Example This statement drops/removes the existing customers_seq sequence, which was
created in the earlier section.
DROP SEQUENCE customers_seq;
Using Oracle sequences to generate Primary Key values:
When a sequence number is generated, the sequence is incremented, independent of the
transaction committing or rolling back.
If two users concurrently increment the same sequence, then the sequence numbers each user
acquires may have gaps, because sequence numbers are being generated by the other user.
One user can never acquire the sequence number generated by another user.
After a sequence value is generated by one user, that user can continue to access that value
regardless of whether the sequence is incremented by another user.
Using CURRVAL and NEXTVAL Pseudo columns:
You must qualify CURRVAL and NEXTVAL with the name of the sequence. For ex.,
customers_seq.CURRVAL;
customers_seq.NEXTVAL;

NEXTVAL returns the next available sequence value. It returns a unique value every time it is
referenced, even for different users.
CURRVAL obtains the current sequence value.
NEXTVAL must be issued for that sequence before CURRVAL contains a value.
Finding the next value of a sequence: Example This example selects the next value from the
sequence we created in earlier sections.
SELECT customers_seq.nextval
FROM DUAL;
Inserting sequence values into a table: This example increments the customers sequence
and uses its value for a new customer inserted into the sample table customers:
Try it yourself and verify the output:
INSERT INTO customers
VALUES (customers_seq.nextval, 'John', 'Doe');
Reusing the current value of a sequence: This example adds a new customer with the next
customer ID to the master customers table. It then adds customer details with this number to the
customer detail table:
Try it yourself and verify the output:
INSERT INTO customers
(customer_id, customer_first_name, customer_last_name) VALUES (customers_seq.nextval,
'John', 'Doe');
INSERT INTO customer_details (customer_id, address, phone)
VALUES (customers_seq.currval, 'Washington D.C.', 2359);
Using MySQL AUTO_INCREMENT to generate Primary Key values:
Unlike Oracle, almost all the other databases like MySQL, MS Access, MS SQL Server has auto
increment as one of the attribute while creating tables.
Example for MySQL
The following SQL statement defines the "ID" column to be an auto-increment primary key field in
the "Persons" table:
Try it yourself and verify the output:
CREATE TABLE Persons
(
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (ID)
)

MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature.


By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new
record.

6.13. Understanding XML


This section covers basics of XML. The goal is to give you just enough information to get started
so that you can understand what XML is all about.
What is XML?

XML stands for eXtensible Markup Language

XML is a text-based markup language that is fast becoming the standard for data
interchange on the web.

XML is a markup language much like HTML (You will learn about HTML in chapters from
User Interface topic)

As with HTML, you identify data using tags (identifiers enclosed in angle brackets: <...>).
Collectively, the tags are known as markup.

XML was designed to carry data, not to display data

XML tags are not predefined. You must define your own tags

XML is designed to be self-descriptive

XML is a W3C Recommendation

Need of XML to store data structure:


More about XML...
XML documents form a tree structure that starts at "the root" and branches to
XML document must contain a root element. This element is also called parent
other element present in the XML document.
The elements in an XML document form a document tree. The tree starts at the
branches to the lowest level of the tree.
All elements can have sub elements (child elements):
<root>
<child>
<subchild>....</subchild>
</child>
</root>

"the leaves".
element of all
root and

The terms parent, child, and sibling are used to describe the relationships between elements.
Parent elements have children. Children on the same level are called siblings (brothers or
sisters).
All elements can have text content and attributes (just like in HTML).

The image above represents one book in the XML below:


<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
The root element in the example is <bookstore>. All <book> elements in the document are
contained within <bookstore>.
The <book> element has 4 children: <title>, <author>, <year> and <price>.

You can write your own tags and define your own structure in XML document to store
information. Check a simple example below,
<message>
<to>tove@xyz.com</to>
<from>jane@xyz.com</from>
<subject>Reminder</subject>
<body>Don't forget me this weekend!</body>
</message>
The message structure above is quite self-descriptive. It has sender and receiver information, it
also has a subject and message body.
As in HTML, the <to> tag has a matching end tag: </to>.
The data between the tag and its matching end tag defines an element of the XML data.
Note, that the content of the <to> tag is contained entirely within the scope of the
<message>..</message> tag.
It is this ability for one tag to contain others that lets XML represent hierarchical data structures.
Tags and Attributes:
Tags can also contain attributes additional information included as part of the tag itself.
Try it yourself:
<message to="tove@xyz.com" from="jane@xyz.com" subject="Reminder">
<body>Don't forget me this weekend!</body>
</message>
Note that the XML document always needs to be well-formed. A well-formed document is where
all the tags are properly defined with a with a beginning and end tag and are properly nested.
So you can have <message>..<to>..</to>..</message>,but never can have like
<message>..<to>..</message>..</to>
Comments in XML file:
XML comments just look like HTML comments.
<message to="tove@xyz.com" from="jane@xyz.com" subject="Reminder">
<!-- This is XML comment -->
<body>Don't forget me this weekend!</body>
</message>
The XML prolog:
To complete this basic introduction to XML, note that an XML file always starts with a prolog.
The minimal prolog contains a declaration that identifies the document as an XML document:
<?xml version="1.0"?>
The declaration may also contain additional information:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>


The XML declaration is essentially the same as the HTML header, <html>, except that it uses
<?..?> and it may contain the following attributes:
version: Identifies the version of the XML markup language used in the data. This attribute is
mandatory.
encoding: Identifies the character set used to encode the data.
ISO-8859-1 is Latin-1, the Western European and English language character set. (The default is
8-bit Unicode: UTF-8.)
standalone: Tells whether or not this document references an external entity or an external data
type specification.
If there are no external references, then "yes" is appropriate.
Applicability of XML :
So, XML is used in many aspects of web development, often to simplify data storage and
sharing.
Separates Data from HTML:
If you need to display dynamic data in your HTML document, it will take a lot of work to edit the
HTML each time the data changes.
With XML, data can be stored in separate XML files. This way you can concentrate on using
HTML/CSS for display and layout, and be sure that changes in the underlying data will not
require any changes to the HTML.
With a few lines of JavaScript code, you can read an external XML file and update the data
content of your web page.
Simplifies data sharing:
Specific applications stores data in specific formats. For ex., MS-Word stores data in .doc format,
MS-Excel stores it in .xls format. Whereas, XML stores data in plain text format which can easily
be understood and shared by different applications.
Simplifies data transport:
One of the most time-consuming challenges for developers is to exchange data between
incompatible systems over the Internet.
Exchanging data as XML greatly reduces this complexity, since the data can be read by different
incompatible applications.
Simplifies platform changes:
Upgrading to new systems (hardware or software platforms), is always time consuming. Large
amounts of data must be converted and incompatible data is often lost.
XML data is stored in text format. This makes it easier to expand or upgrade to new operating
systems, new applications, or new browsers, without losing data.
High Data availability:
Different applications can access your data, not only in HTML pages, but also from XML data
sources.
With XML, your data can be available to all kinds of "reading machines" (Hand-held devices,

voice machines, news feeds, etc.), and make it more available for blind people, or people with
other disabilities.

6.14. XML Schema Overview

An XML Schema describes the structure of an XML document.

An XML document with correct syntax is called "Well Formed".

An XML document validated against an XML Schema is both "Well Formed" and "Valid".

<xs:element name="message">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="subject" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The Schema above is interpreted like this:
<xs:element name="message"> defines the element called "message"
<xs:complexType> the "message" element is a complex type
<xs:sequence> the complex type is a sequence of elements
<xs:element name="to" type="xs:string"> the element "to" is of type string (text)
<xs:element name="from" type="xs:string"> the element "from" is of type string
<xs:element name="subject" type="xs:string"> the element "subject" is of type string
<xs:element name="body" type="xs:string"> the element "body" is of type string
Data types supported by XML Schema:
XML Schema supports below data types to define the basic structure of the XML document.

Let us discuss some of the above data types in detail.

String Data type:


The string data type can contain characters, line feeds, carriage returns, and tab characters.
The following is an example of a string declaration in a schema:
<xs:element name="customer" type="xs:string"/>
An element in your document might look like this:
<customer>John Smith</customer>
Or it might look like this:
<customer> John Smith </customer>
You can explore more on String data types at below link:
http://www.w3schools.com/schema/schema_dtypes_string.asp
Date Data type:
The date data type is used to specify a date.
The date is specified in the following form "YYYY-MM-DD" where:
YYYY indicates the year
MM indicates the month
DD indicates the day
The following is an example of a date declaration in a schema:
<xs:element name="start" type="xs:date"/>
An element in your document might look like this:
<start>2002-09-24</start>
Time Data type:
The time data type is used to specify a time.
The time is specified in the following form "hh:mm:ss" where:
hh indicates the hour
mm indicates the minute
ss indicates the second
The following is an example of a time declaration in a schema:
<xs:element name="start" type="xs:time"/>
An element in your document might look like this:
<start>09:00:00</start>
Or it might look like this:
<start>09:30:10.5</start>
You can explore more on Date and Time data types at below link:

http://www.w3schools.com/schema/schema_dtypes_date.asp
Integer Data type:
The integer data type is used to specify a numeric value without a fractional component.
The following is an example of an integer declaration in a schema:
<xs:element name="prize" type="xs:integer"/>
An element in your document might look like this:
<prize>999</prize>
Or it might look like this:
<prize>+999</prize>
Or it might look like this:
<prize>-999</prize>
Or it might look like this:
<prize>0</prize>
Decimal Data type:
The decimal data type is used to specify a numeric value.
The maximum number of decimal digits you can specify is 18.
The following is an example of a decimal declaration in a schema:
<xs:element name="prize" type="xs:decimal"/>
An element in your document might look like this:
<prize>999.50</prize>
Or it might look like this:
<prize>+999.5450</prize>
Or it might look like this:
<prize>-999.5230</prize>
Or it might look like this:
<prize>0</prize>
Or it might look like this:
<prize>14</prize>
Creating and Validating XML Schema:
Let us create Item XSD first, which would define the structure of Item elements that an XML
document can store.
Item.xsd:

<?xml version="1.0" encoding="UTF-8" ?>


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="items">
<xs:complexType>
<xs:sequence>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The value of quality field in XML document should contain positive integers.
ItemList.xml:
<?xml version="1.0" encoding="UTF-8"?>
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="item.xsd">
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>-1</quantity>
<price>10.90</price>
</item>
<item>
<title>La Roque</title>
<note></note>
<quantity>10</quantity>
<price>40.50</price>
</item>
</items>
We are specifically setting quantity value to negative. Now, let us validate this XML document
against the XSD structure we have defined.
Goto, http://www.xmlvalidation.com/ and paste XSD and XML documents in the text area to
validate. It will produce below results with errors for invalid value in quantity field.

Now, perform underlying changes in the XML document for quantity field:
<quantity>10</quantity>
Now, again paste XSD and updated XML document in the text area to validate to above given
link. It will produce below results with successful validation.

6.15. What are XML Namespaces?


XML Namespaces provide a method to avoid element name conflicts.
Refer below example where mixing XML documents carries 2 different type of information.
This XML carries HTML table information:
Try it yourself and verify the output:
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>

</tr>
</table>
This XML carries information about a table (a piece of furniture):
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
If these XML fragments were added together, there would be a name conflict. Both contain a
<table> element, but the elements have different content and meaning.
A user or an XML application will not know how to handle these differences.
Solving the namespace conflict using Prefix:
Name conflicts in XML can easily be avoided using a name prefix.
This XML carries information about an HTML table, and a piece of furniture:
Try it yourself and verify the output:
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
In the example above, there will be no conflict because the two <table> elements have different
names.
The xmlns Attribute

When using prefixes in XML, a so-called namespace for the prefix must be defined.

The namespace is defined by the xmlns attribute in the start tag of an element.

The namespace declaration has the following syntax. xmlns:prefix="URI".

<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>

</h:tr>
</h:table>
<f:table xmlns:f="http://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
In the example above, the xmlns attribute in the <table> tag give the h: and f: prefixes a qualified
namespace.
When a namespace is defined for an element, all child elements with the same prefix are
associated with the same namespace.
Namespaces can be declared in the elements where they are used or in the XML root element:
Try it yourself and verify the output:
<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3schools.com/furniture">
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
Note: The namespace URI is not used by the parser to look up information.
The purpose is to give the namespace a unique name. However, often companies use the
namespace as a pointer to a web page containing namespace information.
Refer below link for more details about XML namespaces:
http://www.w3schools.com/xml/xml_namespaces.asp
Some differences between HTML and XML:

XML is not a replacement for HTML. HTML is a markup language to design web pages.

XML and HTML were designed with different goals:XML was designed to transport and
store data, with focus on what data is. Whereas HTML was designed to display data, with
focus on how data looks.

HTML is about displaying information, while XML is about carrying information.

Unlike HTML, XML tags identify the data rather than specify how to display it. Whereas
an HTML tag says something like, "Display this data in bold font" (<b>...</b>), an XML
tag acts like a field name in your program. It puts a label on a piece of data that identifies
it (for example, <message>...</message>).

HTML is not case-sensitive where as XML is case-sensitive.

HTML tags are predefined where as XML tags are not predefined.

Usage of XML during Web application development:


You will see that most of the Web applications (mostly Java based) will have XML documents as
configuration files for the applications.
For Example, Hibernate is an ORM tool. All the configurations and mappings are defined in form
of an XML document with predefined tags to represent the configuration information.
Check out below sample hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- Related to the connection START -->
<property name="connection.driver_class">Driver </property>
<property name="connection.url">URL </property>
<property name="connection.user">user </property>
<property name="connection.password">password</property>
<!-- Related to the connection END -->
<!-- Related to hibernate properties START -->
<property name="show_sql">true/false</property>
<property name="dialect">Database dialect class</property>
<property name="hbm2ddl.auto">create/update or what ever</property>
<!-- Related to hibernate properties END-->
<!-- Related to mapping START-->
<mapping resource="hbm file 1 name .xml" / >
<mapping resource="hbm file 2 name .xml" / >
<!-- Related to the mapping END -->
</session-factory>
</hibernate-configuration>
Similarly, Java Web Applications also will have a simple configuration file called web.xml. A Web
container will read these configurations to know about the web applications deployed/hosted.
Check out below sample web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-

app_2_4.xsd"
version="2.4">
<display-name>HelloWorld Application</display-name>
<description>This is a simple web application</description>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>examples.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
Summary
XML is pretty simple and very flexible. It has many uses yet to be discovered, and we are only
beginning to scratch the surface of its potential. It is the foundation for a great many standards
yet to come, providing a common language that different computer systems can use to exchange
data with one another. As each industry group comes up with standards for what it wants to say,
computers will begin to link to each other in ways previously unimaginable.
Test your knowledge by taking this Sample Quiz.

6.17. Assignments
Online Toy Store Management System
You need to submit the solutions for the problems in the link provided at the end of this
chapter below.

Write SQL queries for following cases.


Display all numeric values to a precision of 2 decimal digits.
All comparisons should be case insensitive.
1. Display city and total number of stores present in each city.
2. Display store which is open for maximum time.
3. Display store which has been open at least two hours before current time.
4. Display stores which are either open before 9: 00 am or close after 9 :00 pm.
5. Display store names that start with 'P' and contain 'e' also some where in the name.

6. Display city with second highest number of stores.


7. Display all stores whose name is starting with 'Kid'.
8. Display all outdoor toys.
9. Display total number of toys based on their category.
10. Display stores that sell only outdoor toys.
11. Display number of stores based on category of Toys they sell.
12. Display toys from highest to lowest rating.
13. Display toys that are not given any rating.
14. Display average rating of toys.
15. Display stores that sell highest rated toys.
16. Display toys age group wise.
17. Display stores that sell toys for children of lowest age group.
18. Display average rating for toys that belong to the lowest age group.
19. Display toys with maximum discount.
20. Display calculated price after applying discount for discounted toys.
21. Display toys with lowest price or lowest discounted price.
22. Display difference between the highest priced and lowest priced toy.
23. Display stores that sell the highest priced toys
24. Display stores that sell the lowest discounted priced toys.
25. Display toys whose price is greater than average price of all toys.
26. Display the category of toys which is having its average price greater than minimum toy
price.
27. Display city that has the highest count of toys for the minimum age group.
28. Display city names and the count of toy stores and toys with them city wise.

29. Display toy store name, toy name and total number of each toy available in each toy
store.
30. Display toy store which has maximum available quantity of lowest priced toys.
31. Display toy store with toy name and available quantity details for which stock is present
but available quantity is <12.
32. Display age group wise total number of toys available for age group above 5.
33. Display toy store name where total number of toys available is greater than maximum
available quantity for any toy.
34. Display toy store names that belong to the same city.
35. Display toy names that have the same price.
36. Display toy store which has not stored any toys yet.
37. Display Toys that are not available in any toy store yet.
38. Display Toy store name with Toy names it sells, also those stores which do not sell any
toys yet should be displayed.
39. Display Toy store name with Toy names it sells, also those toys which are not sold in any
toy store yet should be displayed.
40. Display Toy store name with Toy names it sells, also those stores which do not sell any
toys yet should be displayed including those toys which are not sold in any toy store yet.
41. Display stores that do not sell toys below rating of 3.
42. Display stores that do not sell toys that do not have maximum rating.
43. Display stores that do not give any discount.
44. Display toys that are sold only in one store exclusively i.e. not available in any other
store.
45. Display details of toys having '_' (underscore) in their name.
46. Display details of Toys that have more than 6 characters in their name.
47. Display all toy prices in words like One Hundred Fifty
48. Display all Toy store and Toy names without any spaces like PingpongBall.

49. Display Toy name, Price and available quantity for Puzzles and More in following format
Toy name**Price**Available Quantity. For ex: Bat**500**3.
50. Display count of store names that start with the same first alphabet, also display that
alphabet.
Execute the above problems as per the instructions and submit the code along with the
screen print of output through the link provided.
Attachment Size : <10 MB
Acceptable file formats : .doc/.docx
Kindly Note:
File uploaded by you should be named with your CT Reference Number and mention your CT ref
number and e-mail id at the top of the your Screen print document.
Only one document can be uploaded in the link provided and multiple submissions are not
allowed. Make sure that you have completed all these problems and upload all your solutions as
a single document.
Example: ct12345678901.doc
Click here to submit your solution.

You might also like