You are on page 1of 20

UNIT 1.

Database

Data: Data are the collection of raw facts and figure which are unorganized, uninterpreted,
and isolated, which doesn’t give any particular sense. They are randomly gathered. So that
they can be processed to obtain the desired result.
E.g. length = 2, breadth = 3
Information: Information are the processed form of data. Information gives a meaning full
result after data are interpreted.

Database
 It is organized form of record about some person, organization or something store
under certain media.
 It is a collection of related information about a subject organized in a useful manner
that provides a base or foundation for procedure, such as retrieving information,
drawing conclusion and make decision.

DBMS:(Database management system)


 DBMS is a computerized record keeping system. It is a software that defines,
manipulates and manage the database. It allows to access the file, update the record and
retrieve data as requested.
 In other word, DBMS is defined as the collection of interrelated data and set of programs to
access their data. The collection of data is usually a database which contains the
information about any particular organization.
 The primary goal of DBMS is to provide an effective and efficient environment for both
data retrieval and storing data in database.
Examples: MS-Access, Oracle, FoxPro, dBase, SQL server, MySQL, Delphi, Sybase, etc
Advantages of DBMS

1)Sharing data: Using DBMS, data stored in database can be shared among multiple
users or computer. For example, branches of bank share data from database.

2) Reduce data redundancy: Data redundancy refer to duplication or repetition of same data
over and over. DBMS reduces such type of unnecessary repetition of data.
1
3) Data backup and recovery: Using DBMS, we can easily create spare copy of original
files and documents that can be later used in case of accidental or intentional loss and
damages.
4) Data security: Using DBMS we can restrict use of database to the unauthorized person.
It helps to ensure date security.

5) Multiple user interface: DBMS facilitates sharing of data which means same data can be
accessed from multiple device and location. Hence, user can experience multiple interface
to access same set of data.

6)Data Integrity: Data Integrity refers to consistency of data. Using database we can
arrange same sort of data in a like order. We can define and enforce constraints for data
integrity.

Disadvantages of DBMS
1) Expensive: It may be expensive to run and operate DBMS for any organization.

2) Changing Technology: It is very much difficult to incorporate latest changing technology


in existing system. In order to create and maintain database technical manpower and
trainings are required.
3)Regular updates: There are frequent requests for updates while using DBMS because
they are regularly updated with new functionality and bug fixes.
4) Backup is needed: Since data maybe damage anytime accidentally or intentionally. So,
it is mandatory to create a backup.

Field :
 It is also known as attribute.
 A database field refers to a set of values arranged in a table and has the same data
type. A field is also known as a column or attribute.
Record:
 A record is a row of a table that gives information about a person or things.
 A record or row contains information about single items in a database. It is also called
tuple.
Table:

 A table is the arrangements of rows and columns. Each table must have unique name
and must be simple.
2
 It is the place where data and information are stored.

Database Model

 This arrangement of data in several structure are known as data base model.
 A Database model defines the logical design and structure of a database and are used to
show how data will be stored, accessed and updated in a Database Management
System. It refers to the layout of a database and helps in designing a database.
The various types of database models are
Hierarchical database model
 It is one of the oldest type of database model. In this model data are represented in the
forms of record, each record has multiple field or attributes.
 All records are arranged in database as tree like structure.
 The relationship between the records is called parent child relationship in which one
child record relates to only a single parent i.e. child possess property only property
of a single parent.
 Here child are restricted to use the property of a parent to whom it doesn't belong.

department

Advantages
 It is the simplest and the easiest model.
 It supports one to one or one to many relationship.
 Searching is easier and faster if parent is known.
Disadvantages
 It is an old fashion and outdated database model.
 It doesn’t support many to one relationship.
 It doesn’t reduce data redundancy because some data are written over different places.
Network database model
 A network model consists of collection of record which are interrelated to each other
