Database Management System UNIT-1
Database Management System UNIT-1
UNIT-1
• Data is something which can be recorded like name, mobile
no, customer-id, stsudent_id etc. ( Data is a real world
entity)
• Information is something which we extract by
manipulating / processing the stored data.
Example:
1. Design.
2. Development
3. Maintenance and
4. User friendly deployment of such systems.
• There are several DBMS , at times we say relational DBMS
like ORACLE, MS-SQL.
Slide No:L1-2
Database System Applications
• DBMS contains information about a particular enterprise
– Collection of interrelated data
– Set of programs to access the data
– An environment that is both convenient and efficient to use
• Database Applications:
– Banking: all transactions
– Airlines: reservations, schedules
– Universities: registration, grades
– Sales: customers, products, purchases
– Online retailers: order tracking, customized
recommendations
– Manufacturing: production, inventory, orders, supply chain
– Human resources: employee records, salaries, tax
deductions
• Databases touch all aspects of our lives
Slide No:L1-1
Why Use a DBMS?
Slide No:L1-3
Purpose of Database Systems
• In the early days, database applications were built
directly on top of file systems
• Drawbacks of using file systems to store data:
– Data redundancy and inconsistency
• Multiple file formats, duplication of information in
different files
– Difficulty in accessing data
• Need to write a new program to carry out each
new task
– Data isolation — multiple files and formats
– Integrity problems
• Integrity constraints (e.g. account balance > 0)
become “buried” in program code rather than
being stated explicitly
• Hard to add new constraints or change existing
ones
Slide No:L1-6
Purpose of Database Systems (Cont.)
Slide No:L1-7
Levels of Abstraction
Slide No:L1-8
View of Data
Slide No:L2-1
Instances and Schemas
Slide No:L2-2
Data Independence
Slide No:L2-3
Logical data independence- is the ability to modify the logical
schema without causing application program (in view label) to be
rewritten.
For example: consider two users A & B. Both are selecting the fields
"EmployeeNumber" and "EmployeeName". If user B adds a new
column (e.g. salary) to his table, it will not effect the external view
for user A, though the internal schema of the database has been
changed for both users A & B.
Data Models
1.Hierarchical model
2.Netwk model
3.Relational Model
4. Object -oriented model
5.Enity-relationship model
Slide No:L2-4
Hierarical Model
This model organizes data into a tree-like structure, where each record has a
single parent or root. Sibling records are sorted in a particular order. It allows
one-to-many relationship. This model was primarily used by IBM systems ,but
they are rarely seen today due to certain operational inefficiencies.
Network Model
The network model builds on the hierarccal model by allowing many-
to-one and one-to-many relationships between linked records,
implying multiple parent records.
Based on the mathematical set theory, the model is constructed with
sets of related records. Each set consists of one owner or parent record
and one or more child records.
Object-oriented model
Object-oriented Data Model is based upon real world situations.
These situations are represented as objects, with different attributes.
This model defines a database as a collection of objects or reusable
software elements. There are several kind of object-oriented databases
like.
Slide No:L3-4
Entity - Relationship model
Slide No:L2-6
DATA BASE LANGUAGE
Slide No:L3-1
SQL
Data Definition Language (DDL)
• Specification notation for defining the database schema
Example: create table account (
account_number char(10),
branch_name char(10),
balance integer)
• DDL compiler generates a set of tables stored in a data
dictionary
• Data dictionary contains metadata (i.e., data about data)
– Database schema
– Data storage and definition language
• Specifies the storage structure and access methods
used
– Integrity constraints
• Domain constraints
• Referential integrity (e.g. branch_name must
correspond to a valid branch in the branch table)
– Authorization
Slide No:L3-2
Structured Query Language(SQL)
• SQL: widely used non-procedural language
– Example: Find the name of the customer with
customer-id 192-83-7465
select customer.customer_name
from customer
where customer.customer_id = ‘192-83-7465’
– Example: Find the balances of all accounts held by
the customer with customer-id 192-83-7465
select account.balance
from depositor, account
where depositor.customer_id = ‘192-83-7465’
and
depositor.account_number =
account.account_number
Slide No:L3-5
SQL
Slide No:L3-6
SQL Constraints
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT
• INDEX
NOT NULL Constraint
• The DEFAULT constraint can also be used to insert system values, by using
functions like GETDATE()
Example:
CREATE TABLE Orders ( ID int NOT NULL, OrderNumber int NOT NULL,
OrderDate date DEFAULT GETDATE() );
Data Types in SQL
• CHARACTER [(length)] or CHAR [(length)]
• VARCHAR (length)
• BOOLEAN
• SMALLINT
• INTEGER or INT
• DECIMAL [(p[,s])] or DEC [(p[,s])]
• NUMERIC [(p[,s])]
• REAL
• FLOAT(p)
• DOUBLE PRECISION
• DATE
• TIME ----------------------(HH:MM:SS)
• TIMESTAMP-----------------(YYYY-MM-DD HH:MM:SS)
Operator in SQL
An operator is a reserved word or a character used primarily in an SQL
statement's WHERE clause to perform operation(s), such as
comparisons and arithmetic operations. These Operators are used to
specify conditions in an SQL statement and to serve as conjunctions for
multiple conditions in a statement.
• Roles of DBA:
– Storage structure and access method definition
– Schema and physical organization modification
– Granting users authority to access the database
– Backing up data
– Monitoring performance and responding to
changes
Slide No:L4-2
Data storage and Querying
• Storage management
• Query processing
• Transaction processing
Slide No:L5-1
Storage Management
Slide No:L5-2
Query Processing
Slide No:L5-3
Query Processing (Cont.)
Slide No:L5-4
Transaction Management
Slide No:L5-5
Database Architecture
Slide No:L6-1
In 1-tier architecture, the DBMS is the only entity where the user
directly sits on the DBMS and uses it.
• Any changes done here will directly be done on the DBMS itself. It
does not provide handy tools for end-users.
• Database designers and programmers normally prefer to use single-
tier architecture.
• End user has no idea about the existence of the database beyond
the application server.
• The database also has no idea about any other user beyond the
application.
(web
browser)
Old Modern
Slide No:L6-3
Database Design
•Database is absolutely an integral part of software system.
•Database engineering guarantee you to produce high quality database
design to use in database creation, management and maintenance.
•A Database designer design the conceptual model of the database first
before he/she implement it.
•An ERD contains different symbols and connectors that visualize two
important information:
Database creation and patching - ERD tool like Visual Para supports database
generation tool that can automate the database creation and patching
process by means of ER diagrams.
▪ Generalization
▪ Specialization
▪ Aggregration
ISA relationship
Generalization.
• Integrity Constraints
• Relational Algebra
• Relational Calculus
Relational data model is the primary data model, which is used
widely around the world for data storage and processing. This
model is simple and it has all the properties and capabilities
required to process data with storage efficiency.
Concepts
• Key constraints
• Domain constraints
Syntax:
DROP VIEW view_name;
For example, if we want to update the view MarksView and add the field AGE
to this View from StudentMarks Table, we can do this as:
Syntax:
INSERT view_name(column1, column2 , column3,..)
VALUES(value1, value2, value3..);
INSERT INTO DetailsView ( NAME, ADDRESS) VALUES("Suresh","Gurgaon");
• Project (∏) - This is a unary operator and is similar to select operation above. It
creates the subset of relation based on the conditions specified. Here, it selects
only selected columns/attributes from the relation- vertical subset of relation.. It
is denoted as below: ∏a1, a2, a3 (r)
• Where ∏ is the operator for projection, r is the relation and a1, a2, a3 are the
attributes of the relations which will be shown in the resultant subset.
• ∏std_name, address, course (STUDENT) - This will select all the records from STUDENT table
but only selected columns – std_name, address and course.
• ∏STD_ID, address, course (σ STD_NAME = “James”(STUDENT)) - this selects the record for ‘James’
and displays only std_ID, address and his course columns. Here we can see two
unary operators are combined, and it has two operations performing.
Rename (ρ)
• Rename (ρ) - This is a unary operator used to rename the tables and
columns of a relation.
• Let us see another example to rename the columns of the table. If the
STUDENT table has ID, NAME and ADDRESS columns and if they have to
be renamed to STD_ID, STD_NAME, STD_ADDRESS, then we have to write
as follows.
• ρ STD_ID, STD_NAME, STD_ADDRESS(STUDENT) – It will rename the columns in the order
the names appear in the table
Union (U)
• Union (U) - It is a binary operator, which combines the tuples of two
relations. It is denoted by R U S: Where R and S are the relations and U is
the operator.
• It is different from Cartesian product in:
• Union combines the tuples of two relations into one relation. Cartesian
product combines the attributes of two relations into one relation
whereas
• In Union, both relations should have same number of columns. Cartesian
product does not concentrate on number of attribute or rows. It blindly
combines the attributes.
• In Union, both relations should have same types of attributes in same
order. It need not have same number of tuples in both the relation. If
there is a duplicate tuples as a result of union, then it keeps only one
tuple. If a tuple is present in any one relation, then it keeps that tuple in
the new relation.
Cartesian product (X):
• Cartesian product (X): - This is a binary operator. It combines the tuples of
two relations into one relation.
• RXS: Where R and S are two relations and X is the operator. If relation R
has m tuples and relation S has n tuples, then the resultant relation will
have mn tuples.
• Example, if we perform Cartesian product on EMPLOYEE (5 tuples) and
DEPT relations (3 tuples), then we will have new tuple with 15 tuples.
• This set intersection
can also be written
as a combination of
set difference
operations.
R ∩ S R-(R-S)
Assignment
• Assignment - As the name indicates, the assignment operator ‘←’ is used
to assign the result of a relational operation to temporary relational
variable.
T ←S – denotes relation S is assigned to temporary relation T
A relational operation ∏a1, a2 (σ p (E)) with selection and projection can be
divided as below.
α a α a 1 a 1
α a γ a 1 b 1
α a γ b 1 s
β a γ a 1
β a γ b 3
γ a γ a 1
γ a γ b 1
γ a β b 1
r
e.g.
r ÷ s: Students who have taken both "a”
A B C and “b” courses, with instructor
“1”
α a γ
(Find students who have taken all
γ a γ courses given by instructor 1)
Join
• The join operation is one of the most useful operations in relational
algebra and the most commonly used way to combine information from
two or more relations.
• A join can be defined as a cross product followed by selection and
projections. Joins arise much more frequently in practice than plain cross
product.
• Join is of two types: INNER JOIN , OUTERJOIN.
• Theta Join, Equijoin, and Natural Join are called inner joins.
1.
EQUI-JOIN:
A common special case of join operation is when the join
condition is consists of equalities of the form R.x=S.x .That is
equalities between two fields in R and S.
2.
ACCOUNT NUMBER BRANCH NAME BALANCE LOAN NUMBER BRANCH NAME AMOUNT
Queries-2: Find the loan number for each loan of an amount greater or equal
to 10000.
ANS: {t| ∃ s ∈ loan(t[loan number] = s[loan number] ∧
s[amount]>=10000)}
LOAN NUMBER
L33
L35
L98
Queries-3: Find the names of all customers who have a loan and an account
at the bank.
Saurabh
Mehak
CUSTOMER NAME
Saurabh
More examples
Syntax of TRC Query: (atomic formula)
Let ‘op’ is an operator in the set {<,>,=,<=,>=,≠} and let X and Y be tuple
variables , a and b are the attributes of X and Y respectively.. Then an
Atomic formula in TRC is one of the following.
• (a1, a2, a3, … an) ϵ R, where R is a relation with n attributes; each ‘ai ’
1<=i<=n is either a constant or a variable.
• X.a op Y.b
• X op constant, or constant op X
Example:
Domain Relational Calculus
• In contrast to tuple relational calculus, domain relational calculus used to
select a list of attributes from the relation based on the condition. It is
same as TRC, but differs by selecting the attributes rather than selecting
whole tuples.
• It uses domain variable. A domain variable is a variable that ranges over
the values in the domain of some attributes.
• A DRC query is of the form {< a1, a2, a3, … an > | P(a1, a2, a3, … an)}
• The results of the query is set of all tuples (a1, a2, a3, … an) for which the
formula evaluates to true.
Syntax of DRC Query: (atomic formula)
Let ‘op’ is an operator in the set {<,>,=,<=,>=,≠} and let X and Y be domain
variables. Then an Atomic formula in DRC is one of the following.
• (a1, a2, a3, … an) ϵ R, where R is a relation with n attributes; each ‘ai ’
1<=i<=n is either a constant or a variable.
• X op Y
• X op constant, or constant op X
Select:
• it is used to query the database.
• Comes alone or sometimes with where clause.
• where clause is used when the retrieval is specific.
Select tuples/columns from table name where expression;
EXISTS Syntax
SELECT column_name(s) FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition)
;
SELECT SupplierName FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.Suppli
erID = Suppliers.supplierID AND Price < 20);
The SQL ANY and ALL Operators
• The ANY and ALL operators are used with a WHERE or HAVING
clause.
• The ANY operator returns true if any of the subquery values meet
the condition.
• The ALL operator returns true if all of the subquery values meet the
condition.
ANY SYNTAX:
SELECT column_name(s) FROM table_name WHERE column_name ANY
(SELECT column_name FROM table_name WHERE condition);
ALL SYNTAX :
SELECT column_name(s) FROM table_name WHERE column_name ALL
(SELECT column_name FROM table_name WHERE condition);
SELECT ProductName FROM Products WHERE ProductID
= ANY (SELECT ProductID FROM OrderDetails WHERE Quantity = 10);
101 y Ut brazil
102 g TU brazil
103 t Da canara
104 y Bb canara
105 u Um india
106 z Mu india
Q: Show all orders, sorted by total amount, the largest first, within each year
Count Country
13 USA
11 France
11 Germany
9 Brazil
7 UK
Q: List fname,lname and the total amount ordered for each customer ?
1) COUNT()
2) SUM()
3) AVG()
4) MIN()
5) MAX ()
• COUNT():The COUNT function returns the total number of values in the
specified field. It works on both numeric and non-numeric data types. All
aggregate functions by default exclude nulls values before working on the
data.
• COUNT (*) is a special implementation of the COUNT function that returns the
count of all the rows in a specified table. COUNT (*) also considers Nulls and
duplicates.
• MIN ():The MIN function returns the smallest value in the specified table field.
• MAX (): It returns the largest value from the specified table field.
• SUM ():It returns the sum of all the values in the specified column. SUM
works on numeric fields only. Null values are excluded from the result
returned.
SELECT SUM(column_name) FROM table_name WHERE condition;
• AVG(): It returns the average of the values in a specified column. it works only
on numeric data types.
Nested Queries in SQL
In nested queries, a query is written inside a query. The result of inner query
is used in execution of outer query. We will use STUDENT, COURSE,
STUDENT_COURSEtables for understanding nested queries.
STUDENT
COURSE STUDENT_COURSE
C_ID C_NAME S_ID C_ID
C1 DSA S1 C1
C2 Programming S1 C3
C3 DBMS S2 C1
S3 C2
S4 C2
S4 C3
There are mainly two types of nested queries:
Select S_ID from STUDENT_COURSE where C_ID IN
(SELECT C_ID from COURSE where C_NAME = ‘DSA’ or C_NAME=’DBMS’);
The inner query will return a set with members C1 and C3 and outer query
will return those S_IDs for which C_ID is equal to any member of set (C1
and C3 in this case). So, it will return S1, S2 and S4.
Note: If we want to find out names of STUDENTs who have either enrolled in
‘DSA’ or ‘DBMS’, it can be done as:
Select S_NAME from STUDENT where S_ID IN
(Select S_ID from STUDENT_COURSE where C_ID IN
(SELECT C_ID from COURSE where C_NAME=’DSA’ or C_NAME=’DBMS’));
NOT IN: If we want to find out S_IDs of STUDENTs who have neither
enrolled in ‘DSA’ nor in ‘DBMS’, it can be done as:
Select S_ID from STUDENT where S_ID NOT IN
(Select S_ID from STUDENT_COURSE where C_ID IN
(SELECT C_ID from COURSE where C_NAME=’DSA’ or C_NAME=’DBMS’));
• The innermost query will return a set with members C1 and C3. Second
inner query will return those S_IDs for which C_ID is equal to any member
of set (C1 and C3 in this case) which are S1, S2 and S4. The outermost
query will return those S_IDs where S_ID is not a member of set (S1, S2
and S4). So it will return S3.
Co-related Nested Queries:
SQL JOIN
• EQUI JOIN
• NON-EQUIJOIN
• INNER JOIN
• NATURAL JOIN
• CROSS JOIN
• OUTER JOIN
– LEFT OUTER JOIN
– RIGHT OUTER JOIN
– FULL OUTER JOIN
EQUI JOIN
SQL EQUI JOIN performs a JOIN against equality or matching column(s) values of the
associated tables. An equal sign (=) is used as comparison operator in the where
clause to refer equality.
SYNTAX:
SELECT column_list FROM table1, table2 WHERE table1.column_name = table2.column_name;
OR
SELECT * FROM table1 JOIN table2 ON (join_condition);
Non Equi join
• The SQL NON EQUI JOIN uses comparison operator instead of the equal
sign like >, <, >=, <= along with conditions.
Syntax:
SELECT * FROM table1 FULL [OUTER] JOIN table2 ON table1.cname=table2.cname;
PL/SQL
• The PL/SQL programming language was developed by Oracle Corporation
in the late 1980s as procedural extension language for SQL and the Oracle
relational database.
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
PL/SQL -BLOCKS/SUB BLOCKS
PL/SQL-ARRAYS
PL/SQL-PROCEDURES
PL/SQL-FUNCTIONS
PL/SQL-CURSORS
PL/SQL-RECORDS
PL/SQL-TRIGGERS
PL/SQL- PACKAGES
PL/SQL-COLLECTIONS
PL/SQL-TRANSACTIONS
PL/SQL-OBJECT ORIENTED
Trigger
Triggers are stored programs, which are automatically executed or fired
when some events occur.
Triggers are, in fact, written to be executed in response to any of the
following events −
• Auditing.
• When the above code is executed at the SQL prompt, the output
will be −
O/P: Trigger created.
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (7, 'Kriti', 22,
'HP', 7500.00 );
When a record is created in the CUSTOMERS table, the above create
trigger, display_salary_changes will be fired and it will display the following result .
Old salary:
New salary: 7500
Salary difference:
Because this is a new record, old salary is not available and the above result comes as
null.
Let us now perform one more DML operation on the CUSTOMERS table.
The UPDATE statement will update an existing record in the table −
UPDATE customers SET salary = salary + 500 WHERE id = 2;
• When a record is updated in the CUSTOMERS table, the above create
trigger, display_salary_changes will be fired and it will display the following result .
• Old salary: 1500
• New salary: 2000
• Salary difference: 500
Embedded SQL
Embedded SQL is a method of inserting inline SQL statements or queries into
the code of a programming language, which is known as a host language.
Because the host language cannot parse SQL, the inserted SQL is parsed by an
embedded SQL preprocessor.
/*atoi(const char *str) converts the string argument str to an integer (type int)*/
printf("Employee name? ");
gets(emp_name.arr);
emp_name.len = strlen(emp_name.arr);
EXEC SQL INSERT INTO EMP (EMPNO, ENAME) VALUES (:emp_number, :emp_name);
Null values in SQL
A field with a NULL value is a field with no value. If a new record is inserted /
updated with out any value to a particular field, then its value automatically
considered as NULL. A NULL value is different from a zero value or a field that
contains spaces.
The IS NOT NULL operator is used to test for non-empty values (NOT NULL values).
IS NOT NULL-SYNTAX: SELECT column_names FROM table_name
WHERE column_name IS NOT NULL;
Ex: SELECT Name, Address FROM Customers
WHERE Address IS NOT NULL;
End of 3rd unit.