Database Management System xii
Database Management System xii
What is data?
Collection of logically related data that has been recorded, organized and
made available for searching is called Database.
Properties of database
Banking
Online shopping
Payroll
Inventory management
Loan and investment
Reservation system And many more
What are the needs for a database?
Self Describing: DBMS not only contains the database but also the
description of the data that it stores.
Insulation between program and data: program can access data
uninterruptedly even when structure of data stored in DBMS is modified.
Data Sharing: DBMS includes concurrency control software to allow
simultaneous access of data in the database without any inconsistency
problems.
Backup and Recovery: DBMS has provision of Backup and recovery of
data that helps user to protect the data from damage or loss.
What are different types of users of DBMS?
End users: users who use database for retrieving, updating and creating
reports as per requirement.
Database Administrator (DBA): DBA is responsible for granting data
access permission to user at different level, monitoring database usage
and provide technical support.
Application Programmer: One who write programs to create application
to interact with database. Application programmer uses high level
languages and SQL to do so.
System Analyst: they determine and specify user requirements that help
in designing efficient and reliable database.
What are the advantages of DBMS approach?
What is RDBMS?
Relation: a table is called relation. Another way we can say that collection of
tuple is called relation.
Tuple: A row of table is called tuple. In another way we can say that a single
record is called tuple
Attribute: a column of table is called attribute.
Domain: the data type of values in each column is called the Domain.
Constraints are restrictions that when applied ensure accuracy, validity and
reliability of data in a database.
Domain constraint: it specifies that the value in every attribute in each tuple
must be from the domain of that attribute.
Super Key: this is a set of attribute in a relation, for which no two tuples in a
relation state have the same combination of values.
Null Value Constraints: sometimes an attribute can not have null values. That
attribute can constrained to be Not Null.
Entity Integrity Constraint: it ensures that all tuples are unique in a table.
Primary key implements entity integrity constraint.
The key which is Primary key in another related table is called Foreign
Key.
A table can have more than Foreign keys.
Foreign key can have null values.
What is SQL?
All SQL commands are categorized in two types i.e. Data Definition Language
(DDL) and Data Manipulation language (DML) where DDL is used to define
structure and constraints of data and DML is used to insert, modify and delete
data in a database.
Char(n) Fixed length character string. n denotes max no of characters. Char(6) : sanjay, harish
Varchar(n) Variable length character string. n denotes max no of characters. Varchar(10): ram, arjun
Fixed point number. M denotes no of digits stored for values and d Decimal(3,2): 34.89
Decimal(m,d)
denotes no of decimal points. Decimal(4): 2813
Syntax:
Example:
Screenshot:
Example:
Screenshot:
Default Constraint
Screenshot:
Check Constraint
Screenshot:
Primary Key
Screenshot:
Example:
Create Order
(
Ono int Primary Key,
Odate date,
Itemno int,
Qty int,
Total int,
FOREIGN KEY (Itemno) REFERENCES Item (Itemno)
);
Screen Shot:
How to Name constraints in SQL?
Constraints can be named in the Create Table command. The advantage is that
named constraints can be easily deleted or updated using the Alter Table
command. A constraint can be named by using the keyword CONSTRAINT
followed by the name of the constraint and its specification.
Example:
Create Order
(
Ono int Primary Key,
Odate date,
Itemno int,
Qty int,
Total int,
FOREIGN KEY (Itemno) REFERENCES Item (Itemno)
);
Screenshot:
Drop table
Syntax:
Example:
Alter table
It is used to modify structure of a table. Following are the alteration can be
done using this command:
Adding a column:
Example:
Dropping a column
Example:
Modifying a table:
A column can also be modified using Alter Table command as given below:
Insert command
This command is used to insert a tuple in a relation. We must specify the name
of the relation in which tuple is to be inserted and the values. The values must
be in the same order as specified during the Create Table command.
Syntax:
Example:
Screenshot:
Update command
Update command is used to modify the attribute values of one or more tuples
in a table.
Syntax:
UPDATE <tablename>
SET <expression>
WHERE <criteria>
Example:
UPDATE Item
SET Rate = 30
WHERE itemno = ‘i0001’;
Screenshot:
Delete command
Syntax:
Example:
Screenshot:
Select command
Syntax:
Code:
Screenshot:
Query2: Display itemname with its rate from item table.
Code:
Screenshot:
Code:
Screenshot:
Query4: display item records which name begins with s.
Code:
Screenshot:
Query5: retrieve item details which rate is less than 100 and qty is more than 100.
Code:
Select * from item where rate < 100 and qty > 100;
Screenshot: