Data Base Management System
Data Base Management System
UNIT-1
1 Introduction to DBMS
A database management system (DBMS) is system software for creating and managing
databases. The DBMS provides users and programmers with a systematic way to create,
retrieve, update and manage the database
What is Data?
In computing, data is information that has been translated into a form that is more convenient
to move or process. Relative to today's computers and transmission media, data is
information converted into binary digital form
Collection of Information is called Data
Data Mining
Easily and quickly retrieve the data from data warehouse. Data Mining is defined as
the procedure of extracting information from huge sets of data
Definition of Data
What is Information?
Example: The class average score is the information that can be concluded from the given
data.
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 simplest
form of data storage. Below is an example of Employee table.
1 ADAM 34 13000
2 ALEX 28 15000
3 STUART 20 18000
4 ROSS 42 19020
What is 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 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
represents names of employee
DBMS and RDBMS both are used to store information in physical database but they have
some differences, which is given below
DBMS RDBMS
In DBMS data are store as a file. In RDBMS data are store in tabular form.
In DBMS relationship between two tables In RDBMS relationship between two tables
or files are maintained programmatically or files can be specified at the time of table
creation.
DBMS does not support client server RDBMS supports client server architecture.
architecture.
In DBMS there are no security of data. In RDBMS there are multiple level of
security, at logging level, at command level,
at object level.
DBMS may satisfy less than 7 rules of RDBMS usually satisfy more than 7 to 8
Dr.E.F. codd rules of Dr. E.F. codd.
Examples of DBMS are file systems, xml Example of RDBMS are mysql, postgre, sql
etc. server, oracle etc.
Traditionally data was organized in file formats. DBMS was all new concepts then and all the
research was done to make it to overcome all the deficiencies in traditional style of data
management. Modern DBMS has the following characteristics:
1. Real-world entity: Modern DBMS are more realistic and uses real world entities to
design its architecture. It uses the behavior and attributes too. For example, a school
database may use student as entity and their age as their attribute.
2. Relation-based tables: DBMS allows entities and relations among them to form as
tables. This eases the concept of data saving. A user can understand the architecture of
database just by looking at table names etc.
3. Isolation of data and application: A database system is entirely different than its data.
Where database is said to active entity, data is said to be passive one on which the
database works and organizes. DBMS also stores metadata which is data about data,
to ease its own process.
4. Less redundancy: DBMS follows rules of normalization, which splits a relation when
any of its attributes is having redundancy in values. Following normalization, which
itself is a mathematically rich and scientific process, make the entire database to
contain as less redundancy as possible.
5. Consistency: DBMS always enjoy the state on consistency where the previous form of
data storing applications like file processing does not guarantee this. Consistency is a
state where every relation in database remains consistent. There exist methods and
techniques, which can detect attempt of leaving database in inconsistent state.
6. Query Language: DBMS is equipped with query language, which makes it more
efficient to retrieve and manipulate data. A user can apply as many and different
filtering options, as he or she wants. Traditionally it was not possible where file-
processing system was used.
7. ACID Properties: DBMS follows the concepts for ACID properties, which stands for
Atomicity, Consistency, Isolation and Durability. These concepts are applied on
transactions, which manipulate data in database. ACID properties maintains database
in healthy state in multi-transactional environment and in case of failure.
1. Better use of data or information - We can easily and efficiently access well-
managed and synchronized forms of data with the help of DBMS. It makes data
handling simple, provides an integrated perspective of how a certain business is
operating and also aids in keeping track of how one element of the business affects
another portion.
2. Secured Data - The likelihood of security problems increases as a database becomes
more functional and accessible. The danger to data security rises as a result of the rate
at which data is shared or transferred growing along with the user base. It is
frequently utilized in the business world where organizations spend a lot of time,
money, and effort making sure data is protected and handled effectively. Data
management systems (DBMS) offer a stronger framework for data privacy and
security policies, assisting businesses in enhancing data security.
3. Reduces Data Inconsistency and Redundancy - The major issues faced during the
process of storing data are inconsistency and redundancy. Inconsistent data may lead
to a big loss to an individual or a business model and the storage capacity is not
utilized properly because of the data redundancy. When multiple copies with different
versions or values of the same data exist in various locations, then it causes
inconsistency. Data Redundancy and inconsistency can both be significantly
decreased by properly designing a database with the help of a database management
system.
4. Better Recovery and Backups - Backup and recovery are handled automatically by
the DBMS. Users don't need to regularly back up their data because the DBMS
handles this for them. Additionally, it returns the database to its prior state following a
crash or system failure.
5. Fast Data Sharing - Database administration makes it possible for consumers to
access more and better-managed data. DBMS enables end users to quickly scan their
environment and react to any alterations made there.
6. Helps in decision-making - Because of the well-managed data and improved data
access provided by DBMS, we are able to produce better-quality information and, as a
result, make better judgments. Accuracy, validity, and the time it takes to read data
are all improved by better data quality. Although DBMS does not ensure data quality,
it does offer a framework that makes it simple to enhance data quality.
7. Increases Privacy - The privacy rule in a database specifies the privacy restrictions
that can only be accessed by authorized users. A user can only view the data he is
permitted to view since there are different degrees of database access. For instance, on
social networking sites, different accounts that a user wishes to access have varying
access restrictions and a user can only see his/her account details, not others.
8. User Friendly - Data are presented in a straightforward and logical manner by
database management systems (DBMS). It is simple to carry out many activities, such
as the addition, deletion, or creation of files or data.
9. Data Abstraction - In order to give users an abstract overview of the data, database
systems are primarily used. Since numerous intricate algorithms are employed by
developers to boost the effectiveness of databases that are concealed from users by
several degrees of data abstraction, consumers can easily engage with the system.
4. Applications of DBMS
DBMS performs multiple functions. Some of the main functions of DBMS include data
definition, data manipulation, data alteration, security and access control, recovery and
system., concurrency control, storage and data retrieval, user access, data integrity, and
performance optimization.
5. Purpose of Database System
The Database Management System (DBMS) is defined as a software system that allows the
user to define, create and maintain the database and provide control access to the data.
It is a collection of programs used for managing data and simultaneously it supports different
types of users to create, manage, retrieve, update and store information.
Purpose
The diagram given below explains the process as to how the transformation of data to
information to knowledge to action happens respectively in the DBMS −
7. Components of a DBMS
The DBMS accepts the SQL commands generated from a variety of user interfaces, produces
query evaluation plans, executes these plans against the database, and returns the answers. As
shown, the major software modules or components of DBMS are as follows:
(i) Query processor: The query processor transforms user queries into a series of low level
instructions. It is used to interpret the online user's query and convert it into an efficient series
of operations in a form capable of being sent to the run time data manager for execution. The
query processor uses the data dictionary to find the structure of the relevant portion of the
database and uses this information in modifying the query and preparing and optimal plan to
access the database.
(ii) Run time database manager: Run time database manager is the central software
component of the DBMS, which interfaces with user-submitted application programs and
queries. It handles database access at run time. It converts operations in user's queries
coming. Directly via the query processor or indirectly via an application program from the
user's logical view to a physical file system. It accepts queries and examines the external and
conceptual schemas to determine what conceptual records are required to satisfy the user’s
request. It enforces constraints to maintain the consistency and integrity of the data, as well as
its security. It also performs backing and recovery operations. Run time database manager is
sometimes referred to as the database control system and has the following components:
• Authorization control: The authorization control module checks the authorization of users
in terms of various privileges to users.
• Command processor: The command processor processes the queries passed by
authorization control module.
Integrity checker: It .checks the integrity constraints so that only valid data can be entered
into the database.
Query optimizer: The query optimizers determine an optimal strategy for the query
execution.
• Transaction manager: The transaction manager ensures that the transaction properties
should be maintained by the system.
• Scheduler: It provides an environment in which multiple users can work on same piece of
data at the same time in other words it supports concurrency.
(iii) Data Manager: The data manager is responsible for the actual handling of data in the
database. It provides recovery to the system which that system should be able to recover the
data after some failure. It includes Recovery manager and Buffer manager. The buffer
manager is responsible for the transfer of data between the main memory and secondary
storage (such as disk or tape). It is also referred as the cache manger.
8. Database Environment
One of the primary aims of a database is to supply users with an abstract view of data, hiding
a certain element of how data is stored and manipulated. Therefore, the starting point for the
design of a database should be an abstract and general description of the information needs of
the organization that is to be represented in the database. And hence you will require an
environment to store data and make it work as a database. In this chapter, you will learn
about the database environment and its architecture.
9. Functions of DBMS
One of the most important tasks for DBMS is to create a database for complex data and
manage the data. It gives relief to the user by creating a structure for the complex data sets so
that users can access it and manipulate them very easily. Modern database systems not only
provide storage for the data but they store and manage the metadata (data of data) like data
procedural rules, validation rules etc. DBMS also provides performance tuning, which makes
accessing data faster and easier.
Security Management
Security is another aspect which is handled by the Database Management Systems. Database
systems provide a high level of security measures using various security algorithms to keep
the data safe and ensure the data privacy. There are certain security rules that ensure what
data can be accessed from the database and which user can access it. It also makes sure what
operations (read, write, delete) can be performed on the specific data. It is very important for
the organizations where multi-user databases are required.
Backup and Recovery Management
To keep the data safe and ensure the integrity, the database system provides the features for
backup and recovery management. If the system fails due to some reason then it recovers the
data and keeps the data safe.
DBMS provides a database access language which is also called a query language. Query
languages are non-procedural languages used to access the database and manipulate the data.
SQL is an example of a query language. The majority of DBMS vendors provide the support
of various query languages to access the data.
DBMS provides the functionality of data transformation, which means programmers need not
worry about the logical and physical representation of the data. DBMS stores the data in the
determined data structure.
For example, if a user asks for the date from a database and he receives it as 14 December
2022, but in the database, it is stored in different columns of month, date and year.
Multi User Access control is another feature which is provided by the modern Database
Systems. So, more than one user can access the database at the same time without any
problem. This feature makes sure the integrity of the data present in the database. It also
follows the ACID property, so the database will be consistent while multiple users are
accessing it concurrently. It is very useful for the database of organizations where multiple
database engineers are working concurrently.
Database systems provide data integrity management by maximizing the data consistency and
minimizing the data redundancy. The data dictionary is the feature database system used to
store the relationships of the data to keep the data integrity. Data integrity is needed where a
transaction based database system is present.
When a user requests data from the database, it uses some environments like browsers
(Chrome or Firefox etc.) to get the data.
Key Responsibilities:
Schema Definition: DBAs define the structure of the database, including tables,
indexes, and relationships.
Security Management: They set up user accounts, manage access permissions, and
ensure that data is protected from unauthorised access.
Performance Monitoring: DBAs continuously monitor the database’s performance
and optimise it for speed and efficiency.
Backup and Recovery: Regular backups are crucial. DBAs ensure that data is
backed up and can be recovered in case of a failure.
Data Integrity: They make sure that the data remains accurate and consistent over its
lifecycle.
No DBMS Knowledge: They do not need to understand how the database works.
Use Pre-Developed Applications: Interaction with the database is through
applications with user-friendly interfaces.
Task-Oriented: Their primary focus is on performing specific tasks, like data entry
or retrieval.
Example: Consider front desk staff at a hotel using a reservation management system. They
input guest information, check room availability, and update bookings without needing to
understand how the database processes this data.
System Analysts
System Analysts come in the middle of end users and the technical support staff. They
understand the requirements of the users and translate them into technical needs for the
database designers and programmers.
Key Responsibilities:
Requirements Analysis: Understanding and documenting what the users need from
the database.
Feasibility Studies: Assessing whether the requirements can be met within the
constraints of time, budget, and technology.
Design Oversight: Ensuring that the database design meets the specified
requirements.
Example: A system analyst working on an online learning platform needs to compile a list of
requirements from the teachers and students. This includes but is not limited to managing the
course content, student enrollment, and the track progress among the learners.
Sophisticated Users
Sophisticated Users are those who have a deeper understanding of the DBMS. They can
directly interact with the database using complex queries and tools. These database users in
DBMS often include data analysts, scientists, and engineers who need detailed and flexible
access to the data.
Characteristics:
Example: Accountants could employ SQL to sort through the numerous records of a
particular company’s sales database and produce highly reliable and detailed financial
statements and projections. It enables them to pull out certain data and calculate and analyse
trends all from a database.
Database Designers
Database Designers are greatly involved in the creation of the basic structure of any DBMS.
They are responsible for the task of determining the structural framework of the database
compliance to various conditions and optimum execution. Their duties entail a lot of planning
and designing to ensure that the database meets the needs of the organisation.
Key Responsibilities:
Application Programmers
Application programmers, or Back-End Developers as they are often called, are the people
who sit down and write the actual code that interacts with the database. They develop
applications that enable the user to carry out different operations on the data and guarantee
that they are intuitive and effective.
Key Responsibilities:
Developing Applications: Writing code to build applications that interact with the
database.
Maintaining Code: Regularly updating and debugging applications to ensure they
run smoothly.
Optimising Performance: Ensuring that the applications are optimised for speed and
efficiency.
User Support: Providing support and updates to ensure that user needs are met
effectively.
Example: Suppose software developers have been asked to create a mobile banking
application. They have to provide a code through which the user can view his/her balance,
transfer money, or review his/her transactions. This means that the app should be fast, highly
secure, and convenient in its usage facets.
Characteristics:
Occasional Access: They access the database intermittently, often to gather specific
information.
User-Friendly Interfaces: They rely on interfaces that are easy to use and understand
without needing in-depth knowledge of the DBMS.
Specific Tasks: Their interaction is task-oriented, such as generating reports or
retrieving specific data.
Example: A marketing manager might be a casual user who accesses the database quarterly
to generate reports on sales performance and customer demographics.
Specialised Users
Specialised Users can be defined as the individual who needs databases to fulfil specific, and
frequently demanding purposes. These database users in DBMS are the ones that require
unconventional data processing and need specific applications to accomplish this task.
11. 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.
Logical data independence is used to separate the external level from the conceptual view.
If we do any changes in the conceptual view of the data, then the user view of the data
would not be affected.
Physical data independence can be defined as the capacity to change the internal
schema without having to change the conceptual schema.
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.
Physical data independence is used to separate conceptual levels from the internal
levels.
A Database stores a lot of critical information to access data quickly and securely. Hence it is
important to select the correct architecture for efficient data management. DBMS
Architecture helps users to get their requests done while connecting to the database. We
choose database architecture depending on several factors like the size of the database,
number of users, and relationships between the users. There are two types of database models
that we generally use, logical model and physical model. Several types of architecture are
there in the database which we will deal with in the next section.
Types of DBMS Architecture
There are several types of DBMS Architecture that we use according to the usage
requirements. Types of DBMS Architecture are discussed here.
1-Tier Architecture
2-Tier Architecture
3-Tier Architecture
1-Tier Architecture
In 1-Tier Architecture the database is directly available to the user, the user can directly sit on
the DBMS and use it that is, the client, server, and Database are all present on the same
machine. For Example: to learn SQL we set up an SQL server and the database on the local
system. This enables us to directly interact with the relational database and execute
operations. The industry won’t use this architecture they logically go for 2-tier and 3-tier
Architecture.
A database is a storehouse for a large amount of data, which is used in organizations and
firms. A database management system is a program that helps in updating, accessing,
retrieving, representing, and manipulating the data in a database system.
A database administrator carries out several activities and operations to ensure the integrity,
security, and availability of the data stored in the database.
1. Database backup:
A database administrator has the responsibility to back up every data in the database,
recurrently. This is necessary, so that operations can be restored in times of disaster or
downtime.
2. Database availability:
A database administrator has the responsibility of ensuring database accessibility to
users from time to time.
3. Database restore:
A database administrator has the responsibility of restoring a file from a backup state,
when there is a need for it.
4. Database design:
A database administrator has the responsibility of designing a database that meets the
demands of users. Hence, having knowledge of database design is crucial for an
administrator.
5. Data move:
A database administrator has the responsibility of moving a database set, say from a
physical base to a cloud base, or from an existing application to a new application.
6. Database upgrade:
A database administrator has the responsibility of upgrading database software files
when there is a new update for them, as this protects software from security breaches.
7. Database patch:
In times of new upgrades for database software, the database administrator has the
responsibility of ensuring that the database system functions perfectly and works to
close up any gaps in the new update.
8. Database security:
Datasets are assets, and one major responsibility of database administrators is to
protect the data and ensure adequate security in an organization’s database.
9. Capacity planning:
A database administrator has the responsibility of planning for increased capacity, in
case of sudden growth in database need.
10. Database monitoring:
A database administrator has the responsibility of monitoring the database and the
movement of data in the database. Administrators provide access for users who
require access to the database.
11. Error log review:
A database administrator has the responsibility of interpreting the error messages sent
by a database when there is a fault or bridge.
The relational model for database management is an approach to logically represent and
manage the data stored in a database. In this model, the data is organized into a collection of
two-dimensional inter-related tables, also known as relations. Each relation is a collection
of columns and rows, where the column represents the attributes of an entity and the rows (or
tuples) represent the records.
The use of tables to store the data provided a straightforward, efficient, and flexible way to
store and access structured information. Because of this simplicity, this data model provides
easy data sorting and data access. Hence, it is used widely around the world for data storage
and processing.
Consider a case where you wish to store the name, the CGPA attained, and the roll number of
all the students of a particular class. This structured data can be easily stored in a table as
described below:
As we can notice from the above relation:
Any given row of the relation indicates a student i.e., the row of the table describes a
real-world entity.
The columns of the table indicate the attributes related to the entity. In this case, the
roll number, CGPA, and the name of the student.
As discussed earlier, a relational database is based on the relational model. This database
consists of various components based on the relational model. These include:
For our Student relation example, the relational schema will be:
STUDENT(ROLL_NUMBER INTEGER, NAME VARCHAR(20), CGPA FLOAT)
The terms entity, attribute, table, primary key, foreign key, relational database, query, index,
normalization, ACID properties, data warehousing, data mining, backup and recovery, data
migration, and replication were all covered in detail in this article.
INTRODUCTION TO SQL
DATABASE LANGUAGES:
DDL is used to create on object (table) alter the structure of an object and also to drop the
object created, the concepts relating to Data Definition Language.
Table
A table is a unit of storage that holds data in the form of Rows and Columns.
Syntax
Alter Table
Two types
Modify
Add
Syntax:
Alter table <table name> Modify (column definition….);
Alter table <table name> Add (column definition….);
When the user wants to add a new column.
To change the width of a data type or the data type itself.
To include or drop integrity constraints.
Truncate Table
Truncate means only deleted the particular records in the table but table structure
cannot deleted
We have Reuse the table structure.
Truncate command deletes all rows from the specified table
Truncate table employee reuse storage the below command deletes all rows from the
table employee and reuse any space that was occupied by the table.
Syntax
DROP Command
We have used for Drop command the table deleted permanently in the data base we
cannot Reuse the table
1. Insert
2. Select
3. Update
4. Delete
Insert command
Syntax:
INSERT ALL
SELECT COMMAND
To represent the selection of duplicate Rows otherwise to avoid the duplicate values
Update command
The update command we can update rows in the table. A single column may be updated
or more than one column could be updated specific rows could be updated based on a specific
condition.
The update command consists of a ‘set’ clause and an optional –‘where’ clause
Where vencode=’V002’ ;
Delete command
1.commit
2. save point
3. Roll back
Commit
Commit work;
Commit
Savepoint
Save points are like markers to divide a very lengthy transaction to smaller ones.
Roll back
A roll back is used to undo the work done in the current transaction.
Rollback work
Roll back;
DCL
The user they will provide grand privileges to the other user
1) Avg ( )
2) Max ( )
3) Min ( )
4) Sum ( )
5) Count ( )
A group functions returns a result based on a group of rows this function purely mathematical
function. Expressions that are not encapsulated within an aggregate function and must be
included in the GROUP BY Clause at the end of the SQL statement. This is an
aggregate function such as the SUM, COUNT, MIN, MAX, or AVG functions. This is the
column or expression that the aggregate function will be used on Optional.
1. AVG() Function
The Avg() function will return the average of values of the column specified in the
argument of the column.
Select Avg (salary) from Employee;
Using Where clause
SQL >Select Avg (salary) from employee where salary >6000;
2. Max() Function
To perform an operation which gives the maximum of a set of values the max function
can be made use of….
3. Min() Function
This function will give the least of all values of the column present in the argument.
SQL >Select Min(Midlevel) from itemfile where P_categery =’Spares’;
4. Sum() Function
The sum function can be used to obtain the sum of a range of values of a record set
5. Count Function
In order to count the number of rows count function is used, count(*) it counts all
Records inclusive of duplicates and Nulls.
This query will eliminate the duplicate and null values in the column and provide the
result.
P_category Max(itemrate)
Accessories 4000
Spares 2000
HAVING CLAUSE
The having clause is used to specify certain conditions on rows retrieved by using group
by clause this clause should be preceded by group by clause
Group By
ORDERBY [Asce][Desc]
Clause sorts or orders the results based on the data in one or more columns in the
ascending or descending Order.
Set operators combine the results of two queries into a single one.
1. Union
2. Union all
3. Inter sect
4. Minus
1. The queries which one related by a set operators should have the same number of
columns and the corresponding columns must be of the same data type.
2. Such a query should not contain any column of type long.
3. The label under which the rows are displayed are those from the first select statement
Union
Query
Result
001
002
003
004
006
015
Union All
The Union all operator returns all rows selected by either query including duplicates.
SQL>Select orderno from order_master UNION ALL select orderno from order_detail;
Result
001
002
003
006
001
001
001
002
Intersect
Intersect operator returns only rows that are common to both the queries
SQL> select orderno from order_master intersect select orderno from order_detail;
Minus
Minus operator returns all distinct rows selected only by the first query and not by the second
SQL> Select orderno from order_master minus select orderno from order_detail;
1. The sub query Returns Multiple rows where as it is a single row sub query
This select statement is single row subqueries returns multiple rows where as it is a single
row subquery
Note
Comparison Operator
Any
All
In
Not in
Between
Example
Subquery or Inner query or Nested query is a query in a query. SQL subquery is usually
added in the WHERE Clause of the SQL statement. Most of the time, a subquery is used
when you know how to search for a value using a SELECT statement, but do not know the
exact value in the database. Subqueries are an alternate way of returning data from multiple
tables. Subqueries can be used with the following SQL statements along with the comparision
operators like =, <, >, >=, <= etc.
1. SELECT
2. INSERT
3. UPDATE
4. DELETE
1) Usually, a subquery should return only one record, but sometimes it can also return
multiple records when used with operators LIKE IN, NOT IN in the where clause. The query
syntax would be like,
FROM student_details
FROM student_details
but, if you do not know their names, then to get their id's you need to write the query in this
manner,
FROM student_details
FROM student_details
Subquery Output:
Id first_name
-------- -------------
100 Rahul
102 Stephen
In the above sql statement, first the inner query is processed first and then the outer query is
processed.
3) Subquery can be used with INSERT statement to add rows of data from one or more tables
to another table. Lets try to group all the students who study Maths in a table 'maths_group'.
4) A subquery can be used in the SELECT statement as follows. Lets use the product and
order_items table defined in the sql_joins section.
Correlated Subquery
A query is called correlated subquery when both the inner query and the outer query are
interdependent. For every row processed by the inner query, the outer query is processed as
well. The inner query depends on the outer query before it can be processed.
SELECT p.product_name FROM product p
1) You can nest as many queries you want but it is recommended not to nest more than 16
subqueries in oracle.
Non-Corelated Subquery
Subquery Errors
3) Minimize subquery errors: Use drag and drop, copy and paste to avoid running subqueries
with spelling and database types. Watch your multiple field SELECT comma use, extra or to
few getting SQL error message "Incorrect syntax".
Adding SQL Sub query comments are good habit (/* your command comment */) which can
save you time, clarify your previous work results in less SQL headaches.
Like clause is used as condition in SQL query. Like clause compares data with an expression
using wildcard operators. It is used to find similar data from the table.
There are two wildcard operators that are used in like clause.
101 Adam 15
102 Alex 18
103 Abhi 17
The above query will return all records where s_name starts with character 'A'.
101 Adam 15
102 Alex 18
103 Abhi 17
Example
101 Adam 15
Example
The above query will return all records from Student table where s_name contain 'x' as last
character.
102 Alex 18
Order by clause is used with Select statement for arranging retrieved data in sorted order.
The Order by clause by default sort data in ascending order. To sort data in descending
order DESC keyword is used with Order by clause.
Syntax of Order By
The above query will return result in ascending order of the salary.
The above query will return result in descending order of the salary.
Group by clause 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.
FROM table_name
WHERE condition
GROUP BY column_name
Here we want to find name and age of employees grouped by their salaries
Name Age
Rohan 34
Shane 29
Anu 22
from Emp
Result will be
Name Salary
Rohan 6000
Shane 8000
Scott 9000
You must remember that Group By clause will always come at the end, just like the Order by
clause.
Having clause is used with SQL Queries to give more precise condition for a statement. It is
used to mention condition in Group based SQL functions, just like WHERE clause.
FROM table_name
GROUP BY column_name
Suppose we want to find the customer whose previous_balance sum is more than 3000.
SELECT *
AND and OR operators are used with Where clause to make more precise conditions for
fetching data from database by combining more than one condition together.
AND operator
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.
OR operator
OR operator is also used to combine multiple conditions with Where clause. The only
difference between AND and OR is their behaviour. When we use AND to combine two or
more than two conditions, records satisfying all the condition will be in the result. But in case
of OR, atleast one condition from the conditions specified must be satisfied by any record to
be in the result.
Example of OR
A view in SQL is a logical subset of data from one or more tables. View is used to restrict
data access.
SELECT column_name(s)
FROM table_name
WHERE condition
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 separately and replace too but using both together works better.
Syntax of displaying a view is similar to fetching data from table using Select statement.
Force keyword is used while creating a view. This keyword force to create 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.
SELECT column_name(s)
FROM table_name
WHERE condition
Update a View
UPDATE view-name
set value
WHERE condition;
Read-Only View
We can create a view with read-only option to restrict access to the view.
SELECT column_name(s)
FROM table_name
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
1. Simple View
2. Complex View
Keys
This ensures the accuracy and reliability of the data in the table. It is used for identifying
unique rows from table. Data should not Repeaters as in the table. A key is an attribute or a
set of attributes in a relation that identifies a tuple in a relation. The keys are defined in a
table to access or sequence the stored data quickly and smoothly. They are also used to create
relationship between different tables.
Types of Keys
1. Unique key
2. Primary key
3. Foreign key
4. Super key
5. Candidate key
7. Alternate key
1.Unique key
The unique key uniquely identifies each record in a database table, the data should not
Repeaters.
2.Primary key
The primary key uniquely identifies each record in a database table; primary key must
contain unique values and cannot contain NULL values.
Primary key can`t accept null values Unique key can accept only one null
values
We can have only one primary in the We can have more than one unique key in
table the table
Primary key can be made foreign key into In SQL server unique key can be made
another table. foreign key into another table.
3.Foreign keys
Foreign keys are the columns of a table that points to the primary key of another table. They
act as a cross-reference between tables.
4.Super key
A super key is a set or one of more columns (attributes) to uniquely identify rows in a table.
Often people get confused between super key and candidate key, so we will also discuss a
little about candidate key here.
5.Candidate key
A candidate key is a column, or set of columns, in a table that can uniquely identify any
database record without referring to any other data. Each table may have one or
more candidate keys, but one candidate key is special, and it is called the primary key.
6.Composite key
A composite key, in the context of relational databases, is a combination of two or more
columns in a table that can be used to uniquely identify each row in the table. Uniqueness is
only guaranteed when the columns are combined; when taken individually the columns do
not guarantee uniqueness.
7.Alternate key
An alternate key is a key associated with one or more columns whose values uniquely
identify every row in the table, but which is not the primary key. For example, where the
primary key for a table may be the employee id, the alternate key might combine the first,
middle, and last names of the employee.
23. Specifying Foreign Key Constraints in SQL
Foreign Key
The foreign key a constraint is a column or list of columns that points to the primary
key column of another table
The main purpose of the foreign key is only those values are allowed in the present
table that will match the primary key column of another table.
Reference Table
Child Table
Integrity constraints are the set of predefined rules that are used to maintain the quality of
information. Integrity constraints ensure that the data insertion, data updating, data deleting
and other processes have to be performed in such a way that the data integrity is not
affected. They act as guidelines ensuring that data in the database remain accurate and
consistent. So, integrity constraints are used to protect databases. The various types of
integrity constraints are
Types of Integrity Constraints:
Domain Constraints
Not-Null Constraints
Entity integrity Constraints
Key Constraints
Primary Key Constrains
Referential integrity constraints
Domain Constraints
These are defined as the definition of valid set of values for an attribute. The data type of
domain include string, char, time, integer, date, currency etc. The value of the attribute
must be available in comparable domains.
Example:
Not-Null Constraints
It specifies that within a tuple, attributes overs which not-null constraint is specified must
not contain any null value.
Example:
Let, the not-null constraint be specified on the “Semester” attribute in the relation/table
given below, then the data entry of 4th tuple will violate this integrity constraint, because
the “Semester” attribute in this tuple contains null value. To make this database instance a
legal instance, its entry must not be allowed by database management system.
21CSE103 Mukesh 20
Mukesh 5th 20
Key Constraints
Keys are the entity set that are used to identify an entity within its entity set uniquely. An
entity set can contain multiple keys, bit out of them one key will be primary key. A primary
key is always unique, it does not contain any null value in table.
Example: