You are on page 1of 40

DBMS

 DBMS is a collection of programs that facilitates users to create and maintain a


database.
MySQL, Oracle, SQL Server, Amazon simple DB (Cloud-based), etc.
 The database is a collection of inter-related data which is used to retrieve, insert and
delete the data efficiently. It is also used to organize the data in the form of a table,
schema, views, and reports, etc.

 For example: The college Database organizes the data about the admin, staff,
students and faculty etc.

 Using the database, you can easily retrieve, insert, and delete the information.

What is a database system?

The collection of database and DBMS software together is known as a database system.

Characteristics of DBMS

o It uses a digital repository established on a server to store and manage the


information.[ A digital repository is an instrument for managing and storing digital
content]
o DBMS contains automatic backup and recovery procedures.
o It contains ACID properties which maintain data in a healthy state in case of failure.
o It can reduce the complex relationship between data.
o It is used to support manipulation and processing of data.
o It is used to provide security of data.
o It can view the database from different viewpoints according to the requirements of
the

What are the advantages of DBMS?

o Redundancy control [Data redundancy occurs when the same piece of data is stored
in two or more separate places]
o Restriction for unauthorized access
o Provides multiple user interfaces [It provides different types of user interfaces like
graphical user interfaces, application program interfaces]
o Provides backup and recovery
o Enforces integrity constraints
o Ensure data consistency
o Easy accessibility
o Easy data extraction and data processing due to the use of queries

Disadvantages of DBMS

o Cost of Hardware and Software: It requires a high speed of data processor and large
memory size to run DBMS software.
o Size: It occupies a large space of disks and large memory to run them efficiently.
o Complexity: Database system creates additional complexity and requirements.
o Higher impact of failure: Failure is highly impacted the database because in most of
the organization, all the data stored in a single database and if the database is damaged
due to electric failure or database corruption then the data may be lost forever.

Data Integrity

o There are the following categories of data integrity exist with each RDBMS:

o Entity integrity: It specifies that there should be no duplicate rows in a table.


o Domain integrity: It enforces valid entries for a given column by restricting the type,
the format, or the range of values.
o Referential integrity: It specifies that rows cannot be deleted, which are used by
other records.

o User-defined integrity: It enforces some specific business rules that are defined by
users. These rules are different from entity, domain or referential integrity.

No. DBMS RDBMS

1) DBMS applications store data as RDBMS applications store data in a tabular


file. form.

2) In DBMS, data is generally stored In RDBMS, the tables have an identifier called
in either a hierarchical form or a primary key and the data values are stored in
navigational form. the form of tables.

3) Normalization is not present in Normalization is present in RDBMS.


DBMS.

4) DBMS does not apply any RDBMS defines the integrity constraint for
security with regards to data the purpose of ACID (Atomocity, Consistency,
manipulation. Isolation and Durability) property.

5) DBMS uses file system to store in RDBMS, data values are stored in the form
data, so there will be no relation of tables, so a relationship between these data
between the tables. values will be stored in the form of a table as
well.

6) DBMS has to provide some RDBMS system supports a tabular structure of


uniform methods to access the the data and a relationship between them to
stored information. access the stored information.

7) DBMS does not support RDBMS supports distributed database.


distributed database.

8) DBMS is meant to be for small RDBMS is designed to handle large amount


organization and deal with small of data. it supports multiple users.
data. it supports single user.

9) Examples of DBMS are file Example of RDBMS are mysql, postgre, sql
systems, xml etc. server, oracle etc.

S.NO. File System DBMS

File system is a software that


manages and organizes the files
in a storage medium within a DBMS is a software for managing the
1. computer. database.

Redundant data can be present in


2. a file system. In DBMS there is no redundant data.

It doesn’t provide backup and It provides backup and recovery of data even
3. recovery of data if it is lost. if it is lost.

There is no efficient query


4. processing in file system. Efficient query processing is there in DBMS.

There is less data consistency in There is more data consistency because of the
5. file system. process of normalization.

It is less complex as compared to It has more complexity in handling as


6. DBMS. compared to file system.

File systems provide less security DBMS has more security mechanisms as
7. in comparison to DBMS. compared to file system.

It has a comparatively higher cost than a file


8. It is less expensive than DBMS. system.
What is RDBMS

RDBMS stands for Relational Database Management Systems..

All modern database management systems like SQL, MS SQL Server, IBM DB2, ORACLE,
My-SQL and Microsoft Access are based on RDBMS.

It is called Relational Data Base Management System (RDBMS) because it is based on


relational model introduced by E.F. Codd.

How it works

Data is represented in terms of tuples (rows) in RDBMS. Relational database is most


commonly used database. It contains number of tables and each table has its own primary
key.

Due to a collection of organized set of tables, data can be accessed easily in RDBMS.

NULL Values

The NULL value of the table specifies that the field has been left blank during record
creation. It is totally different from the value filled with zero or a field that contains space.

1-Tier Architecture

o In this architecture, the database is directly available to the user. It means the user can
directly sit on the DBMS and uses it.
o Any changes done here will directly be done on the database itself. It doesn't provide
a handy tool for end users.
o The 1-Tier architecture is used for development of the local application, where
programmers can directly communicate with the database for the quick response.

48) What is 2-Tier architecture?

o The 2-Tier architecture is the same as basic client-server. In the two-tier


architecture, applications on the client end can directly communicate with the
database at the server side.

49) What is the 3-Tier architecture?

o The 3-Tier architecture contains another layer between the client and server.
Introduction of 3-tier architecture is for the ease of the users as it provides the GUI,
which, make the system secure and much more accessible. In this architecture, the
application on the client-end interacts with an application on the server which further
communicates with the database system.
11) What do you understand by Data Model?

o The Data model is specified as a collection of conceptual tools for describing data,
data relationships, data semantics and constraints. These models are used to describe
the relationship between the entities and their attributes.

There is the number of data models:

o Hierarchical data model


o network model
o relational model
o Entity-Relationship model and so on.

Data model Schema and Instance


o The data which is stored in the database at a particular moment of time is called an
instance of the database.
o The overall design of a database is called schema.
o A database schema is the blueprint of the database. It represents the logical view of
the entire database.
o A schema contains schema objects like table, foreign key, primary key, views,
columns, data types, stored procedure, etc.

Data Independence

Data independence refers characteristic of being able to modify the schema at one level of the
database system without altering the schema at the next higher level.

1. Logical Data Independence

o Logical data independence refers characteristic of being able to change the conceptual
schema without having to change the external schema.
o Logical data independence is used to separate the external level from the conceptual
view.

Physical Data Independence

o Physical data independence can be defined as the capacity to change the internal
schema without having to change the conceptual schema.
o If we do any changes in the storage size of the database system server, then the
Conceptual structure of the database will not be affected.
o Physical data independence is used to separate conceptual levels from the internal
levels.

Database Language

1. Data Definition Language

o DDL stands for Data Definition Language. It is used to define database structure or
pattern.
o It is used to create schema, tables, indexes, constraints, etc. in the database.
o Using the DDL statements, you can create the skeleton of the database.
o Data definition language is used to store the information of metadata like the number
of tables and schemas, their names, indexes, columns in each table, constraints, etc.

Here are some tasks that come under DDL:

o Create: It is used to create objects in the database.


o Alter: It is used to alter the structure of the database.
o Drop: It is used to delete objects from the database.
o Truncate: It is used to remove all records from a table.
o Rename: It is used to rename an object.
o Comment: It is used to comment on the data dictionary.

These commands are used to update the database schema that's why they come under Data
definition language.

2. Data Manipulation Language

DML stands for Data Manipulation Language. It is used for accessing and manipulating data
in a database. It handles user requests.

Here are some tasks that come under DML:

7.3M
131
Difference between JDK, JRE, and JVM

o Select: It is used to retrieve data from a database.


o Insert: It is used to insert data into a table.
o Update: It is used to update existing data within a table.
o Delete: It is used to delete all records from a table.
o Merge: It performs UPSERT operation, i.e., insert or update operations.
o Call: It is used to call a structured query language or a Java subprogram.
o Explain Plan: It has the parameter of explaining data.
o Lock Table: It controls concurrency.

3. Data Control Language

o DCL stands for Data Control Language. It is used to retrieve the stored or saved data.
o The DCL execution is transactional. It also has rollback parameters.

(But in Oracle database, the execution of data control language does not have the
feature of rolling back.)

Here are some tasks that come under DCL:

o Grant: It is used to give user access privileges to a database.


o Revoke: It is used to take back permissions from the user.

There are the following operations which have the authorization of Revoke:

CONNECT, INSERT, USAGE, EXECUTE, DELETE, UPDATE and SELECT.

4. Transaction Control Language

TCL is used to run the changes made by the DML statement. TCL can be grouped into a
logical transaction.

Here are some tasks that come under TCL:

o Commit: It is used to save the transaction on the database.


o Rollback: It is used to restore the database to original since the last Commit.

45) Explain ACID properties

o ACID properties are some basic rules, which has to be satisfied by every transaction
to preserve the integrity. These properties and rules are:

o ATOMICITY: Atomicity is more generally known as ?all or nothing rule.' Which


implies all are considered as one unit, and they either run to completion or not
executed at all.

o CONSISTENCY: This property refers to the uniformity of the data. Consistency


implies that the database is consistent before and after the transaction.
o ISOLATION: This property states that the number of the transaction can be executed
concurrently without leading to the inconsistency of the database state.

o DURABILITY: This property ensures that once the transaction is committed it will
be stored in the non-volatile memory and system crash can also not affect it anymore.

o 28) What is the E-R model?

o E-R model is a short name for the Entity-Relationship model. This model is based on
the real world. It contains necessary objects (known as entities) and the relationship
among these objects. Here the primary objects are the entity, attribute of that entity,
relationship set, an attribute of that relationship set can be mapped in the form of E-R
diagram.

o In E-R diagram, entities are represented by rectangles, relationships are represented


by diamonds, attributes are the characteristics of entities and represented by ellipses,
and data flow is represented through a straight line.

o For example, Suppose we design a school database. In this database, the student will
be an entity with attributes like address, name, id, age, etc. The address can be another
entity with attributes like city, street name, pin code, etc and there will be a
relationship between them.

1. Entity:

An entity may be any object, class, person or place. In the ER diagram, an entity can be
represented as rectangles.

Consider an organization as an example- manager, product, employee, department etc. can be


taken as an entity.

a. Weak Entity

An entity that depends on another entity called a weak entity. The weak entity doesn't contain
any key attribute of its own. The weak entity is represented by a double rectangle.

2. Attribute
The attribute is used to describe the property of an entity. Eclipse is used to represent an
attribute.

For example, id, age, contact number, name, etc. can be attributes of a student

a. Key Attribute

The key attribute is used to represent the main characteristics of an entity. It represents a
primary key. The key attribute is represented by an ellipse with the text underlined.

b. Composite Attribute

An attribute that composed of many other attributes is known as a composite attribute. The
composite attribute is represented by an ellipse, and those ellipses are connected with an
ellipse.

c. Multivalued Attribute

An attribute can have more than one value. These attributes are known as a multivalued
attribute. The double oval is used to represent multivalued attribute.

For example, a student can have more than one phone number.

d. Derived Attribute

An attribute that can be derived from other attribute is known as a derived attribute. It can be
represented by a dashed ellipse.

For example, A person's age changes over time and can be derived from another attribute
like Date of birth.

30) What is an Entity type?

An entity type is specified as a collection of entities, having the same attributes. Entity type
typically corresponds to one or several related tables in the database. A characteristic or trait
which defines or uniquely identifies the entity is called entity type.

For example, a student has student_id, department, and course as its characteristics.

31) What is an Entity set?

The entity set specifies the collection of all entities of a particular entity type in the database.
An entity set is known as the set of all the entities which share the same properties.

For example, a set of people, a set of students, a set of companies, etc.

3. Relationship
A relationship is used to describe the relation between entities. Diamond or rhombus is used
to represent the relationship.

1. One to one (1:1)


2. One to many (1:M)
3. Many to one (M:1)
4. Many to many (M:M)

One-to-one

In one-to-one mapping, an entity in E1 is associated with at most one entity in E2, and an
entity in E2 is associated with at most one entity in E1.

One-to-many

In one-to-many mapping, an entity in E1 is associated with any number of entities in E2, and
an entity in E2 is associated with at most one entity in E1.

Many-to-one

In one-to-many mapping, an entity in E1 is associated with at most one entity in E2, and an
entity in E2 is associated with any number of entities in E1.

Many-to-many

In many-to-many mapping, an entity in E1 is associated with any number of entities in E2,


and an entity in E2 is associated with any number of entities in E1.

n Relational Model concept

Relational model can represent as a table with columns and rows. Each row is known as a
tuple. Each table of the column has a name or attribute.

Domain: It contains a set of atomic values that an attribute can take.

Attribute: It contains the name of a column in a particular table. Each attribute Ai must have
a domain, dom(Ai)

Relational instance: In the relational database system, the relational instance is represented
by a finite set of tuples. Relation instances do not have duplicate tuples.

Relational schema: A relational schema contains the name of the relation and name of all
columns or attributes.

Relational key: In the relational key, each row has one or more attributes. It can identify the
row in the relation uniquely.
) Normalization
o Normalization is the process of organizing the data in the database.
o Normalization is used to minimize the redundancy from a relation or set of relations.
It is also used to eliminate the undesirable characteristics like Insertion, Update and
Deletion Anomalies.
o Normalization divides the larger table into the smaller table and links them using
relationship.
o The normal form is used to reduce redundancy from the database table.

41) What is 1NF?

1NF is the First Normal Form. It is the simplest type of normalization that you can
implement in a database. The primary objectives of 1NF are to:

o Every column must have atomic (single value)


o To Remove duplicate columns from the same table
o Create separate tables for each group of related data and identify each row with a
unique column

42) What is 2NF?

2NF is the Second Normal Form. A table is said to be 2NF if it follows the following
conditions:

o The table is in 1NF, i.e., firstly it is necessary that the table should follow the rules of
1NF.
o Every non-prime attribute is fully functionally dependent on the primary key, i.e.,
every non-key attribute should be dependent on the primary key in such a way that if
any key element is deleted, then even the non_key element will still be saved in the
database.

43) What is 3NF?

3NF stands for Third Normal Form. A database is called in 3NF if it satisfies the following
conditions:

o It is in second normal form.


o There is no transitive functional dependency.
o For example: X->Z

44) What is BCNF?

BCMF stands for Boyce-Codd Normal Form. It is an advanced version of 3NF, so it is also
referred to as 3.5NF. BCNF is stricter than 3NF.

A table complies with BCNF if it satisfies the following conditions:

o It is in 3NF.
o For every functional dependency X->Y, X should be the super key of the table. It
merely means that X cannot be a non-prime attribute if Y is a prime attribute.

Keys
 PRIMARY KEY
• It is a set of one or more attributes of the table that uniquely identify a record in
database table
• It cannot accept null values, duplicate values
• Only one candidate key can be primary key
• Ex: In Student Table out of many attributes the registration number of the
student can be considered as the primary key
 UNIQUE KEY
• There is a similarity between the Primary Key and the Unique Key
• It is a set of one or more attributes of the table that uniquely identify a record in
database table
• It can accept null values
• But it cannot accept duplicate values
 A LT E R N AT E K E Y ( S E C O N D A RY K E Y )
• It can work as a primary key i.e., (Unique, Not Null values, No duplicate)
• Basically it is a candidate key that currently is not primary key
 CANDIDATE KEY

• It is a set of one or more attributes that can identify a record uniquely in table
• There are multiple candidate keys in a table
• Each candidate key can work as Primary Key
• Ex: Roll No., Registration No. both represent the same form
• Candidate Key => Alternate Key => Primary Key
 SUPER KEY
• It is the superset of Primary Key, Candidate Key, Unique Key and Alternate Key
• It is a set of one or more than one keys that can be used to identify a record
uniquely in a table
 COMPOSITE KEY

• It is a combination of more than one field/column of a table


• It can be a candidate key, primary key
• Ex: Name -> cannot just be a primary key
• Date of Birth -> cannot just be a primary key, but if a mix of both

 FOREIGN KEY

• It is a field in database table that is primary key in another table


• Ex: Student Table (Student Name, Registration No., Mentor Code)
• The second table is the Mentor Table (Mentor Name, Mentor Email, Mentor
Code)
• The second table (Mentor Code is the Primary Key), which acts as Foreign
Key for the Student Table (under the Mentor Code)

DBMS Interfaces
Some of the user-friendly interfaces provided by a DBMS include the following:
1. Menu-Based Interfaces for Browsing – These interfaces present the user with
lists of options called menus that lead the user through the formulation of a
request. These are often used in browsing interfaces which allow user to look
through the contents of a database in an exploratory and unstructured
manner.
2. Forms-Based Interfaces – These interface displays a form to each user. Users
can fill out all of the form entries to insert new data. Forms are usually
designed and programmed for users as interfaces to carry out transactions.
3. Graphical User Interfaces – A GUI typically displays a schema to the user in
diagrammatic form. It can utilize both menus and forms. The user can specify a
query by manipulating the diagram.
4. Natural Language Interfaces - These interfaces accept requests written in
English or some other language and attempt to understand them. A natural
language interface usually has its own schema, and refers to the words in its
schema to interpret the request. It the interpretation is successful, the
interface generates a high-level query corresponding to the natural language
request and submits it to the DBMS for processing.
5. Speech Input and Output – Limited use of speech as an input query and
speech as an answer to a question or result of a request is common. The
speech input is detected using a library of predefined words and is used to set
up the parameters that are supplied to the queries.
6. Interfaces for Parametric Users – a small number of abbreviated commands is
included, with the goal of minimizing the number of keystrokes required for
each request.
7. Interfaces for the Database Administrator (DBA) – Most database systems
contain privileged commands that can be used only the DBA’s staff. These
include commands for creating accounts, setting system parameters, granting
account authorization, changing a schema and reorganizing the storage
structures of a database.

Relational Algebra
Relational algebra is a procedural query language. It gives a step by step process to
obtain the result of the query. It uses operators to perform queries.

Types of Relational operation

1. Select Operation:

o The select operation selects tuples that satisfy a given predicate.


o It is denoted by sigma (σ).

1. Notation: σ p(r)

σ is used for selection prediction

r is used for relation

p is used as a propositional logic formula which may use connectors like: AND OR and
NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.

For example: LOAN Relation

BRANCH_NAME LOAN_NO AMOUNT


Downtown L-17 1000

Redwood L-23 2000

Perryride L-15 1500

Downtown L-14 1500

Mianus L-13 500

Roundhill L-11 900

Perryride L-16 1300

Input:

1. σ BRANCH_NAME="perryride" (LOAN)

Output:

BRANCH_NAME LOAN_NO AMOUNT

Perryride L-15 1500

Perryride L-16 1300

2. Project Operation:

o This operation shows the list of those attributes that we wish to appear in the result.
Rest of the attributes are eliminated from the table.
o It is denoted by ∏.

1. Notation: ∏ A1, A2, An (r)

Where

A1, A2, A3 is used as an attribute name of relation r.

Example: CUSTOMER RELATION

NAME STREET CITY

Jones Main Harrison

Smith North Rye


Hays Main Harrison

Curry North Rye

Johnson Alma Brooklyn

Brooks Senator Brooklyn

Input:

1. ∏ NAME, CITY (CUSTOMER)

Output:

NAME CITY

Jones Harrison

Smith Rye

Hays Harrison

Curry Rye

Johnson Brooklyn

Brooks Brooklyn

3. Union Operation:

o Suppose there are two tuples R and S. The union operation contains all the tuples that
are either in R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.

1. Notation: R ∪ S

A union operation must hold the following condition:

o R and S must have the attribute of the same number.


o Duplicate tuples are eliminated automatically.

Example:

DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO

Johnson A-101

Smith A-121

Mayes A-321

Turner A-176

Johnson A-273

Jones A-472

Lindsay A-284

BORROW RELATION

CUSTOMER_NAME LOAN_NO

Jones L-17

Smith L-23

Hayes L-15

Jackson L-14

Curry L-93

Smith L-11

Williams L-17

Input:

1. ∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)

Output:

CUSTOMER_NAME

Johnson

Smith

Hayes
Turner

Jones

Lindsay

Jackson

Curry

Williams

Mayes

4. Set Intersection:

o Suppose there are two tuples R and S. The set intersection operation contains all
tuples that are in both R & S.
o It is denoted by intersection ∩.

1. Notation: R ∩ S

Example: Using the above DEPOSITOR table and BORROW table

Input:

1. ∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)

Output:

CUSTOMER_NAME

Smith

Jones

5. Set Difference:

o Suppose there are two tuples R and S. The set intersection operation contains all
tuples that are in R but not in S.
o It is denoted by intersection minus (-).

1. Notation: R - S
Example: Using the above DEPOSITOR table and BORROW table

Input:

1. ∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)

Output:

CUSTOMER_NAME

Jackson

Hayes

Willians

Curry

6. Cartesian product

o The Cartesian product is used to combine each row in one table with each row in the
other table. It is also known as a cross product.
o It is denoted by X.

1. Notation: E X D

Example:

EMPLOYEE

EMP_ID EMP_NAME EMP_DEPT

1 Smith A

2 Harry C

3 John B

DEPARTMENT

DEPT_NO DEPT_NAME

A Marketing
B Sales

C Legal

Input:

1. EMPLOYEE X DEPARTMENT

EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME

1 Smith A A Marketing

1 Smith A B Sales

1 Smith A C Legal

2 Harry C A Marketing

2 Harry C B Sales

2 Harry C C Legal

3 John B A Marketing

3 John B B Sales

3 John B C Legal

Output:

7. Rename Operation:

The rename operation is used to rename the output relation. It is denoted by rho (ρ).

Example: We can use the rename operator to rename STUDENT relation to STUDENT1.

1. ρ(STUDENT1, STUDENT)

Join Operations:

A Join operation combines related tuples from different relations, if and only if a given join
condition is satisfied. It is denoted by ⋈.

Example:

EMPLOYEE
EMP_CODE SALARY

101 50000

102 30000

103 25000

EMP_CODE EMP_NAME

101 Stephan

102 Jack

103 Harry

SALARY

1. Operation: (EMPLOYEE ⋈ SALARY)

Result:

7.5M
134
Difference between JDK, JRE, and JVM

EMP_CODE EMP_NAME SALARY

101 Stephan 50000

102 Jack 30000

103 Harry 25000

Types of Join operations:


1. Natural Join:

o A natural join is the set of tuples of all combinations in R and S that are equal on their
common attribute names.
o It is denoted by ⋈.

Example: Let's use the above EMPLOYEE table and SALARY table:

Input:

1. ∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)

Output:

EMP_NAME SALARY

Stephan 50000

Jack 30000

Harry 25000

2. Outer Join:

The outer join operation is an extension of the join operation. It is used to deal with missing
information.

Example:
EMPLOYEE

EMP_NAME STREET CITY

Ram Civil line Mumbai

Shyam Park street Kolkata

Ravi M.G. Street Delhi

Hari Nehru nagar Hyderabad

FACT_WORKERS

EMP_NAME BRANCH SALARY

Ram Infosys 10000

Shyam Wipro 20000

Kuber HCL 30000

Hari TCS 50000

Input:

1. (EMPLOYEE ⋈ FACT_WORKERS)

Output:

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

Hari Nehru nagar Hyderabad TCS 50000

An outer join is basically of three types:

a. Left outer join


b. Right outer join
c. Full outer join

a. Left outer join:


o Left outer join contains the set of tuples of all combinations in R and S that are equal
on their common attribute names.
o In the left outer join, tuples in R have no matching tuples in S.
o It is denoted by ⟕.

Example: Using the above EMPLOYEE table and FACT_WORKERS table

Input:

1. EMPLOYEE ⟕ FACT_WORKERS

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

Hari Nehru street Hyderabad TCS 50000

Ravi M.G. Street Delhi NULL NULL

b. Right outer join:

o Right outer join contains the set of tuples of all combinations in R and S that are equal
on their common attribute names.
o In right outer join, tuples in S have no matching tuples in R.
o It is denoted by ⟖.

Example: Using the above EMPLOYEE table and FACT_WORKERS Relation

Input:

1. EMPLOYEE ⟖ FACT_WORKERS

Output:

EMP_NAME BRANCH SALARY STREET CITY

Ram Infosys 10000 Civil line Mumbai

Shyam Wipro 20000 Park street Kolkata

Hari TCS 50000 Nehru street Hyderabad


Kuber HCL 30000 NULL NULL

c. Full outer join:

o Full outer join is like a left or right join except that it contains all rows from both
tables.
o In full outer join, tuples in R that have no matching tuples in S and tuples in S that
have no matching tuples in R in their common attribute name.
o It is denoted by ⟗.

Example: Using the above EMPLOYEE table and FACT_WORKERS table

Input:

1. EMPLOYEE ⟗ FACT_WORKERS

Output:

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000

Shyam Park street Kolkata Wipro 20000

Hari Nehru street Hyderabad TCS 50000

Ravi M.G. Street Delhi NULL NULL

Kuber NULL NULL HCL 30000

3. Equi join:

It is also known as an inner join. It is the most common join. It is based on matched data as
per the equality condition. The equi join uses the comparison operator(=).

Example:

CUSTOMER RELATION

CLASS_ID NAME

1 John

2 Harry
3 Jackson

PRODUCT

PRODUCT_ID CITY

1 Delhi

2 Mumbai

3 Noida

Input:

1. CUSTOMER ⋈ PRODUCT

Output:

CLASS_ID NAME PRODUCT_ID CITY

1 John 1 Delhi

2 Harry 2 Mumbai

3 Harry 3 Noida

Integrity Constraints

o Integrity constraints are a set of rules. It is used to maintain the quality of information.
o Integrity constraints ensure that the data insertion, updating, and other processes have
to be performed in such a way that data integrity is not affected.
o Thus, integrity constraint is used to guard against accidental damage to the database.

Types of Integrity Constraint

1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an
attribute.
o The data type of domain includes string, character, integer, time, date, currency, etc.
The value of the attribute must be available in the corresponding domain.
Example:

2. Entity integrity constraints

o The entity integrity constraint states that primary key value can't be null.
o This is because the primary key value is used to identify individual rows in relation
and if the primary key has a null value, then we can't identify those rows.
o A table can contain a null value other than the primary key field.

Example:

3. Referential Integrity Constraints

o A referential integrity constraint is specified between two tables.


o In the Referential integrity constraints, if a foreign key in Table 1 refers to the
Primary Key of Table 2, then every value of the Foreign Key in Table 1 must be null
or be available in Table 2.

Example:
4. Key constraints

o Keys are the entity set that is used to identify an entity within its entity set uniquely.
o An entity set can have multiple keys, but out of which one key will be the primary
key. A primary key can contain a unique and null value in the relational table.

Example:
SQL
SQL Constraints

SQL constraints are used to specify rules for the data in a table.

Constraints are used to limit the type of data that can go into a table. This ensures the
accuracy and reliability of the data in the table. If there is any violation between the
constraint and the data action, the action is aborted.

Constraints can be column level or table level. Column level constraints apply to a column,
and table level constraints apply to the whole table.

The following constraints are commonly used in SQL:

 NOT NULL - Ensures that a column cannot have a NULL value


 UNIQUE - Ensures that all values in a column are different
 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely
identifies each row in a table
 FOREIGN KEY - Prevents actions that would destroy links between tables
 CHECK - Ensures that the values in a column satisfies a specific condition
 DEFAULT - Sets a default value for a column if no value is specified
 CREATE INDEX - Used to create and retrieve data from the database very quickly

SQL (Structured Query Language) is used to perform operations on the records stored in the
database, such as updating records, inserting records, deleting records, creating and
modifying database tables, views, etc.

SQL is not a database system, but it is a query language.

Why SQL?

o The basic use of SQL for data professionals and SQL users is to insert, update, and
delete the data from the relational database.
o SQL allows the data professionals and users to retrieve the data from the relational
database management systems.
o It also helps them to describe the structured data.
o It allows SQL users to create, drop, and manipulate the database and its tables.
o It also helps in creating the view, stored procedure, and functions in the relational
database.
o It allows you to define the data and modify that stored data in the relational database.
o It also allows SQL users to set the permissions or constraints on table columns, views,
and stored procedures.

o Advantages of SQL

1. No programming needed

o SQL does not require a large number of coding lines for managing the database
systems. We can easily access and maintain the database by using simple SQL
syntactical rules. These simple rules make the SQL user-friendly.

2. High-Speed Query Processing

o A large amount of data is accessed quickly and efficiently from the database by using
SQL queries. Insertion, deletion, and updation operations on data are also performed
in less time.

3. Standardized Language

o SQL follows the long-established standards of ISO and ANSI, which offer a uniform
platform across the globe to all its users.

4. Portability

o The structured query language can be easily used in desktop computers, laptops,
tablets, and even smartphones. It can also be used with other applications according to
the user's requirements.

5. Interactive language

o We can easily learn and understand the SQL language. We can also use this language
for communicating with the database because it is a simple query language. This
language is also used for receiving the answers to complex queries in a few seconds.

6. More than one Data View

o The SQL language also helps in making the multiple views of the database structure
for the different database users.

Disadvantages of SQL

o With the advantages of SQL, it also has some disadvantages, which are as follows:

1. Cost

o The operation cost of some SQL versions is high. That's why some programmers
cannot use the Structured Query Language.

2. Interface is Complex
o Another big disadvantage is that the interface of Structured query language is
difficult, which makes it difficult for SQL users to use and manage it.

3. Partial Database control

o The business rules are hidden. So, the data professionals and users who are using this
query language cannot have full database control.

SQL Statement

SQL statements tell the database what operation you want to perform on the structured data
and what information you would like to access from the database.

Simple Example of SQL statement:

1. SELECT "column_name" FROM "table_name";

Each SQL statement begins with any of the SQL keywords and ends with the semicolon (;).
The semicolon is used in the SQL for separating the multiple Sql statements which are going
to execute in the same call. In this SQL tutorial, we will use the semicolon (;) at the end of
each SQL query or statement.

Most Important SQL Commands and Statements

1. Select Statement
2. Update Statement
3. Delete Statement
4. Create Table Statement
5. Alter Table Statement
6. Drop Table Statement
7. Create Database Statement
8. Drop Database Statement
9. Insert Into Statement
10. Truncate Table Statement
11. Describe Statement
12. Distinct Clause
13. Commit Statement
14. Rollback Statement
15. Create Index Statement
16. Drop Index Statement
17. Use Statement

Let's discuss each statement in short one by one with syntax and one example:

1. SELECT Statement

This SQL statement reads the data from the SQL database and shows it as the output to the
database user.

Syntax of SELECT Statement:

1. SELECT column_name1, column_name2, .…, column_nameN


2. [ FROM table_name ]
3. [ WHERE condition ]
4. [ ORDER BY order_column_name1 [ ASC | DESC ], .... ];

Example of SELECT Statement:

1. SELECT Emp_ID, First_Name, Last_Name, Salary, City


2. FROM Employee_details
3. WHERE Salary = 100000
4. ORDER BY Last_Name

This example shows the Emp_ID, First_Name, Last_Name, Salary, and City of those
employees from the Employee_details table whose Salary is 100000. The output shows all
the specified details according to the ascending alphabetical order of Last_Name.

3. UPDATE Statement

This SQL statement changes or modifies the stored data in the SQL database.

Syntax of UPDATE Statement:

1. UPDATE table_name
2. SET column_name1 = new_value_1, column_name2 = new_value_2, ...., column_na
meN = new_value_N
3. [ WHERE CONDITION ];

Example of UPDATE Statement:

1. UPDATE Employee_details
2. SET Salary = 100000
3. WHERE Emp_ID = 10;
This example changes the Salary of those employees of the Employee_details table
whose Emp_ID is 10 in the table.

3. DELETE Statement

This SQL statement deletes the stored data from the SQL database.

Syntax of DELETE Statement:

1. DELETE FROM table_name


2. [ WHERE CONDITION ];

Example of DELETE Statement:

1. DELETE FROM Employee_details


2. WHERE First_Name = 'Sumit';

This example deletes the record of those employees from the Employee_details table
whose First_Name is Sumit in the table.

4. CREATE TABLE Statement

This SQL statement creates the new table in the SQL database.

Syntax of CREATE TABLE Statement:

1. CREATE TABLE table_name


2. (
3. column_name1 data_type [column1 constraint(s)],
4. column_name2 data_type [column2 constraint(s)],
5. .....
6. .....,
7. column_nameN data_type [columnN constraint(s)],
8. PRIMARY KEY(one or more col)
9. );

Example of CREATE TABLE Statement:

1. CREATE TABLE Employee_details(


2. Emp_Id NUMBER(4) NOT NULL,
3. First_name VARCHAR(30),
4. Last_name VARCHAR(30),
5. Salary Money,
6. City VARCHAR(30),
7. PRIMARY KEY (Emp_Id)
8. );

This example creates the table Employee_details with five columns or fields in the SQL
database. The fields in the table are Emp_Id, First_Name, Last_Name, Salary, and City.
The Emp_Id column in the table acts as a primary key, which means that the Emp_Id
column cannot contain duplicate values and null values.

5. ALTER TABLE Statement

This SQL statement adds, deletes, and modifies the columns of the table in the SQL database.

Syntax of ALTER TABLE Statement:

1. ALTER TABLE table_name ADD column_name datatype[(size)];

The above SQL alter statement adds the column with its datatype in the existing database
table.

1. ALTER TABLE table_name MODIFY column_name column_datatype[(size)];

The above 'SQL alter statement' renames the old column name to the new column name of
the existing database table.

1. ALTER TABLE table_name DROP COLUMN column_name;

The above SQL alter statement deletes the column of the existing database table.

Example of ALTER TABLE Statement:

1. ALTER TABLE Employee_details


2. ADD Designation VARCHAR(18);

This example adds the new field whose name is Designation with size 18 in
the Employee_details table of the SQL database.

6. DROP TABLE Statement

This SQL statement deletes or removes the table and the structure, views, permissions, and
triggers associated with that table.

Syntax of DROP TABLE Statement:

1. DROP TABLE [ IF EXISTS ]


2. table_name1, table_name2, ……, table_nameN;
The above syntax of the drop statement deletes specified tables completely if they exist in the
database.

Example of DROP TABLE Statement:

1. DROP TABLE Employee_details;

