Database Management System
Database Management System
Database Management System
Data :- Raw facts and figures which are useful to an organization. We cannot take decisions
on the basis of data.
Information:-Well processed data is called information. We can take decisions on the basis
of information
Database
Database is a computer based record keeping system which is used to record, maintain and
retrieve data. It is an organized collection of interrelated (persistent) data.
Record: Collection of fields is called a record. A record can have fields of different data
types.
Primary Key: Primary key is a key that can uniquely identifies the records/tuples in a
relation. This key can never be duplicated and NULL.
Foreign Key: Foreign Key is a key that is defined as a primary key in some other relation.
This key is used to enforce referential integrity in RDBMS.
Candidate Key: Set of all attributes which can serve as a primary key in a relation.
Alternate Key: All the candidate keys other than the primary keys of a relation are
alternate keys for a relation
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:
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.
(I) Users issue a query using particular database language, for example, SQL commands.
(ii) The passes query is presented to a query optimizer, which uses information about how
the data is stored to produce an efficient execution plan for the evaluating the query.
(iii) The DBMS accepts the users SQL commands and analyses them.
(iv) The DBMS produces query evaluation plans, that is, the external schema for the user,
the corresponding external/conceptual mapping, the conceptual schema, the
conceptual/internal mapping, and the storage structure definition. Thus, an evaluation\ plan
is a blueprint for evaluating a query.
(v) The DBMS executes these plans against the physical database and returns the answers
to the user.
Using components such as transaction manager, buffer manager, and recovery manager,
the DBMS supports concurrency and recovery.
SQL is a non procedural language that is used to create, manipulate and process the
databases(relations).
Characteristics of SQL
SQL can be linked to most of other high level languages that makes it first choice for
the database programmers.
Just like any other programming language, the facility of defining data of various types is
available in SQL also. Following are the most common data types of SQL.
1. NUMBER
2. CHAR
3. VARCHAR / VARCHAR2
4. DATE
5. LONG
6. RAW/LONG RAW
1. NUMBER
Used to store a numeric value in a field/column. It may be decimal, integer or a real value.
General syntax is:
Number(n,d)
e.g marks number(3) declares marks to be of type number with maximum value 999.
pct number(5,2) declares pct to be of type number of 5 digits with two digits to the
right of decimal point.
2. CHAR
Char (size)
where size represents the maximum number of characters in a column. The CHAR type data
can hold at most 255 characters.
e.g name char(25) declares a data item name of type character of upto 25 size long.
3. VARCHAR/VARCHAR2
This data type is used to store variable length alphanumeric data. General syntax is,
varchar(size) / varchar2(size)
where size represents the maximum number of characters in a column. The maximum
allowed size in this data type is 2000 characters.
4. DATE
Date data type is used to store dates in columns. SQL supports the various date formats
other that the standard DD-MON-YY.
5. LONG
This data type is used to store variable length strings of upto 2 GB size.
e.g description long;
6. RAW/LONG RAW
To store binary data (images/pictures/animation/clips etc.) RAW or LONG RAW data type is
used. A column LONG RAW type can hold upto 2 GB of binary data.
CREATE
DROP
DDL ALTER
RENAME
TRUNCATE
SELECT
INSERT
DML
UPDATE
DELETE
GRANT
DCL
REVOKE
COMMIT
TCL
ROLLBACK
DDL contains commands that are used to create the tables, databases, indexes,
views, sequences and synonyms etc.
e.g: Create table, create view, create index, alter table, delete table
Constraints:
Constraints are the conditions that can be enforced on the attributes of a relation.
The constraints come in play when ever we try to insert, delete or update a record in
a relation.
1. NOT NULL
2. UNIQUE
3. PRIMARY KEY
4. FOREIGN KEY
5. CHECK
6. DEFAULT
Constraints:
1. NOT NULL: Ensures that we cannot leave a column as null. That is a value has to be
supplied for that column.
2. UNIQUE: Constraint means that the values under that column are always unique.
3. PRIMARY KEY: Constraint means that a column can not have duplicate values and
not even a null value.
The main difference between unique and primary key constraint is that a column
specified as unique may have null value but primary key constraint does not allow null
values in the column.
3. PRIMARY KEY: Constraint means that a column can not have duplicate values and
not even a null value.
The main difference between unique and primary key constraint is that a column
specified as unique may have null value but primary key constraint does not allow null
values in the column.
5. CHECK: Constraint limits the values that can be inserted into a column of a table.
Class varchar(10),
City varchar(25) );
In SQL we can create a view of the already existing table that contains specific attributes
of the table.
Suppose we need to create a view v_student that contains Roll_no,name and class of
student table, then Create View command can be used:
The above command create a virtual table (view) named v_student that has three
attributes as mentioned and all the rows under those attributes as in student table.
We can also create a view from an existing table based on some specific conditions, like
CREATE VIEW v_student AS SELECT Roll_no, Name, Class FROM student WHERE City
<>’Delhi’;
In SQL if we ever need to change the structure of the database then ALTER TABLE
command is used. By using this command we can add a column in the existing table,
delete a column from a table or modify columns in a table.
Adding a column
If we give command
Removing a column
Modify a column
Sometimes you may need to drop a table which is not in use. DROP TABLE command is
used to Delete / drop a table permanently. It should be kept in mind that we cannot
drop a table if it contains records. That is first all the rows of the table have to be
deleted and only then the table can be dropped.
RENAME
RENAME command is used to rename SQL table.
TRUNCATE
TRUNCATE operation is used to delete all table records.
DML contains command that can be used to manipulate the data base objects and to
query the databases for information retrieval.
e.g Select, Insert, Delete, Update.
INSERT Statement
The simplest way to insert a tuple into a table is to use the insert statement
insert into <table> [(<column i, . . . , column j>)] values (<value i, . . . ,
value j>);
NOTE:- In SQL we can repeat or re-execute the last command typed at SQL
prompt by typing “/” key and pressing enter.
SELECT Command
DISTINCT keyword is used to restrict the duplicate rows from the results of a
SELECT statement.
e.g. SELECT DISTINCT name FROM student;
UPDATE
UPDATE statement modifies records into the table.
DELETE
DELETE query removes entries from the table.
GRANT
GRANT command gives permissions to SQL user account.
For example, I want to grant all privileges to ‘explainjava’ database for user
‘dmytro@localhost’.
REVOKE
REVOKE statement is used to remove privileges from user accounts
TCL include commands to control the transactions in a data base system. The
commonly used commands in TCL are
COMMIT, ROLLBACK
COMMIT:
COMMIT is one of the transactional SQL command which is used to save changes.
For example, if you delete/update some records from a table, then this change will be
existing only on the current session. If you close your session, then it may not be saved
in the table/database. But, after delete/update transaction, if you save this operation
by COMMIT command, then it will be permanently saved in table/database.
Syntax:
COMMIT;
ROLLBACK:
ROLLBACK is one of the transactional SQL command which is used to rollback the
changes.
For example, if you delete/update some records from a table, then this change will be
existing only on the current session. If you close your session, then it may not be saved
in the table/database. But, after delete/update transaction, if you save this operation
by ROLLBACK command, then it will be permanently saved in table/database.
Syntax:
ROLLBACK;
Operators in SQL:
Arithmetic Operators +, -, *, /
Relational Operators are used when two values are to be compared and
Logical operators are used to connect search conditions in the WHERE Clause in
SQL.
ORDER BY Clause
ORDER BY clause is used to display the result of a query in a specific order(sorted order).
The sorting can be done in ascending or in descending order. It should be kept in mind that
the actual data in the database is not sorted but only the results of the query are displayed
in sorted order.
The above query returns name and city columns of table student sorted by name in
increasing/ascending order.
It displays all the records of table student ordered by city in descending order.
Note:- If order is not specifies that by default the sorting will be performed in
ascending order.
GROUP BY Clause
The GROUP BY clause can be used in a SELECT statement to collect data across multiple
records and group the results by one or more columns.
HAVING Clause
The HAVING clause is used in combination with the GROUP BY clause. It can be used in a
SELECT statement to filter the records that a GROUP BY returns.
Note: select statement can contain only those attribute which are already present in the
group by clause.
SQL provide large collection of inbuilt functions also called library functions that can be used
directly in SQL statements.
TYPES OF FUNCTION
2. String functions
Numeric Functions
Here are some examples of the use of some of these numeric functions:
select initcap ("now is the time for all good men to come to the aid of the party") as
"SLOGAN" from dual;
Symbol Explanation
Each 9 represents one digit in the
9
result
Represents a leading zero to be
0
displayed
Floating dollar sign printed to the
$
left of number
L Any local floating currency symbol
. Prints the decimal point
Prints the comma to represent
,
thousands
Group Functions
Sub Queries:
A Subquery or Inner query or a Nested query is a query within another SQL query and
A subquery is used to return data that will be used in the main query as a condition to
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
Eg:
SELECT
employee_id, first_name, last_name, salary
FROM
employees
WHERE
salary > (SELECT
AVG(salary)
FROM
employees);