Beruflich Dokumente
Kultur Dokumente
Quality information needs data management. Data management means managing creation,
reading, update & deletion of data.
Approaches to data management:
File
Database
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 client–server 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:
Order_Detail Table :
Sale_Detail Table :
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:
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
'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.
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
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 it’s 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 table’s 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:
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.
Similarly, let's execute the following query:
Hence, the changes made after S1 are removed from the database.
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
START WITH
MAXVALUE
NOMAXVALUE
MINVALUE
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
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 re-
creating 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.
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.
<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:
XML Schema supports below data types to define the basic structure of the XML document.
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.
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.