Data Base System Notes
Data Base System Notes
1. Databases Overview: Basic Concept; File Processing & Database Approach, Database Applications,
2. Database Architecture: DB Development Process, Three Schema Architecture, Data Modeling, E-R Modeling
(Basic Concepts)
3. Logical Design: E-R Modeling (Entities, Attributes, Relationships; Cardinality Constraints), RDBMS: Logical
5. The Relational Model: Types, Relations, Relational Algebra, Relational Calculus, Integrity
7. Normalization: Third Normal Form (3NF), Boyce Codd Normal Form (BCNF)
9. DB Development & Management: Introduction to SQL and Basic Commands, SQL Integrity Constraints.
11. SQL Commands: Saving, Listing, Editing, Restoring Table Contents; Logical Operators, Management
Commands
12. Arithmetic Operators, Complex Queries and SQL Functions, Aggregate Function, Grouping Functions 13.
14. Clint-Server & Distributed Environment, ODBC, Bridges, and Connectivity Issues.
15. Concurrency Control with Locking, Serializability, Deadlocks, Database Recovery Management.
Advantages, Security and Authorization. Physical Design: Storage and File Structure, Efficiency And Tuning
What is Data?
Data is nothing but facts and statistics stored or free flowing over a network, generally it's raw and
unprocessed.
Database:
Database is a collection of related data and data is a collection of facts and figures that can be processed to
produce information.
A Database is a collection of related data organized in a way that data can be easily accessed, managed and
updated.
Database can be software based or hardware based, with one sole purpose, storing data.
During early computer days, data was collected and stored on tapes, which were mostly write-only, which means
once data is stored on it, it can never be read again. They were slow and bulky, and soon computer scientists
realized that they needed a better solution to this problem.
What is DBMS?
A DBMS is software that allows creation, definition and manipulation of database, allowing users to store, process
and analyze data easily.
DBMS provides us with an interface or a tool, to perform various operations like creating database, storing data in
it, updating data, creating tables in the database and a lot more.
DBMS also provides protection and security to the databases. It also maintains data consistency in case of multiple
users.
Here are some examples of popular DBMS used these days:
Data Base Application:
MySql
Oracle
SQL Server
IBM DB2
PostgreSQL
Amazon SimpleDB (cloud based) etc.
Characteristics:
Traditionally, data was organized in file formats. DBMS was a new concept then, and all the research was done to
make it overcome the deficiencies in traditional style of data management. A modern DBMS has the following
characteristics –
1. Real-world entity
2. Relation-based tables
3. Isolation of data and application
4. Less redundancy
5. Consistency
6. Query Language
7. ACID Properties
8. Multiuser and Concurrent Access
9. Multiple views
10. Security
Database Approach
When an organization uses the database approach, many programs and users share the data in
the database. A school’s database most likely at a minimum contains data about students,
instructors, schedule of classes, and student schedules. As shown in the above image, various
areas within the school share and interact with the data in this database. The database does
secure its data, however, so that only authorized users can access certain data items. While a user
is working with the database, the DBMS resides in the memory of the computer.
Advantages of DBMS
Segregation of application program.
Minimal data duplicity or data redundancy.
Easy retrieval of data using the Query Language.
Reduced development time and maintenance need.
With Cloud Datacenters, we now have Database Management Systems capable of storing almost infinite data.
Seamless integration into the application programming languages which makes it very easier to add a database
to almost any application or website.
1. Hardware
2. Software
3. Data
4. Procedures
5. Database Access Language
Evolution of DB.
Database Architecture:
A Database Management system is not always directly available for users and applications to access and store data
in it. A Database Management system can be
Centralized (all the data stored at one location),
decentralized(multiple copies of database at different locations)
or hierarchical, depending upon its architecture.
1-tier DBMS architecture also exist, this is when the database is directly available to the user for using it to store
data. Generally such a setup is used for local application development, where programmers communicate directly
with the database for quick response.
It’s the database designers who design the schema to help programmers
because there are three different ways to look at them. Each schema
1) User view,
2) logical schema,
3) physical schema.
1) User views
User views specify which users are permitted access to what data in a database.
For example, an employee database might contain employee names, addresses, and phone numbers.
2) Logical Schema
A database's logical schema is its overall logical plan. This schema is developed with diagrams that define the
content of database tables and describe how the tables are linked together for data access.
3) Physical Schema
The physical schema of a database refers to how data is stored on the computer on which it resides.
Data Modeling:
Data models define how the logical structure of a database is modeled. Data Models are fundamental entities
to introduce abstraction in a DBMS. Data models define how data is connected to each other and how they
are processed and stored inside the system.
The Relational Model is the most widely used database model, there are other models too:
Hierarchical Model
Network Model
Entity-relationship Model
Relational Model
Hierarchical Model
This database model organizes data into a tree-like-structure, with a single root, to which all the other data is
linked. The hierarchy starts from the Root data, and expands like a tree, adding child nodes to the parent nodes.
In this model, a child node will only have a single parent node.
This model efficiently describes many real-world relationships like index of a book, recipes etc.
In hierarchical model, data is organized into tree-like structure with one one-to-many relationship between two
different types of data, for example, one department can have many courses, many professors and of-course many
students.
Network Model
This is an extension of the Hierarchical model. In this model data is organized more like a graph, and are allowed to
have more than one parent node.
In this database model data is more related as more relationships are established in this database model. Also, as
the data is more related, hence accessing the data is also easier and fast. This database model was used to map
many-to-many data relationships.
This was the most widely used database model, before Relational Model was introduced.
Entity-Relationship Model:
Entity-Relationship (ER) Model is based on the notion of real-world entities and relationships among them.
While formulating real-world scenario into the database model, the ER Model creates entity set, relationship
set, general attributes, and constraints.
In this database model, relationships are created by dividing object of interest into entity and its characteristics
into attributes.
Different entities are related using relationships.
ER Model is based on −
Entity − An entity in an ER Model is a real-world entity having properties called attributes. Every attribute is defined
by its set of values called domain. For example, in a school database, a student is considered as an entity. Student
has various attributes like name, age, class, etc.
Relationship − The logical association among entities is called relationship. Relationships are mapped with entities in
various ways. Mapping cardinalities define the number of association between two entities.
Mapping cardinalities −
o one to one
o one to many
o many to one
o many to many
Relational Model:
The most popular data model in DBMS is the Relational Model.
It is more scientific a model than others.In this model, data is
organized in two-dimensional tables and the relationship is
maintained by storing a common field.The basic structure of
data in the relational model is tables. All the information related
to a particular type is stored in rows of that table.
Hence, tables are also known as relations in relational model.
The ER model defines the conceptual view of a database. It works around realworld entities and the
associations among them. At view level, the ER model is considered a good option for designing databases.
Entity:
An entity can be a real-world objectthat can be easily identifiable. For example, in a school database, students,
teachers, classes, and courses offered can be considered as entities. All these entities have some attributes or
properties that give them their identity.
An entity set is a collection of similar types of entities. An entity set may contain entities with attribute sharing
similar values. For example, a Students set may contain all the students of a school; likewise a Teachers set
may contain all the teachers of a school from all faculties. Entity sets need not be disjoint.
Attributes:
Entities are represented by means of their properties called attributes. All attributes have values. For example,
a student entity may have name, class, and age as attributes.
There exists a domain or range of values that can be assigned to attributes. For example, a student's name
cannot be a numeric value. It has to be alphabetic. A student's age cannot be negative, etc.
Types of Attributes:
Simple attribute: Simple attributes are atomic values, which cannot be divided further. For example, a
student's phone number is an atomic value of 10 digits.
Composite attribute:
Composite attributes are made of more than one simple attribute. For example, a
student's complete name may have first name and last-named.
Derived attribute:
Derived attributes are the attributes that do not exist in the physical database, but their
values are derived from other attributes present in the database. For example, average
salary in a department should not be saved directly in the database, instead it can be
derived. For another example, age can be derived from data_of_birth.
Single-value attribute:
Single-value attributes contain single value.
For example: Social_Security_Number.
Multi-value attributes:
Multi-value attributesmay contain more than one value. For example, a person can have
more than one phone number, email_address, etc.
Relationship Set :
A set of relationships of similar type is called a relationship set. Like entities, a relationship too can have
attributes. These attributes are called descriptive attributes.
Degree of Relationship:
The number of participating entities in a relationship defines the degree of the relationship.
Mapping Cardinalities:
Cardinality defines the number of entities in one entity set, which can be associated with the number of
entities of other set via relationship set.
One-to-one:
One entity from entity set
A can be associated with at
most one entity of entity
set B and vice versa.
One-to-many:
One entity from entity set
A can be associated with more
than one entities of entity set B,
however an entity from entity set
B can be associated with at most one entity.
Many-to-one:
Msc IT (3rd Term) ILM College Jauharabad M.Wasim
P a g e | 10
Many-to-many:
One entity from A can be associated
with more than one entity from B and vice versa.
Concepts
Tables − In relational data model, relations are saved in the format of Tables. This format stores the relation
among entities. A table has rows and columns, where rows represents records and columns represent the
attributes.
Tuple − A single row of a table, which contains a single record for that relation is called a tuple.
Relation instance − A finite set of tuples in the relational database system represents relation instance.
Relation instances do not have duplicate tuples.
Relation schema − A relation schema describes the relation name (table name), attributes, and their names.
Relation key − Each row has one or more attributes, known as relation key, which can identify the row in the
relation (table) uniquely.
Attribute domain − Every attribute has some pre-defined value scope, known as attribute domain.
Constraints
Every relation has some conditions that must hold for it to be a valid relation. These conditions are
called Relational Integrity Constraints. There are three main integrity constraints −
Key constraints
Domain constraints
Key Constraints
There must be at least one minimal subset of attributes in the relation, which can identify a tuple uniquely.
This minimal subset of attributes is called keyfor that relation. If there is more than one such minimal subset,
these are called candidate keys.
In a relation with a key attribute, no two tuples can have identical values for key attributes.
Domain Constraints
Attributes have specific values in real-world scenario. For example, age can only be a positive integer. The
same constraints have been tried to employ on the attributes of a relation. Every attribute is bound to have a
specific range of values. For example, age cannot be less than zero and telephone numbers cannot contain a
digit outside 0-9.
Referential integrity constraint states that if a relation refers to a key attribute of a different or same relation,
then that key element must exist.
The primary operations that we can perform using relational algebra are:
1. Select
2. Project
3. Union
4. Set Different
5. Cartesian product
6. Rename
Syntax: σp(r)
σ
Where, represents the Select Predicate, r is the name of relation (table name in which you want to look for
data), and p is the prepositional logic, where we specify the conditions that must be satisfied by the data. In
prepositional logic, one can use unary and binary operators like =, <, > etc., to specify the conditions.
Let's take an example of the Student table we specified above in the Introduction of relational algebra, and fetch
data for students with age more than 17.
σage> 17 (Student)
This will fetch the tuples (rows) from table Student, for which age will be greater than 17.
You can also use, and, or etc. operators, to specify two conditions, for example,
∏Name, Age(Student)
Above statement will show us only the Name and Age columns for all the rows of data in Student table.
Natural Join
Outer Join
Theta join etc
Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural query language, that is, it tells what
to do but never explains how to do it.
Notation − {T | Condition}
For example −
{ T.name | Author(T) AND T.article = 'database' }
Output − Returns tuples with 'name' from Author who has written article on 'database'.
TRC can be quantified. We can use Existential (∃) and Universal Quantifiers (∀).
For example −
{ R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}
Output − The above query will yield the same result as the previous one.
Notation −
Where a1, a2 are attributes and P stands for formulae built by inner attributes.
For example −
{< article, page, subject >| ∈TutorialsPoint∧ subject = 'database'}
Output − Yields Article, Page, and Subject from the relation TutorialsPoint, where subject is database.
Just like TRC, DRC can also be written using existential and universal quantifiers. DRC also involves relational
operators.
The expression power of Tuple Relation Calculus and Domain Relation Calculus is equivalent to Relational
Algebra.
And the primary key specified for the entity in the ER model, will
become the primary key for the table in relational model.
For example, for the below ER Diagram in ER Model,
A table with name Student will be created in relational model, which will have 4
columns, id, name, age, address and id will be the primary key for this table.
As discuss above, entity gets mapped to table, hence we will create table for Teacher and a table for Student with
all the attributes converted into columns.
Now, an additional table will be created for the relationship, for example StudentTeacher or give it any name you
like. This table will hold the primary key for both Student and Teacher, in a tuple to describe the relationship, which
teacher teaches which student.
If there are additional attributes related to this relationship, then they become the columns for this table, like
subject name.
Also proper foreign key constraints must be set for all the tables.
Points to Remember
Similarly we can generate relational database schema using the ER diagram. Following are some key points to keep
in mind while doing so:
1. Entity gets converted into Table, with all the attributes becoming fields (columns) in the table.
2. Relationship between entities is also converted into table with primary keys of the related entities also stored
in it as foreign keys.
3. Primary Keys should be properly set.
4. For any relationship of Weak Entity, if primary key of any other entity is included in a table, foreign key
constraint must be defined.
Normalization of Database:
Database Normalization is a technique of organizing the data in the database. Normalization is a systematic
approach of decomposing tables to eliminate data redundancy(repetition) and undesirable characteristics like
Insertion, Update and Deletion Anomalies. It is a multi-step process that puts data into tabular form, removing
duplicated data from the relation tables.
If a table is not properly normalized and have data redundancy then it will not only eat up extra memory space but
will also make it difficult to handle and update the database, without facing data loss. Insertion, Updation and
Deletion Anomalies are very frequent if database is not normalized. To understand these anomalies let us take an
example of a Student table.
In the table above, we have data of 4 Computer Sci. students. As we can see, data for the fields branch, hod(Head of
Department) and office_tel is repeated for the students who are in the same branch in the college, this is Data
Redundancy.
Insertion Anomaly
Suppose for a new admission, until and unless a student opts for a branch, data of the student cannot be inserted,
or else we will have to set the branch information as NULL.
Also, if we have to insert data of 100 students of same branch, then the branch information will be repeated for all
those 100 students.
These scenarios are nothing but Insertion anomalies.
Updation Anomaly
What if Mr. X leaves the college? or is no longer the HOD of computer science department? In that case all the
student records will have to be updated, and if by mistake we miss any record, it will lead to data inconsistency.
This is Updation anomaly.
Deletion Anomaly
In our Student table, two different information’s are kept together, Student information and Branch information.
Hence, at the end of the academic year, if student records are deleted, we will also lose the branch information.
This is Deletion anomaly.
We re-arrange
as below, to convert it
Each attribute must contain only a single value from its pre-defined domain.
Prime attribute − An attribute, which is a part of the candidate-key, is known as a prime attribute.
Non-prime attribute − An attribute, which is not a part of the prime-key, is said to be a non-prime attribute.
If we follow second normal form, then every non-prime attribute should be fully functionally dependent on
prime key attribute. That is, if X →A holds, then there should not be any proper subset Y of X, for which
We see here in Student_Project relation that the prime key attributes are Stu_ID and Proj_ID. According to
the rule, non-key attributes, i.e. Stu_Name and Proj_Name must be dependent upon both and not on any of
the prime key attribute individually. But we find that Stu_Name can be identified by Stu_ID and Proj_Name
can be identified by Proj_ID independently. This is called partial dependency, which is not allowed in Second
Normal Form.
We broke the relation in two as depicted in the above picture. So there exists no partial dependency.
o X is a superkey or,
o A is prime attribute.
We find that in the above Student_detail relation, Stu_ID is the key and only prime key attribute. We find
that City can be identified by Stu_ID as well as Zip itself. Neither Zip is a superkey nor is City a prime attribute.
Additionally, Stu_ID→ Zip → City, so there exists transitive dependency.
To bring this relation into third normal form, we break the relation into two relations as follows –
In the above image, Stu_ID is the super-key in the relation Student_Detail and Zip is the super-key in the
relation ZipCodes. So,
Stu_ID→Stu_Name, Zip
and
Zip → City
A database is usually a fundamental component of the information system, especially in business oriented
systems. Thus database design is part of system development. The following picture shows how database
design is involved in the system development lifecycle.
The phases in the middle of the picture (Database Design, Database Implementation) are the phases that you
concentrate on in the Database Design course. The other phases are briefly described. They are part of the
contents of the Systems Analysis and Design courses, for example.
There are various methods of how the different phases of information system design, analysis and
implementation can be done. Here the main tasks or goals are described but no method is introduced.
Database Planning
The database planning includes the activities that allow the stages of the database system development
lifecycle to be realized as efficiently and effectively as possible. This phase must be integrated with the overall
Information System strategy of the organization.
The very first step in database planning is to define the mission statement and objectives for the database
system. That is the definition of:
- the major aims of the database system
- the purpose of the database system
- the supported tasks of the database system
- the resources of the database system
Systems Definition
In the systems definition phase, the scope and boundaries of the database application are described. This
description includes:
- links with the other information systems of the organization
- what the planned system is going to do now and in the future
- who the users are now and in the future.
The major user views are also described. i.e. what is required of a database system from the perspectives of
particular job roles or enterprise application areas.
During the requirements collection and analysis phase, the collection and analysis of the information about
the part of the enterprise to be served by the database are completed. The results may include eg:
- the description of the data used or generated
- the details how the data is to be used or generated
- any additional requirements for the new database system
Database Design
In the conceptual database design phase, the model of the data to be used independent of all physical
considerations is to be constructed. The model is based on the requirements specification of the system.
In the logical database design phase, the model of the data to be used is based on a specific data model, but
independent of a particular database management system is constructed. This is based on the target data
model for the database e.g. relational data model.
In the physical database design phase, the description of the implementation of the database on secondary
storage is created. The base relations, indexes, integrity constraints, security, etc. are defined using the SQL
language.
This in an optional phase. When there is a need for a new database management system (DBMS), this phase is
done. DBMS means a database system like Access, SQL Server, MySQL, Oracle.
In this phase the criteria for the new DBMS are defined. Then several products are evaluated according to the
criteria. Finally the
recommendation for the selection is decided.
Application Design
In the application design phase, the design of the user interface and the application programs that use and
process the database are defined and designed.
Protyping
The purpose of a prototype is to allow the users to use the prototype to identify the features of the system
using the computer.
There are horizontal and vertical prototypes. A horizontal prototype has many features (e.g. user interfaces)
but they are not working. A vertical prototype has very few features but they are working. See the following
picture.
Implementation
During the implementation phase, the physical realization of the database and application designs are to be
done. This is the programming phase of the systems development.
This phase is needed when a new database is replacing an old system. During this phase the existing data will
be transferred into the new database.
Testing
Before the new system is going to live, it should be thoroughly tested. The goal of testing is to find errors! The
goal is not to prove the software is working well.
Operational Maintenance
The operational maintenance is the process of monitoring and maintaining the database system.
Monitoring means that the performance of the system is observed. If the performance of the system falls
below an acceptable level, tuning or reorganization of the database may be required.
Maintaining and upgrading the database system means that, when new requirements arise, the new
development lifecycle will be done.
Attributes become columns in the physical database. Choose an appropriate data type for each of the columns.
Unique identifiers become columns that are not allowed to have NULL values. These are referred to as primary
keys in the physical database. Consider creating a unique index on the identifiers to enforce uniqueness.
Spaces are not allowed in entity names in a physical schema because these names must translate into SQL calls to create
the tables. Table names should therefore conform to SQL naming rules.
Because primary key attributes are complete inventions, they can be of any indexable data type. (Each database engine
has different rules about which data types can be indexable.) Making primary keys of type INT is almost purely arbitrary.
It is almost arbitrary because it is actually faster to search on numeric fields in many database engines. However, one
could just have well have chosen CHAR as the type for the primary key fields. The bottom line is that this choice should
be driven by the criteria for choosing identifiers.
The most common relationship is the 1-to-M relationship. This relationship is mapped by placing the primary key on the
"one" side of the relationship into the table on the "many" side.
1-to-1 relationships should be mapped by picking one of the tables and giving it a foreign key column that matches the
primary key from the other table. In theory, it does not matter which table is chosen, but practical considerations may
dictate which column makes the most sense as a foreign key.
CREATE table CD (CDId INT NOT NULL, RecordLabelId INT, CDTitle TEXT, PRIMARY KEY (CDId))
CREATE table Artist (ArtistId INT NOT NULL, ArtistName TEXT, PRIMARY KEY (ArtistId))
CREATE table Song (SongId INT NOT NULL, CDId INT, SongName TEXT, PRIMARY KEY (SongId))
CREATE table RecordLabel (RecordLabelId INT NOT NULL, RecordLabelName TEXT, PRIMARY KEY (RecordLabelId))
Example script to create the database in MySQL
Data models are meant to be database independent. These techniques and data models may therefore be applied not
only to MySQL, but also to Oracle, Sybase, Ingres or any other relational database engine.
Query optimization:
A query optimizer is a critical database management system (DBMS) component that analyzes Structured
Query Language (SQL) queries and determines efficient execution mechanisms. A query optimizer generates
one or more query plans for each query, each of which may be a mechanism used to run a query. The most
efficient query plan is selected and used to run the query.
Sql Statements are used to retrieve data from the database. We can get same results by writing different sql
queries. But use of the best query is important when performance is considered. So you need to sql query
tuning based on the requirement. Here is the list of queries which we use reqularly and how these sql queries
can be optimized for better performance.
Example:
The sql query becomes faster if you use the actual columns names in SELECT statement instead
of than '*'.
For Example: Write the query as
SELECT id, first_name, last_name, age, subject FROM student_details;
Instead of:
SELECT * FROM student_details;
ODBC
Stands for "Open Database Connectivity." With all the different types of databases available, such as Microsoft Access,
Filemaker, and MySQL, it is important to have a standard way of transferring data to and from each kind of database. For
this reason, the SQL Access group created the ODBC standard back in 1992. Any application that supports ODBC can
access information from an ODBC-compatible database, regardless of what database management system the database
uses.
For a database to be ODBC-compatible, it must include an ODBC database driver. This allows other applications to
connect to and access information from the database with a standard set of commands. The driver translates standard
ODBC commands into commands understood by the database's proprietary system. Thanks to ODBC, a single application
(such as Web server program) can access information from several different databases using the same set of commands.
Binary Locks − A lock on a data item can be in two states; it is either locked or unlocked.
Shared/exclusive − This type of locking mechanism differentiates the locks based on their uses. If a lock is
acquired on a data item to perform a write operation, it is an exclusive lock. Allowing more than one
transaction to write on the same data item would lead the database into an inconsistent state. Read locks are
shared because no data value is being changed.
Two-phase locking has two phases, one is growing, where all the locks are being acquired by the transaction; and the
second phase is shrinking, where the locks held by the transaction are being released.
To claim an exclusive (write) lock, a transaction must first acquire a shared (read) lock and then upgrade it to an
exclusive lock.
Timestamp-based Protocols
The most commonly used concurrency protocol is the timestamp based protocol. This protocol uses either system time
or logical counter as a timestamp.
Lock-based protocols manage the order between the conflicting pairs among transactions at the time of execution,
whereas timestamp-based protocols start working as soon as a transaction is created.
Every transaction has a timestamp associated with it, and the ordering is determined by the age of the transaction. A
transaction created at 0002 clock time would be older than all other transactions that come after it. For example, any
transaction 'y' entering the system at 0004 is two seconds younger and the priority would be given to the older one.
In addition, every data item is given the latest read and write-timestamp. This lets the system know when the last ‘read
and write’ operation was performed on the data item.
Time-stamp ordering rules can be modified to make the schedule view serializable.
DBMS - Deadlock
In a multi-process system, deadlock is an unwanted situation that arises in a shared resource environment, where a
process indefinitely waits for a resource that is held by another process.
For example, assume a set of transactions {T0, T1, T2, ...,Tn}. T0 needs a resource X to complete its task. Resource X is
held by T1, and T1 is waiting for a resource Y, which is held by T2. T2 is waiting for resource Z, which is held by T0. Thus,
all the processes wait for each other to release resources. In this situation, none of the processes can finish their task.
This situation is known as a deadlock.
Deadlocks are not healthy for a system. In case a system is stuck in a deadlock, the transactions involved in the
deadlock are either rolled back or restarted.
Deadlock Prevention
To prevent any deadlock situation in the system, the DBMS aggressively inspects all the operations, where transactions
are about to execute. The DBMS inspects the operations and analyzes if they can create a deadlock situation. If it finds
that a deadlock situation might occur, then that transaction is never allowed to be executed.
There are deadlock prevention schemes that use timestamp ordering mechanism of transactions in order to
predetermine a deadlock situation.
Wait-Die Scheme
In this scheme, if a transaction requests to lock a resource (data item), which is already held with a conflicting lock by
another transaction, then one of the two possibilities may occur −
If TS(Ti) < TS(Tj) − that is Ti, which is requesting a conflicting lock, is older than Tj − then Ti is allowed to wait until
the data-item is available.
If TS(Ti) > TS(tj) − that is Ti is younger than Tj − then Ti dies. Ti is restarted later with a random delay but with the
same timestamp.
This scheme allows the older transaction to wait but kills the younger one.
Wound-Wait Scheme
In this scheme, if a transaction requests to lock a resource (data item), which is already held with conflicting lock by
some another transaction, one of the two possibilities may occur −
If TS(Ti) < TS(Tj), then Ti forces Tj to be rolled back − that is TiwoundsTj. Tj is restarted later with a random delay
but with the same timestamp.
If TS(Ti) > TS(Tj), then Ti is forced to wait until the resource is available.
This scheme, allows the younger transaction to wait; but when an older transaction requests an item held by a younger
one, the older transaction forces the younger one to abort and release the item.
In both the cases, the transaction that enters the system at a later stage is aborted.
Deadlock Avoidance
Aborting a transaction is not always a practical approach. Instead, deadlock avoidance mechanisms can be used to
detect any deadlock situation in advance. Methods like "wait-for graph" are available but they are suitable for only
those systems where transactions are lightweight having fewer instances of resource. In a bulky system, deadlock
prevention techniques may work well.
Wait-for Graph
This is a simple method available to track if any deadlock situation may arise. For each transaction entering into the
system, a node is created. When a transaction Ti requests for a lock on an item, say X, which is held by some other
transaction Tj, a directed edge is created from Ti to Tj. If Tj releases item X, the edge between them is dropped and
Ti locks the data item.
The system maintains this wait-for graph for every transaction waiting for some data items held by others. The system
keeps checking if there's any cycle in the graph.
First, do not allow any request for an item, which is already locked by another transaction. This is not always
feasible and may cause starvation, where a transaction indefinitely waits for a data item and can never acquire
it.
The second option is to roll back one of the transactions. It is not always feasible to roll back the younger
transaction, as it may be important than the older one. With the help of some relative algorithm, a transaction
is chosen, which is to be aborted. This transaction is known as the victim and the process is known as victim
selection.
We can have checkpoints at multiple stages so as to save the contents of the database periodically.
A state of active database in the volatile memory can be periodically dumped onto a stable storage, which may
also contain logs and active transactions and buffer blocks.
<dump> can be marked on a log file, whenever the database contents are dumped from a non-volatile memory
to a stable one.
Recovery
When the system recovers from a failure, it can restore the latest dump.
It can recover the system by consulting undo-redo lists to restore the state of all transactions up to the last
checkpoint.
Remote backup &minu; Here a backup copy of the database is stored at a remote location from where it can be
restored in case of a catastrophe.
Alternatively, database backups can be taken on magnetic tapes and stored at a safer place. This backup can
later be transferred onto a freshly installed database to bring it to the point of backup.
Grown-up databases are too bulky to be frequently backed up. In such cases, we have techniques where we can restore
a database just by looking at its logs. So, all that we need to do here is to take a backup of all the logs at frequent
intervals of time. The database can be backed up once a week, and the logs being very small can be backed up every
day or as frequently as possible.
Remote Backup
Remote backup provides a sense of security in case the primary location where the database is located gets destroyed.
Remote backup can be offline or real-time or online. In case it is offline, it is maintained manually.
Online backup systems are more real-time and lifesavers for database administrators and investors. An online backup
system is a mechanism where every bit of the real-time data is backed up simultaneously at two distant places. One of
them is directly connected to the system and the other one is kept at a remote place as backup.
As soon as the primary database storage fails, the backup system senses the failure and switches the user system to the
remote storage. Sometimes this is so instant that the users can’t even realize a failure.
Crash Recovery
DBMS is a highly complex system with hundreds of transactions being executed every second. The durability and
robustness of a DBMS depends on its complex architecture and its underlying hardware and system software. If it fails
or crashes amid transactions, it is expected that the system would follow some sort of algorithm or techniques to
recover lost data.
Failure Classification
To see where the problem has occurred, we generalize a failure into various categories, as follows −
Transaction failure
A transaction has to abort when it fails to execute or when it reaches a point from where it can’t go any further. This is
called transaction failure where only a few transactions or processes are hurt.
Logical errors − Where a transaction cannot complete because it has some code error or any internal error
condition.
System errors − Where the database system itself terminates an active transaction because the DBMS is not
able to execute it, or it has to stop because of some system condition. For example, in case of deadlock or
resource unavailability, the system aborts an active transaction.
System Crash
There are problems − external to the system − that may cause the system to stop abruptly and cause the system to
crash. For example, interruptions in power supply may cause the failure of underlying hardware or software failure.
Disk Failure
In early days of technology evolution, it was a common problem where hard-disk drives or storage drives used to fail
frequently.
Disk failures include formation of bad sectors, unreachability to the disk, disk head crash or any other failure, which
destroys all or a part of disk storage.
Storage Structure
We have already described the storage system. In brief, the storage structure can be divided into two categories −
Volatile storage − As the name suggests, a volatile storage cannot survive system crashes. Volatile storage
devices are placed very close to the CPU; normally they are embedded onto the chipset itself. For example,
main memory and cache memory are examples of volatile storage. They are fast but can store only a small
amount of information.
Non-volatile storage − These memories are made to survive system crashes. They are huge in data storage
capacity, but slower in accessibility. Examples may include hard-disks, magnetic tapes, flash memory, and non-
volatile (battery backed up) RAM.
It should check the states of all the transactions, which were being executed.
A transaction may be in the middle of some operation; the DBMS must ensure the atomicity of the transaction
in this case.
It should check whether the transaction can be completed now or it needs to be rolled back.
There are two types of techniques, which can help a DBMS in recovering as well as maintaining the atomicity of a
transaction −
Maintaining the logs of each transaction, and writing them onto some stable storage before actually modifying
the database.
Maintaining shadow paging, where the changes are done on a volatile memory, and later, the actual database is
updated.
Log-based Recovery
Log is a sequence of records, which maintains the records of actions performed by a transaction. It is important that
the logs are written prior to the actual modification and stored on a stable storage media, which is failsafe.
When a transaction enters the system and starts execution, it writes a log about it.
<Tn, Start>
Deferred database modification − All logs are written on to the stable storage and the database is updated
when a transaction commits.
Immediate database modification − Each log follows an actual database modification. That is, the database is
modified immediately after every operation.
Checkpoint
Keeping and maintaining logs in real time and in real environment may fill out all the memory space available in the
system. As time passes, the log file may grow too big to be handled at all. Checkpoint is a mechanism where all the
previous logs are removed from the system and stored permanently in a storage disk. Checkpoint declares a point
before which the DBMS was in consistent state, and all the transactions were committed.
Recovery
When a system with concurrent transactions crashes and recovers, it behaves in the following manner −
The recovery system reads the logs backwards from the end to the last checkpoint.
If the recovery system sees a log with <Tn, Start> and <Tn, Commit> or just <Tn, Commit>, it puts the transaction
in the redo-list.
If the recovery system sees a log with <Tn, Start> but no commit or abort log found, it puts the transaction in
undo-list.
All the transactions in the undo-list are then undone and their logs are removed. All the transactions in the redo-list
and their previous logs are removed and then redone before saving their logs.
1 Abdullah 9876723452 17
2 Abdul 9991165674 19
3 Wasim 7898756543 18
4 Malik 8987867898 19
5 Bakar 9990080080 17
Let's take a simple Student table, with fields student_id, name, phone and age.
Super Key
Super Key is defined as a set of attributes within a table that can uniquely identify each record within a table. Super
Key is a superset of Candidate key.
In the table defined above super key would include student_id, (student_id, name), phoneetc.
Confused? The first one is pretty simple as student_id is unique for every row of data, hence it can be used to
identity each row uniquely.
Next comes, (student_id, name), now name of two students can be same, but their student_idcan't be same hence this
combination can also be a key.
Similarly, phone number for every student will be unique, hence again, phone can also be a key.
So they all are super keys.
Candidate Key
Candidate keys are defined as the minimal set of fields which can uniquely identify each record in a table. It is an
attribute or a set of attributes that can act as a Primary Key for a table to uniquely identify each record in that
table. There can be more than one candidate key.
Msc IT (3rd Term) ILM College Jauharabad M.Wasim
P a g e | 33
In our example, student_id and phone both are candidate keys for table Student.
A candiate key can never be NULL or empty. And its value should be unique.
There can be more than one candidate keys for a table.
A candidate key can be a combination of more than one columns(attributes).
Primary Key
Primary key is a candidate key that is most appropriate to become the main key for any table. It is a key that can
uniquely identify each record in a table.
For the table Student we can make the student_id column as the primary key.
Composite Key
Key that consists of two or more attributes that uniquely identify any record in a table is called Composite key. But
the attributes which together form the Composite key are not a key independently or individually.
In the above picture we have a Score table which stores the marks scored by a student in a particular subject.
In this table student_id and subject_id together will form the primary key, hence it is a composite key.
Non-key Attributes
Non-key attributes are the attributes or fields of a table, other than candidate key attributes/fields in a table.
Msc IT (3rd Term) ILM College Jauharabad M.Wasim
P a g e | 34
Non-prime Attributes
Non-prime Attributes are attributes other than Primary Key attribute(s)..
Introduction to SQL
Structure Query Language(SQL) is a database query language used for storing and managing data in Relational
DBMS. SQL was the first commercial language introduced for E.F Codd's Relational model of database. Today
almost all RDBMS(MySql, Oracle, Infomix, Sybase, MS Access) use SQL as the standard database query language.
SQL is used to perform all types of data operations in RDBMS.
SQL Command
SQL defines following ways to manipulate data stored in an RDBMS.
DDL: Data Definition Language
This includes changes to the structure of the table like creation of table, altering table, deleting a table etc.
All DDL commands are auto-committed. That means it saves all the changes permanently in the database.
Command Description
Command Description
Command Description
Command Description
Command Description
Creating a Database
To create a database in RDBMS, create command is used. Following is the syntax,
CREATE DATABASE <DB_NAME>;
create table command will tell the database system to create a new table with the given table name and column
information.
The above command will create a new table with name Student in the current database with 3 columns,
namely student_id, name and age. Where the column student_id will only store integer, name will hold upto 100
characters and age will again store only integer value.
If you are currently not logged into your database in which you want to create the table then you can also add the
database name along with table name, using a dot operator .
For example, if we have a database with name Test and we want to create a table Student in it, then we can do so
using the following query:
CREATE TABLE Test.Student(
student_id INT,
name VARCHAR(100),
age INT);
Datatype Use
VARCHAR used for columns which will be used to store characters and integers, basically a string.
CHAR used for columns which will store char values(single character).
TEXT used for columns which will store text which is generally long in length. For example, if
you create a table for storing profile information of a social networking website, then
for about me section you can have a column of type TEXT.
The above query will delete all the records from the table student.
In DML commands, we will study about the DELETE command which is also more or less same as
the TRUNCATE command. We will also learn about the difference between the two in that tutorial.
DROP command
DROP command completely removes a table from the database. This command will also destroy the table structure
and the data stored in it. Following is its syntax,
DROP TABLE table_name
Here is an example explaining it,
DROP TABLE student;
The above query will delete the Student table completely. It can also be used on Databases, to delete the complete
database. For example, to drop a database,
DROP DATABASE Test;
The above query will drop the database with name Test from the system.
RENAME query
RENAME command is used to set a new name for any existing table. Following is the syntax,
INSERT command
Insert command is used to insert data into a table. Following is its general syntax,
INSERT INTO table_nameVALUES(data1, data2, ...)
Lets see an example,
Consider a table student with the following fields.
101 Abdul 15
101 Adam 15
102 Abdullah
101 Adam 15
102 Abdullah
103 Abdul 14
Suppose the column age in our tabel has a default value of 14.
Also, if you run the below query, it will insert default value into the age column, whatever the default value may
be.
INSERT INTO Student VALUES(103,'Chris')
UPDATE command
UPDATE command is used to update any record of data in a table. Following is its general syntax,
101 Adam 15
102 Abdullah
103 Abdul 14
101 Adam 15
102 Alex 18
103 chris 14
In the above statement, if we do not use the WHERE clause, then our update query will update age for all the
columns of the table to 18.
101 Adam 15
102 Alex 18
103 Abdul 17
For example, if we have to update the age column of student table every year for every student, then we can
simply run the following UPDATE statement to perform the following operation:
UPDATE student SET age = age+1;
As you can see, we have used age = age + 1 to increment the value of age by 1.
NOTE: This style only works for integer values.
DELETE command
101 Adam 15
102 Alex 18
103 Abdul 17
101 Adam 15
102 Alex 18
COMMIT command
COMMIT command is used to permanently save any transaction into the database.
When we use any DML command like INSERT, UPDATE or DELETE, the changes made by these commands are not
permanent, until the current session is closed, the changes made by these commands can be rolled back.
To avoid that, we use the COMMIT command to mark the changes as permanent.
Following is commit command's syntax,
COMMIT;
ROLLBACK command
This command restores the database to last commited state. It is also used with SAVEPOINT command to jump to a
savepoint in an ongoing transaction.
If we have used the UPDATE command to make some changes into the database, and realise that those changes
were not required, then we can use the ROLLBACK command to rollback those changes, if they were not commited
using the COMMIT command.
Following is rollback command's syntax,
ROLLBACK TO savepoint_name;
SAVEPOINT command
SAVEPOINT command is used to temporarily save a transaction so that you can rollback to that point whenever
required.
Following is savepoint command's syntax,
SAVEPOINT savepoint_name;
In short, using this command we can name the different states of our data in any table and then rollback to that
state using the ROLLBACK command whenever required.
id name
1 Abdul
2 Adam
4 Alex
Let’s use some SQL queries on the above table and see the results.
INSERT INTO class VALUES(5, 'Abdul');
COMMIT;
SAVEPOINT A;
SAVEPOINT B;
SAVEPOINT C;
id name
1 Abdul
2 Adam
4 Alex
5 abdulit
6 Chris
7 Bravo
Now let's use the ROLLBACK command to roll back the state of data to the savepoint B.
ROLLBACK TO B;
id name
1 Abdul
2 Adam
4 Alex
5 Abdulit
6 Chris
Now let's again use the ROLLBACK command to roll back the state of data to the savepoint A
ROLLBACK TO A;
id name
1 Abdul
2 Adam
4 Alex
5 Abduljit
So now you know how the commands COMMIT, ROLLBACK and SAVEPOINT works.
System: This includes permissions for creating session, table, etc and all types of other system privileges.
Object: This includes permissions for any command or query to perform any operation on the database tables.
GRANT: Used to provide any user access privileges or other priviliges for the database.
Now we will use the SELECT statement to display data of the table, based on a condition, which we will add to
our SELECT query using WHERE clause.
Let's write a simple SQL query to display the record for student with s_id as 101.
SELECT s_id,
name,
age,
address
FROM student WHERE s_id = 101;
Following will be the result of the above query.
Operator Description
= Equal to
!= Not Equal to
Wildcard operators
There are two wildcard operators that are used in LIKE clause.
101 Adam 15
102 Alex 18
103 Abdul 17
SELECT * from Student where s_name like 'A%';
The above query will return all records where s_name starts with character 'A'.
101 Adam 15
102 Alex 18
103 Abdul 17
Example
SELECT * from Student where s_name like '_d%';
The above query will return all records from Student table where s_name contain 'b' as second character.
Msc IT (3rd Term) ILM College Jauharabad M.Wasim
P a g e | 50
101 Adullah 15
Example
SELECT * from Student where s_name like '%x';
The above query will return all records from Student table where s_name contain 'x' as last character.
102 Alex 18
Order By Clause
Order by clause is used with Select statement for arranging retrieved data in sorted order. The Order by clause by
default sort data in ascending order. To sort data in descending order DESC keyword is used with Order by clause.
Syntax of Order By
SELECT column-list|* from table-name order byasc|desc;
The above query will return result in ascending order of the salary.
The above query will return result in descending order of the salary.
Group By Clause
Group by clause is used to group the results of a SELECT query based on one or more columns. It is also used with
SQL functions to group the result from one or more tables.
fromEmpgroup by salary
name age
Rohan 34
shane 29
Abdul 22
fromEmp
group by salary
name salary
Rohan 6000
Shane 8000
Scott 9000
You must remember that Group By clause will always come at the end, just like the Order by clause.
HAVING Clause
having clause is used with SQL Queries to give more precise condition for a statement. It is used to mention
condition in Group based SQL functions, just like WHERE clause.
Syntax for having will be,
selectcolumn_name, function(column_name)
FROM table_name
GROUP BY column_name
HAVINGfunction(column_name) condition
Distinct keyword
The distinct keyword is used with Select statement to retrieve unique values from the table. Distinctremoves all
the duplicate records while retrieving from database.
Example
Consider the following Emp table.
The above query will return only the unique salary from Emp table
salary
5000
8000
10000
AND operator
AND operator is used to set multiple conditions with Where clause.
Msc IT (3rd Term) ILM College Jauharabad M.Wasim
P a g e | 55
Example of AND
Consider the following Emp table
The above query will return records where salary is less than 10000 and age greater than 25.
OR operator
OR operator is also used to combine multiple conditions with Where clause. The only difference between AND and
OR is their behaviour. When we use AND to combine two or more than two conditions, records satisfying all the
condition will be in the result. But in case of OR, atleast one condition from the conditions specified must be
satisfied by any record to be in the result.
Example of OR
The above query will return records where either salary is greater than 10000 or age greater than 25.
SQL Constraints
SQL Constraints are rules used to limit the type of data that can go into a table, to maintain the accuracy and
integrity of the data inside table.
Constraints can be divided into following two types,
Constraints are used to make sure that the integrity of data is maintained in the database. Following are the most
used constraints that can be applied to a table.
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
The above query will declare that the s_id field of Student table will not take NULL value.
UNIQUE Constraint
UNIQUE constraint ensures that a field or column will only have unique values. A UNIQUE constraint field will not
have duplicate data. UNIQUE constraint can be applied at column level or table level.
The above query will declare that the s_id field of Student table will only have unique values and wont take NULL
value.
The above query specifies that s_id field of Student table will only have unique value.
10 Order1 101
11 Order2 103
12 Order3 102
In Customer_Detail table, c_id is the primary key which is set as foreign key in Order_Detail table. The value that is
entered in c_id which is set as foreign key in Order_Detail table must be present in Customer_Detail table where it
is set as primary key. This prevents invalid data to be inserted into c_id column of Order_Detail table.
On Delete Cascade : This will remove the record from child table, if that value of foriegn key is deleted from
the main table.
On Delete Null : This will set all the values in that record of child table as NULL, for which the value of foriegn
key is deleted from the main table.
If we don't use any of the above, then we cannot delete data from the main table for which data in child table
exists. We will get an error if we try to do so.
CHECK Constraint
CHECK constraint is used to restrict the value of a column between a range. It performs check on the values, before
storing them into the database. Its like condition checking before saving data into a column.
Age int);
The above query will restrict the s_id value to be greater than zero.
SQL Functions
SQL provides many built-in functions to perform operations on data. These functions are useful while performing
mathematical calculations, string concatenations, sub-strings etc. SQL functions are divided into two catagories,
Aggregrate Functions
Scalar Functions
Aggregrate Functions
These functions return a single value after calculating from a group of values.Following are some frequently used
Aggregrate functions.
1) AVG()
Average returns average value after calculating from values in a numeric column.
Its general Syntax is,
SELECT AVG(column_name) from table_name
avg(salary)
8200
2) COUNT()
Count returns the number of rows present in the table either based on some condition or without condition.
Its general Syntax is,
SELECT COUNT(column_name) from table-name
count(name)
Example of COUNT(distinct)
Msc IT (3rd Term) ILM College Jauharabad M.Wasim
P a g e | 61
count(distinct salary)
3) FIRST()
First function returns first value of a selected column
Syntax for FIRST function is,
SELECT FIRST(column_name) from table-name
Example of FIRST()
Consider following Emp table
first(salary)
9000
4) LAST()
LAST return the return last value from selected column
Syntax of LAST function is,
SELECT LAST(column_name) from table-name
Example of LAST()
Consider following Emp table
last(salary)
8000
5) MAX()
MAX function returns maximum value from selected column of the table.
Syntax of MAX function is,
SELECT MAX(column_name) from table-name
Example of MAX()
Consider following Emp table
Msc IT (3rd Term) ILM College Jauharabad M.Wasim
P a g e | 63
MAX(salary)
10000
6) MIN()
MIN function returns minimum value from a selected column of the table.
Syntax for MIN function is,
SELECT MIN(column_name) from table-name
Example of MIN()
Consider following Emp table,
MIN(salary)
6000
7) SUM()
SUM function returns total sum of a selected columns numeric values.
Syntax for SUM is,
SELECT SUM(column_name) from table-name
Example of SUM()
Consider following Emp table
SUM(salary)
41000
Scalar Functions
Scalar functions return a single value from an input value. Following are soe frequently used Scalar Functions.
1) UCASE()
UCASE function is used to convert value of string column to Uppercase character.
Syntax of UCASE,
SELECT UCASE(column_name) from table-name
Example of UCASE()
Consider following Emp table
Result is,
UCASE(name)
ABDUL
SHANE
ABDULLAH
SCOTT
TIGER
2) LCASE()
LCASE function is used to convert value of string column to Lowecase character.
Syntax for LCASE is,
SELECT LCASE(column_name) from table-name
Example of LCASE()
Consider following Emp table
LCASE(name)
abdul
shane
abdullah
scott
tiger
3) MID()
MID function is used to extract substrings from column values of string type in a table.
Syntax for MID function is,
SELECT MID(column_name, start, length) from table-name
Example of MID()
Consider following Emp table
MID(name,2,2)
du
ha
ul
co
ig
4) ROUND()
ROUND function is used to round a numeric field to number of nearest integer. It is used on Decimal point values.
Syntax of Round function is,
SELECT ROUND(column_name, decimals) from table-name
Example of ROUND()
Consider following Emp table
ROUND(salary)
9001
8001
6000
10000
8000
Join in SQL
SQL Join is used to fetch data from two or more tables, which is joined to appear as single set of data. SQL Join is
used for combining column from two or more tables by using values common to both tables. Join Keyword is used
in SQL queries for joining two or more tables. Minimum required condition for joining table, is (n-1) where n, is
number of tables. A table can also join to itself known as, Self Join.
Types of Join
The following are the types of JOIN that we can use in SQL.
Inner
Outer
Left
Right
fromtable-name1
CROSS JOIN
table-name2;
ID NAME
1 abdul
2 adam
4 alex
The class_info table,
ID Address
1 ISLAMABAD
2 KHUSHAB
3 QUETTA
from class,
ID NAME ID Address
1 abdul 1 ISLAMABAD
2 adam 1 ISLAMABAD
4 alex 1 ISLAMABAD
1 abdul 2 KHUSHAB
2 adam 2 KHUSHAB
4 alex 2 KHUSHAB
1 abdul 3 QUETTA
2 adam 3 QUETTA
4 alex 3 QUETTA
fromtable-name1
INNER JOIN
table-name2
ID NAME
1 abdul
2 adam
3 alex
4 abdullah
The class_info table,
ID Address
1 ISLAMABAD
2 KHUSHAB
3 QUETTA
Inner JOIN query will be,
SELECT * from class, class_info where class.id = class_info.id;
ID NAME ID Address
1 abdul 1 ISLAMABAD
2 adam 2 KHUSHAB
3 alex 3 QUETTA
Natural JOIN
Natural Join is a type of Inner join which is based on column having same name and same datatype present in both
the tables to be joined.
fromtable-name1
NATURAL JOIN
Msc IT (3rd Term) ILM College Jauharabad M.Wasim
P a g e | 71
table-name2;
ID NAME
1 abdul
2 adam
3 alex
4 abdullah
The class_info table,
ID Address
1 ISLAMABAD
2 KHUSHAB
3 QUETTA
Natural join query will be,
SELECT * from class NATURAL JOIN class_info;
ID NAME Address
1 abdul ISLAMABAD
2 adam KHUSHAB
3 alex QUETTA
In the above example, both the tables being joined have ID column(same name and same datatype), hence the
records for which value of ID matches in both the tables will be the result of Natural Join of these two tables.
Outer JOIN
Outer Join is based on both matched and unmatched data. Outer Joins subdivide further into,
fromtable-name1
table-name2
on table-name1.column-name = table-name2.column-name;
fromtable-name1,
table-name2
on table-name1.column-name = table-name2.column-name(+);
ID NAME
1 abdul
2 adam
3 alex
4 abudllah
5 ashish
The class_info table,
ID Address
1 ISLAMABAD
2 KHUSHAB
3 QUETTA
7 SIALKOT
8 PAKPATTAN
ID NAME ID Address
1 abdul 1 ISLAMABAD
2 adam 2 KHUSHAB
3 alex 3 QUETTA
fromtable-name1
table-name2
on table-name1.column-name = table-name2.column-name;
fromtable-name1,
table-name2
on table-name1.column-name(+) = table-name2.column-name;
ID NAME
1 abdul
2 adam
3 alex
4 abdullah
Msc IT (3rd Term) ILM College Jauharabad M.Wasim
P a g e | 74
5 ashish
ID Address
1 ISLAMABAD
2 KHUSHAB
3 QUETTA
7 SIALKOT
8 PAKPATTAN
ID NAME ID Address
1 Abbul 1 ISLAMABAD
2 Adam 2 KHUSHAB
3 Alex 3 QUETTA
fromtable-name1
table-name2
on table-name1.column-name = table-name2.column-name;
ID NAME
1 abdul
2 adam
3 alex
4 abdullah
5 ashish
The class_info table,
ID Address
1 ISLAMABAD
2 KHUSHAB
3 QUETTA
7 SIALKOT
8 PAKPATTAN
Full Outer Join query will be like,
SELECT * FROM class FULL OUTER JOIN class_info on (class.id=class_info.id);
ID NAME ID Address
1 abdul 1 ISLAMABAD
2 adam 2 KHUSHAB
3 alex 3 QUETTA