1.6.info Management Basics
1.6.info Management Basics
1.6.info Management Basics
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.
File
Database
-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)
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 :
Improved data access to users through use of host and query language
Disadvantages of DBMS :
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:
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:
Now both the Student table and Subject table are normalized to first normal form.
Order_Detail Table :
Sale_Detail Table :
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
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.
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:
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:
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
(
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
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.
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 '*' :
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
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.
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.
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:
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:
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.
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:
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
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.
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.
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.
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:
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::
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:
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) ]
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
Now let's execute and understand the use of rollback and savepoint:
Hence, the changes made after S2 are removed from the database.
Hence, the changes made after S1 are removed from the database.
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
This integer value can be any positive or negative integer, but it cannot be 0.
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.
START WITH
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.
MAXVALUE
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.
NOMAXVALUE
MINVALUE
MINVALUE must be less than or equal to START WITH and must be less than
MAXVALUE.
NOMINVALUE
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
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.
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:
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:
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)
)
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 tags are not predefined. You must define your own tags
"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).
You can write your own tags and define your own structure in XML document to store
information. Check a simple example below,
<message>
<to>[email protected]</to>
<from>[email protected]</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="[email protected]" from="[email protected]" 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="[email protected]" from="[email protected]" 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:
voice machines, news feeds, etc.), and make it more available for blind people, or people with
other disabilities.
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.
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:
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.
</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.
<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.
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 tags are predefined where as XML tags are not predefined.
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.
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.