This example drops the Employee_details table if it exists in the SQL database. This
removes the complete information if available in the table.

7. CREATE DATABASE Statement

This SQL statement creates the new database in the database management system.

Syntax of CREATE DATABASE Statement:

1. CREATE DATABASE database_name;

Example of CREATE DATABASE Statement:

1. CREATE DATABASE Company;

The above example creates the company database in the system.

8. DROP DATABASE Statement

This SQL statement deletes the existing database with all the data tables and views from the
database management system.

Syntax of DROP DATABASE Statement:

1. DROP DATABASE database_name;

Example of DROP DATABASE Statement:

1. DROP DATABASE Company;

The above example deletes the company database from the system.

9. INSERT INTO Statement

This SQL statement inserts the data or records in the existing table of the SQL database. This
statement can easily insert single and multiple records in a single query statement.

Syntax of insert a single record:

1. INSERT INTO table_name


2. (
3. column_name1,
4. column_name2, .…,
5. column_nameN
6. )
7. VALUES
8. (value_1,
9. value_2, ..…,
10. value_N
11. );

Example of insert a single record:

1. INSERT INTO Employee_details


2. (
3. Emp_ID,
4. First_name,
5. Last_name,
6. Salary,
7. City
8. )
9. VALUES
10. (101,
11. Akhil,
12. Sharma,
13. 40000,
14. Bangalore
15. );

This example inserts 101 in the first column, Akhil in the second column, Sharma in the
third column, 40000 in the fourth column, and Bangalore in the last column of the
table Employee_details.

Syntax of inserting a multiple records in a single query:

1. INSERT INTO table_name


2. ( column_name1, column_name2, .…, column_nameN)
3. VALUES (value_1, value_2, ..…, value_N), (value_1, value_2, ..…, value_N),….;

Example of inserting multiple records in a single query:


1. INSERT INTO Employee_details
2. ( Emp_ID, First_name, Last_name, Salary, City )
3. VALUES (101, Amit, Gupta, 50000, Mumbai), (101, John, Aggarwal, 45000, Calcut
ta), (101, Sidhu, Arora, 55000, Mumbai);

This example inserts the records of three employees in the Employee_details table in the
single query statement.

10. TRUNCATE TABLE Statement

This SQL statement deletes all the stored records from the table of the SQL database.

Syntax of TRUNCATE TABLE Statement:

1. TRUNCATE TABLE table_name;

Example of TRUNCATE TABLE Statement:

1. TRUNCATE TABLE Employee_details;

This example deletes the record of all employees from the Employee_details table of the
database.

11. DESCRIBE Statement

This SQL statement tells something about the specified table or view in the query.

Syntax of DESCRIBE Statement:

1. DESCRIBE table_name | view_name;

Example of DESCRIBE Statement:

1. DESCRIBE Employee_details;

This example explains the structure and other details about the Employee_details table.

12. DISTINCT Clause

This SQL statement shows the distinct values from the specified columns of the database
table. This statement is used with the SELECT keyword.

Syntax of DISTINCT Clause:

1. SELECT DISTINCT column_name1, column_name2, ...


2. FROM table_name;
Example of DISTINCT Clause:

1. SELECT DISTINCT City, Salary


2. FROM Employee_details;

This example shows the distinct values of the City and Salary column from
the Employee_details table.

13. COMMIT Statement

This SQL statement saves the changes permanently, which are done in the transaction of the
SQL database.

Syntax of COMMIT Statement:

1. COMMIT

Example of COMMIT Statement:

1. DELETE FROM Employee_details


2. WHERE salary = 30000;
3. COMMIT;

This example deletes the records of those employees whose Salary is 30000 and then saves
the changes permanently in the database.

14. ROLLBACK Statement

This SQL statement undo the transactions and operations which are not yet saved to the SQL
database.

Syntax of ROLLBACK Statement:

1. ROLLBACK

Example of ROLLBACK Statement:

1. DELETE FROM Employee_details


2. WHERE City = Mumbai;
3. ROLLBACK;

This example deletes the records of those employees whose City is Mumbai and then undo
the changes in the database.

15. CREATE INDEX Statement


This SQL statement creates the new index in the SQL database table.

Syntax of CREATE INDEX Statement:

1. CREATE INDEX index_name


2. ON table_name ( column_name1, column_name2, …, column_nameN );

Example of CREATE INDEX Statement:

1. CREATE INDEX idx_First_Name


2. ON employee_details (First_Name);

This example creates an index idx_First_Name on the First_Name column of


the Employee_details table.

16. DROP INDEX Statement

This SQL statement deletes the existing index of the SQL database table.

Syntax of DROP INDEX Statement:

1. DROP INDEX index_name;

Example of DROP INDEX Statement:

1. DROP INDEX idx_First_Name;

This example deletes the index idx_First_Name from the SQL database.

17. USE Statement

This SQL statement selects the existing SQL database. Before performing the operations on
the database table, you have to select the database from the multiple existing databases.

Syntax of USE Statement:

1. USE database_name;

Example of USE DATABASE Statement:

1. USE Company;

This example uses the company database.

You might also like