You are on page 1of 188

Database Concept

Overview of DBMS
Components of DBMS
Database Architecture
Types of Database Model
DBMS - ER Model
ER Model: Basic Concepts
ER Model: Creating ER Diagram
ER Model: Generalization and Specialization
DBMS - Relational Model
Codd's 12 rule of RDBMS
Basic Concepts of RDBMS
Relational Algebra
Relational Calculus
ER Model to Relational Model
Types of Database Key
Database Normalization
First Normal Form (1NF)
Second Normal Form (2NF)
Third Normal Form (3NF)
Boyce-Codd Normal Form (BCNF)
Fourth Normal Form (4NF)
SQL Concept
SQL Introduction
DDL Command
Create query
Alter query
Truncate, Drop and Rename query
DML Command
INSERT command
UPDATE command
DELETE command
TCL Command
All TCL Command
DCL Command
All DCL Command
SELECT query
WHERE clause
LIKE clause
ORDER BY clause
Group BY clause
Having clause
DISTINCT keyword
AND & OR operator
Advance SQL
SQL Constraints
SQL function
SQL Join
SQL Alias
SQL SET operation
SQL Sequences
SQL Views
What is Data?
Data is nothing but facts and statistics stored or free flowing over a network, generally it's raw and unprocessed. For
example:
When you visit any website, they might store you IP address, that is data, in return they might add a cookie in
your browser, marking you that you visited the website, that is data, your name, it's data, your age, it's data.

Data becomes information when it is processed, turning it into


something meaningful. Like, based on the cookie data
saved on user's browser, if a website can analyse that generally men of age 20-25 visit us more, that is information,
derived from the data collected.

What is a Database?
A Database is a collection of related data organised 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 realised that they
needed a better solution to this problem.

Larry Ellison, the co-founder of Oracle was amongst the first few, who realised the need for a software based Database
Management System.

What is DBMS?
A DBMS is a software that allows creation, definition and manipulation of database, allowing users to store, process
and analyse 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:

MySql
Oracle
SQL Server
IBM DB2
PostgreSQL
Amazon SimpleDB (cloud based) etc.

Characteristics of Database Management System


A database management system has following characteristics:

1. Data stored into Tables: Data is never directly stored into the database. Data is stored into tables, created inside
the database. DBMS also allows to have relationships between tables which makes the data more meaningful and
connected. You can easily understand what type of data is stored where by looking at all the tables created in a
database.
2. Reduced Redundancy: In the modern world hard drives are very cheap, but earlier when hard drives were too
expensive, unnecessary repetition of data in database was a big problem. But DBMS follows Normalisation
which divides the data in such a way that repetition is minimum.
3. Data Consistency: On Live data, i.e. data that is being continuosly updated and added, maintaining the
consistency of data can become a challenge. But DBMS handles it all by itself.
4. Support Multiple user and Concurrent Access: DBMS allows multiple users to work on it(update, insert,
delete data) at the same time and still manages to maintain the data consistency.
5. Query Language: DBMS provides users with a simple Query language, using which data can be easily fetched,
inserted, deleted and updated in a database.
6. Security: The DBMS also takes care of the security of data, protecting the data from un-authorised access. In a
typical DBMS, we can
create user accounts with different access permissions, using which we can easily secure
our data by restricting user access.
7. DBMS supports transactions, which allows us to better handle and manage data integrity in real world
applications where multi-threading is extensively used.

Advantages of DBMS
Segregation of applicaion program.
Minimal data duplicacy or data redundancy.
Easy retrieval of data using the Query Language.
Reduced development time and maintainance 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.

Disadvantages of DBMS
It's Complexity
Except MySQL, which is open source, licensed DBMSs are generally costly.
They are large in size.
Components of DBMS
The database management system can be divided into five major components, they are:

1. Hardware
2. Software
3. Data
4. Procedures
5. Database Access Language

Let's have a simple diagram to see how they all fit together to form a database management system.

DBMS Components: Hardware

When we say Hardware, we mean computer, hard disks, I/O channels for data, and any other physical component
involved before any data is successfully stored into the memory.

When we run Oracle or MySQL on our personal computer, then our computer's Hard Disk, our Keyboard using which
we type in all the commands, our computer's RAM, ROM all become a part of the DBMS hardware.

DBMS Components: Software

This is the main component, as this is the program which controls everything. The DBMS software is more like a
wrapper around the physical
database, which provides us with an easy-to-use interface to store, access and update data.

The DBMS software is capable of understanding the Database Access Language and intrepret it into actual database
commands to execute them on the DB.
DBMS Components: Data

Data is that resource, for which DBMS was designed. The motive behind the creation of DBMS was to store and utilise
data.

In a typical Database, the user saved Data is present and meta data is stored.

Metadata is data about the data. This is information stored by the DBMS to better understand the data stored in it.

For example: When I store my Name in a database, the DBMS will store when the name was stored in the database,
what is the size of the name, is it stored as related data to some other data, or is
it independent, all this information is
metadata.

DBMS Components: Procedures

Procedures refer to general instructions to use a database management


system. This includes procedures to setup and
install a DBMS, To login and logout of DBMS software, to manage databases, to take backups, generating reports etc.

DBMS Components: Database Access Language

Database Access Language is a simple language designed to write commands to access, insert, update and delete data
stored in any database.

A user can write commands in the Database Access Language and submit it to the DBMS for execution, which is then
translated and executed by the DBMS.

User can create new databases, tables, insert data, fetch stored data, update data and delete the data using the access
language.

Users
Database Administrators: Database Administrator or DBA is the
one who manages the complete database
management system. DBA takes care
of the security of the DBMS, it's availability, managing the license keys,
managing user accounts and access etc.
Application Programmer or Software Developer: This user group is involved in developing and desiging the
parts of DBMS.
End User: These days all the modern applications, web or mobile, store user data. How do you think they do it?
Yes, applications are programmed in such a way that they collect user data and store the data on DBMS systems
running on their server. End users are the one who store, retrieve, update and delete data.
Understanding DBMS 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 centralised(all the data stored at one location), decentralised(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.

Database Architecture is logically of two types:

1. 2-tier DBMS architecture


2. 3-tier DBMS architecture

2-tier DBMS Architecture


2-tier DBMS architecture includes an Application layer between the user and the DBMS, which is responsible to
communicate the user's request to the database management system and then send the response from the DBMS to the
user.

An application interface known as ODBC(Open Database Connectivity) provides an API that allow client side program
to call the
DBMS. Most DBMS vendors provide ODBC drivers for their DBMS.

Such an architecture provides the DBMS extra security as it is not exposed to the End User directly. Also, security can
be improved by adding security and authentication checks in the Application layer too.

3-tier DBMS Architecture


3-tier DBMS architecture is the most commonly used architecture for web applications.
It is an extension of the 2-tier architecture. In the 2-tier architecture, we have an application layer which can be accessed
programatically to perform various operations on the DBMS. The application generally understands the Database
Access Language and processes end users requests to the DBMS.

In 3-tier architecture, an additional Presentation or GUI Layer is added, which provides a graphical user interface for the
End user to interact with the DBMS.

For the end user, the GUI layer is the Database System, and the end user has no idea about the application layer and the
DBMS system.

If you have used MySQL, then you must have seen PHPMyAdmin, it is the best example of a 3-tier DBMS
architecture.
DBMS Database Models
A Database model defines the logical design and structure of a database and defines how data will be stored, accessed
and updated in a database management system. While 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 organises data into a tree-like-structure, with a
single root, to which all the other data is linked. The
heirarchy 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 organised 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
organised 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
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.

E-R Models are defined to represent the relationships into pictorial form to make it easier for different stakeholders to
understand.

This model is good to design a database, which can then be turned into tables in relational model(explained below).

Let's take an example, If we have to design a School Database, then Student will be an entity with attributes name,
age, address etc. As Address is generally complex, it can be another entity with attributes street name, pincode, city
etc, and there will be a relationship between them.

Relationships can also be of different types. To learn about E-R Diagrams in details, click on the link.
Relational Model
In this model, data is organised in two-dimensional tables and the relationship is maintained by storing a common field.

This model was introduced by E.F Codd in 1970, and since then it has been the most widely used database model,
infact, we can say the only database model used around the world.

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.

In the coming tutorials we will learn how to design tables, normalize


them to reduce data redundancy and how to use
Structured Query language
to access data from tables.
Basic Concepts of ER Model in DBMS
As we described in the tutorial Database models, Entity-relationship model is a model used for design and
representation of relationships between data.

The main data objects are termed as Entities, with their details defined as attributes, some of these attributes are
important and are used to identity the entity, and different entities are related using relationships.

In short, to understand about the ER Model, we must understand about:

Entity and Entity Set


What are Attributes? And Types of Attributes.
Keys
Relationships

Let's take an example to explain everything. For a School Management Software, we will have to store Student
information, Teacher information, Classes, Subjects taught in each class etc.

ER Model: Entity and Entity Set


Considering the above example, Student is an entity, Teacher is an entity, similarly, Class, Subject etc are also
entities.

An Entity is generally a real-world object which has characteristics and holds relationships in a DBMS.

If a Student is an Entity, then the complete dataset of all the students will be the Entity Set

ER Model: Attributes
If a Student is an Entity, then student's roll no., student's name, student's age, student's gender etc will be its attributes.

An attribute can be of many types, here are different types of attributes defined in ER database model:

1. Simple attribute: The attributes with values that are atomic and cannot be broken down further are simple
attributes. For example, student's age.
2. Composite attribute: A composite attribute is made up of more than one simple attribute. For example, student's
address will contain, house no., street name, pincode etc.
3. Derived attribute: These are the attributes which are not present in the whole database management system, but
are derived using other attributes. For example, average age of students in a class.
4. Single-valued attribute: As the name suggests, they have a single value.
5. Multi-valued attribute: And, they can have multiple values.

ER Model: Keys
If the attribute roll no. can uniquely identify a student entity, amongst all the students, then the attribute roll no. will be
said to be a key.

Following are the types of Keys:


1. Super Key
2. Candidate Key
3. Primary Key

We have covered Keys in details here in Database Keys tutorial.

ER Model: Relationships
When an Entity is related to another Entity, they are said to have a relationship. For example, A Class Entity is related
to Student entity, becasue students study in classes, hence this is a relationship.

Depending upon the number of entities involved, a degree is assigned to relationships.

For example, if 2 entities are involved, it is said to be Binary relationship, if 3 entities are involved, it is said to be
Ternary relationship, and so on.

In the next tutorial, we will learn how to create ER diagrams and design databases using ER diagrams.
Working with ER Diagrams
ER Diagram is a visual representation of data that describes how data is
related to each other. In ER Model, we
disintegrate data into entities,
attributes and setup relationships between entities, all this can be represented visually
using the ER diagram.

For example, in the below diagram, anyone can see and understand what the diagram wants to convey: Developer
develops a website, whereas a Visitor visits a website.

Components of ER Diagram
Entitiy, Attributes, Relationships etc form the components of ER Diagram and there are defined symbols and shapes to
represent each one of them.

Let's see how we can represent these in our ER Diagram.

Entity

Simple rectangular box represents an Entity.

Relationships between Entities - Weak and Strong

Rhombus is used to setup relationships between two or more entities.


Attributes for any Entity

Ellipse is used to represent attributes of any entity. It is connected to the entity.

Weak Entity

A weak Entity is represented using double rectangular boxes. It is generally connected to another entity.

Key Attribute for any Entity

To represent a Key attribute, the attribute name inside the Ellipse is underlined.

Derived Attribute for any Entity

Derived attributes are those which are derived based on other attributes, for example, age can be derived from date of
birth.

To represent a derived attribute, another dotted ellipse is created inside the main ellipse.
Multivalued Attribute for any Entity

Double Ellipse, one inside another, represents the attribute which can have multiple values.

Composite Attribute for any Entity

A composite attribute is the attribute, which also has attributes.

ER Diagram: Entity

An Entity can be any object, place, person or class. In ER Diagram, an entity


is represented using rectangles. Consider
an example of an Organisation- Employee, Manager, Department, Product and many more can be taken as entities in an
Organisation.
The yellow rhombus in between represents a relationship.

ER Diagram: Weak Entity

Weak entity is an entity that depends on another entity. Weak entity doesn't have anay key attribute of its own. Double
rectangle is used to represent a weak entity.

ER Diagram: Attribute

An Attribute describes a property or characterstic of an entity. For example, Name, Age, Address etc can be attributes
of a Student. An attribute is represented using eclipse.
ER Diagram: Key Attribute

Key attribute represents the main characterstic of an Entity. It is used to represent a Primary key. Ellipse with the text
underlined, represents Key Attribute.

ER Diagram: Composite Attribute

An attribute can also have their own attributes. These attributes are known as Composite attributes.
ER Diagram: Relationship

A Relationship describes relation between entities. Relationship is represented using diamonds or rhombus.

There are three types of relationship that exist between Entities.

1. Binary Relationship
2. Recursive Relationship
3. Ternary Relationship

ER Diagram: Binary Relationship

Binary Relationship means relation between two Entities. This is further divided into three types.
One to One Relationship

This type of relationship is rarely seen in real world.

The above example describes that one student can enroll only for one course and a course will also have only one
Student. This is not what you will usually see in real-world relationships.

One to Many Relationship

The below example showcases this relationship, which means that 1 student can opt for many courses, but a course can
only have 1 student. Sounds weird! This is how it is.

Many to One Relationship


It reflects business rule that many entities can be associated with just one entity. For example, Student enrolls for only
one Course but a Course can have many Students.

Many to Many Relationship

The above diagram represents that one student can enroll for more than one courses. And a course can have more than 1
student enrolled in it.

ER Diagram: Recursive Relationship

When an Entity is related with itself it is known as Recursive Relationship.


ER Diagram: Ternary Relationship

Relationship of degree three is called Ternary relationship.

A Ternary relationship involves three entities. In such relationships


we always consider two entites together and then
look upon the third.

For example, in the diagram above, we have three related entities, Company, Product and Sector.
To understand the
relationship better or to define rules around the model, we should relate two entities and then derive the third one.

A Company produces many Products/ each product is produced by exactly one company.
A Company operates in only one Sector / each sector has many companies operating in it.

Considering the above two rules or relationships, we see that although the complete relationship involves three entities,
but we are looking at two entities at a time.
The Enhanced ER Model
As the complexity of data increased in the late 1980s, it became more
and more difficult to use the traditional ER Model
for database modelling. Hence some improvements or enhancements were made to the existing ER Model to make it
able to handle the complex applications better.

Hence, as part of the Enhanced ER Model, along with other improvements, three new concepts were added to the
existing ER Model, they were:

1. Generalization
2. Specialization
3. Aggregration

Let's understand what they are, and why were they added to the existing ER Model.

Generalization
Generalization is a bottom-up approach in which two lower level entities combine to form a higher level entity. In
generalization,
the higher level entity can also combine with other lower level entities to make further higher level
entity.

It's more like Superclass and Subclass system, but the only difference is the approach, which is bottom-up. Hence,
entities are combined to form a more generalised entity, in other words, sub-classes are combined to form a super-class.

For example, Saving and Current account types entities can be generalised and an entity with name Account can be
created, which covers both.
Specialization
Specialization is opposite to Generalization. It is a top-down
approach in which one higher level entity can be broken
down into two lower level entity. In specialization, a higher level entity may not have any lower-level entity sets, it's
possible.

Aggregration
Aggregration is a process when relation between two entities is treated as a single entity.
In the diagram above, the relationship between Center and Course together, is acting as an Entity, which is in
relationship with another entity Visitor.
Now in real world, if a Visitor or a Student visits a Coaching Center, he/she
will never enquire about the center only or just about the course, rather he/she will ask enquire about both.
Codd's Rule for Relational DBMS
E.F Codd was a Computer Scientist who invented the Relational model for Database management. Based on relational
model, the Relational database was created. Codd proposed 13 rules popularly known as Codd's 12 rules
to test
DBMS's concept against his relational model. Codd's rule actualy define what quality a DBMS requires in order to
become a Relational Database Management System(RDBMS). Till now, there is hardly any commercial product that
follows all the 13 Codd's rules. Even Oracle follows only eight and half(8.5) out of 13. The Codd's 12 rules are as
follows.

Rule zero

This rule states that for a system to qualify as an RDBMS, it must be able to manage database entirely through the
relational capabilities.

Rule 1: Information rule

All information(including metadata) is to be represented as stored data in cells of tables. The rows and columns have to
be strictly unordered.

Rule 2: Guaranted Access

Each unique piece of data(atomic value) should be accesible by : Table Name + Primary Key(Row) +
Attribute(column).

NOTE: Ability to directly access via POINTER is a violation of this rule.

Rule 3: Systematic treatment of NULL

Null has several meanings, it can mean missing data, not


applicable or no value. It should be handled consistently. Also,
Primary key must not be null, ever. Expression on NULL must give null.

Rule 4: Active Online Catalog

Database dictionary(catalog) is the structure description of the complete Database


and it must be stored online. The
Catalog must be governed by same rules as rest of the database. The same query language should be used on
catalog as
used to query database.

Rule 5: Powerful and Well-Structured Language

One well structured language must be there to provide all manners of access to the data stored in the database. Example:
SQL, etc. If the database allows access to the data without the use of this language, then that is a violation.

Rule 6: View Updation Rule


All the view that are theoretically updatable should be updatable by the system as well.

Rule 7: Relational Level Operation

There must be Insert, Delete, Update operations at each level of relations. Set operation like Union, Intersection and
minus should also be supported.

Rule 8: Physical Data Independence

The physical storage of data should not matter to the system. If say, some file supporting table is renamed or moved
from one disk to another,
it should not effect the application.

Rule 9: Logical Data Independence

If there is change in the logical structure(table structures) of the database the user view of data should not change. Say,
if a table is split into two tables, a new view should give result as the join of the two tables. This rule is most difficult to
satisfy.

Rule 10: Integrity Independence

The database should be able to enforce its own integrity rather than using other programs. Key and Check constraints,
trigger etc, should be stored in Data Dictionary. This also make RDBMS independent of front-end.

Rule 11: Distribution Independence

A database should work properly regardless of its distribution across a network. Even if a database is geographically
distributed, with data stored in pieces, the end user should get an impression that it is stored at the same place. This lays
the foundation of distributed database.

Rule 12: Nonsubversion Rule

If low level access is allowed to a system it should not be able to subvert or bypass integrity rules to change the data.
This can be achieved by some sort of looking or encryption.
Basic Relational DBMS Concepts
A Relational Database management System(RDBMS) is a database management system based on the relational
model introduced by E.F Codd. In relational model, data is stored in relations(tables) and is represented in form of
tuples(rows).

RDBMS is used to manage Relational database. Relational database


is a collection of organized set of tables related to
each other, and from which data can be accessed easily. Relational Database is the most commonly used database these
days.

RDBMS: What is Table ?

In Relational database model, a table is a collection of data elements organised in terms of rows and columns. A table is
also considered as a convenient representation of relations. But a table can have duplicate row of data while a true
relation cannot have duplicate data. Table is the most simplest form of data storage. Below is an example of an
Employee table.

ID Name Age Salary


1 Adam 34 13000
2 Alex 28 15000
3 Stuart 20 18000
4 Ross 42 19020

RDBMS: What is a Tuple?

A single entry in a table is called a Tuple or Record or Row. A tuple in a table represents a set of related data. For
example, the above Employee table has 4 tuples/records/rows.

Following is an example of single record or tuple.

1 Adam 34 13000

RDBMS: What is an Attribute?

A table consists of several records(row), each record can be broken down into several smaller parts of data known as
Attributes. The above Employee table consist of four attributes, ID, Name, Age and Salary.

Attribute Domain

When an attribute is defined in a relation(table), it is defined to hold only a certain type of values, which is known as
Attribute Domain.

Hence, the attribute Name will hold the name of employee for every tuple. If we save employee's address there, it will
be violation of the Relational database model.

Name
Adam
Alex
Stuart - 9/401, OC
Street, Amsterdam
Ross

What is a Relation Schema?

A relation schema describes the structure of the relation, with the name of the relation(name of table), its attributes and
their names and type.

What is a Relation Key?

A relation key is an attribute which can uniquely identify a particular tuple(row) in a relation(table).

Relational Integrity Constraints


Every relation in a relational database model should abide by or follow a few constraints to be a valid relation, these
constraints are called as Relational Integrity Constraints.

The three main Integrity Constraints are:

1. Key Constraints
2. Domain Constraints
3. Referential integrity Constraints

Key Constraints

We store data in tables, to later access it whenever required. In every table one or more than one attributes together are
used to fetch data from tables. The Key Constraint specifies that there should be such an attribute(column) in a
relation(table), which can be used to fetch data for any tuple(row).

The Key attribute should never be NULL or same for two different row of data.

For example, in the Employee table we can use the attribute ID to fetch data for each of the employee. No value of ID is
null and it is unique for every row, hence it can be our Key attribute.

Domain Constraint

Domain constraints refers to the rules defined for the values that can be stored for a certain attribute.

Like we explained above, we cannot store Address of employee in the column for Name.

Similarly, a mobile number cannot exceed 10 digits.


Referential Integrity Constraint

We will study about this in detail later. For now remember this example, if I say Supriya is my girlfriend, then a girl
with name Supriya should also exist for that relationship to be present.

If a table reference to some data from another table, then that table
and that data should be present for referential
integrity constraint to
hold true.
What is Relational Algebra?
Every database management system must define a query language to allow users to access the data stored in the
database. Relational Algebra is a procedural query language used to query the database tables to access data in
different ways.

In relational algebra, input is a relation(table from which data has to be accessed) and output is also a relation(a
temporary table holding the data asked for by the user).

Relational Algebra works on the whole table at once, so we do not have to use loops etc to iterate over all the
rows(tuples) of data one by one. All we have to do is specify the table name from which we need the data, and in a
single line of command, relational algebra will traverse the entire given table to fetch data for you.

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

Select Operation (σ)


This is used to fetch rows(tuples) from table(relation) which satisfies a given condition.

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,

σage > 17 and gender = 'Male' (Student)

This will return tuples(rows) from table Student with information of male students, of age more than 17.(Consider the
Student table has an attribute Gender too.)

Project Operation (∏)


Project operation is used to project only a certain set of attributes
of a relation. In simple words, If you want to see only
the names all of the students in the Student table, then you can use Project Operation.

It will only project or show the columns or attributes asked for, and will also remove duplicate data from the columns.

Syntax: ∏A1, A2...(r)

where A1, A2 etc are attribute names(column names).

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.

Union Operation ( )
This operation is used to fetch data from two relations(tables) or temporary relation(result of another operation).

For this operation to work, the relations(tables) specified should have same number of attributes(columns) and same
attribute domain. Also the duplicate tuples are autamatically eliminated from the result.

Syntax: A B

where A and B are relations.

For example, if we have two tables RegularClass and ExtraClass, both have a column student to save name of
student, then,

∏Student(RegularClass) ∏Student(ExtraClass)

Above operation will give us name of Students who are attending both regular classes and extra classes, eliminating
repetition.

Set Difference (-)


This operation is used to find data present in one relation and not present in the second relation. This operation is also
applicable on two
relations, just like Union operation.

Syntax: A - B

where A and B are relations.

For example, if we want to find name of students who attend the regular class but not the extra class, then, we can use
the below operation:

∏Student(RegularClass) - ∏Student(ExtraClass)

Cartesian Product (X)


This is used to combine data from two different relations(tables) into one and fetch data from the combined relation.

Syntax: A X B

For example, if we want to find the information for Regular Class and
Extra Class which are conducted during morning,
then, we can use the following operation:

σtime = 'morning' (RegularClass X ExtraClass)

For the above query to work, both RegularClass and ExtraClass should have the attribute time.

Rename Operation (ρ)


This operation is used to rename the output relation for any query operation which returns result like Select, Project etc.
Or to simply rename a relation(table)

Syntax: ρ(RelationNew, RelationOld)

Apart from these common operations Relational Algebra is also used for Join operations like,

Natural Join
Outer Join
Theta join etc.
What is Relational Calculus?
Contrary to Relational Algebra which is a procedural query language to fetch data and which also explains how it is
done, Relational Calculus
in non-procedural query language and has no description about how the query will work or
the data will b fetched. It only focusses on what to do, and not on how to do it.

Relational Calculus exists in two forms:

1. Tuple Relational Calculus (TRC)


2. Domain Relational Calculus (DRC)

Tuple Relational Calculus (TRC)


In tuple relational calculus, we work on filtering tuples based on the given condition.

Syntax: { T | Condition }

In this form of relational calculus, we define a tuple variable, specify the table(relation) name in which the tuple is to be
searched for, along with a condition.

We can also specify column name using a . dot operator, with the tuple variable to only get a certain attribute(column)
in result.

A lot of informtion, right! Give it some time to sink in.

A tuple variable is nothing but a name, can be anything, generally we use a single alphabet for this, so let's say T is a
tuple variable.

To specify the name of the relation(table) in which we want to look for data, we do the following:

Relation(T) , where T is our tuple variable.

For example if our table is Student, we would put it as Student(T)

Then comes the condition part, to specify a condition applicable for a particluar attribute(column), we can use the . dot
variable with the tuple variable to specify it, like in table Student, if we want to get data for students with age greater
than 17, then, we can write it as,

T.age > 17 , where T is our tuple variable.

Putting it all together, if we want to use Tuple Relational Calculus to fetch names of students, from table Student, with
age greater than 17, then, for T being our tuple variable,

T.name | Student(T) AND T.age > 17

Domain Relational Calculus (DRC)


In domain relational calculus, filtering is done based on the domain of the attributes and not based on the tuple values.

Syntax: { c1, c2, c3, ..., cn | F(c1, c2, c3, ... ,cn)}
where, c1, c2... etc represents domain of attributes(columns) and F defines the formula including the condition for
fetching the data.

For example,

{< name, age > | Student age > 17}

Again, the above query will return the names and ages of the students in the table Student who are older than 17.
ER Model to Relational Model
As we all know that ER Model can be represented using ER Diagrams which is a great way of designing and
representing the database design in more of a flow chart form.

It is very convenient to design the database using the ER Model by creating an ER diagram and later on converting it
into relational model to design your tables.

Not all the ER Model constraints and components can be directly transformed into relational model, but an approximate
schema can be derived.

So let's take a few examples of ER diagrams and convert it into relational model schema, hence creating tables in
RDBMS.

Entity becomes Table


Entity in ER Model is changed into tables, or we can say for every Entity in ER model, a table is created in Relational
Model.

And the attributes of the Entity gets converted to columns of the table.

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.

Relationship becomes a Relationship Table


In ER diagram, we use diamond/rhombus to reprsent a relationship between two entities. In Relational model we create
a relationship table
for ER Model relationships too.

In the ER diagram below, we have two entities Teacher and Student with a relationship between them.

As discussd 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 foriegn 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, foriegn key
constraint must be defined.
Introduction to Database Keys
Keys are very important part of Relational database model. They are used to establish and identify relationships between
tables and also to uniquely identify any record or row of data inside a table.

A Key can be a single attribute or a group of attributes, where the combination may act as a key.

Why we need a Key?


In real world applications, number of tables required for storing the
data is huge, and the different tables are related to
each other as well.

Also, tables store a lot of data in them. Tables generally extends to


thousands of records stored in them, unsorted and
unorganised.

Now to fetch any particular record from such dataset, you will have to apply some conditions, but what if there is
duplicate data present and every time you try to fetch some data by applying certain condition,
you get the wrong data.
How many trials before you get the right data?

To avoid all this, Keys are defined to easily identify any row of data in a table.

Let's try to understand about all the keys using a simple example.

student_id name phone age


1 Akon 9876723452 17
2 Akon 9991165674 19
3 Bkon 7898756543 18
4 Ckon 8987867898 19
5 Dkon 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), phone etc.

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_id can'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.

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 independentely 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.

Secondary or Alternative key

The candidate key which are not selected as primary key are known as secondary keys or alternative keys.

Non-key Attributes

Non-key attributes are the attributes or fields of a table, other than candidate key attributes/fields in a table.

Non-prime Attributes

Non-prime Attributes are attributes other than Primary Key attribute(s)..


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 Anamolies. It is a multi-step process that puts data into tabular form, removing duplicated data from the
relation tables.

Normalization is used for mainly two purposes,

Eliminating reduntant(useless) data.


Ensuring data dependencies make sense i.e data is logically stored.

The video below will give you a good overview of Database Normalization. If you want you can skip the video, as the
concept is covered in detail, below the video.

Problems Without Normalization


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
Anamolies are very frequent if database
is not normalized. To understand these anomalies let us take an example
of a
Student table.

rollno name branch hod office_tel


401 Akon CSE Mr. X 53337
402 Bkon CSE Mr. X 53337
403 Ckon CSE Mr. X 53337
404 Dkon CSE Mr. X 53337

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 informations 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.

Normalization Rule
Normalization rules are divided into the following normal forms:

1. First Normal Form


2. Second Normal Form
3. Third Normal Form
4. BCNF
5. Fourth Normal Form
First Normal Form (1NF)

For a table to be in the First Normal Form, it should follow the following 4 rules:

1. It should only have single(atomic) valued attributes/columns.


2. Values stored in a column should be of the same domain
3. All the columns in a table should have unique names.
4. And the order in which data is stored, does not matter.

In the next tutorial, we will discuss about the First Normal Form in details.

Second Normal Form (2NF)

For a table to be in the Second Normal Form,

1. It should be in the First Normal form.


2. And, it should not have Partial Dependency.

To understand what is Partial Dependency and how to normalize a table to 2nd normal for, jump to the Second Normal
Form tutorial.

Third Normal Form (3NF)

A table is said to be in the Third Normal Form when,

1. It is in the Second Normal form.


2. And, it doesn't have Transitive Dependency.

Here is the Third Normal Form tutorial. But we suggest you to first study about the second normal form and then head
over to the third normal form.

Boyce and Codd Normal Form (BCNF)

Boyce and Codd Normal Form is a higher version of the Third Normal form. This form deals with certain type of
anomaly that is not handled by 3NF. A 3NF table which does not have multiple overlapping candidate keys is said to be
in BCNF. For a table to be in BCNF, following conditions must be satisfied:

R must be in 3rd Normal Form


and, for each functional dependency ( X → Y ), X should be a super Key.

To learn about BCNF in detail with a very easy to understand example, head to Boye-Codd Normal Form tutorial.

Fourth Normal Form (4NF)

A table is said to be in the Fourth Normal Form when,

1. It is in the Boyce-Codd Normal Form.


2. And, it doesn't have Multi-Valued Dependency.
Here is the Fourth Normal Form tutorial. But we suggest you to understand other normal forms before you head over
to the fourth normal form.
What is First Normal Form (1NF)?

In our last tutorial we learned and understood how data redundancy or


repetition can lead to several issues like Insertion,
Deletion and Updation anomalies and how Normalization can reduce data redundancy and make the data more
meaningful.

In this tutorial we will learn about the 1st Normal Form which is more like the Step 1 of the Normalization process. The
1st Normal form expects you to design your table in such a way that it can easily be extended and it is easier for you to
retrieve data from it whenever required.

If tables in a database are not even in the 1st Normal Form, it is considered as bad database design.

Rules for First Normal Form


The first normal form expects you to follow a few simple rules while designing your database, and they are:

Rule 1: Single Valued Attributes

Each column of your table should be single valued which means they should not contain multiple values. We will
explain this with help of an
example later, let's see the other rules for now.

Rule 2: Attribute Domain should not change

This is more of a "Common Sense" rule. In each column the values stored must be of the same kind or type.
For example: If you have a column dob to save date of births of a set of people, then you cannot or you must not save
'names' of some of them in that column along with 'date of birth' of others in that column. It should hold only 'date of
birth' for all the records/rows.

Rule 3: Unique name for Attributes/Columns

This rule expects that each column in a table should have a unique name. This is to avoid confusion at the time of
retrieving data or performing any other operation on the stored data.

If one or more columns have same name, then the DBMS system will be left confused.

Rule 4: Order doesn't matters

This rule says that the order in which you store the data in your table doesn't matter.

Time for an Example


Although all the rules are self explanatory still let's take an example where we will create a table to store student data
which will have student's roll no., their name and the name of subjects they have opted for.

Here is our table, with some sample data added to it.

roll_no name subject


101 Akon OS, CN
103 Ckon Java
102 Bkon C, C++

Our table already satisfies 3 rules out of the 4 rules, as all our column names are unique, we have stored data in the
order we wanted to and we have not inter-mixed different type of data in columns.

But out of the 3 different students in our table, 2 have opted for more than 1 subject. And we have stored the subject
names in a single column. But as per the 1st Normal form each column must contain atomic value.
How to solve this Problem?

It's very simple, because all we have to do is break the values into atomic values.

Here is our updated table and it now satisfies the First Normal Form.

roll_no name subject


101 Akon OS
101 Akon CN
103 Ckon Java
102 Bkon C
102 Bkon C++

By doing so, although a few values are getting repeated but values for the subject column are now atomic for each
record/row.

Using the First Normal Form, data redundancy increases, as there will
be many columns with same data in multiple
rows but each row as a whole
will be unique.
What is Second Normal Form?
If you want you can skip the video, as the concept is covered in detail below the video.

For a table to be in the Second Normal Form, it must satisfy two conditions:

1. The table should be in the First Normal Form.


2. There should be no Partial Dependency.

What is Partial Dependency? Do not worry about it. First let's understand what is Dependency in a table?

What is Dependency?
Let's take an example of a Student table with columns student_id, name, reg_no(registration number), branch and
address(student's home address).

student_id name reg_no branch address

In this table, student_id is the primary key and will be unique for every row, hence we can use student_id to fetch
any row of data from this table

Even for a case, where student names are same, if we know the student_id we can easily fetch the correct record.

student_id name reg_no branch address


10 Akon 07-WY CSE Kerala
11 Akon 08-WY IT Gujarat

Hence we can say a Primary Key for a table is the column or a group of columns(composite key) which can uniquely
identify each record in the table.

I can ask from branch name of student with student_id 10, and I can get it. Similarly, if I ask for name of student with
student_id 10 or 11, I will get it. So all I need is student_id and every other column depends on it, or can be fetched
using it.

This is Dependency and we also call it Functional Dependency.

What is Partial Dependency?


Now that we know what dependency is, we are in a better state to understand what partial dependency is.

For a simple table like Student, a single column like student_id can uniquely identfy all the records in a table.

But this is not true all the time. So now let's extend our example to
see if more than 1 column together can act as a
primary key.

Let's create another table for Subject, which will have subject_id and subject_name fields and subject_id will be
the primary key.

subject_id subject_name
1 Java
2 C++
3 Php

Now we have a Student table with student information and another table Subject for storing subject information.

Let's create another table Score, to store the marks obtained by students in the respective subjects. We will also be
saving name of the teacher who teaches that subject along with marks.

score_id student_id subject_id marks teacher


1 10 1 70 Java Teacher
2 10 2 75 C++ Teacher
3 11 1 80 Java Teacher

In the score table we are saving the student_id to know which student's marks are these and subject_id to know for
which subject the marks are for.

Together, student_id + subject_id forms a Candidate Key(learn about Database Keys) for this table, which can be
the Primary key.

Confused, How this combination can be a primary key?

See, if I ask you to get me marks of student with student_id 10, can you get it from this table? No, because you don't
know for which subject. And if I give you subject_id, you would not know for which student. Hence we need
student_id + subject_id to uniquely identify any row.

But where is Partial Dependency?

Now if you look at the Score table, we have a column names teacher which is only dependent on the subject, for Java
it's Java Teacher and for C++ it's C++ Teacher & so on.

Now as we just discussed that the primary key for this table is a composition of two columns which is student_id &
subject_id but the teacher's name only depends on subject, hence the subject_id, and has nothing to do with
student_id.

This is Partial Dependency, where an attribute in a table depends on only a part of the primary key and not on the
whole key.

How to remove Partial Dependency?


There can be many different solutions for this, but out objective is to remove teacher's name from Score table.

The simplest solution is to remove columns teacher from Score table and add it to the Subject table. Hence, the Subject
table will become:

subject_id subject_name teacher


1 Java Java Teacher
2 C++ C++ Teacher
3 Php Php Teacher

And our Score table is now in the second normal form, with no partial dependency.

score_id student_id subject_id marks


1 10 1 70
2 10 2 75
3 11 1 80

Quick Recap
1. For a table to be in the Second Normal form, it should be in the First Normal form and it should not have Partial
Dependency.
2. Partial Dependency exists, when for a composite primary key, any attribute in the table depends only on a part of
the primary key and not
on the complete primary key.
3. To remove Partial dependency, we can divide the table, remove the attribute which is causing partial dependency,
and move it to some other
table where it fits in well.
Third Normal Form (3NF)

In our last tutorial, we learned about the second normal form and even normalized our Score table into the 2nd Normal
Form.

So let's use the same example, where we have 3 tables, Student, Subject and Score.

Student Table

student_id name reg_no branch address


10 Akon 07-WY CSE Kerala
11 Akon 08-WY IT Gujarat
12 Bkon 09-WY IT Rajasthan

Subject Table

subject_id subject_name teacher


1 Java Java Teacher
2 C++ C++ Teacher
3 Php Php Teacher

Score Table

score_id student_id subject_id marks


1 10 1 70
2 10 2 75
3 11 1 80
In the Score table, we need to store some more information, which is the exam name and total marks, so let's add 2 more
columns to the Score table.

score_id student_id subject_id marks exam_name total_marks

Requirements for Third Normal Form


For a table to be in the third normal form,

1. It should be in the Second Normal form.


2. And it should not have Transitive Dependency.

What is Transitive Dependency?

With exam_name and total_marks added to our


Score table, it saves more data now. Primary key for our Score table is
a composite key, which means it's made up of two attributes or columns →
student_id + subject_id.

Our new column exam_name depends on both student and subject. For example, a mechanical engineering student will
have Workshop exam but a computer science student won't. And for some subjects you have Prctical exams and for
some you don't. So we can say that exam_name is dependent on both student_id and subject_id.

And what about our second new column total_marks? Does it depend on our Score table's primary key?

Well, the column total_marks depends on exam_name as with exam type the total score changes. For example,
practicals are of less marks while theory exams are of more marks.

But, exam_name is just another column in the score table. It is not a primary key or even a part of the primary key, and
total_marks depends on it.

This is Transitive Dependency. When a non-prime attribute depends on other non-prime attributes rather than
depending upon the prime attributes or primary key.

How to remove Transitive Dependency?


Again the solution is very simple. Take out the columns exam_name and total_marks from Score table and put them in
an Exam table and use the exam_id wherever required.

Score Table: In 3rd Normal Form

score_id student_id subject_id marks exam_id

The new Exam table

exam_id exam_name total_marks


1 Workshop 200
2 Mains 70
3 Practicals 30

Advantage of removing Transitive Dependency

The advantage of removing transitive dependency is,

Amount of data duplication is reduced.


Data integrity achieved.
Boyce-Codd Normal Form (BCNF)
Boyce-Codd Normal Form or BCNF is an extension to the third normal form, and is also known as 3.5 Normal Form.

Follow the video above for complete explanation of BCNF. Or, if you want, you can even skip the video and jump to
the section below for the complete tutorial.

In our last tutorial, we learned about the third normal form and we also learned how to remove transitive dependency
from a table, we suggest you to follow the last tutorial before this one.

Rules for BCNF


For a table to satisfy the Boyce-Codd Normal Form, it should satisfy the following two conditions:

1. It should be in the Third Normal Form.


2. And, for any dependency A → B, A should be a super key.

The second point sounds a bit tricky, right? In simple words, it means, that for a dependency A → B, A cannot be a
non-prime attribute, if B is a prime attribute.

Time for an Example


Below we have a college enrolment table with columns student_id, subject and professor.

student_id subject professor


101 Java P.Java
101 C++ P.Cpp
102 Java P.Java2
103 C# P.Chash
104 Java P.Java

As you can see, we have also added some sample data to the table.

In the table above:

One student can enrol for multiple subjects. For example, student with student_id 101, has opted for subjects -
Java & C++
For each subject, a professor is assigned to the student.
And, there can be multiple professors teaching one subject like we have for Java.

What do you think should be the Primary Key?

Well, in the table above student_id, subject together form the primary key, because using student_id and subject,
we can find all the columns of the table.

One more important point to note here is, one professor teaches only one subject, but one subject may have two
different professors.

Hence, there is a dependency between subject and professor here, where subject depends on the professor name.

This table satisfies the 1st Normal form because all the values are atomic, column names are unique and all the values
stored in a particular column are of same domain.

This table also satisfies the 2nd Normal Form as their is no Partial Dependency.

And, there is no Transitive Dependency, hence the table also satisfies the 3rd Normal Form.

But this table is not in Boyce-Codd Normal Form.

Why this table is not in BCNF?

In the table above, student_id, subject form primary key, which means subject column is a prime attribute.

But, there is one more dependency, professor → subject.

And while subject is a prime attribute, professor is a non-prime attribute, which is not allowed by BCNF.

How to satisfy BCNF?

To make this relation(table) satisfy BCNF, we will decompose this table into two tables, student table and professor
table.

Below we have the structure for both the tables.

Student Table

student_id p_id
101 1
101 2
and so on...
And, Professor Table

p_id professor subject


1 P.Java Java
2 P.Cpp C++
and so on...

And now, this relation satisfy Boyce-Codd Normal Form. In the next tutorial we will learn about the Fourth Normal
Form.

A more Generic Explanation


In the picture below, we have tried to explain BCNF in terms of relations.
Fourth Normal Form (4NF)
Fourth Normal Form comes into picture when Multi-valued Dependency
occur in any relation. In this tutorial we will
learn about Multi-valued Dependency, how to remove it and how to make any table satisfy the fourth normal form.

Follow the video above for complete explanation of 4th Normal Form. Or, if you want, you can even skip the video and
jump to the section below for the complete tutorial.

In our last tutorial, we learned about the boyce-codd normal form, we suggest you to follow the last tutorial before this
one.

Rules for 4th Normal Form


For a table to satisfy the Fourth Normal Form, it should satisfy the following two conditions:

1. It should be in the Boyce-Codd Normal Form.


2. And, the table should not have any Multi-valued Dependency.

Let's try to understand what multi-valued dependency is in the next section.

What is Multi-valued Dependency?


A table is said to have multi-valued dependency, if the following conditions are true,

1. For a dependency A → B, if for a single value of A, multiple value of B exists, then the table may have multi-
valued dependency.
2. Also, a table should have at-least 3 columns for it to have a multi-valued dependency.
3. And, for a relation R(A,B,C), if there is a multi-valued dependency between, A and B, then B and C should be
independent of each other.
If all these conditions are true for any relation(table), it is said to have multi-valued dependency.

Time for an Example


Below we have a college enrolment table with columns s_id, course and hobby.

s_id course hobby


1 Science Cricket
1 Maths Hockey
2 C# Cricket
2 Php Hockey

As you can see in the table above, student with s_id 1 has opted for two courses, Science and Maths, and has two
hobbies, Cricket and Hockey.

You must be thinking what problem this can lead to, right?

Well the two records for student with s_id 1, will give rise to two more records, as shown below, because for one
student, two hobbies exists, hence along with both the courses, these hobbies should be specified.

s_id course hobby


1 Science Cricket
1 Maths Hockey
1 Science Hockey
1 Maths Cricket

And, in the table above, there is no relationship between the columns course and hobby. They are independent of each
other.

So there is multi-value dependency, which leads to un-necessary repetition of data and other anomalies as well.

How to satisfy 4th Normal Form?


To make the above relation satify the 4th normal form, we can decompose the table into 2 tables.

CourseOpted Table

s_id course
1 Science
1 Maths
2 C#
2 Php

And, Hobbies Table,

s_id hobby
1 Cricket
1 Hockey
2 Cricket
2 Hockey

Now this relation satisfies the fourth normal form.

A table can also have functional dependency along with multi-valued dependency. In that case, the functionally
dependent columns are moved in a separate table and the multi-valued dependent columns are moved to separate tables.

If you design your database carefully, you can easily avoid these issues.
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
create to create new table or database
alter for alteration
truncate delete data from table
drop to drop a table
rename to rename a table

DML: Data Manipulation Language

DML commands are used for manipulating the data stored in the table and not the table itself.

DML commands are not auto-committed. It means changes are not permanent to database, they can be rolled back.

Command Description
insert to insert a new row
update to update existing row
delete to delete a row
merge merging two rows or two tables

TCL: Transaction Control Language

These commands are to keep a check on other commands and their affect
on the database. These commands can annul
changes made by other commands by rolling the data back to its original state. It can also make any temporary change
permanent.

Command Description
commit to permanently save
rollback to undo change
savepoint to save temporarily

DCL: Data Control Language

Data control language are the commands to grant and take back authority from any database user.

Command Description
grant grant permission of right
revoke take back permission.

DQL: Data Query Language

Data query language is used to fetch data from tables based on conditions that we can easily apply.

Command Description
select retrieve records from one or more table
DML, DDL, DCL and TCL Statements in SQL with
Examples
Posted May 28, 2015 by Vishwanath Dalvi in Database, SQL Server

SQL language is divided into four types of primary language statements: DML, DDL, DCL and TCL. Using these
statements, we can define
the structure of a database by creating and altering database objects, and we can manipulate
data in a table through updates or deletions. We also can control which user can read/write data or manage transactions
to create a single unit of work.

The four main categories of SQL statements are as follows:

1. DML (Data Manipulation Language)


2. DDL (Data Definition Language)
3. DCL (Data Control Language)
4. TCL (Transaction Control Language)
DML (Data Manipulation Language)

DML statements affect records in a table. These are basic operations we perform on data such as selecting a few records
from a table, inserting new records, deleting unnecessary records, and updating/modifying existing records.

DML statements include the following:

SELECT – select records from a table


INSERT – insert new records
UPDATE – update/Modify existing records
DELETE – delete existing records

DDL (Data Definition Language)

DDL statements are used to alter/modify a database or table structure


and schema. These statements handle the design
and storage of database objects.

CREATE – create a new Table, database, schema


ALTER – alter existing table, column description
DROP – delete existing objects from database

DCL (Data Control Language)

DCL statements control the level of access that users have on database objects.

GRANT – allows users to read/write on certain database objects


REVOKE – keeps users from read/write permission on database objects

TCL (Transaction Control Language)

TCL statements allow you to control and manage transactions to maintain the integrity of data within SQL statements.

BEGIN Transaction – opens a transaction


COMMIT Transaction – commits a transaction
ROLLBACK Transaction – ROLLBACK a transaction in case of any error
SQL Server commands - DML, DDL, DCL, TCL

Table of Contents
Data Manipulation Language - DML
Data Definition Language - DDL
Data Control Language - DCL
Transaction Control Language - TCL

For those who are in first touch with SQL and it's standard commands... SQL commands in simple words are commands
or instructions which we are using with queries to communicate with our database.

With these commands, we can do administration for our database as well as use/read data, edit it or copy and delete
etc...

It is not uncommon to create our database, tables an relationship between them using some of these commands which
we will cover in later part of this article.

SQL Server commands are grouped in these four main logical groups, and they are:

Data Manipulation Language (DML)


Data Definition Language (DDL)
Data Control Language (DCL) 
Transaction Control Language (TCL)

Using these commands we can define structure of our database, do the insert or update to the data, we can control the
access or privileges over our database.

First, we will go with Data Manipulation Language commands group.

Data Manipulation Language - DML

↑ Back to top

DML commands are mainly used for manipulation with the records in our table, so with them, we can select/read data
with some criteria or not, we can insert new data or edit existing ones... and of course we can delete records if
we don't
need them anymore.

DML commands are:

SELECT - select/read records from table in our database,

INSERT - we can insert new records in our table,

UPDATE - edit/update existing records,

DELETE - delete existing records in our table

DML Commands are mainly used in similar way and by their name we can realize for what they are mainly used in
practise, on next image we will show example of all four commands in real examples:
Short explanation about these commands:

From SELECT part we can see very simple syntax, after we write keyword SELECT with asterisk sign
(which replace
ALL columns) so with this command in our output we will get all columns from table employee, in case
that we don't
need or won't all the columns we can list columns that we
want to see in output... also in our SELECT command after
FROM part we could use WHERE clause to filter output records with some condition.

INSERT command
is also very simple, after keyword INSERT we select table name (INTO keyword is optional) after
name of table in brackets we can specify which columns we will use in this insert,
after that we put keyword VALUES
and than in bracket we are typing values that we want to insert. Arrangement of columns in insert part must be the same
as in VALUES part so that we insert correct values in correct columns.

UPDATE is very useful when we want to change some data in our record in table, this command has very simple
syntax as we can se in image above... after keyword UPDATE we list name of table after that with keyword
SET and
column that we want to change and than assignment sign with new
values (we can combine more than one column)...
after that the most important thing is to use WHERE clause to specify criteria for the record that we want to change that
can be for example
an ID of Person in table.

DELETE is very powerful and simple command, after keyword DELETE we list table name from which one we want
to delete the record and after that the most important thing
is to use WHERE clause to specify criteria
for the record that
we want to change that can be for example an ID of Person in table (Same as in UPDATE).

Warning: UPDATE and DELETE without WHERE clause will set same value from update/delete all the records
from the table!

Data Definition Language - DDL

↑ Back to top
DDL commands we use for definition and creation objects in database (Table, Procedure, Views...). These commands
are mainly used for design and definition the structure of our database.

DDL commands are:

CREATE - we can create a new table, database, procedure, view, trigger...

ALTER - usually we use for editing database objects (table, procedure, view...) for example, add or delete column from
table

DROP - we use for deleting database objects

Rename- is used to rename an object existing in database.

Usage for these commands is showed this image:

As for the previous image commands we will wrote some short explanation for DDL commands.

CREATE TABLE will obviously create a


new table after two keywords CREATE and TABLE we pick a name for the
new table and in the body of this command we type the columns/attributes
for new table... very similar syntax is also
for
creating new Views, Procedures or Triggers.

ALTER we can use to edit our object, for this example on the last image we have added new column/attribute
BirthDate in our table Person.

DROP is used to delete objects from a database, we use DROP with a keyword for an object that we want to create and
a name for that object.

Data Control Language - DCL

↑ Back to top

DCL commands are used for access control and permission management for users in our database. With them we can
easily allow or deny some actions for users on the tables or records (row level security).

DCL commands are:


GRANT -  we can give certain permissions on the table (and other objects) for certain users of database,

DENY - bans certain permissions from users.

REVOKE - with this command we can take back permission from users.

Example of usage of these commands are in this image here:

GRANT in first case we gave privileges to user Almir to do SELECT, INSERT, UPDATE and DELETE on the table
called employees.

REVOKE with this command we can take back privilege to default one... in this case, we take back command INSERT
on the table
employees for user Almir.

DENY is
a specific command. We can conclude that every user has a list of privilege which is denied or granted so
command DENY is there to explicitly ban you some privileges on the database objects.

Transaction Control Language - TCL

↑ Back to top

With TCL commands we can mange and control T-SQL transactions so we can be sure that our transaction is
successfully done and that integrity of our database is not violated.

TCL commands are:

BEGIN TRAN - begin of transaction

COMMIT TRAN - commit for completed transaction

ROLLBACK - go back to beginning if something was not right in transaction.


On this image we have simple example of these three commands combined in one transaction. With BEGIN
TRANSACTION obviously we will begin our transaction, as we can see we will update some author with some value of
ID... query is
more or less clear and the important part is IF-ELSE part of query. If we affect five rows with our first
query than we will COMMIT this transaction. Obviously we will not affect five rows, so we will affect one row for this
reason we will not go into the
IF part but we will go into the ELSE part so our transaction will be returned to beginning
state because it will trigger ROLLBACK command and everything that happened in UPDATE it will be undone.

SAVEPOINT:

Suppose there are set of update, delete transactions performed on the tables. But there are some transactions which we
are very sure about correctness. After that set of transactions we are uncertain about the correctness. So what we can do
here is we can set
a SAVEPOINT at the correct transaction telling the database that, in case of rollback, rollback till the
savepoint marked. Hence the changes done till savepoint will be unchanged and all the transactions after that will be
rolled back.

In the case of multiple transactions, savepoint can be given after each transactions and transaction can be rolled back to
any of the transactions.

01. TRANSACTION
T1;   -- Transaction can be insert, update or delete
02.  
03. SAVEPOINT S1;
04. TRANSACTION
T2;
05. SAVEPOINT S2;
06. TRANSACTION
T3;
07. SAVEPOINT S3;
08. TRANSACTION
T4;
09. ROLLBACK
TO S1; -- This will rollback all the changes by T1 and T2 and will have only the
changes done on T1

AUTOCOMMIT : 

AUTOCOMMIT command automatically commits each transaction after its execution. If this command is set,
then no need to explicitly issue commit. We cannot rollback our transactions, if AUTOCOMMIT is on. This
needs to be set /unset before we begin any transactions.
These SQL commands are mainly categorized into four categories as discussed below:

1. DDL(Data Definition Language) : DDL or Data Definition Language actually consists of the SQL commands
that can be used to define the database schema. It simply deals with descriptions of the database schema and is
used to create and modify the structure of database objects in database.

Examples of DDL commands:

CREATE – is used to create the database or its objects (like table, index, function, views, store procedure
and triggers).
DROP – is used to delete objects from the database.
ALTER-is used to alter the structure of the database.
TRUNCATE–is used to remove all records from a table, including all spaces allocated for the records are
removed.
COMMENT –is used to add comments to the data dictionary.
RENAME –is used to rename an object existing in the database.
2. DML(Data Manipulation Language) : The SQL commands that deals with the manipulation of data present in
database belong to DML or
Data Manipulation Language and this includes most of the SQL statements.

Examples of DML:

SELECT – is used to retrieve data from the a database.


INSERT – is used to insert data into a table.
UPDATE – is used to update existing data within a table.
DELETE – is used to delete records from a database table.
3. DCL(Data Control Language) : DCL includes commands such as GRANT and REVOKE which mainly deals
with the rights, permissions and other controls of the database system.

Examples of DCL commands:

GRANT-gives user’s access privileges to database.


REVOKE-withdraw user’s access privileges given by using the GRANT command.
4. TCL(transaction Control Language) : TCL commands deals with the transaction within the database.

Examples of TCL commands:

COMMIT– commits a Transaction.


ROLLBACK– rollbacks a transaction in case of any error occurs.
SAVEPOINT–sets a savepoint within a transaction.
SET TRANSACTION–specify characteristics for the transaction.
First of all, let’s define what is DDL, DML, DCL, and TCL.

DDL is Data Definition Language


DML is Data Manipulation Language
DCL is Data Control Language
TCL is Transaction Control Language

As you see from its name it allows to define, manipulate and control data and transactions in SQL language.

It’s four types of SQL sub-languages, that’s why it’s no sense to search for a difference between DDL vs DML or DCL
vs TCL.

SQL commands list:

Language Command List


CREATE
DROP
ALTER
 DDL
RENAME
TRUNCATE

SELECT
INSERT
 DML UPDATE
DELETE

GRANT
 DCL REVOKE

START TRANSACTION
COMMIT
 TCL
ROLLBACK

Keep reading and I’ll explain in details what are DDL, DML, DCL, and TCL with examples.

Table of Contents [hide]

What is DDL in SQL?


CREATE
DROP
ALTER
RENAME
TRUNCATE
What is DML in SQL?
SELECT
INSERT
UPDATE
DELETE
What is DCL in SQL?
GRANT
REVOKE
What is TCL in SQL?
START TRANSACTION (BEGIN, BEGIN WORK)
COMMIT
ROLLBACK

What is DDL in SQL?


DDL allows you to create SQL statements to make operations with database data structures (schemas, tables etc.).

These are SQL DDL commands list and examples:

CREATE

CREATE statement is used to create a new database, table, index or stored procedure.

Create database example:

1 CREATE DATABASE explainjava;

Create table example:

1 CREATE TABLE user (


2   id INT(16) PRIMARY KEY AUTO_INCREMENT,
3   name VARCHAR(255) NOT NULL
4 );

DROP

DROP statement allows you to remove database, table, index or stored procedure.

Drop database example:

1 DROP DATABASE explainjava;


Drop table example:

1 DROP TABLE user;

ALTER

ALTER is used to modify existing database data structures (database, table).

Alter table example:

1 ALTER TABLE user ADD COLUMN lastname VARCHAR(255) NOT NULL;

RENAME

RENAME command is used to rename SQL table.

Rename table example:

1 RENAME TABLE user TO student;

TRUNCATE

TRUNCATE operation is used to delete all table records.

Logically it’s the same as DELETE command.

Differences between DELETE and TRUNCATE commands are:

TRUNCATE is really faster


TRUNCATE cannot be rolled back
TRUNCATE command does not invoke ON DELETE triggers

Example:

1 TRUNCATE student;
What is DML in SQL?
DML
is a Data Manipulation Language, it’s used to build SQL queries to manipulate (select, insert, update, delete etc.)
data in the database.

This is DML commands list with examples:

SELECT

SELECT query is used to retrieve a data from SQL tables.

Example:

1 SELECT * FROM student;

INSERT

INSERT command is used to add new rows into the database table.

Example:

1 INSERT INTO student (name, lastname) VALUES ('Dmytro', 'Shvechikov');

UPDATE

UPDATE statement modifies records into the table.

Example:

1 UPDATE student SET name = 'Dima' WHERE lastname = 'Shvechikov';

DELETE

DELETE query removes entries from the table.

Example:
1 DELETE FROM student WHERE name = 'Dima';

What is DCL in SQL?


DCL a Data Control Language.

Its commands are responsible for access restrictions inside of the database.

Let’s take a look at DCL statements definitions.

GRANT

GRANT command gives permissions to SQL user account.

For example, I want to grant all privileges to ‘explainjava’ database for user ‘dmytro@localhost’.

Let’s create a user first:

1 CREATE USER 'dmytro'@'localhost' IDENTIFIED BY '123';

Then I can grant all privileges using GRANT statement:

1 GRANT ALL PRIVILEGES ON explainjava.* TO 'dmytro'@'localhost';

and we have to save changes using FLUSH command:

1 FLUSH PRIVILEGES;

REVOKE

REVOKE statement is used to remove privileges from user accounts.

Example:
1 REVOKE ALL PRIVILEGES ON explainjava.* FROM 'dmytro'@'localhost';

and save changes:

1 FLUSH PRIVILEGES;

What is TCL in SQL?


TCL is a Transaction Control Language.

Its commands are used to manage transactions in SQL databases.

This is TCL commands list:

START TRANSACTION (BEGIN, BEGIN WORK)

START TRANSACTION is used to start a new SQL transaction.

BEGIN and BEGIN WORK are aliases for START TRANSACTION.

Example:

1 START TRANSACTION;

after that, you’re doing manipulations with a data (insert, update, delete) and at the end, you need to commit a
transaction.

COMMIT

As a mentioned above COMMIT command finishes transaction and stores all changes made inside of a transaction.

Example:

1 START TRANSACTION;
2 INSERT INTO student (name, lastname) VALUES ('Dmytro', 'Shvechikov');
3 COMMIT;

ROLLBACK
ROLLBACK statement reverts all changes made in the scope of transaction.

Example:

1 START TRANSACTION;
2 INSERT INTO student (name, lastname) VALUES ('Dmytro', 'Shvechikov');
3 ROLLBACK;
SQL | CREATE
There are two CREATE statements available in SQL:

1. CREATE DATABASE
2. CREATE TABLE

CREATE DATABASE

A Database is defined as a structured set of data. So, in SQL the very first step to store the data in a well structured
manner is to create a database. The CREATE DATABASE statement is used to create a new database in SQL.

Syntax:
CREATE DATABASE database_name;
database_name: name of the database.

Example Query:
This query will create a new database in SQL and name the database as my_database.

CREATE DATABASE my_database;

CREATE TABLE

We have learned above about creating databases. Now to store the data
we need a table to do that. The CREATE
TABLE statement is used to create a table in SQL. We know that a table comprises of rows and columns. So while
creating tables we have to provide all the information
to SQL about the names of the columns, type of data to be stored
in columns, size of the data etc. Let us now dive into details on how to use CREATE TABLE statement to create tables
in SQL.

Syntax:
CREATE TABLE table_name
(
column1 data_type(size),
column2 data_type(size),
column3 data_type(size),
....
);
table_name: name of the table.
column1 name of the first column.
data_type: Type of data we want to store in the particular column.
For example,int for integer data.
size: Size of the data we can store in a particular column. For example if for
a column we specify the data_type as int and size as 10 then this column can store an integer
number of maximum 10 digits.

Example Query:
This query will create a table named Students with three columns, ROLL_NO, NAME and SUBJECT.
CREATE TABLE Students
(
ROLL_NO int(3),
NAME varchar(20),
SUBJECT varchar(20),
);

This query will create a table named Students. The ROLL_NO field is of type int and can store an integer number of
size 3. The next two columns NAME and SUBJECT are of type varchar and can store characters and the size 20
specifies that these two fields can hold maximum of 20 characters.
SQL | DROP, TRUNCATE
DROP

DROP is used to delete a whole database or just a table.The DROP statement destroys the objects like an existing database,
table, index, or view.
A DROP statement in SQL removes a component from a relational database management system (RDBMS).
Syntax:
DROP object object_name

Examples:
DROP TABLE table_name;
table_name: Name of the table to be deleted.
DROP DATABASE database_name;
database_name: Name of the database to be deleted.

TRUNCATE

TRUNCATE statement is a Data Definition Language (DDL) operation that


is used to mark the extents of a table for
deallocation (empty for reuse). The result of this operation quickly removes all data from a table, typically bypassing a number
of integrity enforcing mechanisms. It was officially introduced in the SQL:2008 standard.
The TRUNCATE TABLE mytable statement is logically (though not physically) equivalent to the DELETE FROM mytable
statement (without a WHERE clause).
Syntax:
TRUNCATE TABLE table_name;
table_name: Name of the table to be truncated.
DATABASE name - student_data

DROP vs TRUNCATE

Truncate is normally ultra-fast and its ideal for deleting data from a temporary table.
Truncate preserves the structure of the table for future use, unlike
drop table where the table is deleted with its full
structure.
Table or Database deletion using DROP statement cannot be rolled back, so it must be used wisely.
Queries

To delete the whole database


DROP DATABASE student_data;

After running the above query whole database will be deleted.

To truncate Student_details table from student_data database.


TRUNCATE TABLE Student_details;

After running the above query Student_details table will be truncated, i.e, the data will be deleted but the structure will
remain in the memory for further operations.
SQL | ALTER (ADD, DROP, MODIFY)
ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop
various constraints on the existing table.

ALTER TABLE – ADD

ADD is used to add columns into the existing table. Sometimes we may require to add additional information, in that
case we do not require to
create the whole database again, ADD comes to our rescue.

Syntax:
ALTER TABLE table_name
ADD (Columnname_1 datatype,
Columnname_2 datatype,

Columnname_n datatype);

ALTER TABLE – DROP

DROP COLUMN is used to drop column in a table. Deleting the unwanted columns from the table.

Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;

ALTER TABLE-MODIFY

It is used to modify the existing columns in a table. Multiple columns can also be modified at once.
*Syntax may vary slightly in different databases.

Syntax(Oracle,MySQL,MariaDB):
ALTER TABLE table_name
MODIFY column_name column_type;

Syntax(SQL Server):
ALTER TABLE table_name
ALTER COLUMN column_name column_type;

Queries

Sample Table:

Student

ROLL_NO NAME
1 Ram
2 Abhi
3 Rahul
4 Tanu

QUERY:

To ADD 2 columns AGE and COURSE to table Student.


ALTER TABLE Student ADD (AGE number(3),COURSE varchar(40));

OUTPUT:

ROLL_NO NAME AGE COURSE


1 Ram
2 Abhi
3 Rahul
4 Tanu

MODIFY column COURSE in table Student


ALTER TABLE Student MODIFY COURSE varchar(20);

After running the above query maximum size of Course Column is reduced to 20 from 40.

DROP column COURSE in table Student.


ALTER TABLE Student DROP COLUMN COURSE;

OUTPUT:

ROLL_NO NAME AGE


1 Ram
2 Abhi
3 Rahul
4 Tanu
SQL | DROP, TRUNCATE
DROP

DROP is used to delete a whole database or just a table.The DROP statement destroys the objects like an existing database,
table, index, or view.
A DROP statement in SQL removes a component from a relational database management system (RDBMS).
Syntax:
DROP object object_name

Examples:
DROP TABLE table_name;
table_name: Name of the table to be deleted.
DROP DATABASE database_name;
database_name: Name of the database to be deleted.

TRUNCATE

TRUNCATE statement is a Data Definition Language (DDL) operation that


is used to mark the extents of a table for
deallocation (empty for reuse). The result of this operation quickly removes all data from a table, typically bypassing a number
of integrity enforcing mechanisms. It was officially introduced in the SQL:2008 standard.
The TRUNCATE TABLE mytable statement is logically (though not physically) equivalent to the DELETE FROM mytable
statement (without a WHERE clause).
Syntax:
TRUNCATE TABLE table_name;
table_name: Name of the table to be truncated.
DATABASE name - student_data

DROP vs TRUNCATE

Truncate is normally ultra-fast and its ideal for deleting data from a temporary table.
Truncate preserves the structure of the table for future use, unlike
drop table where the table is deleted with its full
structure.
Table or Database deletion using DROP statement cannot be rolled back, so it must be used wisely.
Queries

To delete the whole database


DROP DATABASE student_data;

After running the above query whole database will be deleted.

To truncate Student_details table from student_data database.


TRUNCATE TABLE Student_details;

After running the above query Student_details table will be truncated, i.e, the data will be deleted but the structure will
remain in the memory for further operations.
SQL | Comments
As is any programming languages comments matter a lot in SQL also.
In this set we will learn about writing comments
in any SQL snippet.

Comments can be written in the following three formats:

1. Single line comments.


2. Multi line comments
3. In line comments

Single line comments: Comments starting and ending in a single line are considered as single line comments.
Line starting with ‘–‘ is a comment and will not be executed.
Syntax:
--single line comment
--another comment
SELECT * FROM Customers;

Multi line comments: Comments starting in one line and ending in different line are considered as multi line
comments. Line starting with ‘/*’ is considered as starting point of comment and are terminated when ‘*/’ is
encountered.
Syntax:

/* multi line comment


another comment */
SELECT * FROM Customers;

In line comments: In line comments are an extension of multi line comments, comments can be stated in between
the statements and are enclosed in between ‘/*’ and ‘*/’.
Syntax:
SELECT * FROM /* Customers; */

More examples:
Multi line comment ->
/* SELECT * FROM Students;
SELECT * FROM STUDENT_DETAILS;
SELECT * FROM Orders; */
SELECT * FROM Articles;
In line comment ->
SELECT * FROM Students;
SELECT * FROM /* STUDENT_DETAILS;
SELECT * FROM Orders;
SELECT * FROM */ Articles;
SQL: create command
create is a DDL SQL command used to create a table or a database in relational database management system.

Creating a Database
To create a database in RDBMS, create command is used. Following is the syntax,
CREATE DATABASE <DB_NAME>;

Example for creating Database


CREATE DATABASE Test;

The above command will create a database named Test, which will be an empty schema without any table.

To create tables in this newly created database, we can again use the create command.

Creating a Table
createcommand can also be used to create tables. Now when we create a table, we have to specify the details of the
columns of
the tables too. We can specify the names and datatypes of various columns in the create command itself.

Following is the syntax,


CREATE TABLE <TABLE_NAME>
(
column_name1 datatype1,
column_name2 datatype2,
column_name3 datatype3,
column_name4 datatype4
);

create table command will tell the database system to create a new table with the given table name and column
information.

Example for creating Table


CREATE TABLE Student(
student_id INT,
name VARCHAR(100),
age INT);

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);

Most commonly used datatypes for Table columns

Here we have listed some of the most commonly used datatypes used for columns in tables.

Datatype Use
INT used for columns which will store integer values.
FLOAT used for columns which will store float values.
DOUBLE used for columns which will store float values.
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).
DATE used for columns which will store date values.
used for columns which will store text which is generally long in length. For example, if you create a table
TEXT for storing
profile information of a social networking website, then for about me section you can have a
column of type TEXT.
SQL | ALTER (RENAME)
Sometimes we may want to rename our table to give it a more relevant name. For this purpose we can use ALTER
TABLE to rename the name of table.
*Syntax may vary in different databases.

Syntax(Oracle,MySQL,MariaDB):
ALTER TABLE table_name
RENAME TO new_table_name;

Columns can be also be given new name with the use of ALTER TABLE.
Syntax(Oracle):
ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;

Syntax(MySQL,MariaDB):
ALTER TABLE table_name
CHANGE COLUMN old_name TO new_name;

Sample Table:

Student

ROLL_NO NAME AGE


1 Ram 20
2 Abhi 21
3 Rahul 22
4 Tanu 19

QUERY:

Change the name of column NAME to FIRST_NAME in table Student.


ALTER TABLE Student RENAME COLUMN NAME TO FIRST_NAME;

OUTPUT:

ROLL_NO FIRST_NAME AGE


1 Ram 20
2 Abhi 21
3 Rahul 22
4 Tanu 19

Change the name of the table Student to Student_Details


ALTER TABLE Student RENAME TO Student_Details;

OUTPUT:

Student_Details

ROLL_NO FIRST_NAME AGE


1 Ram 20
2 Abhi 21
3 Rahul 22
4 Tanu 19
SQL: ALTER command
alter command is used for altering the table structure, such as,

to add a column to existing table


to rename any existing column
to change datatype of any column or to modify its size.
to drop a column from the table.

ALTER Command: Add a new Column


Using ALTER command we can add a column to any existing table. Following is the syntax,
ALTER TABLE table_name ADD(
column_name datatype);

Here is an Example for this,


ALTER TABLE student ADD(
address VARCHAR(200)
);

The above command will add a new column address to the table student, which will hold data of type varchar which
is nothing but string, of length 200.

ALTER Command: Add multiple new Columns


Using ALTER command we can even add multiple new columns to any existing table. Following is the syntax,
ALTER TABLE table_name ADD(
column_name1 datatype1,
column-name2 datatype2,
column-name3 datatype3);

Here is an Example for this,


ALTER TABLE student ADD(
father_name VARCHAR(60),
mother_name VARCHAR(60),
dob DATE);

The above command will add three new columns to the student table

ALTER Command: Add Column with default value


ALTER command can add a new column to an existing table with a default value too. The default value is used when no
value is inserted in the column. Following is the syntax,
ALTER TABLE table_name ADD(
column-name1 datatype1 DEFAULT some_value
);

Here is an Example for this,


ALTER TABLE student ADD(
dob DATE DEFAULT '01-Jan-99'
);

The above command will add a new column with a preset default value to the table student.

ALTER Command: Modify an existing Column


ALTER command can also be used to modify data type of any existing column. Following is the syntax,
ALTER TABLE table_name modify(
column_name datatype
);

Here is an Example for this,


ALTER TABLE student MODIFY(
address varchar(300));

Remember we added a new column address in the beginning? The above command will modify the address column of
the student table, to now hold upto 300 characters.

ALTER Command: Rename a Column


Using ALTER command you can rename an existing column. Following is the syntax,
ALTER TABLE table_name RENAME
old_column_name TO new_column_name;

Here is an example for this,


ALTER TABLE student RENAME
address TO location;

The above command will rename address column to location.

ALTER Command: Drop a Column


ALTER command can also be used to drop or remove columns. Following is the syntax,
ALTER TABLE table_name DROP(
column_name);

Here is an example for this,


ALTER TABLE student DROP(
address);

The above command will drop the address column from the table student.
Truncate, Drop or Rename a Table
In this tutorial we will learn about the various DDL commands which are used to re-define the tables.

TRUNCATE command
TRUNCATE command removes all the records from a table. But this command will not destroy the table's structure. When
we use TRUNCATE command on a table its (auto-increment) primary key is also initialized. Following is its syntax,
TRUNCATE TABLE table_name

Here is an example explaining it,


TRUNCATE TABLE student;

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,
RENAME TABLE old_table_name to new_table_name

Here is an example explaining it.


RENAME TABLE student to students_info;

The above query will rename the table student to students_info.


DML(Data Manipulation Language) : The SQL commands that deals with the manipulation of data present in
database belong to DML or
Data Manipulation Language and this includes most of the SQL statements.

Examples of DML:

SELECT – is used to retrieve data from the a database.


INSERT – is used to insert data into a table.
UPDATE – is used to update existing data within a table.
DELETE – is used to delete records from a database table.
SQL | SELECT Query
Select is the most commonly used statement in SQL. The SELECT Statement in SQL is used to retrieve or fetch data from
a database. We can fetch either the entire table or according to some specified rules. The data returned is stored in a result
table. This result table is also
called result-set.

With the SELECT clause of a SELECT command statement, we specify the columns that we want to be displayed in the
query result and, optionally, which column headings we prefer to see above the result table.

The select clause is the first clause and is one of the last clauses of the select statement that the database server evaluates.
The reason for this is that before we can determine what to include in the final result set, we need to know all of the
possible columns that could be included in the final result set.

Sample Table:

Basic Syntax:
SELECT column1,column2 FROM table_name
column1 , column2: names of the fields of the table
table_name: from where we want to fetch

This query will return all the rows in the table with fields column1 , column2.

To fetch the entire table or all the fields in the table:


SELECT * FROM table_name;

Query to fetch the fields ROLL_NO, NAME, AGE from the table Student:
SELECT ROLL_NO, NAME, AGE FROM Student;

Output:

ROLL_NO NAME Age


1 Ram 18
2 RAMESH 18
3 SUJIT 20
4 SURESH 18
To fetch all the fields from the table Student:
SELECT * FROM Student;

Output:

ROLL_NO NAME ADDRESS PHONE Age


1 Ram Delhi XXXXXXXXXX 18
2 RAMESH GURGAON XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
4 SURESH Delhi XXXXXXXXXX 18
Viewing Data in the Table (Select Command)

Once
data has been inserted into a table, the next most logical operation would be to view what has been inserted. The SELECT SQL
verb is used to achieve this.

All Rows and All Columns

Syntax:   SELECT * FROM Table_name;

eg:  Select * from Student;   It will show all the table records.

SELECT First_name, DOB FROM STUDENT WHERE Reg_no = 'S101'; Cover it by single inverted comma if its datatype is
varchar or char.

This
Command will show one row. because you have given condition for only one row and particular records. If condition which has
given in WHERE Clause is true then records will be fetched otherwise it will show no records selected.

Eliminating Duplicates:

A table could hold duplicate rows. In such a case, you can eliminate duplicates.

Syntax: SELECT DISTINCT col, col, .., FROM table_name;

eg :   SELECT DISTINCT * FROM Student;

or :  SELECT DISTINCT first_name, city, pincode FROM Student;

It scans through entire rows, and eliminates rows that have exactly the same contents in each column.

Sorting DATA:

The Rows retrieved from the table will be sorted in either Ascending or Descending order depending on the
condition specified in select statement, the Keyword has used ORDER BY.

SELECT * FROM Student 


ORDER BY First_Name;

it will in show records as alphabetical order from A to Z ascending order. If you want Descending order means Z to A
then used DESC Keyword at last. 

eg : SELECT first_name, city,pincode FROM Student 


 ORDER BY First_name DESC;

For More SELECT Command Click Here.


SQL | INSERT INTO Statement
The INSERT INTO statement of SQL is used to insert a new row in a table. There are two ways of using INSERT INTO
statement for inserting rows:

1. Only values: First method is to specify only the value of data to be inserted without the column names.
Syntax:
INSERT INTO table_name VALUES (value1, value2, value3,...);
table_name: name of the table.
value1, value2,.. : value of first column, second column,... for the new record

2. Column names and values both: In the second method we will specify both the columns which we want to fill and
their corresponding values as shown below:
Syntax:
INSERT INTO table_name (column1, column2, column3,..) VALUES ( value1, value2, value3,..);
table_name: name of the table.
column1: name of first column, second column ...
value1, value2, value3 : value of first column, second column,... for the new record

Queries:

Method 1 (Inserting only values) :


INSERT INTO Student VALUES ('5','HARSH','WEST BENGAL','XXXXXXXXXX','19');
Output:
The table Student will now look like:

ROLL_NO NAME ADDRESS PHONE Age


1 Ram Delhi XXXXXXXXXX 18
2 RAMESH GURGAON XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
4 SURESH Delhi XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
2 RAMESH GURGAON XXXXXXXXXX 18
5 HARSH WEST BENGAL XXXXXXXXXX 19

Method 2 (Inserting values in only specified columns):


INSERT INTO Student (ROLL_NO, NAME, Age) VALUES ('5','PRATIK','19');

Output:
The table Student will now look like:

ROLL_NO NAME ADDRESS PHONE Age


1 Ram Delhi XXXXXXXXXX 18
2 RAMESH GURGAON XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
4 SURESH Delhi XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
2 RAMESH GURGAON XXXXXXXXXX 18
5 PRATIK null null 19

Notice that the columns for which the values are not provided are filled by null. Which is the default values for those
columns.

Using SELECT in INSERT INTO Statement

We can use the SELECT statement with INSERT INTO statement to copy rows from one table and insert them into another
table.The use of this statement is similar to that of INSERT INTO statement. The difference is
that the SELECT statement is
used here to select data from a different table. The different ways of using INSERT INTO SELECT statement are shown
below:

Inserting all columns of a table: We can copy all the data of a table and insert into in a different table.
Syntax:
INSERT INTO first_table SELECT * FROM second_table;
first_table: name of first table.
second_table: name of second table.

We have used the SELECT statement to copy the data from one table and INSERT INTO statement to insert in a
different table.

Inserting specific columns of a table: We can copy only those columns of a table which we want to insert into in a
different table.
Syntax:
INSERT INTO first_table(names_of_columns1) SELECT names_of_columns2 FROM second_table;
first_table: name of first table.
: name of second table.
second_table
names of columns1: name of columns separated by comma(,) for table 1.
names of columns2: name of columns separated by comma(,) for table 2.

We have used the SELECT statement to copy the data of the selected columns only from the second table and INSERT
INTO statement to insert in first table.

Copying specific rows from a table: We can copy specific rows from a table to insert into another table by using
WHERE clause with the SELECT statement. We have to provide appropriate condition in the WHERE clause to select
specific rows.
Syntax:

INSERT INTO table1 SELECT * FROM table2 WHERE condition;


first_table: name of first table.
second_table: name of second table.
condition: condition to select specific rows.

Table2: LateralStudent

ROLL_NO NAME ADDRESS PHONE Age


7 SOUVIK DUMDUM XXXXXXXXXX 18
8 NIRAJ NOIDA XXXXXXXXXX 19
9 SOMESH ROHTAK XXXXXXXXXX 20

Queries:

Method 1(Inserting all rows and columns):


INSERT INTO Student SELECT * FROM LateralStudent;

Output:
This query will insert all the data of the table LateralStudent in the table Student. The table Student will now look
like,

ROLL_NO NAME ADDRESS PHONE Age


1 Ram Delhi XXXXXXXXXX 18
2 RAMESH GURGAON XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
4 SURESH Delhi XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
2 RAMESH GURGAON XXXXXXXXXX 18
7 SOUVIK DUMDUM XXXXXXXXXX 18
8 NIRAJ NOIDA XXXXXXXXXX 19
9 SOMESH ROHTAK XXXXXXXXXX 20

Method 2(Inserting specific columns):


INSERT INTO Student(ROLL_NO,NAME,Age) SELECT ROLL_NO, NAME, Age FROM LateralStudent;

Output:
This query will insert the data in the columns ROLL_NO, NAME and Age of the table LateralStudent in the table
Student and the remaining columns in the Student table will be filled by null which is the default value of the remaining
columns. The table Student will now look like,

ROLL_NO NAME ADDRESS PHONE Age


1 Ram Delhi XXXXXXXXXX 18
2 RAMESH GURGAON XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
4 SURESH Delhi XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
2 RAMESH GURGAON XXXXXXXXXX 18
7 SOUVIK null null 18
8 NIRAJ null null 19
9 SOMESH null null 20
Select specific rows to insert:
INSERT INTO Student SELECT * FROM LateralStudent WHERE Age = 18;

Output:
This query will select only the first row from table LateralStudent to insert into the table Student. The table Student will
now look like,

ROLL_NO NAME ADDRESS PHONE Age


1 Ram Delhi XXXXXXXXXX 18
2 RAMESH GURGAON XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
4 SURESH Delhi XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
2 RAMESH GURGAON XXXXXXXXXX 18
7 SOUVIK DUMDUM XXXXXXXXXX 18
Using INSERT SQL command
Data Manipulation Language (DML) statements are used for managing data in database. DML commands are not auto-
committed. It means changes made by DML command are not permanent to database, it can be rolled back.

Talking about the Insert command, whenever we post a Tweet on Twitter, the text is stored in some table, and as we
post a new tweet, a
new record gets inserted in that table.

INSERT command
Insert command is used to insert data into a table. Following is its general syntax,
INSERT INTO table_name VALUES(data1, data2, ...)

Lets see an example,

Consider a table student with the following fields.

s_id name age


INSERT INTO student VALUES(101, 'Adam', 15);

The above command will insert a new record into student table.

s_id name age


101 Adam 15

Insert value into only specific columns

We can use the INSERT command to insert values for only some specific columns of a row. We can specify the column
names along with the values to be inserted like this,
INSERT INTO student(id, name) values(102, 'Alex');

The above SQL query will only insert id and name values in the newly inserted record.

Insert NULL value to a column

Both the statements below will insert NULL value into age column of the student table.
INSERT INTO student(id, name) values(102, 'Alex');

Or,
INSERT INTO Student VALUES(102,'Alex', null);

The above command will insert only two column values and the other column is set to null.

S_id S_Name age


101 Adam 15
102 Alex

Insert Default value to a column


INSERT INTO Student VALUES(103,'Chris', default)

S_id S_Name age


101 Adam 15
102 Alex
103 chris 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')

More than one are there to Insert data into a table

eg: INSERT INTO student (reg_no, first_name, last_name, dob,address, pincode)


VALUES('A101', 'Mohd', 'Imran', '01-MAR-89','Allahabad', 211001);

eg : INSERT INTO student VALUES('A101', 'Mohd', 'Imran', '01-MAR-89','Allahabad', 211001);

