Vous êtes sur la page 1sur 62

Unit I

Components of Database System

The database system can be divided into four components.

● Users :​ Users may be of various type such as DB administrator, System developer


and End users.
● Database application :​ Database application may be Personal, Departmental,
Enterprise and Internal
● DBMS :​ Software that allow users to define, create and manages database access,
Ex: MySql, Oracle etc.
● Database :​ Collection of logical data.

Functions of DBMS
● Provides data Independence
● Concurrency Control
● Provides Recovery services
● Provides Utility services
● Provides a clear and logical view of the process that manipulates data.

Advantages of DBMS
● Segregation of applicaion program.
● Minimal data duplicacy.
● Easy retrieval of data.
● Reduced development time and maintainance need.

Disadvantages of DBMS
● Complexity
● Costly
● Large in size

Comparison Chart

BASIS FOR
DATA INFORMATION
COMPARISON

Meaning Data means raw facts Facts, concerning a particular


gathered about someone event or subject, which are
or something, which is refined by processing is called
bare and random. information.

What is it? It is just text and It is refined data.


numbers.

Based on Records and Analysis


Observations
Form Unorganized Organized

Useful May or may not be Always


useful.

Specific No Yes

Dependency Does not depend on Without data, information


information. cannot be processed.

DEFINITION OF DBMS

DBMS is software which is used to manage the collection of interrelated data.

File systems Vs DBMS:

The typical file processing system is supported by the operating systems. Files
are created and manipulated by writing programs so the permanent records are
stored in various files. Before the advent of DBMS, organizations typically stored
the information using such systems.

Ex: Using COBOL we can maintain several files (collection of records) to access
those files we have to go through the application programs which have written for
creating files, updating file, inserting the records
The problems in file processing system are

Data redundancy and consistency

Difficulty in accessing data


Data isolation
Integrity problems
Atomicity problems
Security problems

To solve the above problems DBMS has been invented.

View of data:

The man purpose of DBMS is to provide users with an abstract view of the data.

The data abstraction is in three levels.

Physical level: How the data are actually stored that means what data structures are
used to store data on Hard disk

Ex: Sequential , Tree structured

Logical Level : What data are stored in database

View level : It is the part of data base


Ex: Required records in table.

Instance:

The collection of information stored in the database at a particular moment is called an


instance of the data base.

Data base schema:

Logical schema
Physical schema
 
Data independence:

The ability to modify schema definition in one level with affecting a schema definition in
the next higher level is called data independence.

Physical independence

Logical independence

Data models:

Underlying
the structure
of a data base
is the data
model. The
collection
of conceptual
tools for describing
data, data

relationships, data semantics.

Three types of data models are there:


Object based logical model:

These are used to describe the data at logical level, view level. It is divided

into several types. Entity relationship model

Object oriented model


The semantic data model
The function data model

Record based logical model :

In contrast to object based model they are used both to specify the overall logical
structure of data base and to pride a higher level description of the
implementation.

Relation model
Network model
Hierarchical model

Database languages:

Database system provides two types of languages

Data definition language :

Database schema is specified by a set of definitions expressed by a special


language called DDL.

Data manipulation language:

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

1) 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 following fields.
S_id S_Name age
INSERT into Student values(101,'Adam',15);
The above command will insert a record into ​Student​ table.
S_id S_Name age

101 Adam 15

Example to 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 value other column is set to null.
S_id S_Name age

101 Adam 15

102 Alex

Example to 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 ​age​ column of student table has 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')

2) UPDATE command
Update command is used to update a row of a table. Following is its general syntax,
UPDATE​ ​table-name​ set column-name = value ​where​ ​condition​;
Lets see an example,
update Student set age=18 where s_id=102;
S_id S_Name age

101 Adam 15

102 Alex 18

103 chris 14

Example to Update multiple columns


UPDATE Student set s_name='Abhi',age=17 where s_id=103;
The above command will update two columns of a record.
S_id S_Name age

101 Adam 15

102 Alex 18

103 Abhi 17

3) Delete command
Delete command is used to delete data from a table. Delete command can also be used
with condition to delete a particular row. Following is its general syntax,
DELETE​ from ​table-name;​
Example to Delete all Records from a Table
DELETE from Student;
The above command will delete all the records from ​Student​ table.

Example to Delete a particular Record from a Table


Consider the following ​Student​ table
S_id S_Name age

101 Adam 15

102 Alex 18

103 Abhi 17
DELETE from Student where s_id=103;
The above command will delete the record where s_id is 103 from ​Student​ table.
S_id S_Name age

101 Adam 15

102 Alex 18

TCL command
Transaction Control Language(TCL) commands are used to manage transactions in
database.These are used to manage the changes made by DML statements. It also
allows statements to be grouped together into logical transactions.

Commit command
Commit command is used to permanently save any transaaction into database.
Following is Commit command's syntax,
commit​;
Rollback command
This command restores the database to last commited state. It is also use with
savepoint command to jump to a savepoint in a transaction.
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 necessary.
Following is savepoint command's syntax,
savepoint​ ​savepoint-name;​

Example of Savepoint and Rollback


Following is the ​class​ table,
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;
The resultant table will look like,
ID NAME

1 abhi

2 adam

4 alex

5 abhijit

6 chris

7 bravo
Now ​rollback​ to ​savepoint B
rollback to B;
SELECT * from class;
The resultant table will look like
ID NAME

1 abhi

2 adam

4 alex

5 abhijit

6 chris
Now ​rollback​ to ​savepoint A
rollback to A;
SELECT * from class;
The result table will look like
ID NAME

1 abhi

2 adam

4 alex

5 abhijit

DCL command
Data Control Language(DCL) is used to control privilege in Database. To perform any
operation in the database, such as for creating tables, sequences or views we need
privileges. Privileges are of two types,

● System :​ creating session, table etc are all types of system privilege.

● Object :​ any command or query to work on tables comes under object privilege.
DCL defines two commands,

● Grant :​ Gives user access privileges to database.


● Revoke :​ Take back permissions from user.

To Allow a User to create Session


grant​ create session to ​username;​

To Allow a User to create Table


grant​ create table to ​username;​

To provide User with some Space on Tablespace to store Table


alter​ user ​username​ quota unlimited on system;

To Grant all privilege to a User


grant​ sysdba to ​username

To Grant permission to Create any Table


grant​ ​create​ any table to ​username

To Grant permission to Drop any Table


grant​ ​drop​ any table to ​username

To take back Permissions


revoke​ create table from ​username

WHERE clause
Where clause is used to specify condition while retriving data from table. ​Where​ clause
is used mostly with ​Select,​ ​Update​ and ​Delete​ query. If condititon specified
by ​where​ clause is true then only the result from table is returned.
Syntax for WHERE clause
SELECT​ column-name1,
column-name2,
column-name3,
column-nameN
from table-name ​WHERE [condition]​;

Example using WHERE clause


Consider a ​Student​ table,
s_id s_Name age address

101 Adam 15 Noida

102 Alex 18 Delhi

103 Abhi 17 Rohtak

104 Ankit 22 Panipat


Now we will use a SELECT statement to display data of the table, based on a condition,
which we will add to the SELECT query using WHERE clause.
SELECT s_id,
s_name,
age,
address
from Student ​WHERE​ s_id=101;
s_id s_Name age address

101 Adam 15 Noida

SELECT Query
Select query is used to retrieve data from a tables. It is the most used SQL query. We
can retrieve complete tables, or partial by mentioning conditions using WHERE clause.

Syntax of SELECT Query


SELECT​ column-name1, column-name2, column-name3, column-nameN from
table-name;​
Example for SELECT Query
Conside the following ​Student​ table,
S_id S_Name age address

101 Adam 15 Noida

102 Alex 18 Delhi

103 Abhi 17 Rohtak

104 Ankit 22 Panipat


SELECT s_id, s_name, age from Student.
The above query will fetch information of s_id, s_name and age column from Student
table
S_id S_Name age

101 Adam 15

102 Alex 18

103 Abhi 17

104 Ankit 22

Example to Select all Records from 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.
SELECT * from student;
The above query will show all the records of Student table, that means it will show
complete Student table as result.
S_id S_Name age address

101 Adam 15 Noida

102 Alex 18 Delhi

103 Abhi 17 Rohtak


104 Ankit 22 Panipat

Example to Select particular Record based on Condition


SELECT * from Student ​WHERE​ s_name = 'Abhi';
103 Abhi 17 Rohtak

Example to Perform Simple Calculations using Select Query


Conside the following ​Employee​ table.
eid Name age salary

101 Adam 26 5000

102 Ricky 42 8000

103 Abhi 22 10000

104 Rohan 35 5000


SELECT eid, name, salary+3000 from Employee;
The above command will display a new column in the result, showing 3000 added into
existing salaries of the employees.
eid Name salary+3000

101 Adam 8000

102 Ricky 11000

103 Abhi 13000

104 Rohan 8000

The functions of DBA​:

Schema definition
Access methods definition
Schema and physical organization modification
Granting authorization for data
Integrity constraint specification

Structure
of a
DBMS

 Figure 1.3
shows the
structure
of a typical
DBM
Database Design:

The database design process can be divided into six steps.

The ER model is most relevant to the first three steps.

Requirements Analysis :

The very first step in designing a database application is to understand what data is
to be stored in the database, what applications must be built on top of it, and what
operations are most frequent and subject to performance requirements. In other
words, we must find out what the users want from the database.

Conceptual Database Design :


The information gathered in the requirements analysis step is used to develop to
high level description of the data to the stored in the database, along with the
constraints that are known to hold over this data. This step is often carried out using
the ER model, or a similar high level data model.

Logical Database Design:

We must choose a DBMS to implement our database design, and convert the
conceptual database design into a database schema in the data model of the
chosen DBMS.
Schema Refinement :

The fourth step in database design is to analyse the collection of relations in our
relational database schema to identify potential problems, and to refine it. In
contrast to the requirements analysis and conceptual design steps, which are
essentially subjective, schema refinement can be guided by some elegant and
powerful theory.

E-R
Diagram

ER-Diagram is a visual representation of data that describes how data is related to each
other.
Symbols and Notations
Components of E-R Diagram
The E-R diagram has three main components.
1) Entity
An ​Entity​ can be any object, place, person or class. In E-R 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 from an
Organisation.

Weak Entity
Weak entity is an entity that depends on another entity. Weak entity doen't have key
attribute of their own. Double rectangle represents weak entity.
2) 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.

Key Attribute
Key attribute represents the main characterstic of an Entity. It is used to represent
Primary key. Ellipse with underlying lines represent Key Attribute.

Composite Attribute
An attribute can also have their own attributes. These attributes are known
as ​Composite​ attribute.
3) Relationship
A Relationship describes relations between ​entities​. Relationship is represented using
diamonds.

There are three types of relationship that exist between Entities.


● Binary Relationship
● Recursive Relationship
● Ternary Relationship

Binary Relationship
Binary Relationship means relation between two Entities. This is further divided into
three types.
1. One to One :​ 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
relationship.
2. One to Many :​ It reflects business rule that one entity is associated with many
number of same entity. The example for this relation might sound a little weird, but
this menas that one student can enroll to many courses, but one course will have
one Student.

The arrows in the diagram describes that one student can enroll for only one course.
3. Many to One :​ 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.

4. Many to Many :

The above diagram represents that many students can enroll for more than one
courses.
Recursive Relationship
When an Entity is related with itself it is known as ​Recursive​ Relationship.

Ternary Relationship
Relationship of degree three is called Ternary relationship.
SQL Views
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

Example of 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 View


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 seperately and replace too but using both together works better.
Example of Displaying a View
Syntax of displaying a view is similar to fetching data from table using Select statement.
SELECT * from ​sale_view​;

unit ii

Introduction to the Relational Model

The main
construct
for
representing data in the relational model is a relation. A relation consists of a relation
schema and a relation instance. The relation instance is a table, and the relation
schema describes the column heads for the table. We first describe the relation schema
and then the relation instance. The schema specifies the relation’s name, the name of
each field (or column, or attribute), and the domain of each field. A domain is referred to
in a relation schema by the domain name and has a set of associated values.

Eg:

Students(sid: string, name: string, login: string, age: integer, gpa: real)

This says, for instance, that the field named sid has a domain named string. The set of
values associated with domain string is the set of all character strings.

An instance of a relation is a set of tuples, also called records, in which each tuple
has the same number of fields as the relation schema. A relation instance can be
thought of as a table in which each tuple is a row, and all rows have the same number
of fields.

RDBMS Concepts
A ​Relational Database management System​(RDBMS) is a database management
system based on relational model introduced by E.F Codd. In relational model, data is
represented in terms of tuples(rows).
RDBMS​ is used to manage Relational database. ​Relational database​ is a collection of
organized set of tables from which data can be accessed easily. Relational Database is
most commonly used database. It consists of number of tables and each table has its
own primary key.

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

1 Adam 34 13000

2 Alex 28 15000

3 Stuart 20 18000

4 Ross 42 19020

What is a Record ?
A single entry in a table is called a ​Record​ or ​Row​. A ​Record​ in a table represents set
of related data. For example, the above ​Employee​ table has 4 records. Following is an
example of single record.
1 Adam 34 13000

What is Field ?
A table consists of several records(row), each record can be broken into several smaller
entities known as ​Fields​. The above ​Employee​ table consist of four
fields, ​ID​, ​Name​, ​Age​ and ​Salary​.

What is a Column ?
In ​Relational​ table, a column is a set of value of a particular type. The term ​Attribute​ is
also used to represent a column. For example, in Employee table, Name is a column
that represent names of employee.
Name

Adam

Alex

Stuart

Ross
Database Keys
Keys are very important part of Relational database. They are used to establish and
identify relation between tables. They also ensure that each record within a table can be
uniquely identified by combination of one or more fields within a table.

Super Key
Super Key​ is defined as a set of attributes within a table that uniquely identifies each
record within a table. Super Key is a superset of Candidate key.

Candidate Key
Candidate keys are defined as the set of fields from which primary key can be selected.
It is an attribute or set of attribute that can act as a primary key for a table to uniquely
identify each record in that table.

Primary Key
Primary key is a candidate key that is most appropriate to become main key of the
table. It is a key that uniquely identify each record in a table.

Composite Key
Key that consist of two or more attributes that uniquely identify an entity occurance is
called ​Composite key​. But any attribute that makes up the ​Composite key​ is not a
simple key in its own.
Integrity Constraints Over Relations

An integrity constraint (IC) is a condition that is specified on a database schema,


and restricts the data that can bstored in an instance of the database. If a database
instance satisfies all the integrity constraints specified on the database schema, it is a
legal instance. A DBMS enforces integrity constraints, in that it permits only legal
instances to be stored in the database.

Key Constraints

Consider the Students relation and the constraint that no two students have the same
student id. This IC is an example of a key constraint. A key constraint is a statement
that a certain minimal subset of the fields of a relation is a unique identifier for a tuple. A
set of fields that uniquely identifies a tuple according to a key constraint is called a
candidate key for the relation; we often abbreviate this to just key.In the case of the
Students relation, the (set of fields containing just the) sid field is a candidate key.

 There are two parts to the definition of (candidate) key:


Two distinct tuples in a legal instance (an instance that satisfies all ICs, including the
key constraint) cannot have identical values in all the fields of a key.

No subset of the set of fields in a key is a unique identifier for a tuple.

The first part of the definition means that in any legal instance, the values in the key
fields uniquely identify a tuple in the instance

The second part of the definition means, for example, that the set of fields {sid, name} is
not a key for Students, because this set properly contains the key {sid}.Theset {sid,
name} is an example of a ​superkey​, which is a set of fields that contains a key.

Out of all the


available
candidate
keys, a database
designer can
identify a ​primary
key. Intuitively,
a tuple can be
referred to from
elsewhere
in the database
by storing the
values of its
primary key fields.
For example,
we can refer to a
Students
tuple by storing its
sid value.

Specifying Key Constraints in SQL

CREATE TABLE Students ( sid CHAR(20), name CHAR(30), login CHAR(20), age
INTEGER, gpa REAL, UNIQUE (name, age), CONSTRAINT StudentsKey PRIMARY
KEY (sid) )
This definition says that sid is the primary key and that the combination of name and
age is also a key. The definition of the primary key also illustrates how we can name a
constraint by preceding it with CONSTRAINT constraint-name. If the constraint is
violated, the constraint name is returned and can be used to identify the error.

Foreign Key Constraints

Sometimes the information stored in a relation is linked to the information stored in


another relation. If one of the relations is modified, the other must be checked, and
perhaps modified, to keep the data consistent. An IC involving both relations must be
specified if a DBMS is to make such checks. The most common IC involving two
relations is a foreign key constraint.

A foreign key is a key used to link two tables together. This is sometimes
also called as a referencing key.
A Foreign Key is a column or a combination of columns whose values match
a Primary Key in a different table.
The relationship between 2 tables matches the Primary Key in one
of the tables with a Foreign Key in the second table.
If a table has a primary key defined on any field(s), then you cannot have
two records having the same value of that field(s).

Example
Consider the structure of the following two tables.
CUSTOMERS table

CREATE TABLE CUSTOMERS​(

ID INT NOT NULL​,


NAME VARCHAR ​(​20​)​ NOT NULL​,

AGE INT NOT NULL​,

ADDRESS CHAR ​(​25​)​ ​,

SALARY DECIMAL ​(​18​,​ ​2)


​ ,

PRIMARY KEY ​(​ID​)

);

ORDERS table

CREATE TABLE ORDERS ​(

ID INT NOT NULL​,

DATE DATETIME​,

CUSTOMER_ID INT references CUSTOMERS​(​ID​),

AMOUNT ​double​,

PRIMARY KEY ​(​ID​)

);

If the ORDERS table has already been created and the foreign key has not
yet been set, the use the syntax for specifying a foreign key by altering a
table.

ALTER TABLE ORDERS


ADD FOREIGN KEY (Customer_ID) REFERENCES CUSTOMERS (ID);
The SQL ​LIKE​ clause is used to compare a value to similar values using
wildcard operators. There are two wildcards used in conjunction with the
LIKE operator.
● The percent sign (%)

● The underscore (_)


The percent sign represents zero, one or multiple characters. The
underscore represents a single number or character. These symbols can be
used in combinations.

Syntax
The basic syntax of % and _ is as follows −
SELECT FROM table_name
WHERE column LIKE 'XXXX%'

or

SELECT FROM table_name


WHERE column LIKE '%XXXX%'

or

SELECT FROM table_name


WHERE column LIKE 'XXXX_'

or

SELECT FROM table_name


WHERE column LIKE '_XXXX'

or

SELECT FROM table_name


WHERE column LIKE '_XXXX_'
You can combine N number of conditions using AND or OR operators. Here,
XXXX could be any numeric or string value.

Example
The following table has a few examples showing the WHERE part having
different LIKE clause with '%' and '_' operators −

Sr.No. Statement & Description

1 WHERE SALARY LIKE '200%'


Finds any values that start with 200.

WHERE SALARY LIKE '%200%'


2
Finds any values that have 200 in any position.

WHERE SALARY LIKE '_00%'


3
Finds any values that have 00 in the second and third positions.

WHERE SALARY LIKE '2_%_%'


4
Finds any values that start with 2 and are at least 3 characters in length.

WHERE SALARY LIKE '%2'


5
Finds any values that end with 2.

WHERE SALARY LIKE '_2%3'


6
Finds any values that have a 2 in the second position and end with a 3.
WHERE SALARY LIKE '2___3'
7
Finds any values in a five-digit number that start with 2 and end with 3.

Let us take a real example, consider the CUSTOMERS table having the
records as shown below.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example, which would display all the records from the
CUSTOMERS table, where the SALARY starts with 200.

SQL​>​ SELECT ​*​ FROM CUSTOMERS

WHERE SALARY LIKE ​'200%'​;

This would produce the following result −


+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
+----+----------+-----+-----------+----------+
The ​SQL ​ORDER BY​ clause is used to sort the data in ascending or
descending order, based on one or more columns. Some databases sort the
query results in an ascending order by default.

Syntax
The basic syntax of the ORDER BY clause is as follows −
SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
You can use more than one column in the ORDER BY clause. Make sure
whatever column you are using to sort that column should be in the
column-list.

Example
Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
The following code block has an example, which would sort the result in an
ascending order by the NAME and the SALARY −

SQL​>​ SELECT ​*​ FROM CUSTOMERS

ORDER BY NAME​,​ SALARY​;

This would produce the following result −


+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+----+----------+-----+-----------+----------+
The following code block has an example, which would sort the result in the
descending order by NAME.

SQL​>​ SELECT ​*​ FROM CUSTOMERS

ORDER BY NAME DESC​;

This would produce the following result −


+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
+----+----------+-----+-----------+----------+
The SQL ​GROUP BY​ clause is used in collaboration with the SELECT
statement to arrange identical data into groups. This GROUP BY clause
follows the WHERE clause in a SELECT statement and precedes the ORDER
BY clause.

Syntax
The basic syntax of a GROUP BY clause is shown in the following code block.
The GROUP BY clause must follow the conditions in the WHERE clause and
must precede the ORDER BY clause if one is used.
SELECT column1, column2
FROM table_name
WHERE [ conditions ]
GROUP BY column1, column2
ORDER BY column1, column2

Example
Consider the CUSTOMERS table is having the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
If you want to know the total amount of the salary on each customer, then
the GROUP BY query would be as follows.

SQL​>​ SELECT NAME​,​ SUM​(​SALARY​)​ FROM CUSTOMERS

GROUP BY NAME​;

This would produce the following result −


+----------+-------------+
| NAME | SUM(SALARY) |
+----------+-------------+
| Chaitali | 6500.00 |
| Hardik | 8500.00 |
| kaushik | 2000.00 |
| Khilan | 1500.00 |
| Komal | 4500.00 |
| Muffy | 10000.00 |
| Ramesh | 2000.00 |
+----------+-------------+
Now, let us look at a table where the CUSTOMERS table has the following
records with duplicate names −

+----+----------+-----+-----------+----------+

|​ ID ​|​ NAME ​|​ AGE ​|​ ADDRESS ​|​ SALARY ​|

+----+----------+-----+-----------+----------+

|​ ​1​ ​|​ ​Ramesh​ ​|​ ​32​ ​|​ ​Ahmedabad​ |


​ ​ ​2000.00​ ​|

|​ ​2​ ​|​ ​Ramesh​ ​|​ ​25​ ​|​ ​Delhi​ ​ ​


| ​1500.00​ ​|

|​ ​3​ ​|​ kaushik ​|​ ​23​ ​|​ ​Kota​ ​|​ ​2000.00​ ​|

|​ ​4​ ​|​ kaushik ​|​ ​25​ ​|​ ​Mumbai​ ​|​ ​6500.00​ ​|

|​ ​5​ ​|​ ​Hardik​ ​|​ ​27​ ​|​ ​Bhopal​ ​|​ ​8500.00​ ​|

|​ ​6​ ​|​ ​Komal​ ​|​ ​22​ ​|​ MP ​|​ ​4500.00​ ​|

|​ ​7​ ​|​ ​Muffy​ ​|​ ​24​ ​|​ ​Indore​ ​|​ ​10000.00​ ​|

+----+----------+-----+-----------+----------+

Now again, if you want to know the total amount of salary on each
customer, then the GROUP BY query would be as follows −

SQL​>​ SELECT NAME​,​ SUM​(​SALARY​)​ FROM CUSTOMERS

GROUP BY NAME​;
This would produce the following result −
+---------+-------------+
| NAME | SUM(SALARY) |
+---------+-------------+
| Hardik | 8500.00 |
| kaushik | 8500.00 |
| Komal | 4500.00 |
| Muffy | 10000.00 |
| Ramesh | 3500.00 |
+---------+-------------+

The SQL ​DISTINCT​ keyword is used in conjunction with the SELECT


statement to eliminate all the duplicate records and fetching only unique
records.
There may be a situation when you have multiple duplicate records in a
table. While fetching such records, it makes more sense to fetch only those
unique records instead of fetching duplicate records.

Syntax
The basic syntax of DISTINCT keyword to eliminate the duplicate records is
as follows −
SELECT DISTINCT column1, column2,.....columnN
FROM table_name
WHERE [condition]

Example
Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
First, let us see how the following SELECT query returns the duplicate salary
records.

SQL​>​ SELECT SALARY FROM CUSTOMERS

ORDER BY SALARY​;

This would produce the following result, where the salary (2000) is coming
twice which is a duplicate record from the original table.
+----------+
| SALARY |
+----------+
| 1500.00 |
| 2000.00 |
| 2000.00 |
| 4500.00 |
| 6500.00 |
| 8500.00 |
| 10000.00 |
+----------+
Now, let us use the DISTINCT keyword with the above SELECT query and
then see the result.

SQL​>​ SELECT DISTINCT SALARY FROM CUSTOMERS

ORDER BY SALARY​;

This would produce the following result where we do not have any duplicate
entry.
+----------+
| SALARY |
+----------+
| 1500.00 |
| 2000.00 |
| 4500.00 |
| 6500.00 |
| 8500.00 |
| 10000.00 |
+----------+
Relational Algebra
Relational algebra is a procedural query language, which takes instances of
relations as input and yields instances of relations as output. It uses
operators to perform queries. An operator can be either ​unary​ or ​binary​.
They accept relations as their input and yield relations as their output.
Relational algebra is performed recursively on a relation and intermediate
results are also considered relations.
The fundamental operations of relational algebra are as follows −
● Select

● Project

● Union

● Set different

● Cartesian product

● Rename
We will discuss all these operations in the following sections.
Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation​ − σ​p(r)

Where ​σ​ stands for selection predicate and ​r​ stands for relation. ​p​ is
prepositional logic formula which may use connectors like ​and, or,​ and ​not​.
These terms may use relational operators like − =, ≠, ≥, < , >, ≤.
For example​ −

σ​subject = "database"(Books)

Output​ − Selects tuples from books where subject is 'database'.


σ​subject = "database" and price = "450"​(Books)
Output​ − Selects tuples from books where subject is 'database' and 'price'
is 450.

σ​subject = "database" and price = "450" or year > "2010"​(Books)


Output​ − Selects tuples from books where subject is 'database' and 'price'
is 450 or those books published after 2010.
Project Operation (∏)
It projects column(s) that satisfy a given predicate.
Notation − ∏​A1, A2, An​ (r)
Where A​1​, A​2​ , A​n​ are attribute names of relation ​r​.
Duplicate rows are automatically eliminated, as relation is a set.
For example​ −

∏​subject, author​ (Books)


Selects and projects columns named as subject and author from the relation
Books.
Union Operation (​∪​)
It performs binary union between two given relations and is defined as −

r ​∪​ s = { t | t ​∈​ r or t ​∈​ s}


Notation​ − r U s
Where ​r​ and ​s​ are either database relations or relation result set
(temporary relation).
For a union operation to be valid, the following conditions must hold −
● r​, and ​s​ must have the same number of attributes.

● Attribute domains must be compatible.

● Duplicate tuples are automatically eliminated.

∏ ​author​ (Books) ​∪​ ∏ ​author​ (Articles)


Output​ − Projects the names of the authors who have either written a book
or an article or both.
Set Difference (−)
The result of set difference operation is tuples, which are present in one
relation but are not in the second relation.
Notation​ − ​r​ − ​s
Finds all the tuples that are present in ​r​ but not in ​s​.

∏ ​author​ (Books) − ∏ ​author​ (Articles)


Output​ − Provides the name of authors who have written books but not
articles.
Cartesian Product (Χ)
Combines information of two different relations into one.
Notation​ − r Χ s
Where ​r​ and ​s​ are relations and their output will be defined as −
r Χ s = { q t | q ​∈​ r and t ​∈​ s}

σ​author = 'tutorialspoint'​(Books Χ Articles)


Output​ − Yields a relation, which shows all the books and articles written
by tutorialspoint.
Rename Operation (ρ)
The results of relational algebra are also relations but without any name.
The rename operation allows us to rename the output relation. 'rename'
operation is denoted with small Greek letter ​rho​ ​ρ.​
Notation​ − ​ρ​ ​x​ (E)
Where the result of expression ​E​ is saved with name of ​x​.
Additional operations are −
● Set intersection

● Assignment

● Natural join
Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural
query language, that is, it tells what to do but never explains how to do it.
Relational calculus exists in two forms −
Tuple Relational Calculus (TRC)
Filtering variable ranges over tuples
Notation​ − {T | Condition}
Returns all tuples T that satisfies a condition.
For example​ −

{ T.name | Author(T) AND T.article = 'database' }


Output​ − Returns tuples with 'name' from Author who has written article
on 'database'.
TRC can be quantified. We can use Existential (​∃​) and Universal Quantifiers
(​∀​).
For example​ −

{ R| ​∃​T ​∈​ Authors(T.article='database' AND R.name=T.name)}


Output​ − The above query will yield the same result as the previous one.
Domain Relational Calculus (DRC)
In DRC, the filtering variable uses the domain of attributes instead of entire
tuple values (as done in TRC, mentioned above).
Notation​ −
{ a​1​, a​2​, a​3​, ..., a​n​ | P (a​1​, a​2​, a​3​, ... ,a​n​)}
Where a1, a2 are attributes and ​P​ stands for formulae built by inner
attributes.
For example​ −

{< article, page, subject > | ​∈​ TutorialsPoint ​∧​ subject = 'database'}
Output​ − Yields Article, Page, and Subject from the relation TutorialsPoint,
where subject is database.
Just like TRC, DRC can also be written using existential and universal
quantifiers. DRC also involves relational operators.
The expression power of Tuple Relation Calculus and Domain Relation
Calculus is equivalent to Relational Algebra.

Joins in SQL
SQL Join is used to fetch data from two or more tables, which is joined to appear as single
set of data. SQL Join is used for combining column from two or more tables by using values
common to both tables. ​Join​ Keyword is used in SQL queries for joining two or more tables.
Minimum required condition for joining table, is ​(n-1)​ where ​n​, is number of tables. A table
can also join to itself known as, ​Self Join​.
Types of Join
The following are the types of JOIN that we can use in SQL.
● Inner
● Outer
● Left
● Right

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


The ​class​ table,

ID NAME

1 abhi

2 adam

4 alex

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 result 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

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


The ​class​ table,

ID NAME

1 abhi

2 adam

3 alex

4 anu

The ​class_info​ table,


ID Address

1 DELHI

2 MUMBAI

3 CHENNAI

Inner​ JOIN query will be,


SELECT * from class, class_info where class.id = class_info.id;

The result 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.
Natural Join Syntax is,
SELECT *

from ​table-name1

NATURAL JOIN

table-name2​;
Example of Natural JOIN
The ​class​ table,

ID NAME

1 abhi

2 adam

3 alex

4 anu

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 result 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,
● Left Outer Join
● Right Outer Join
● Full Outer Join

Left Outer Join


The left outer join returns a result table with the ​matched data​ of two tables then remaining
rows of the ​left​ table and null for the ​right​ table's column.
Left Outer Join syntax is,
SELECT column-name-list

from ​table-name1

LEFT OUTER JOIN

table-name2

on table-name1.column-name = table-name2.column-name;

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
The ​class​ table,

ID NAME

1 abhi

2 adam

3 alex

4 anu

5 ashish

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 result 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 result table with the ​matched data​ of two tables then
remaining rows of the ​right table​ and null for the l​ eft​ table's columns.
Right Outer Join Syntax 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
The ​class​ table,

ID NAME

1 abhi

2 adam

3 alex

4 anu

5 ashish

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 result 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 result table with the ​matched data​ of two table then remaining
rows of both ​left​ table and then the ​right​ table.
Full Outer Join Syntax 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

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 result 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

Vous aimerez peut-être aussi