with the help of relationship.
3
 Each records have multiple fields and each field has only one data value.
 In this type of model a parent may have multiple child, as well as child can have
multiple parent.

Advantages
 It accepts many to many relationships. So, it is more flexible.
 It reduces data redundancy.
 This network mode is simple and easy to design.
 Searching is faster due to use of multi-directional pointer.
Disadvantages
 Needs long program to handle the relationship.
 Lack of structural independence.
 Less security
Relational database model

 In relational database model, the data are organized into tables which contains
multiple row and columns.
 These tables are called relations.
 A row in a table represents a relationship among a set of values. Since, a table is
collection of such relationship.

4
 It is generally referred to the mathematical term relations from which the relational
database model derives its name. It is also known as RDBMS.

Advantages

 There is less data redundancy.


 Breaking of complex database into simple is very much easier.
 Database processing is faster than other model.
Disadvantage
 Establishing more relationships complex.
 It requires powerful computer and data storage device
 There are too many rules because of complex relationships.

Centralized database VS Distributed database

Centralized database
 It is a simple type which works on client server basis. In this type clients or user are
directly connected to the centrally totally located server.
 This server hosts the data of its client or user and helps them to store and retrieve
data as requirement.
 This type of system are used in small scale industries which doesn't have to deal
with large volume of data and user.
 Centralized database runs on single computer which may have single or multiple
user. Since database is centralized, security is not a crucial part here.
 The maintenance of database is easier because of data are centrally stored. This
5
type of system denote allow unauthorized person to access data.

Advantages
 Suitable for small scale industries.
 Operation and maintenance is easier.
 Since it prevent unauthorized person being accessed to database, it minimizes risk factor.
Disadvantages
 Data are not secured in this type of system.
 Not suitable for large scale industries.
 Failure of centrally located serves will collapse whole network.
Distributed database

 This type of database system are complex in structure, instead of storing and
retrieving data from centrally located server, it uses several numbers of database and
server randomly located at different place.
 It is the collection of multiple logically interrelated database which are distributed in
many geographical location. Since server are located at different locations user can
experience a good speed of bandwidth.
 Similarly, backup and recovery process is lot more easier there, which makes data
more secured. This type of system is used by large organization who has to deal with
large volume of data and user all around the world.
 Since it is distributed in nature there may arises security issue and are costly to
maintain and operate. Simply, distributed database system are the collection of
several number of centralized database system in different locations.

6
Advantages
 Backup and recovery of data is easier.
 It can handle large volume of data and user all over the word.
 User can experience high speed bandwidth.
Disadvantages

 Very expensive to operate and maintain.


 Data security may be real issue.
 Differences between centralized and distributed database system.

Centralized Distributed
Simple type Complex type
Located on particular location Many geographical location
Only one server Multiple server in many location
Suitable for small scale industries Suitable for large scale industries
Maintenance is easy Maintenance is difficult
Security is high Security is low
Low speed High Speed
Cheap Expensive

7
Failure of server affect whole network Doesn’t affect whole network
High chance of data loss Less chance of data loss

Structure query Language (SQL)


 SQL stands for Structured Query Language. It is an international standard data base
query language for accessing and managing data in the database.
 SQL was introduced and developed by IBM in early 1970’s. It was able to control
relational database. SQL is not a complete programming language.
 It is only used for communicating with
database. SQL is made of three sub languages: DDL,
DML and DCL

1) DDL (Data Definition Language): DDL is used by the database designer and programmers
to specify the content and the structure of table. It is used to define the physical
characteristics of record
It is also used to specify the structure of each table, set of associated values with each
attribute, integrity constraints, security and authorization information for each table and
physical storage structure of each table on disk.

 CREATE - to create objects in the database


 ALTER - alters the structure of the database
 DROP - delete objects from the database
 TRUNCATE - remove all records from a table, including all spaces allocated for the
records are removed