Note : Character expression placed within the insert into statement must be enclosed in single
quotes (').

Inserting data into a table from another table:

In addition to inserting data one row at a time into a table, it is quite possible to populate a table
with data that already exist in another table. You can store same record in a table that already
stored in another table.

Eg : suppose you want to insert data from course table to university table then use this example:

INSERT INTO university SELECT course_id, course_name FROM course;

Data will be inserted into university table automatically, what will be in course table.

you can give condition also in WHERE clause.


SQL | UPDATE Statement
The UPDATE statement in SQL is used to update the data of an existing table in database. We can update single columns as
well as multiple columns using UPDATE statement as per our requirement.

Basic Syntax
UPDATE table_name SET column1 = value1, column2 = value2,...
WHERE condition;

table_name: name of the table


column1: name of first , second, third column....
value1: new value for first, second, third column....
condition: condition to select the rows for which the
values of columns needs to be updated.

NOTE: In the above query the SET statement is used to set new values to the particular column and the WHERE
clause is
used to select the rows for which the columns are needed to be updated. If we have not used the WHERE clause then the
columns in all the rows will be updated. So the WHERE clause is used to choose the particular rows.

Example Queries

Updating single column: Update the column NAME and set the value to ‘PRATIK’ in all the rows where Age is
20.
UPDATE Student SET NAME = 'PRATIK' WHERE Age = 20;

Output:
This query will update two rows(third row and fifth row) and the table Student will now look like,
ROLL_NO NAME ADDRESS PHONE Age
1 Ram Delhi XXXXXXXXXX 18
2 RAMESH GURGAON XXXXXXXXXX 18
3 PRATIK ROHTAK XXXXXXXXXX 20
4 SURESH Delhi XXXXXXXXXX 18
3 PRATIK ROHTAK XXXXXXXXXX 20
2 RAMESH GURGAON XXXXXXXXXX 18

Updating multiple columns: Update the columns NAME to ‘PRATIK’ and ADDRESS to ‘SIKKIM’ where
ROLL_NO is 1.
UPDATE Student SET NAME = 'PRATIK', ADDRESS = 'SIKKIM' WHERE ROLL_NO = 1;

Output:
The above query will update two columns in the first row and the table Student will now look like,

ROLL_NO NAME ADDRESS PHONE Age


1 PRATIK SIKKIM XXXXXXXXXX 18
2 RAMESH GURGAON XXXXXXXXXX 18
3 PRATIK ROHTAK XXXXXXXXXX 20
4 SURESH Delhi XXXXXXXXXX 18
3 PRATIK ROHTAK XXXXXXXXXX 20
2 RAMESH GURGAON XXXXXXXXXX 18

Note: For updating multiple columns we have used comma(,) to separate the names and values of two columns.

Omitting WHERE clause: If we omit the WHERE clause from the update query then all of the rows will get updated.
UPDATE Student SET NAME = 'PRATIK';

Output:
The table Student will now look like,

ROLL_NO NAME ADDRESS PHONE Age


1 PRATIK Delhi XXXXXXXXXX 18
2 PRATIK GURGAON XXXXXXXXXX 18
3 PRATIK ROHTAK XXXXXXXXXX 20
4 PRATIK Delhi XXXXXXXXXX 18
3 PRATIK ROHTAK XXXXXXXXXX 20
2 PRATIK GURGAON XXXXXXXXXX 18
Using UPDATE SQL command
Let's take an example of a real-world problem. These days, Facebook provides an option for Editing your status update,
how do you think it works? Yes, using the Update SQL command.

Let's learn about the syntax and usage of the UPDATE command.

UPDATE command
UPDATE command is used to update any record of data in a table. Following is its general syntax,
UPDATE table_name SET column_name = new_value WHERE some_condition;

WHERE is used to add a condition to any SQL query, we will soon study about it in detail.

Lets take a sample table student,

student_id name age


101 Adam 15
102 Alex
103 chris 14
UPDATE student SET age=18 WHERE student_id=102;

S_id S_Name age


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.

Updating Multiple Columns

We can also update values of multiple columns using a single UPDATE statement.
UPDATE student SET name='Abhi', age=17 where s_id=103;

The above command will update two columns of the record which has s_id 103.

s_id name age


101 Adam 15
102 Alex 18
103 Abhi 17

UPDATE Command: Incrementing Integer Value

When we have to update any integer value in a table, then we can fetch and update the value in the table in a single
statement.

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.

UPDATE Operation:

The UPDATE command is used to change or modify data values in a table and UPDATE
command can Update all the rows from the table or a set of rows from the table.

eg : UPDATE Student SET course='MCA';

Course is a column name, suppose ant time you want to update something like that in the student
table course should be MCA for all students then you can use this type of query. It will update all
the rows in the table all rows will have MCA course.

Now, if you want update particular row then see below.

UPDATE Student SET course='MCA' where reg_no='A101'; it will update only one row that will
have Register no. A101.

you can use different-different types of condition in WHERE clause, eg salary updation, if salary
has increased someone's then simply multiply, addition you can do in salary column.

I will show example. Suppose someone's has increased salary by 10% then what should to do. See
example:

UPDATE employee SET salary= salary+salary*0.1 WHERE employee_id='E101'; Salary will be


automatically increased by 10% of salary.
SQL | DELETE Statement
The DELETE Statement in SQL is used to delete existing records from a table. We can delete a single record or multiple
records depending on the condition we specify in the WHERE clause.

Basic Syntax:
DELETE FROM table_name WHERE some_condition;
table_name: name of the table
some_condition: condition to choose particular record.

Note: We can delete single as well as multiple records depending on the condition we provide in WHERE clause. If we omit
the WHERE clause then all of the records will be deleted and the table will be empty.

Sample Table:

Example Queries:

Deleting single record: Delete the rows where NAME = ‘Ram’. This will delete only the first row.
DELETE FROM Student WHERE NAME = 'Ram';

Output:
The above query will delete only the first row and the table Student will now look like,

ROLL_NO NAME ADDRESS PHONE Age


2 RAMESH GURGAON XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
4 SURESH Delhi XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
2 RAMESH GURGAON XXXXXXXXXX 18
Deleting multiple records: Delete the rows from the table Student where Age is 20. This will delete 2 rows(third row
and fifth row).
DELETE FROM Student WHERE Age = 20;

Output:
The above query will delete two rows(third row and fifth row) and the table Student will now look like,

ROLL_NO NAME ADDRESS PHONE Age


1 Ram Delhi XXXXXXXXXX 18
2 RAMESH GURGAON XXXXXXXXXX 18
4 SURESH Delhi XXXXXXXXXX 18
2 RAMESH GURGAON XXXXXXXXXX 18
Delete all of the records: There are two queries to do this as shown below,
query1: "DELETE FROM Student";
query2: "DELETE * FROM Student";

Output:
All of the records in the table will be deleted, there are no records left to display. The table Student will become
empty!
Using DELETE SQL command
When you ask any question in Studytonight's Forum it gets saved into a table. And using the Delete option, you can
even delete a question asked by you. How do you think that works? Yes, using the Delete DML command.

Let's study about the syntax and the usage of the Delete command.

DELETE command
DELETE command is used to delete data from a table.

Following is its general syntax,


DELETE FROM table_name;

Let's take a sample table student:

s_id name age


101 Adam 15
102 Alex 18
103 Abhi 17

Delete all Records from a Table


DELETE FROM student;

The above command will delete all the records from the table student.

Delete a particular Record from a Table

In our student table if we want to delete a single record, we can use the WHERE clause to provide a condition in our
DELETE statement.

DELETE FROM student WHERE s_id=103;

The above command will delete the record where s_id is 103 from the table student.

S_id S_Name age


101 Adam 15
102 Alex 18

Isn't DELETE same as TRUNCATE

TRUNCATE command is different from DELETE command. The delete command will delete all the rows from a table
whereas truncate command not only deletes all the records stored in the table, but it also re-initializes the table(like a
newly created table).

For eg: If you have a table with 10 rows and an auto_increment primary key, and if you use DELETE
command to
delete all the rows, it will delete all the rows, but will not re-initialize the primary key, hence if you will insert any row
after using the DELETE command, the auto_increment primary key will start from 11. But in case of TRUNCATE command,
primary key is re-initialized, and it will again start from 1.

DELETE Operation:

The DELETE command can remove all the rows from the table or a set of rows from the table.

eg: DELETE FROM student; It will DELETE all the rows from student table.

eg: DELETE FROM student WHERE reg_no='A101'; If condition will be satisfied then it will
delete a row from the table Register number A101 will be deleted from the table

If you want to delete table column then use ALTER TABLE command.

eg : ALTER TABLE student DROP COLUMN dob; The DOB column will be deleted from the
student table.
DCL(Data Control Language) : DCL includes commands such as GRANT and REVOKE which mainly deals with
the rights, permissions and other controls of the database system.

Examples of DCL commands:

GRANT-gives user’s access privileges to database.


REVOKE-withdraw user’s access privileges given by using the GRANT command.
Using GRANT and REVOKE
Data Control Language(DCL) is used to control privileges in Database.
To perform any operation in the database, such
as for creating tables, sequences or views, a user needs privileges. Privileges are of two types,

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.

In DCL we have two commands,

GRANT: Used to provide any user access privileges or other priviliges for the database.
REVOKE: Used to take back permissions from any user.

Allow a User to create session

When we create a user in SQL, it is not even allowed to login and create a session until and unless proper
permissions/priviliges are granted to the user.

Following command can be used to grant the session creating priviliges.


GRANT CREATE SESSION TO username;

Allow a User to create table

To allow a user to create tables in the database, we can use the below command,
GRANT CREATE TABLE TO username;

Provide user with space on tablespace to store table

Allowing a user to create table is not enough to start storing data in that table. We also must provide the user with
priviliges to use the available tablespace for their table and data.
ALTER USER username QUOTA UNLIMITED ON SYSTEM;

The above command will alter the user details and will provide it access to unlimited tablespace on system.

NOTE: Generally unlimited quota is provided to Admin users.

Grant all privilege to a User

sysdba is a set of priviliges which has all the permissions in it. So if we want to provide all the privileges to any user,
we can simply grant them the sysdba permission.
GRANT sysdba TO username

Grant permission to create any table


Sometimes user is restricted from creating come tables with names which are reserved for system tables. But we can
grant privileges to a user to create any table using the below command,
GRANT CREATE ANY TABLE TO username

Grant permission to drop any table

As the title suggests, if you want to allow user to drop any table from the database, then grant this privilege to the user,
GRANT DROP ANY TABLE TO username

To take back Permissions

And, if you want to take back the privileges from any user, use the REVOKE command.
REVOKE CREATE TABLE FROM username
GRANTING AND REVOKING PERMISSIONS:

Oracle
provides extensive security features in order to safeguard information stored in its tables from unauthorized viewing and
damage. Depending on a
user's status and responsibility, appropriate rights on Oracle's resources can be assigned to the user by
the DBA. The rights that allow the use of some or all of oracle's resources on the Server are called PRIVILEGES.
Objects
that are created by a user are owned and controlled by that user. If a user wishes to access any of the objects belonging
to another user, the owner of the object will have to give permissions for such access. This is called GRANTING of
PRIVILEGES.  
Privileges once given can be taken back by the owner of the object. This is called REVOKING of PRIVILEGES.

GRANT Statement:

Syntax: 

GRANT <object privileges>


ON <object_name>
TO <User_Name>
[WITH GRANT OPTION] 

eg:

GRANT ALL 
ON Student TO Mohd Imran
WITH GRANT OPTION

The WITH GRANT OPTION allows the grantee to in turn grant object privileges to other users.

eg:

GRANT SELECT, UPDATE 


ON Student TO Fareen
WITH GRANT OPTION

The user Fareen has been given permission to view and modify records in the table Student. 

To view the contents of the student table that belongs to Mohd Imran.

eg:
        SELECT  * FROM Mohd Imran.Student;

Mohd Imran is the owner of the table, and he has given privileges to other user for his table.

Objects Privileges that can be given to users.

Each
object privileges that is granted authorized the grantee to perform some operation on the object. A user can grant all the
privileges or grant only specific object privileges.

The list of object privileges is as follows:

Alter: Allows the grantee to change the table definition with the ALTER TABLE command.

DELETE: Allows the grantee to remove the records from the table with the DELETE command.

INDEX:    Allows the grantee to create an index on the table with the CREATE INDEX command.

INSERT:  Allows the grantee to add records to the table with the INSERT command.

SELECT:  Allows the grantee to query the table with the SELECT command.

UPDATE: Allows the grantee to modify the records in the tables with the UPDATE command. 
REVOKING PRIVILEGES GIVEN:

Privileges
once given can be denied to a user using the REVOKE command. The object
owner can revoke privileges granted to
another user. A user of an object who is not the owner, but has been granted the GRANT privileges, has the power to REVOKE
the privileges from grantee.

Revoking the Permission using the REVOKE command.

Syntax: 
  REVOKE <Object_Privileges>
  ON <Object_Name>
FROM <User_Name>
Eg:
  REVOKE  UPDATE
ON  Student
FROM Fareen;
TCL(transaction Control Language) : TCL commands deals with the transaction within the database.

Examples of TCL commands:

COMMIT– commits a Transaction.


ROLLBACK– rollbacks a transaction in case of any error occurs.
SAVEPOINT–sets a savepoint within a transaction.
SET TRANSACTION–specify characteristics for the transaction.
ORACLE TRANSACTION:

A
series of one or more SQL statements that are logically related, or a series of operation performed on Oracle table data is
termed as a Transaction. Oracle treats changes to table data as a two step process. First the changes requested are done. To
make these changes permanent a COMMIT statement has to be given at the SQL prompt. A ROLLBACK statement
given at the
SQL prompt can be used to undo a part of or the entire Transaction.

A
Transaction begins with the first executable SQL statement after a Commit, Rollback or Connection made to the Oracle
engine. All changes made to an Oracle table data via a transaction are made or undo at one instance.

Specially, a Transaction is a group of events that occurs between any of the following events:

Connecting to Oracle
Disconnecting from Oracle
Committing changes to the table
Rollback

CLOSING TRANSACTION:

A
Transaction can be closed by using either a Commit or a Rollback statement. By using these statement, table data can be
changed or all the changes made to the table data undo.

Using COMMIT:

A COMMIT
ends the current transaction and makes permanent any changes made during the transaction. All transaction locks
acquired on tables are released.

Syntax:
   COMMIT;

Using ROLLBACK:

A ROLLBACK does
exactly the opposite of COMMIT. It ends the transaction but undoes any changes made during the
transaction. All transaction locks acquired on tables are released.

Syntax:
 ROLLBACK  [WORK] [TO SAVEPOINT]  < Save-point_Name>

WORK:                                 It is optional and is provided for ANSI compatibility


SAVEPOINT :                 It is optional and it is used to rollback a partial transaction, as far as the specified savepoint.
SAVEPOINTNAME:  It is a savepoint created during the current transaction   

Crating a SAVEPOINT:

SAVEPOINT marks
and saves the current point in the processing of a transaction. When a SAVEPOINT is used with a
ROLLBACK statement, parts of a transaction can be undone. An active savepoint is one that is specified since the last COMMIT
or ROLLBACK.

Syntax: 
 SAVEPOINT  <SavePointName>

Example:

DECLARE
       Total_Sal  number(9);
BEGIN
  INSERT INTO Emp  VALUES('E101', 'Aamir', 10, 7000');
  INSERT INTO Emp  VALUES('E102', 'Aatif', 11, 6500');
SAVEPOINT   no_update;

UPDATE Emp SET salary =salary+2000 WHERE Emp_Name = 'Aamir';


UPDATE Emp SET salary =salary+2000 WHERE Emp_Name = 'Aatif';

SELECT sum(Salary) INTO Total_sal  FROM Emp;

IF  Total_Sal > 15000  THEN


       ROLLBACK  To  SAVEPOINT   no_update;
 END IF;

COMMIT;
END;

The bove PL/SQL block, it will insert two records in the table Emp, then after no_update SavePoint has been declared.
on both of record the updated with 2000 respectively, now calculated all the salary in Emp Table,
If Salary has been reached more
than 15000 in its table, then it is automatically rolled back, it means undo that was updated earlier by 2000. If this type situation is
coming then you can use these commands.
SQL | TRANSACTIONS
What are Transactions?

Transactions group a set of tasks into a single execution unit. Each transaction begins with a specific task and ends when
all the tasks in the group successfully complete. If any of the tasks fail, the transaction fails. Therefore, a transaction has
only two results: success or failure.

Incomplete steps result in the failure of the transaction. A database


transaction, by definition, must be atomic,
consistent, isolated and durable. These are popularly known as
ACID properties.

How to implement Transactions using SQL?

Following commands are used to control transactions.It is imortant to


note that these statements cannot be used while
creating tables and are
only used with the DML Commands such as – INSERT, UPDATE and DELETE.

1. SET TRANSACTION: Places a name on a transaction.


Syntax:
SET TRANSACTION [ READ WRITE | READ ONLY ];

2. COMMIT: If everything is in order with all statements within a single transaction, all changes are recorded
together in the database is called committed. The COMMIT command saves all the transactions to the database
since the last COMMIT or ROLLBACK command.
Syntax:
COMMIT;

Example: Sample table 1

Following is an example which would delete those records from the table which have age = 20 and then
COMMIT the changes in the database.
Queries:
DELETE FROM Student WHERE AGE = 20;
COMMIT;

Output:
Thus, two rows from the table would be deleted and the SELECT statement would look like,

3. ROLLBACK: If any error occurs with any of the SQL grouped statements, all changes need to be aborted. The
process of reversing changes is called rollback. This command can only be used to undo transactions since the
last COMMIT or ROLLBACK command was issued.
Syntax:
ROLLBACK;

Example:
From the above example Sample table1,
Delete those records from the table which have age = 20 and then ROLLBACK the changes in the database.
Queries:
DELETE FROM Student WHERE AGE = 20;
ROLLBACK;

Output:

4. SAVEPOINT: creates points within the groups of transactions in which to ROLLBACK.


A SAVEPOINT is a point in a transaction in which you can roll the transaction back to a certain point without
rolling back the entire transaction.
Syntax for Savepoint command:
SAVEPOINT SAVEPOINT_NAME;

This command is used only in the creation of SAVEPOINT among all the transactions.
In general ROLLBACK is used to undo a group of transactions.
Syntax for rolling back to Savepoint command:
ROLLBACK TO SAVEPOINT_NAME;

you can ROLLBACK to any SAVEPOINT at any time to return the appropriate data to its original state.
Example:
From the above example Sample table1,
Delete those records from the table which have age = 20 and then ROLLBACK the changes in the database by
keeping Savepoints.
Queries:
SAVEPOINT SP1;
//Savepoint created.
DELETE FROM Student WHERE AGE = 20;
//deleted
SAVEPOINT SP2;
//Savepoint created.

Here SP1 is first SAVEPOINT created before deletion.In this example one deletion have taken place.
After deletion again SAVEPOINT SP2 is created.
Output:

Deletion have been taken place, let us assume that you have changed your mind and decided to ROLLBACK to
the SAVEPOINT that you identified as SP1 which is before deletion.
deletion is undone by this statement ,
ROLLBACK TO SP1;
//Rollback completed.

Notice that first deletion took place even though you rolled back to SP1 which is first SAVEPOINT.

5. RELEASE SAVEPOINT:- This command is used to remove a SAVEPOINT that you have created.
Syntax:

RELEASE SAVEPOINT SAVEPOINT_NAME

Once a SAVEPOINT has been released, you can no longer use the ROLLBACK command to undo transactions
performed since the last SAVEPOINT.
It is used to initiate a database transaction and used to specify characteristics of the transaction that follows.

SQL QUIZ
Transactions Quiz
Commit, Rollback and Savepoint SQL commands
Transaction Control Language(TCL) commands are used to manage transactions in the database. These are used to
manage the changes made to the data in a table by DML statements. It also allows statements to be grouped together
into logical transactions.

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.

Using Savepoint and Rollback

Following is the table class,

id name
1 Abhi
2 Adam
4 Alex

Lets use some SQL queries on the above table and see the results.
INSERT INTO class VALUES(5, 'Rahul');
COMMIT;
UPDATE class SET name = 'Abhijit' WHERE id = '5';
SAVEPOINT A;
INSERT INTO class VALUES(6, 'Chris');
SAVEPOINT B;
INSERT INTO class VALUES(7, 'Bravo');
SAVEPOINT C;
SELECT * FROM class;

NOTE: SELECT statement is used to show the data stored in the table.

The resultant table will look like,

id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
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;
SELECT * FROM class;

Now our class table will look like,

id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
6 Chris

Now let's again use the ROLLBACK command to roll back the state of data to the savepoint A
ROLLBACK TO A;
SELECT * FROM class;

Now the table will look like,


id name
1 Abhi
2 Adam
4 Alex
5 Abhijit

So now you know how the commands COMMIT, ROLLBACK and SAVEPOINT works.
SELECT SQL Query
SELECT query is used to retrieve data from a table. It is the most used SQL query. We can retrieve complete table data,
or partial by specifying conditions using the WHERE clause.

Syntax of SELECT query

SELECT query is used to retieve records from a table. We can specify the names of the columns which we want in the
resultset.
SELECT
column_name1,
column_name2,
column_name3,
...
column_nameN
FROM table_name;

Time for an Example

Consider the following student table,

s_id name age address


101 Adam 15 Chennai
102 Alex 18 Delhi
103 Abhi 17 Banglore
104 Ankit 22 Mumbai
SELECT s_id, name, age FROM student;

The above query will fetch information of s_id, name and age columns of the student table and display them,

s_id name age


101 Adam 15
102 Alex 18
103 Abhi 17
104 Ankit 22

As you can see the data from address column is absent, because we did not specif it in our SELECT query.

Select all records from a table

A special character asterisk * is used to address all the data(belonging to all columns) in a query. SELECT statement uses
* character to retrieve all records from a table, for all the columns.

SELECT * FROM student;

The above query will show all the records of student table, that means it will show complete dataset of the table.

s_id name age address


101 Adam 15 Chennai
102 Alex 18 Delhi
103 Abhi 17 Banglore
104 Ankit 22 Mumbai

Select a particular record based on a condition

We can use the WHERE clause to set a condition,


SELECT * FROM student WHERE name = 'Abhi';

The above query will return the following result,

103 Abhi 17 Rohtak

Performing Simple Calculations using SELECT Query

Consider the following employee table.

eid name age salary


101 Adam 26 5000
102 Ricky 42 8000
103 Abhi 25 10000
104 Rohan 22 5000

Here is our SELECT query,


SELECT eid, name, salary+3000 FROM employee;

The above command will display a new column in the result, with 3000 added into existing salaries of the employees.

eid name salary+3000


101 Adam 8000
102 Ricky 11000
103 Abhi 13000
104 Rohan 8000

So you can also perform simple mathematical operations on the data too using the SELECT query to fetch data from
table.
Using the WHERE SQL clause
WHERE clause is used to specify/apply any condition while retrieving, updating or deleting data from a table. This clause
is
used mostly with SELECT, UPDATE and DELETEquery.

When we specify a condition using the WHERE clause then the query executes only for those records for which the
condition specified by the WHERE clause is true.

Syntax for WHERE clause

Here is how you can use the WHERE clause with a DELETE statement, or any other statement,
DELETE FROM table_name WHERE [condition];

The WHERE clause is used at the end of any SQL query, to specify a condition for execution.

Time for an Example

Consider a table student,

s_id name age address


101 Adam 15 Chennai
102 Alex 18 Delhi
103 Abhi 17 Banglore
104 Ankit 22 Mumbai

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.

s_id name age address


101 Adam 15 Noida

Applying condition on Text Fields

In the above example we have applied a condition to an integer value field, but what if we want to apply the condition
on name field. In that case we must enclose the value in single quote ' '. Some databases even accept double quotes,
but single quotes is accepted by all.
SELECT s_id,
name,
age,
address
FROM student WHERE name = 'Adam';

Following will be the result of the above query.

s_id name age address


101 Adam 15 Noida

Operators for WHERE clause condition


Following is a list of operators that can be used while specifying the WHERE clause condition.

Operator Description
= Equal to
!= Not Equal to
< Less than
> Greater than
<= Less than or Equal to
>= Greate than or Equal to
BETWEEN Between a specified range of values
LIKE This is used to search for a pattern in value.
IN In a given set of values
SQL LIKE clause
LIKE clause is used in the condition in SQL query with the WHERE clause. LIKE clause compares data with an expression
using wildcard operators to match pattern given in the condition.

Wildcard operators

There are two wildcard operators that are used in LIKE clause.

Percent sign %: represents zero, one or more than one character.


Underscore sign _: represents only a single character.

Example of LIKE clause

Consider the following Student table.

s_id s_Name age


101 Adam 15
102 Alex 18
103 Abhi 17
SELECT * FROM Student WHERE s_name LIKE 'A%';

The above query will return all records where s_name starts with character 'A'.

s_id s_Name age


101 Adam 15
102 Alex 18
103 Abhi 17

Using _ and %
SELECT * FROM Student WHERE s_name LIKE '_d%';

The above query will return all records from Student table where s_name contain 'd' as second character.

s_id s_Name age


101 Adam 15

Using % only
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.

s_id s_Name age


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 sorts the retrieved data in ascending order. To sort the data in descending order DESC keyword is used with Order
by clause.

Syntax of Order By
SELECT column-list|* FROM table-name ORDER BY ASC | DESC;

Using default Order by

Consider the following Emp table,

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000
SELECT * FROM Emp ORDER BY salary;

The above query will return the resultant data in ascending order of the salary.

eid name age salary


403 Rohan 34 6000
402 Shane 29 8000
405 Tiger 35 8000
401 Anu 22 9000
404 Scott 44 10000
Using Order by DESC

Consider the Emp table described above,


SELECT * FROM Emp ORDER BY salary DESC;

The above query will return the resultant data in descending order of the salary.

eid name age salary


404 Scott 44 10000
401 Anu 22 9000
405 Tiger 35 8000
402 Shane 29 8000
403 Rohan 34 6000
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.

Syntax for using Group by in a statement.


SELECT column_name, function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name

Example of Group by in a Statement

Consider the following Emp table.

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 9000
405 Tiger 35 8000

Here we want to find name and age of employees grouped by their salaries
or in other words, we will be grouping
employees based on their salaries, hence, as a result, we will get a data set, with unique salaries listed, along side the
first employee's name and age to have that salary. Hope you are getting the point here!

group by is used to group different row of data together based on any one column.

SQL query for the above requirement will be,


SELECT name, age
FROM Emp GROUP BY salary

Result will be,

name age
Rohan 34
Shane 29
Anu 22
Example of Group by in a Statement with WHERE clause

Consider the following Emp table

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 9000
405 Tiger 35 8000

SQL query will be,


SELECT name, salary
FROM Emp
WHERE age > 25
GROUP BY salary

Result will be.

name salary
Rohan 6000
Shane 8000
Scott 9000

You must remember that Group By clause will always come at the end of the SQL query, 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 by based SQL queries, just like WHERE clause is used with SELECT query.

Syntax for HAVING clause is,


SELECT column_name, function(column_name)
FROM table_name
WHERE column_name condition
GROUP BY column_name
HAVING function(column_name) condition

Example of SQL Statement using HAVING

Consider the following Sale table.

oid order_name previous_balance customer


11 ord1 2000 Alex
12 ord2 1000 Adam
13 ord3 2000 Abhi
14 ord4 1000 Adam
15 ord5 2000 Alex

Suppose we want to find the customer whose previous_balance sum is more than 3000.

We will use the below SQL query,


SELECT *
FROM sale GROUP BY customer
HAVING sum(previous_balance) > 3000

Result will be,

oid order_name previous_balance customer


11 ord1 2000 Alex
The main objective of the above SQL query was to find out the name of the customer who has had a previous_balance
more than 3000, based on all the previous sales made to the customer, hence we get the first row in the table for
customer Alex.
DISTINCT keyword
The distinct keyword is used with SELECT statement to retrieve unique values from the table. Distinct removes all
the duplicate records while retrieving records from any table in the database.

Syntax for DISTINCT Keyword


SELECT DISTINCT column-name FROM table-name;

Example using DISTINCT Keyword

Consider the following Emp table. As you can see in the table below, there is employee name, along with employee
salary and age.

In the table below, multiple employees have the same salary, so we will be using DISTINCT keyword to list down
distinct salary amount, that is currently being paid to the employees.

eid name age salary


401 Anu 22 5000
402 Shane 29 8000
403 Rohan 34 10000
404 Scott 44 10000
405 Tiger 35 8000
SELECT DISTINCT salary FROM Emp;

The above query will return only the unique salary from Emp table.

salary
5000
8000
10000
AND & OR operator
The AND and OR operators are used with the WHERE clause to make more precise conditions for fetching data from
database by combining more than one condition together.

AND operator
AND operator is used to set multiple conditions with the WHERE clause, alongside, SELECT, UPDATE or DELETE SQL
queries.

Example of AND operator

Consider the following Emp table

eid name age salary


401 Anu 22 5000
402 Shane 29 8000
403 Rohan 34 12000
404 Scott 44 10000
405 Tiger 35 9000
SELECT * FROM Emp WHERE salary < 10000 AND age > 25

The above query will return records where salary is less than 10000 and age greater than 25. Hope you get the concept
here. We have used the AND operator to specify two conditions with WHERE clause.

eid name age salary


402 Shane 29 8000
405 Tiger 35 9000

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 specified conditions will be
there in the result.

But in case of OR operator, atleast one condition from the conditions specified must be satisfied by any record to be in
the resultset.

Example of OR operator

Consider the following Emp table

eid name age salary


401 Anu 22 5000
402 Shane 29 8000
403 Rohan 34 12000
404 Scott 44 10000
405 Tiger 35 9000
SELECT * FROM Emp WHERE salary > 10000 OR age > 25

The above query will return records where either salary is greater than 10000 or age is greater than 25.

402 Shane 29 8000


403 Rohan 34 12000
404 Scott 44 10000
405 Tiger 35 9000
Division Operator in SQL
The division operator is used when we have to evaluate queries which contain the keyword ALL.

Some instances where division operator is used are:

1. Which person has account in all the banks of a particular city?


2. Which students have taken all the courses required to graduate?

In above specified problem statements, the description after the keyword 'all' defines a set which contains some
elements and the final result contains those units which satisfy these requirements.

Another way how you can identify the usage of division operator is by using the logical implication of if...then. In
context of the above two examples, we can see that the queries mean that,

1. If there is a bank in that particular city, that person must have an account in that bank.
2. If there is a course in the list of courses required to be graduated, that person must have taken that course.

Do not worry if you are not clear with all this new things right away, we will try to expain as we move on with this
tutorial.

We shall see the second example, mentioned above, in detail.

Table 1: Course_Taken → It consists of the names of Students against the courses that they have taken.

Student_Name Course
Robert Databases
Robert Programming Languages
David Databases
David Operating Systems
Hannah Programming Languages
Hannah Machine Learning
Tom Operating Systems

Table 2: Course_Required → It consists of the courses that one is required to take in order to graduate.

Course
Databases
Programming Languages

Using Division Operator


So now, let's try to find out the correct SQL query for getting results for the first requirement, which is:

Query: Find all the students who can graduate. (i.e. who have taken all the subjects required for one to
graduate.)

Unfortunately, there is no direct way by which we can express the division operator. Let's walk through the steps, to
write the query for the division operator.

1. Find all the students

Create a set of all students that have taken courses. This can be done easily using the following command.
CREATE TABLE AllStudents AS SELECT DISTINCT Student_Name FROM Course_Taken

This command will return the table AllStudents, as the resultset:

Student_name
Robert
David
Hannah
Tom

2. Find all the students and the courses required to graduate

Next, we will create a set of students and the courses they need to graduate. We can express this in the form of Cartesian
Product of AllStudents and Course_Required using the following command.
CREATE table StudentsAndRequired AS
SELECT AllStudents.Student_Name, Course_Required.Course
FROM AllStudents, Course_Required

Now the new resultset - table StudentsAndRequired will be:

Student_Name Course
Robert Databases
Robert Programming Languages
David Databases
David Programming Languages
Hannah Databases
Hannah Programming Languages
Tom Databases
Tom Programming Languages

3. Find all the students and the required courses they have not taken

Here, we are taking our first step for finding the students who cannot graduate. The idea is to simply find the students
who have not taken certain courses that are required for graduation and hence they wont be able to graduate. This is
simply all those tuples/rows which are
present in StudentsAndRequired and not present in Course_Taken.
CREATE table StudentsAndNotTaken AS
SELECT * FROM StudentsAndRequired WHERE NOT EXISTS
(Select * FROM Course_Taken WHERE StudentsAndRequired.Student_Name = Course_Taken.Student_Name
AND StudentsAndRequired.Course = Course_Taken.Course)

The table StudentsAndNotTaken comes out to be:

Student_Name Course
David Programming Languages
Hannah Databases
Tom Databases
Tom Programming Languages

4. Find all students who cannot graduate

All the students who are present in the table StudentsAndNotTaken are the ones who cannot graduate. Therefore, we
can find the students who cannot graduate as,
CREATE table CannotGraduate AS SELECT DISTINCT Student_Name FROM StudentsAndNotTaken

Student_name
David
Hannah
Tom

5. Find all students who can graduate

The students who can graduate are simply those who are present in AllStudents but not in CannotGraduate. This can
be done by the following query:
CREATE Table CanGraduate AS SELECT * FROM AllStudents
WHERE NOT EXISTS
(SELECT * FROM CannotGraduate WHERE
CannotGraduate.Student_name = AllStudents.Student_name)

The results will be as follows:

Student_name
Robert

Hence we just learned, how different steps can lead us to the final answer. Now let us see how to write all these 5 steps
in one single query so that we do not have to create so many tables.
SELECT DISTINCT x.Student_Name FROM Course_Taken AS x WHERE NOT
EXISTS(SELECT * FROM Course_Required AS y WHERE NOT
EXISTS(SELECT * FROM Course_Taken AS z
WHERE z.Student_name = x.Student_name
AND z.Course = y.Course ))

Student_name
Robert

This gives us the same result just like the 5 steps above.
Advance SQL

SQL Constraints
SQL function
SQL Join
SQL Alias
SQL SET operation
SQL Sequences
SQL Views
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 the following two types,

1. Column level constraints: Limits only column data.


2. Table level constraints: Limits whole table data.

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

NOT NULL Constraint


NOT NULL constraint restricts a column from having a NULL value. Once NOT NULL constraint is applied to a
column, you cannot pass a null value to that column. It enforces a column to contain a proper value.

One important point to note about this constraint is that it cannot be defined at table level.

Example using NOT NULL constraint


CREATE TABLE Student(s_id int NOT NULL, Name varchar(60), Age int);

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. This constraint can be applied at column level or table level.

Using UNIQUE constraint when creating a Table (Table Level)

Here we have a simple CREATE query to create a table, which will have a column s_id with unique values.
CREATE TABLE Student(s_id int NOT NULL UNIQUE, Name varchar(60), Age int);

The above query will declare that the s_id field of Student table will only have unique values and wont take NULL
value.
Using UNIQUE constraint after Table is created (Column Level)
ALTER TABLE Student ADD UNIQUE(s_id);

The above query specifies that s_id field of Student table will only have unique value.

Primary Key Constraint


Primary key constraint uniquely identifies each record in a database.
A Primary Key must contain unique value and it
must not contain null value. Usually Primary Key is used to index the data inside the table.

Using PRIMARY KEY constraint at Table Level


CREATE table Student (s_id int PRIMARY KEY, Name varchar(60) NOT NULL, Age int);

The above command will creates a PRIMARY KEY on the s_id.

Using PRIMARY KEY constraint at Column Level


ALTER table Student ADD PRIMARY KEY (s_id);

The above command will creates a PRIMARY KEY on the s_id.

Foreign Key Constraint


FOREIGN KEY is used to relate two tables. FOREIGN KEY constraint is also used to restrict actions that would
destroy links between tables. To understand FOREIGN KEY, let's see its use, with help of the below tables:

Customer_Detail Table

c_id Customer_Name address


101 Adam Noida
102 Alex Delhi
103 Stuart Rohtak

Order_Detail Table

Order_id Order_Name c_id


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.

If you try to insert any incorrect data, DBMS will return error and will not allow you to insert the data.

Using FOREIGN KEY constraint at Table Level


CREATE table Order_Detail(
order_id int PRIMARY KEY,
order_name varchar(60) NOT NULL,
c_id int FOREIGN KEY REFERENCES Customer_Detail(c_id)
);

In this query, c_id in table Order_Detail is made as foriegn key, which is a reference of c_id column in
Customer_Detail table.

Using FOREIGN KEY constraint at Column Level


ALTER table Order_Detail ADD FOREIGN KEY (c_id) REFERENCES Customer_Detail(c_id);

Behaviour of Foriegn Key Column on Delete

There are two ways to maintin the integrity of data in Child table, when a particular record is deleted in the main table.
When two tables are connected with Foriegn key, and certain data in the main table is deleted, for which a record exits
in the child table, then we must have some mechanism to save the integrity of data in the child table.

1. On Delete Cascade : This will remove the record from child table, if that value of foriegn key is deleted from the
main table.
2. 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.
3. 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.
ERROR : Record in child table exist

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.

Using CHECK constraint at Table Level


CREATE table Student(
s_id int NOT NULL CHECK(s_id > 0),
Name varchar(60) NOT NULL,
Age int
);

The above query will restrict the s_id value to be greater than zero.

Using CHECK constraint at Column Level


ALTER table Student ADD CHECK(s_id > 0);
Data Constraint :

Oracle allows constraints
for attaching  in the table columns via SQL syntax that checks data for
integrity. If data
constraints has attached to table column construct, the oracle engine checks the data which is being enter into the
table column against the data constraint. If data passes this check, then simply it will be stored into the table
otherwise it will be rejected.

The
constraints is a rule which works into the table, it disallows the that
data which are not following the rule of data
constraints.

For example : Suppose, you have table there may have more than one column, among them there is one phone_no
column and you want, it should be unique then you will have to attach a Unique Key Constraint. disallows
the duplicity, Oracle engine will check the data for uniqueness, which is going to be enter if its a unique among them
which has already stored
into the table, then it will be accepted otherwise it will be rejected.

Types of Data Constraints:

There are two types of Data Constraints that can be applied to data being inserted into the table.
First one is the Input/Output Constraint. This data constraint determines the speed at which data can be inserted
or extracted from the table.
Second one is the Business Rule Constraint 

Input/Output Constraints

1. The Primary key Constraint


2. The Foreign key Constraint

Business Rule Constraints

1. Check Constraint
2. Unique Constraint
3. NOT NULL Constraint
Primary key Concept :

A
primary key is one or more column(s) in a table used to uniquely identify each row in the table. A primary key
column in a table has special attributes : 

It defines the column, as a mandatory column i.e. the column can not be left blank. The NOT NULL attribute is
active.
The data held across the column must be UNIQUE.

In simple words we can say a primary key is the combination of (NOT NULL+UNIQUE) key constraints.
A single column primary key is called Simple key. And a multicolumn primary key is called a Composite primary key.

PRIMARY KEY constraint defined at the column level : 

Syntax :
<col_name> <datatype>(<size>) PRIMARY KEY

Example :
CREATE TABLE Sales_Order           
                   (Order_no  varchar2(6)PRIMARY KEY, 
                     Order_Date  date,  Client_no  varchar2(6),
                     Dely_addr  varchar2(25),  Order_status  varchar2(10));

PRIMARY KEY Constraint


defined at the table level:

Syntax:  
 Primary key(<col_name,<col_name>,)

 Example:  

CREATE TABLE sales_order_details           

                   (Detlorder_no  varchar2(6),Product_no  varchar2(7)
                    product_rate   number(8,2),
                    primary
key(Detlorder_no,Product_no));

Here I mentioned composite primary key that mapped to the


columns Detlorder_no and product_no.
Foreign
key concepts:

Foreign
keys represents the relationships between tables. A foreign key is a column(or
group of columns)
whose
values are derived from the primary or unique key of the some other
tables.

The table in which the foreign key is defined is called a


foreign table or detail table. The table that defines the
primary key or unique
key and is referenced by the foreign key is called primary table or master
table.

Syntax: 

<col_name>
<datatype>(<size>)REFERENCES <table_name>[(>col_name>)]

Example: 

CREATE TABLE sales_order_details

(detlorder_no varchar2(60) REFERENCES Sales_order, product_no


varchar2(60), product_rate number(8,2));

The references key word points to the table sales_order. The


table sales_order has the column order_no as its
primary key column.

Foreign key constraint defined at the table level:

Syntax:

Foreign key(<col_name>[,<col_name>])References <Table_name>[(<col_name>,<col_name>)]

Example:

Create table sales_order_details

(Detlorder_no
varchar2(6), product_no varchar2(6), product_rate number(8,2) ,foreign
key(detlorder_no) references sales_order.order_no);

Principal of Foreign Key/References

Rejects an INSERT or UPDATE of a value, if a corresponding value does not exist currently in the master key
table or you can say Primary key table from where foreign key has brought out.

If
ON DELETE CASCADE option is set, a DELETE operation in the master table
will trigger a delete operation for
corresponding records in all detail
table.

Rejects
a DELETE for the master table if corresponding records in the DETAIL table exist or you can say if value
exist in foreign table and you want to DELETE it from Primary key table then it will rejected, First you have to
DELETE from FOREIGN KEY TABLE then PRIMARY KEY table.Foreign key
table also called Detail Table.

It must be matching Data Type for both Primary key and Foreign key attribute.
The CHECK constraint:

Business rule validation can be applied to a table column bu


using CHECK constraint .CHECK constraint must be
specified as a logical expression
that evaluates either to TRUE or FALSE

Syntax: 

<col_name>
<data_Type>(<size>)CHECK (<logical expression>)

Example :

CREATE TABLE Client_Master(client_no varchar2(6) CHECK (client_no


like’c%’),

 Name varchar2(20) CHECK (name=upper(name)),

city varchar2(15) CHECK (city IN(‘Allahabad’,’Lucknow’)));

Data values being inserted into the column_ no must  be started with capital letter C
Date values being inserted with column_name will be in upper
case
Only allowed calcuttta, yanam values for the city column
Unique key Concept

The
Unique key disallows the similar value in same column of a table, the being entered value should be different
from already stored value into the table.
A value entered in column(s) defined in the unique constraint must not be repeated across the column(s).
A table may have many unique keys.

Unique Constraint defined at the column level :

Syntax :
<col_name> <data_type>(<size>)UNIQUE

Below
is the example of Unique key which is containing information for the students, there phone number is the
unique it will not be repeated across the table.

Example
CREATE TABLE Student
(Reg_no varchar2(10) PRIMARY KEY,
Name varchar2(30),
Phone number(10)UNIQUE,
Address varchar2(50));

Unique Constraint Defined at the table level :

Syntax
CREATE TABLE Table_Name
(<col_detail_1>, <col_detail_2>,......<col_detail_n>,
UNIQUE(<Column_1>, <Column_2>......<Column_n>));

Example:
CREATE TABLE Student
(Reg_no varchar2(10) PRIMARY KEY,
Name varchar2(30),
Phone number(10),
Email varchar2(30),Address varchar2(50),
UNIQUE(Phone, Email));
NULL Value Concept

A
NULL value is different from a blank or a zero. NULL values are treated
specially by Oracle. A NULL value can be
inserted into the columns of any data type.

Principal of NULL Values

Setting a NULL value is appropriate when the actual value is unknown, or when a value would not be
meaningful.
A
NULL is not equivalent to a value of zero if the data type is the number and is not equivalent to spaces if the
data type is character.
A NULL value will evaluate to NULL in any expression for example NULL multiplied by 10 is NULL.
NULL value can be inserted into columns of any data type.
If the column has a NULL value, Oracle ignores any UNIQUE, FOREIGN KEY, and CHECK constraints that may
attached to the column.

NOT NULL Constraint

 When
a column is defined as NOT NULL , then that column becomes a mandatory column. it must be enter a value
into that column. If it will blank or left then oracle engine will not execute the query for further process.

Syntax :
<column_name>, <data_type><(size)> NOT NULL

Example
CREATE TABLE Student
(Reg_no varchar2(10) PRIMARY KEY,
Name varchar2(30)NOT NULL,
Phone number(10)UNIQUE,
Address varchar2(50)NOT NULL
      DOB date);

Note
: The NOT NULL constraint can only be applied at column level. Although
NOT NULL can be applied as a CHECK
constraint, Oracle crop recommends that this not done.
Defining Integrity Constraints

Integrity constraints can be defined using the constraint clause, in the ALTER TABLE command.

If
a primary key constraint was being applied to a table and the column has duplicate value in it already, the primary
key constraint will not be set to that column.

Add a PRIMARY KEY data constraint on the column Reg_no belonging to the table Student.

ALTER TABLE Student 


 ADD Primary key(Reg_no);

Add FOREIGN KEY constraint on the column Stu_Reg belonging to the table Course, which reference the table
Student.

ALTER TABLE Course


      ADD CONSTRAINT Reg_fkey
      FOREIGN KEY (Stu_Reg) REFERENCES Student;

The Course table already exist without foreign key, now by this command it is added in Course table.

Dropping Integrity Constraints

Integrity Constraints can be dropped if the rule that it enforces is no longer true or
if the constraint is no longer
needed. Drop the constraint using the ALTER TABLE command with the DROP Clause. See the following example.

ALTER TABLE Student 


    DROP Primary key;

Drop FOREIGN KEY Constraint on column Stu_Reg in table Course.

ALTER TABLE Course


DROP CONSTRAINT Reg_fk;

The Reg_fk is
the name foreign key, you will have to specify foreign key name to drop
the foreign key, because in a
table more than one Foreign can be there.

If you forgot the Foreign key name then you can use this command to know the FOREIGN KEY Name.

SELECT table_name, Constraint_type, Constraint_name FROM user_constraints;


Computation in the Table
PATTERN MATCHING:

The LIKE predicates
allows for a comparison of one string value with aother string value, which is not identical. This is
achieved by using wildcard characters. Two wildcard characters that are available are:

For character datatypes:

The percent sign (%) matches any string.


The underscore (_) matches any single character.

SELECT * FROM Student 
  WHERE  First_name LIKE 'm%';

This statement will fetch all the records whose first_name begin with letters 'm' from STUDENT table.

You can use OR/AND operation also WHERE clause how, see below.

SELECT * FROM Student 
  WHERE  First_name LIKE 'm%' OR First_name LIKE ' f ';

SELECT * FROM Student 
  WHERE  First_name LIKE 'm%' AND Last_name LIKE ' i ';

 SELECT * FROM Student 
        WHERE  First_name LIKE  '___d';

This will show records that is 4 letters word and end with d letter. you can further with some other conditions. So lets try it.

The IN and NOT IN Predicates:

 SELECT name, dob FROM student WHERE name IN (Mohd, Imran, Fareen, Aamir, Aatif );

Data will be retrieved if name exist in that 5 names and NOT IN is the opposite of IN statement.

SELECT name, dob FROM student WHERE name NOT IN (Mohd, Imran, Fareen, Aamir, Aatif );

RANGE SEARCHING:

In
order to select data that is within a range of values, the BETWEEN operator is used. The BETWEEN operator allows the
selection of rows that
contain values within a specified lower and upper limit. The range coded after the word BETWEEN is
inclusive.

SELECT name, address FROM student WHERE marks BETWEEN 75 AND 100; 

The name and address will be fetched whose marks are between 75 and 100;

The NOT Operator:

       The Oracle engine will process all rows in a table and display the result only when none of the conditions specified using
the NOT operator are specified.

SELECT name, dob, address,city FROM student WHERE NOT (City ='Pondicherry' or City = 'Chennai');

which student's city is pondicherry or chennai that will not be fetched. 


What are 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 categories,

1. Aggregate Functions
2. Scalar Functions

Aggregate Functions
These functions return a single value after performing calculations on a group of values. Following are some of the
frequently used Aggregrate functions.

AVG() Function

Average returns average value after calculating it from values in a numeric column.

Its general syntax is,


SELECT AVG(column_name) FROM table_name

Using AVG() function

Consider the following Emp table

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000

SQL query to find average salary will be,


SELECT avg(salary) from Emp;

Result of the above query will be,

avg(salary)
8200

COUNT() Function

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

Using COUNT() function

Consider the following Emp table

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000

SQL query to count employees, satisfying specified condition is,


SELECT COUNT(name) FROM Emp WHERE salary = 8000;

Result of the above query will be,

count(name)
2

Example of COUNT(distinct)

Consider the following Emp table

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000

SQL query is,


SELECT COUNT(DISTINCT salary) FROM emp;

Result of the above query will be,

count(distinct salary)
4
FIRST() Function

First function returns first value of a selected column

Syntax for FIRST function is,


SELECT FIRST(column_name) FROM table-name;

Using FIRST() function

Consider the following Emp table

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000

SQL query will be,


SELECT FIRST(salary) FROM Emp;

and the result will be,

first(salary)
9000

LAST() Function

LAST function returns the return last value of the selected column.

Syntax of LAST function is,


SELECT LAST(column_name) FROM table-name;

Using LAST() function

Consider the following Emp table

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000

SQL query will be,


SELECT LAST(salary) FROM emp;

Result of the above query will be,

last(salary)
8000

MAX() Function

MAX function returns maximum value from selected column of the table.

Syntax of MAX function is,


SELECT MAX(column_name) from table-name;

Using MAX() function

Consider the following Emp table

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000

SQL query to find the Maximum salary will be,


SELECT MAX(salary) FROM emp;

Result of the above query will be,

MAX(salary)
10000

MIN() Function

MIN function returns minimum value from a selected column of the table.

Syntax for MIN function is,


SELECT MIN(column_name) from table-name;
Using MIN() function

Consider the following Emp table,

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000

SQL query to find minimum salary is,


SELECT MIN(salary) FROM emp;

Result will be,

MIN(salary)
6000

SUM() Function

SUM function returns total sum of a selected columns numeric values.

Syntax for SUM is,


SELECT SUM(column_name) from table-name;

Using SUM() function

Consider the following Emp table

eid name age salary


401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000

SQL query to find sum of salaries will be,


SELECT SUM(salary) FROM emp;

Result of above query is,

SUM(salary)
41000
Scalar Functions
Scalar functions return a single value from an input value. Following are some frequently used Scalar Functions in SQL.

UCASE() Function

UCASE function is used to convert value of string column to Uppercase characters.

Syntax of UCASE,
SELECT UCASE(column_name) from table-name;

Using UCASE() function

Consider the following Emp table

eid name age salary


401 anu 22 9000
402 shane 29 8000
403 rohan 34 6000
404 scott 44 10000
405 Tiger 35 8000

SQL query for using UCASE is,


SELECT UCASE(name) FROM emp;

Result is,

UCASE(name)
ANU
SHANE
ROHAN
SCOTT
TIGER

LCASE() Function

LCASE function is used to convert value of string columns to Lowecase characters.

Syntax for LCASE is,


SELECT LCASE(column_name) FROM table-name;

Using LCASE() function

Consider the following Emp table


eid name age salary
401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 SCOTT 44 10000
405 Tiger 35 8000

SQL query for converting string value to Lower case is,


SELECT LCASE(name) FROM emp;

Result will be,

LCASE(name)
anu
shane
rohan
scott
tiger

MID() Function

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;

Using MID() function

Consider the following Emp table

eid name age salary


401 anu 22 9000
402 shane 29 8000
403 rohan 34 6000
404 scott 44 10000
405 Tiger 35 8000

SQL query will be,


SELECT MID(name,2,2) FROM emp;

Result will come out to be,

MID(name,2,2)
nu
ha
oh
co
ig

ROUND() Function

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;

Using ROUND() function

Consider the following Emp table

eid name age salary


401 anu 22 9000.67
402 shane 29 8000.98
403 rohan 34 6000.45
404 scott 44 10000
405 Tiger 35 8000.01

SQL query is,


SELECT ROUND(salary) from emp;

Result will be,

ROUND(salary)
9001
8001
6000
10000
8000
SQL JOIN
SQL Join is used to fetch data from two or more tables, which is joined to appear as single set of data. It 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, which is known as, Self Join.

Types of JOIN
Following are the types of JOIN that we can use in SQL:

Inner
Outer
Left
Right

Cross JOIN or Cartesian Product


This type of JOIN returns the cartesian product of rows from the tables in Join. It will return a table which consists of
records which combines each row from the first table with each row of the second table.

Cross JOIN Syntax is,


SELECT column-name-list
FROM
table-name1 CROSS JOIN table-name2;

Example of Cross JOIN

Following is the class table,

ID NAME
1 abhi
2 adam
4 alex

and the class_info table,

ID Address
1 DELHI
2 MUMBAI
3 CHENNAI

Cross JOIN query will be,


SELECT * FROM
class CROSS JOIN class_info;
The resultset table will look like,

ID NAME ID Address
1 abhi 1 DELHI
2 adam 1 DELHI
4 alex 1 DELHI
1 abhi 2 MUMBAI
2 adam 2 MUMBAI
4 alex 2 MUMBAI
1 abhi 3 CHENNAI
2 adam 3 CHENNAI
4 alex 3 CHENNAI

As you can see, this join returns the cross product of all the records present in both the tables.

INNER Join or EQUI Join


This is a simple JOIN in which the result is based on matched data as per the equality condition specified in the SQL
query.

Inner Join Syntax is,


SELECT column-name-list FROM
table-name1 INNER JOIN table-name2
WHERE table-name1.column-name = table-name2.column-name;

Example of INNER JOIN

Consider a class table,

ID NAME
1 abhi
2 adam
3 alex
4 anu

and the class_info table,

ID Address
1 DELHI
2 MUMBAI
3 CHENNAI

Inner JOIN query will be,


SELECT * from class INNER JOIN class_info where class.id = class_info.id;

The resultset table will look like,

ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI

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.

The syntax for Natural Join is,


SELECT * FROM
table-name1 NATURAL JOIN table-name2;

Example of Natural JOIN

Here is the class table,

ID NAME
1 abhi
2 adam
3 alex
4 anu

and the class_info table,

ID Address
1 DELHI
2 MUMBAI
3 CHENNAI

Natural join query will be,


SELECT * from class NATURAL JOIN class_info;

The resultset table will look like,

ID NAME Address
1 abhi DELHI
2 adam MUMBAI
3 alex CHENNAI

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,

1. Left Outer Join


2. Right Outer Join
3. Full Outer Join

LEFT Outer Join

The left outer join returns a resultset table with the matched data from the two tables and then the remaining rows of
the left table and null from the right table's columns.

Syntax for Left Outer Join is,


SELECT column-name-list FROM
table-name1 LEFT OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column-name;

To specify a condition, we use the ON keyword with Outer Join.

Left outer Join Syntax for Oracle is,


SELECT column-name-list FROM
table-name1, table-name2 on table-name1.column-name = table-name2.column-name(+);

Example of Left Outer Join

Here is the class table,

ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
and the class_info table,

ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT

Left Outer Join query will be,


SELECT * FROM class LEFT OUTER JOIN class_info ON (class.id = class_info.id);

The resultset table will look like,

ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
4 anu null null
5 ashish null null

RIGHT Outer Join

The right outer join returns a resultset table with the matched data from the two tables being joined, then the remaining
rows of the right table and null for the remaining left table's columns.

Syntax for Right Outer Join is,


SELECT column-name-list FROM
table-name1 RIGHT OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column-name;

Right outer Join Syntax for Oracle is,


SELECT column-name-list FROM
table-name1, table-name2
ON table-name1.column-name(+) = table-name2.column-name;

Example of Right Outer Join

Once again the class table,

ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish

and the class_info table,


ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT

Right Outer Join query will be,


SELECT * FROM class RIGHT OUTER JOIN class_info ON (class.id = class_info.id);

The resultant table will look like,

ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
null null 7 NOIDA
null null 8 PANIPAT

Full Outer Join

The full outer join returns a resultset table with the matched data of two table then remaining rows of both left table
and then the right table.

Syntax of Full Outer Join is,


SELECT column-name-list FROM
table-name1 FULL OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column-name;

Example of Full outer join is,

The class table,

ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish

and the class_info table,

ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT

Full Outer Join query will be like,


SELECT * FROM class FULL OUTER JOIN class_info ON (class.id = class_info.id);

The resultset table will look like,

ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
4 anu null null
5 ashish null null
null null 7 NOIDA
null null 8 PANIPAT
SQL Alias - AS Keyword
Alias is used to give an alias name to a table or a column, which can be a resultset table too. This is quite useful in case
of large or complex queries. Alias is mainly used for giving a short alias name for a column or a table with complex
names.

Syntax of Alias for table names,


SELECT column-name FROM table-name AS alias-name

Following is an SQL query using alias,


SELECT * FROM Employee_detail AS ed;

Syntax for defining alias for columns will be like,


SELECT column-name AS alias-name FROM table-name;

Example using alias for columns,


SELECT customer_id AS cid FROM Emp;

Example of Alias in SQL Query


Consider the following two tables,

The class table,

ID Name
1 abhi
2 adam
3 alex
4 anu
5 ashish

and the class_info table,

ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT

Below is the Query to fetch data from both the tables using SQL Alias,
SELECT C.id, C.Name, Ci.Address from Class AS C, Class_info AS Ci where C.id = Ci.id;

and the resultset table will look like,

ID Name Address
1 abhi DELHI
2 adam MUMBAI
3 alex CHENNAI

SQL Alias seems to be quite a simple feature of SQL, but it is highly


useful when you are working with more than 3
tables and have to use JOIN on them.
SET Operations in SQL
SQL supports few Set operations which can be performed on the table data. These are used to get meaningful results
from data stored in the table, under different special conditions.

In this tutorial, we will cover 4 different types of SET operations, along with example:

1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS

UNION Operation
UNION is used to combine the results of two or more SELECT
statements. However it will eliminate duplicate rows from
its resultset. In case of union, number of columns and datatype must be same
in both the tables, on which UNION
operation is being applied.

Example of UNION

The First table,

ID Name
1 abhi
2 adam

The Second table,

ID Name
2 adam
3 Chester

Union SQL query will be,


SELECT * FROM First
UNION
SELECT * FROM Second;

The resultset table will look like,

ID NAME
1 abhi
2 adam
3 Chester

UNION ALL
This operation is similar to Union. But it also shows the duplicate rows.

Example of Union All

The First table,

ID NAME
1 abhi
2 adam

The Second table,

ID NAME
2 adam
3 Chester

Union All query will be like,


SELECT * FROM First
UNION ALL
SELECT * FROM Second;

The resultset table will look like,

ID NAME
1 abhi
2 adam
2 adam
3 Chester

INTERSECT
Intersect operation is used to combine two SELECT statements, but it only retuns the records which are common from
both SELECT statements. In case of Intersect the number of columns and datatype must be same.

NOTE: MySQL does not support INTERSECT operator.

Example of Intersect

The First table,

ID NAME
1 abhi
2 adam

The Second table,

ID NAME
2 adam
3 Chester

Intersect query will be,


SELECT * FROM First
INTERSECT
SELECT * FROM Second;

The resultset table will look like

ID NAME
2 adam

MINUS
The Minus operation combines results of two SELECT statements and return only those in the final result, which belongs
to the first set of the result.

Example of Minus

The First table,

ID NAME
1 abhi
2 adam

The Second table,

ID NAME
2 adam
3 Chester

Minus query will be,


SELECT * FROM First
MINUS
SELECT * FROM Second;
The resultset table will look like,

ID NAME
1 abhi
What is an SQL Sequence?
Sequence is a feature supported by some database systems to produce unique values on demand. Some DBMS like
MySQL supports AUTO_INCREMENT in place of Sequence.

AUTO_INCREMENT is applied on columns, it automatically increments the column value by 1 each time a new record is
inserted into the table.

Sequence is also some what similar to AUTO_INCREMENT but it has some additional features too.

Creating a Sequence
Syntax to create a sequence is,
CREATE SEQUENCE sequence-name
START WITH initial-value
INCREMENT BY increment-value
MAXVALUE maximum-value
CYCLE | NOCYCLE;

The initial-value specifies the starting value for the Sequence.


The increment-value is the value by which sequence will be incremented.
The maximum-value specifies the upper limit or the maximum value upto which sequence will increment itself.
The keyword CYCLE specifies that if the maximum value exceeds the set limit, sequence will restart its cycle from
the begining.
And, NO CYCLE specifies that if sequence exceeds MAXVALUE value, an error will be thrown.

Using Sequence in SQL Query

Let's start by creating a sequence, which will start from 1, increment by 1 with a maximum value of 999.
CREATE SEQUENCE seq_1
START WITH 1
INCREMENT BY 1
MAXVALUE 999
CYCLE;
Now let's use the sequence that we just created above.

Below we have a class table,

ID NAME
1 abhi
2 adam
4 alex

The SQL query will be,


INSERT INTO class VALUE(seq_1.nextval, 'anu');

Resultset table will look like,

ID NAME
1 abhi
2 adam
4 alex
1 anu

Once you use nextval the sequence will increment even if you don't Insert any record into the table.
SQL VIEW
A VIEW in SQL is a logical subset of data from one or more tables. View is used to restrict data access.

Syntax for creating a View,


CREATE or REPLACE VIEW view_name
AS
SELECT column_name(s)
FROM table_name
WHERE condition

As you may have understood by seeing the above SQL query, a view is created using data fetched from some other
table(s). It's more like a temporary table created with data.

Creating a VIEW
Consider following Sale table,

oid order_name previous_balance customer


11 ord1 2000 Alex
12 ord2 1000 Adam
13 ord3 2000 Abhi
14 ord4 1000 Adam
15 ord5 2000 Alex

SQL Query to Create a View from the above table will be,
CREATE or REPLACE VIEW sale_view
AS
SELECT * FROM Sale WHERE customer = 'Alex';

The data fetched from SELECT statement will be stored in another object called sale_view. We can use CREATE and
REPLACE
seperately too, but using both together works better, as if any view with the specified name exists, this query
will replace it with fresh data.

Displaying a VIEW
The syntax for displaying the data in a view is similar to fetching data from a table using a SELECT statement.
SELECT * FROM sale_view;

Force VIEW Creation


FORCE keyword is used while creating a view, forcefully.
This keyword is used to create a View even if the table does
not exist.
After creating a force View if we create the base table and enter values in it, the view will be automatically
updated.

Syntax for forced View is,


CREATE or REPLACE FORCE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition;

Update a VIEW
UPDATE command for view is same as for tables.

Syntax to Update a View is,


UPDATE view-name SET VALUE
WHERE condition;

NOTE: If we update a view it also updates base table data automatically.

Read-Only VIEW
We can create a view with read-only option to restrict access to the view.

Syntax to create a view with Read-Only Access


CREATE or REPLACE FORCE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition WITH read-only;

The above syntax will create view for read-only purpose, we cannot Update or Insert data into read-only view. It will
throw an error.

Types of View
There are two types of view,

Simple View
Complex View
Simple View Complex View
Created from one table Created from one or more table
Does not contain functions Contain functions
Does not contain groups of data Contains groups of data

You might also like