2) DML (Data Manipulation Language): DML is related with manipulation of records such
as retrieval, sorting, display and deletion of records or data. It helps user to use query and
display report of the table.

The following are an overview about the usage of DML statements in SQL:
 SELECT - retrieve data from the a database
 INSERT - insert data into a table

8
 UPDATE - updates existing data within a table
 DELETE - deletes all records from a table, the space for the records remain

3) DCL (Data Control Language): All provides additional feature for security of table and
database. It includes commands for controlling data and access to the database.

The following t are an overview about the usage of DCL statements in SQL:

 GRANT - gives user's access privileges to database


 REVOKE - withdraw access privileges given with the GRANT command

Entity Relationship Database model (ER Model)

The entity relationship database model (ER diagram) is based on the perception of a real world that
contains a collection of basic object called entities and relationship among these objects.
The ER diagram is an overall logical structure of database that can be expressed graphically. It was
developed to facilitates database design. The major objectives of ER diagram is to show relationship
among different entities. It has following components.

1) Entity: The distinguishable object of this real world is known as entities. It has a set of properties
which uniquely identifies an entity. It is denoted by rectangle.

9
2) Attributes: Attributes are the properties possessed by an entity. They are represented by ellipse
or oval sign. For eg, if student is an entity then its attribute can be registration number, name, roll
no, class, address, etc.

3) Relationships: A relationship is a association among several entities. It is represented by


diamond.

There are four types of cardinal relationships:


1. One to One
2. One to Many
3. Many to One
4. Many to Many

1. One to One Relationship


When a single instance of an entity is associated with a single instance of another entity then it is
called one to one relationship. For example, a person has only one passport and a passport is given
to one person.

2. One to Many Relationship


10
When a single instance of an entity is associated with more than one instances of another entity then
it is called one to many relationship. For example – a customer can place many orders but a order
cannot be placed by many customers.

3. Many to One Relationship


When more than one instances of an entity is associated with a single instance of another
entity then it is called many to one relationship. For example – many students can study in a single
college but a student cannot study in many colleges at the same time.

4. Many to Many Relationship


When more than one instances of an entity is associated with more than one instances of another
entity then it is called many to many relationship. For example, a can be assigned to many projects
and a project can be assigned to many students.

Advantages of E-R diagram


SIMPLE : It is simple to draw an ER diagram when we know entities and relationships.

EFFECTIVE : It is an effective communication tool.

EASY TO UNDERSTAND : The design of ER is very logical and hence they are easy to design
and understand.

EASY CONVERTION : It can be easily converted to other type of models.

DATABASE TROUBLESHOOTING : ER diagrams are used to analyze existing databases to find


11
and resolve the key issues in logic or deployment. Drawing the diagram should reveal where it’s
going wrong.

FLEXIBLE: ER models are flexible and can be modified easily to accommodate changes in the
database design or structure.

MINIMIZES DATA REDUNDANCY: ER models help minimize data redundancy by identifying


and eliminating duplicate data entries.

VISUAL REPRESENTATION: ER models provide a visual representation of the database


structure, making it easier for stakeholders to understand and provide feedback.

DOCUMENTATION: ER models provide useful documentation for future reference, making it


easier to maintain and update the database in the long run.

DBA (Database Administrator)

 DBA is the most responsible person in an organization with sound knowledge of DBMS.
He/she is the overall administrator of the program.

 He/she has the maximum amount of privileges for accessing database and defining the role
of the employee which use the system.

 The main goal of DBA is to keep the database server up to date, secure and provide
information to the user on demand.

Qualities of good DBA

 He/she should have sound and complete knowledge about DBMS and its operation.

 He/she should be familiar with several DBMS packages such as MS Access, MY SQL,
Oracle etc

 He/she should have depth knowledge about the OS in which database server is running.

 He/she should have good understanding of network architecture.

 He/she should hove good database designing skill.

Responsibilities

 DBA has responsibility to install, monitor, and upgrade database server.

 He/she should has responsibility to maintain database security by creating backup for
recovery.
12
 He/she has responsibility to conduct training on the uses of database.

 DBA defines user privilege, relationships and manages form, reports in database.

Keys of DBMS:

Key is a field that uniquely identifies the records, tables or data. Key in a table allows us to establish
the relation between multiple tables. Keys are also useful for finding the unique records or
combination of records from a large database tables.

Primary Key – is a column or group of columns in a table that uniquely identify every row in that
table.

Candidate Key – Candidate keys are those attributes that uniquely identify rows of a table. The
Primary Key of a table is selected from one of the candidate keys. So, candidate keys have the same
properties as the primary keys.

Alternate Key – is a column or group of columns in a table that uniquely identify every row in that
table.

Foreign Key – is a column that creates a relationship between two tables. The purpose of Foreign
keys is to maintain data integrity and allow navigation between two different instances of an entity.

Composite Key – is a combination of two or more columns that uniquely identify rows in a table.
The combination of columns guarantees uniqueness, though individual uniqueness is not guaranteed.

Normalization

13
The process of breaking down or decomposing as complex relation into simple relation. It reduces
redundancy (unnecessary repetition of data) using principle of non-loss decomposition in which
table are reduce to smaller tables without loss of information.

Normalization is the database design process in which complex database table is broken down into
simple separate tables. It makes data model more flexible and easier to maintain.

EF Codd has introduced few rules for normalizing the database in 1970 and these rules are known
as normal forms. This process minimizes and controls the duplication of data in a database and also
provides a rapid search for data from database.

Advantages of normalization

 It reduces data redundancy (duplication of data)


 It improves faster sorting and indexing.
 It simplifies the structure of the database table.
 It improves the performance of a system.
 It avoids loss of information.

Types of Normalization

1NF (First Normal Form):

A table is said to be in first normal form if it has atomic values. There shouldn't be any repeating
groups of attribute in the table. First normal form sets the very basic rules for an organized database.

 The data field should be a single (atomic) valued attribute/ columns.


 It eliminates duplicates rows and columns from the same table.
 It minimizes the data redundancy in the database table.

14
2NF (Second Normal Form):

A table is said to be in 2NF if it is a First normal form and it doesn't have the partial dependency.

Second normal form further addresses the concept of removing duplicate data.

 It should be in the first normal form.


 It should not have partial Dependency.
 It identifies data dependencies.
 Non key attributes are functionally depends on key attribute (primary key).

TEACHER table

TEACHER_ID SUBJECT TEACHER_AGE

25 Chemistry 30

25 Biology 30

47 English 35

83 Math 38

83 Computer 38

In the given table, non-prime attribute TEACHER_AGE is dependent on TEACHER_ID which


is a proper subset of a candidate key. That's why it violates the rule for 2NF.

15
To convert the given table into 2NF, we decompose it into two tables:

TEACHER_DETAIL table:

TEACHER_ID TEACHER_AGE

25 30

47 35

83 38

TEACHER_SUBJECT table:

TEACHER_ID SUBJECT

25 Chemistry

25 Biology

47 English

83 Math

83 Computer

3NF (Third Normal Form):

Third normal form goes one large step further.

 It should be in the second normal form.


 It removes transitive dependencies in a table.
 All non-primary key attribute must dependent on primary key attribute or attribute.

16
EMPLOYEE_DETAIL table:

EMP_ID EMP_NAME EMP_ZIP EMP_STATE EMP_CITY

222 Harry 201010 UP Noida

333 Stephan 02228 US Boston

444 Lan 60007 US Chicago

555 Katharine 06389 UK Norwich

666 John 462007 MP Bhopal

Converting in 3Nf

MPLOYEE table:

EMP_ID EMP_NAME EMP_ZIP

222 Harry 201010

333 Stephan 02228

444 Lan 60007

555 Katharine 06389

666 John 462007

EMPLOYEE_ZIP table:

EMP_ZIP EMP_STATE EMP_CITY

201010 UP Noida

02228 US Boston

60007 US Chicago

17
06389 UK Norwich

462007 MP Bhopal

Data integrity:
Data integrity referees to the validity or consistency of data in database. It ensures that the data
should be accurate and consistent.
Mainly there are 3 types of data integrity constraints used in the database system. They are as:
 Domain integrity constraints: it defines a set range of data values for given specific data
field. And also determines whether null values are allowed or not in the data field.

 Entity integrity constraints: it specify that all rows in a table have a unique identifier, known
as the primary key value and it never be null i.e. blank.

 Referential integrity constrains: it exists in a relationship between the two tables in a


database. It ensures that the relationship between the primary keys in the master table and
foreign key in child table are always maintained.

Data security

 Database security is the technique that protects and secures the database against intentional
or accidental threats.
 Security concerns will be relevant not only to the data resides in an organization's database:
the breaking of security may harm other parts of the system, which may ultimately affect the
database structure.
 Consequently, database security includes hardware parts, software parts, human resources,
and data.

DATABASE COMMNADS

1. VIEW EXISTING DATABASE


 To view existing database names, the command is : SHOW DATABASES ;

 CREATING DATABASE IN MYSQL


For creating the database in MySQL, we write the following command : CREATE DATABASE
<databasename> ;
e.g. In order to create a database Student, command is :
CREATE DATABASE Student ;
2. ACCESSING DATABASE
For accessing already existing database , we write :
USE <databasename> ;

18
e.g. to access a database named Student , we write command as :
USE Student ;
3. DELETING DATABASE
For deleting any existing database , the command is :
DROP DATABASE <databasename> ;
e.g. to delete a database , say student, we write command
as ; DROP DATABASE Student ;
4. VIEWING TABLE IN DATABASE
In order to view tables present in currently accessed database , command is : SHOW TABLES ;

CREATING TABLES IN MYSQL


- Tables are created with the CREATE TABLE command. When a table is created, its
columns are
- named, data types and sizes are supplied for each column.
Syntax of CREATE TABLE command is : CREATE TABLE <table-name>
(
<column name> <data type> ,
<column name> <data type> ,
………
);
E.g. in order to create table EMPLOYEE given below :
ECODE ENAME GENDER GRADE GROSS
We write the following command : CREATE TABLE employee (
ECODE integer ,
ENAME varchar(20) , GENDER char(1) ,
GRADE char(2) , GROSS integer
);

Altering table adding, deleting, or modifying columns in an existing table


Adding column Syntax:
Synatx: ALTER TABLE table name
ADD column_name datatype
Example: ALTER TABLE Students ADD Email varchar(255));
Deleting column
Syntax:
ALTER TABLE table name
DROP COLUMN column_name;
Example: ALTER TABLE Students
DROP COLUMN Email
Modifying column (changing the data type of a column in a table)
Sytnax:
ALTER TABLE table name MODIFY COLUMN column_name datatype:
Example: ALTER TABLE Students
MODIFY COLUMN Class int
Deleting table
DROP TABLE table_name,
SQL statement to insert, select, update and delete data
19
Syntax Example: DROP TABLE Students:

Inserting data
Syntax
INSERT INTO table name (column1, column2 column3,...) Example: INSERT INTO Students
(StudentID,
FName, LName, Address, Class)
VALUES (valuel, value2, value3.);
VALUES ('101', 'Ram', Sharma', Pokhara', 7);
Selecting data iselecting data from a database);
Syntax SELECT column1, FROM table name column2.
Example: SELECT * FROM Students; (This will select all the columns from the table Students]
SELECT
FNAME. LNAME FROM Students: [This will select only the First Name and Last
Name from the table Students.]
Selecting data using conditions
Syntax SELECT column1, column2....
FROM table_name
WHERE condition;

20

You might also like