Unit - I Databases and Data Base Users - gptgajendragad.com semister DBMS.pdf · Database...

59
Database Management System(DBMS) Govt. Polytechnic Gajendragada 1 R.S.Malagitti HOD of CSE Unit - I Databases and Data Base Users 1.1 Database and Examples Database is a collection of related data. Database can be any size and no complexity Database may be generated and maintained manually or it may be computerized. Database consists of tables, views, queries, reports and other objects. Example - College Database Table - Student Register No Student Name Father Name Gender DOB Dept No 1 Sachin Veeru Male 2/2/1999 1 2 Sangu Mallu Male 3/4/1998 2 3 Rashmi SS Female 2/5/1998 3 Table – Department Department Number Department Name 1 CSE 2 ME 3 CE 4 ECE Table – Laboratory Lab ID Lab Name Dept ID 1 Computer Lab 1 2 Electronic Lab 4 3 Science Lab 5 1.2 Characteristics of Database Approach The characteristics of the database approach versus the file-processing approach are 1. Self-describing nature of database system 2. Insulation between programs, data and data abstraction 3. Support of multiple view of the data 4. Sharing of data and multiuser transaction processing 1. Self-describing nature of database system ü DBMS describes the structure of database and storage details with constraints ü In DBMS, the database structure is stored in a catolog is called meta-data 2. Insulation between programs, data and data abstraction ü In DBMS, the access programs are written independent of any specific files. This is called program-data independence.

Transcript of Unit - I Databases and Data Base Users - gptgajendragad.com semister DBMS.pdf · Database...

Database Management System(DBMS) Govt. Polytechnic Gajendragada

1 R.S.Malagitti HOD of CSE

Unit - I Databases and Data Base Users

1.1 Database and Examples Ø Database is a collection of related data. Ø Database can be any size and no complexity Ø Database may be generated and maintained manually or it may be computerized. Ø Database consists of tables, views, queries, reports and other objects. Ø Example - College Database Table - Student

Register No

Student Name Father Name Gender DOB Dept No

1 Sachin Veeru Male 2/2/1999 1 2 Sangu Mallu Male 3/4/1998 2 3 Rashmi SS Female 2/5/1998 3

Table – Department Department Number Department Name 1 CSE 2 ME 3 CE 4 ECE

Table – Laboratory Lab ID Lab Name Dept ID 1 Computer Lab 1 2 Electronic Lab 4 3 Science Lab 5

1.2 Characteristics of Database Approach Ø The characteristics of the database approach versus the file-processing approach

are 1. Self-describing nature of database system 2. Insulation between programs, data and data abstraction 3. Support of multiple view of the data 4. Sharing of data and multiuser transaction processing

1. Self-describing nature of database system ü DBMS describes the structure of database and storage details with constraints ü In DBMS, the database structure is stored in a catolog is called meta-data

2. Insulation between programs, data and data abstraction ü In DBMS, the access programs are written independent of any specific files.

This is called program-data independence.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

2 R.S.Malagitti HOD of CSE

ü The user need not worry about how data stored in database and relationship of program and data, this is called as data abstraction.

3. Support of multiple view of the data ü A database may have many users and each one many be interested on a

particular view of the application ü A view is conceptually a table.

4. Sharing of data and multiuser transaction processing ü DBMS must provide control for various users trying to access the database. ü It provides concurrency control mechanism.

1.3 Actors on the Scene Ø The following are the actors of the database.

1. Database Administrator (DBA) 2. Database Designer (DBD) 3. End Users

1. Database Administrator (DBA) The following are the responsibilities or functions of DBA ü The database administrator is to create the database and implement the

technical controls. ü Managing the resources like system license, required tools and other

software and hardware maintenance. ü Administrating the database ü Managing poor system response time ü Providing Security and authorization.

2. Database Designer (DBD) The following are the responsibilities or functions of DBA ü Designers are the group of people who actually work on the designing of

database. ü Designers are identifying the data to be stored in the database ü Planning the database design that meets the requirements of all users ü Designers are communicate with all users in order to understand their

requirements. 3. End Users

End Users are the people who interact with the database through applications or utilities. The following are the types of end users 1. Casual Users 2. Naïve Users 3. Sophisticated Users 4. Standalone Users

Database Management System(DBMS) Govt. Polytechnic Gajendragada

3 R.S.Malagitti HOD of CSE

1. Casual Users

ü These are people who use the database occasionally. ü Example – High level managers who access the database weekly or

monthly 2. Naïve Users

ü These are users who constantly accessing or querying and updating the database.

ü Example – Reservation clerks for airport, railway station, bank officers etc 3. Sophisticated Users

ü These are users who known about the structure defined in the database. These users have skills for accessing the database.

ü Example – Engineers, Scientists, business analysts etc 4. Standalone Users

ü These users are interact with database directly or indirectly. These are maintaining the database.

ü Example - User of text package, tester 1.4 Workers behind the Scene

1.5 Advantages of using DBMS Approach

1. Controlling Redundancy 2. Restricting Unauthorized Access 3. Providing Persistent Storage for Program Objects 4. Providing Backup and Recovery 5. Good Graphical User Interface (GUI) 6. Enforcing Integrity Constraints 7. Efficient Data Access 8. Flexibility 9. Reduced Application Development Time

1. Controlling Redundancy ü DBMS systems provide mechanisms to provide concurrent access of data to

multiple users.

ü It avoids the Duplication, wastage of storage space, inconsistency of data

2. Restricting Unauthorized Access ü DBMS System provide the security and authorization subsystem. This is done

by DBA while creating the user account. 3. Providing Persistent Storage for Program Objects ü DBMS provide persistent storage for program objects and data structures

because object-oriented database systems. 4. Providing Backup and Recovery

Database Management System(DBMS) Govt. Polytechnic Gajendragada

4 R.S.Malagitti HOD of CSE

ü DBMS must provide recovery mechanisms when the hardware or software crashes.

ü The failure may occur at any time and therefore the database should be in a position to recover the system to the point where the crash occurred.

5. Good Graphical User Interface (GUI) ü DBMS should provide a menu driven software so that the users can easily

access the database without remembering the commands. The menus are provided through GUI.

6. Enforcing Integrity Constraints ü The integrity constraint means that the data in the database is correct. This

means that each record in table should have a proper semantic relationship with a record of another table.

7. Efficient Data Access ü The database uses different technique to store and retrieve data. This method

provides efficiency in data access. In fact speed is also good. 8. Flexibility ü The database structure can be changed at any instant of time.

9. Reduced Application Development Time ü Very less time is required to create and develop database.

1.6 A Brief History of Database Applications ü Database applications using hierarchical and network system. ü Flexibility with relational database ü Object-Oriented applications & more complex database ü Interchanging data on the website ü Extending database capabilities for new applications.

1.7 When Not to use a DBMS ü The application is simple. ü Multiple users are not required to access the database ü Data manipulation is not specialized ü Real-Time requirement of program not met to database.

1.8 Data Models, Schemas and Instances Data Models ü Data Model is a set of concepts that can be used to describe the structure of the

database. OR ü Data Model define how the logical structure of database is modelled. ü There are 3 categories of data model

1. Conceptual Data Model - Conceptual data model uses concepts such as entities, attributes and

relationships. These concepts are close to many users. 2. Physical Data Model

Database Management System(DBMS) Govt. Polytechnic Gajendragada

5 R.S.Malagitti HOD of CSE

- It provides the concepts that describe the details of how data is stored in the computer.

3. Implementation Model - It provides the concepts that may be understood by end-users and hide

some details of data storage. Schemas ü The description of data in terms of data model is called schema. In data model

every relation has a schema, which describes the columns and fields. ü A database schema defines its entities and the relationship among them

Instances OR Database State ü The data in the database at a particular moment of time is called instances. It

contains a snapshot of the database ü Database instances tend to change with time

1.9 Three-Schema Architecture and Data Independence

1. Internal Level OR Physical Schema ü It describes about the physical storage and access path. ü It gives details such as how data should be stored in disks.

2. Conceptual Level OR Conceptual Schema ü It describes the structure of database such as entities, relationships, user

operations etc ü This level describes the structure of all relations in the database.

3. External Level OR External Schema

Database Management System(DBMS) Govt. Polytechnic Gajendragada

6 R.S.Malagitti HOD of CSE

ü It describes part of database that a particular user or group is interested in which database and hides the rest of database.

ü It has more than one view, generally view is relation that collects certain information from base relations.

1.9.1 Data Independence ü The data independence is the ability to change the schema at one level of

database without changing the schema at higher level. ü There are two types of data independence

1. Logical Data Independence ü The ability to change the logical or conceptual schema without changing the

external schema is called logical data independence. ü Conceptual schema can be changed without changing the application program. ü Example – Adding new entities, attributes or relationship

2. Physical Data Independence ü The ability to change the physical schema without changing the conceptual

schema is called physical data independence. ü Example – Changing Storage devices will not affect to conceptual schema

1.10 DBMS Languages 1. Data Definition Language (DDL) 2. Storage Definition Language (SDL) 3. View Definition Language (VDL) 4. Data Manipulation Language (DML)

1. Data Definition Language (DDL) ü DDL specifies the conceptual schema. ü The function of DDL is to parse the statement in order to identify the description

of schema structure and stores the details in system catalog 2. Storage Definition Language (SDL) ü SDL is used to specify the internal schema. ü It specifies to select the type of storage method through the SDL command.

3. View Definition Language (VDL) ü The mapping between the conceptual level and internal level is done through

view definition language. 4. Data Manipulation Language (DML) ü DML is used to retrieve, delete and update the data in database. ü DML provide set of operations to DBMS.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

7 R.S.Malagitti HOD of CSE

1.11 DBMS Component Modules

Ø The DDL Compiler coverts the data definition statements into set of tables.

These tables contain the metadata concerning the database, that can be used by other component of DBMS.

Ø The Query processor is used to transforms user queries into series of operations and sent to database manager for execution.

Ø The pre-compiler extracts DML commands from an application program. These commands sent to the DML compiler for compilation.

Ø The database Manager is the central software component of DBMS. - It is responsible for handling data in database. - It provides recovery to the system means recover the data after failure. - It include recovery manager and buffer manager.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

8 R.S.Malagitti HOD of CSE

Ø The file manager is responsible for structure of files and managing the file space. It is also responsible for locating the block containing the required record.

Ø The authorization control module checks the authorization of users in terms of various privileges to users.

Ø The command processor processes the queries passed by authorization control module.

1.12 Client - Server Architecture for DBMS

Ø Clients are send the request to DBMS system in the form of communication messages.

Ø DBMS System send the response to client system. The communication is not part of database, it is independent module.

Ø Server is a computer with DBMS loaded. It supports all the functions of DBMS and also supports 3-level architecture of DBMS.

Ø The applications running on the client machine.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

9 R.S.Malagitti HOD of CSE

1.13 Classification of DBMS 1. Relational Model 2. Network Model 3. Hierarchical Model 4. Object Model 5. Object-Relational Model

Ø Traditional Models are relational model. Well-known DBMS like Oracle, MS-SQL, MYSQL, MS-Access etc. These applications are supported by relational model.

Ø The network model and hierarchical model are used in industry mainly on mainframe platforms.

Ø The object model and object-relational model are represents information in the form of objects as used in object-oriented programming.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

10 R.S.Malagitti HOD of CSE

Unit - IV SQL: Schema Definition, Constraints, Queries and Views

4.1 SQL – Structure Query Language Ø SQL is a Structured Query Language. This language is used for storing,

manipulating and retrieving the data stored in relational database. Ø SQL is a standard language for relational database systems likes MYSQL, MS-

ACCESS, ORACLE…..etc. 4.2 Data Types Ø Data types indicates the type of data that column can hold. The data may be

numeric or non-numerical. Ø Data types are used to represents the type of data can store in the attributes

(column) of the tables. Ø The following are the data types in SQL

1. Numeric Data Types ü Numeric data can be either integer or fractional numbers. ü The Integer data types

Data Type Name Size INT 4 BYTE SMALLINT 2 BYTE BIGINT 8 BYTE

ü The floating-pointing data types Data Type Name Size

DECIMAL Depends on scale and precision

NUMBERIC Depends on scale and precision

FLOAT 4 BYTE DOUBLE 8 BYTE

2. Character or String Data Type ü This data type can hold single character or string

Data Type Name Size CHAR(n) Store up to 255 characters VARCHAR(n) Store up to 255 characters. If

you give greater than 255 characters, it will converted into text

TEXT Store up to 65,535 characters

Database Management System(DBMS) Govt. Polytechnic Gajendragada

11 R.S.Malagitti HOD of CSE

3. Date and Time Data Type Data Type Name Size

DATE Format -‘YYYY-MM-DD’ DATETIME Format – ‘YYYY-MM-DD HH:MI:SS’ YEAR Format – ‘YYYY’ TIME Format – ‘HH:MI:SS’

4. Binary Data Type Data Type Name

BINARY (BLOB) BINARY LARGE OBJECT VARBINARY IMAGE

4.3 Constraints Ø Constraints are database rules. Constraints enforce rules on the data whenever a

row is inserted, updated or deleted from table. Ø Constraints are used to prevent invalid data entry into tables. Ø The types of constraints are-

1. NOT NULL 2. DEFAULT 3. UNIQUE Constraints 4. PRIMARY KEY 5. FOREIGN KEY.

1. NOT NULL ü Ensures that a column cannot have null value.

2. DEFAULT ü Provides a default value for rows of the particular column

3. UNIQUE ü Ensures that all values in a column are unique or different

4. PRIMARY KEY ü It is a key, it is uniquely identified each rows/records in a table. ü The primary key must contains unique value. ü The primary key cannot contain null value. ü The difference between primary key and unique constraints is-

1. The unique constraints of particular column must unique 2. The primary key of column is also unique but it is considered as special key

by using this key the user can link to other table. 5. FOREIGN KEY ü It uniquely identifies the rows/records in any other table. ü Foreign key of column in one table link to a primary key of column in other

table. ü Foreign key indicates the relationship between two tables.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

12 R.S.Malagitti HOD of CSE

CREATE TABLE tablename ( Columnname1 datatype(size) [constraints], Columnname2 datatype(size) [constraints], ……… ColumnnameN datatype(size) [constraints], PRIMARY KEY (columnname),

FOREIGN KEY (columnname) REFERENCES tablename(columnname) );

ü Foreign key prevents invalid data being inserted into given column because the value must be contained in the table of the primary key.

4.3 CREATE TABLE Statement

ü The “CREATE TABLE” command is used to create relations or tables by giving its name and specifying its attributes and initial constraints.

ü The general syntax

ü CREATE TABLE is the command, these are used to create table. ü Columns are nothing but attributes. These are written within the parentheses and

comma (,) operator indicates all columns are separate. ü Data types indicate that what type of data can stored in particular column. ü Constraints are applying rules to particular column. It is optional. ü Primary key and Foreign key constraints also optional. ü Example – Two tables

1.

CREATE TABLE DEPARTMENT( dnumber INT, dname varchar(20),

PRIMARY KEY(dno)); 2.

CREATE TABLE STUDENT( regno INT, sname VARCHAR(20) NOT NULL, sem INT, dno INT, PRIMARY KEY(regno),

FOREIGN KEY(dno) REFERENCES DEPARTMENT(dno));

Database Management System(DBMS) Govt. Polytechnic Gajendragada

13 R.S.Malagitti HOD of CSE

ALTER TABLE TableName ADD columnname DataType;

ALTER TABLE TableName MODIFY columnname DataType;

ALTER TABLE TableName DROP columnname

ALTER TABLE TableName CHANGE Existing_Columnname1, New_columnname2;

ALTER TABLE TableName ADD PRIMARY KEY (columnname)

INSERT INTO TableName(column1,column2,,,,,,,,,,,,,,,,,columN) VALUES (value1,value2,,,,,,,,,,,,,,,,,,,,valueN);

INSERT INTO Tablename VALUES (value1,value2,,,,,,,,,,,,,,,,,,,,valueN);

4.4 ALTER TABLE Statement Ø The ALTER TABLE Statement is used to

- Add new column to an existing table - Modify an existing column means changing data type with size - Drop or Delete an existing column - Add new constraints to an existing column - Drop constraints

Ø Syntax for adding new column to an existing column

Ø Syntax for modifying an existing column

Ø Syntax for dropping an existing column of table

Ø Syntax for changing the column name

Ø Syntax for Adding or Dropping constraints

OR

4.5 INSERT INTO Statement Ø INSERT INTO statement is used to add new rows of data to a table in the

database. Ø The general syntax is,

OR

ALTER TABLE TableName DROP primary key

Database Management System(DBMS) Govt. Polytechnic Gajendragada

14 R.S.Malagitti HOD of CSE

INSERT INTO DEPARTMENT (dno,dname) VALUES (101,’CSE’); OR INSERT INTO DEPARTMENT VALUES (101,’CSE’);

Ø The values can be either numeric or non-numeric. The non-numeric values must be enclosed within the single quotes. The non-numeric value may date and string. Example – ‘aaa’

Ø Example –

Ø The user can also insert multiple values at a time by separating values using

comma operator(,). 4.6 Displaying table Structure ü The “DESC” command is used to display the structure of table. ü It displays the attributes with data types and constraint details. ü The general syntax is,

ü Example –

4.7 SELECT STATEMENT Ø The SELECT statement is used to retrieve or fetch the data from tables present in

database. Ø It is also fetch the data from multiple tables Ø The general syntax is

Here

- Here “SELECT” is a command and select the required columns from the table.

- “*” indicates all columns of the given table. It means the select statement retrieves all columns from the table.

- The user can also specify the required columns by separating these columns using comma operator(,).

- The “FROM” indicates the location of table. It retrieves the values of columns mentioned in “SELECT” clause.

- The “WHERE” is used to specify a condition while retrieving the data from single or joining with multiple tables. If the condition is satisfied then only returns the specific values from table.

DESC Tablename;

DESC STUDENT;

SELECT * | [column1,column2……..] | [expression (aliase) FROM table1,table2…… [WHERE (conditions)]

Database Management System(DBMS) Govt. Polytechnic Gajendragada

15 R.S.Malagitti HOD of CSE

SELECT * FROM STUDENT;

SELECT sno , sname FROM STUDENT;

SELECT sno , sname FROM STUDENT WHERE sname=’xx’

SELECT sno , sname FROM STUDENT WHERE sname=’xx’ AND sno=8;

- The ‘WHERE” is used to filter the rows or fetch the necessary rows. - The condition may have

1. Column name with values i.e columnname=value 2. Joining operations 3. Inner SQL statements

- The condition may have following operations 1. Relational operations [ > , < , = , <= , >= , != ] 2. Logical operations [ AND,OR,NOT ]

- If users wants to apply multiple conditions, it has to use logical operations

Ø Example –

- It retrieves the all records of the columns in STUDENT table

- It retrieves the all records of the “sno” and “sname” columns of the STUDENT.

- It retrieves the records of the “sno” and “sname” columns of the

STUDENT table when the “WHERE” condition is true. It returns the empty set when the condition is false.

- It shows the result only when both condition must be true. 4.7.1 Joining Table Ø A JOIN means for combining fields from two or more tables by using values

common to each other. The primary key and foreign key are used to maintain the relationship of the two or more tables.

Ø Example -

- Here , DEPARTMENT table have primary key of “dnumber” field and STUDENT have foreign key of “dno” field. So that it returns the matching records of between those columns.

SELECT sno, sname,dname FROM STUDENT, DEPARTMENT WHERE dnumber=dno;

Database Management System(DBMS) Govt. Polytechnic Gajendragada

16 R.S.Malagitti HOD of CSE

- If primary key field and foreign key field have the same column name, then it must be used table name followed by column name with DOT(.) operator.

- Example – Assume both STUDENT and DEPARTMENT have the same “dno” field relationship.

Ø The following are the some of the joining types in SQL 1. INNER JOIN - Returns rows when there is a match in both tables. 2. OUTER JOIN - Outer join are used to display rows the do not met the join

condition 3. LEFT JOIN - Returns all rows to the left table and matched rows from the

right table 4. RIGHT JOIN - Returns all rows to the left table and matched rows from the

right table 5. NATURAL JOIN - In this join, no need to specify joining condition.

4.7.2 WHERE Clause Ø The “WHERE” clause is used to filter the records or rows. Ø The “WHERE” clause is used to extract only those records that fulfil a specified

condition. Ø The general syntax is

Here, the operators are = , => , <= , != , AND, OR, LIKE, BETWEEN etc Ø Example –

It retrieves all the records from dno “2” in the STUDENT table.

4.7.3 LIKE operator Ø The LIKE operator is used in WHERE clause to search for a specified pattern in a

given column. Ø The LIKE operator uses “%” symbol to represent any sequence of one or more

characters Ø The general syntax

SELECT sno, sname, dname FROM STUDENT, DEPARTMENT WHERE STUDENT.dno AND DEPARTMENT.dno

SELECT columnname1,columnnane …. FROM tables WHERE columnname operator value

SELECT * FROM STUDENT WHERE sno=2

SELECT columns FROM tablenames WHERE columnname PATTERN

Database Management System(DBMS) Govt. Polytechnic Gajendragada

17 R.S.Malagitti HOD of CSE

SELECT columnnames, aggregate function (columnname) FROM tablename [WHERE condition] GROUP BY columnname

SELECT dno , count(*) FROM STUDENT GROUP BY dno;

ü Here “PATTERN” means single character or any number of characters with “%”symbol.

ü The given pattern is always searching in columns mentioned “WHERE” clause.

Ø Example –

It retrieves all records whose student name start with R

4.8 Aggregate Functions Ø Aggregate functions return a single value calculated from values in a column. Ø The functions are

1. COUNT(*) - It returns the number of rows present in a table 2. AVG (columnname) - It returns the average value of rows in a given column 3. SUM (columnname) - It returns the sum of all rows in a given column 4. MAX (columnname) - It returns the largest value in a given column 5. MIN (columnname) - It returns the lowest value in a given column

Ø Example

4.8.1 GROUP BY Clause Ø The GROUP BY statement is used to group the result-set by one or more columns

with help of aggregate functions. Ø The group by statement must uses aggregate functions. Ø The general syntax is

Ø Example –

SELECT * FROM student WHERE sname LIKE ‘R%’ ;

SELECT COUNT (*) FROM student;

SELECT AVG (totalmarks) FROM student;

SELECT SUM (totalmarks) FROM student;

SELECT MAX (totalmarks) FROM student;

SELECT MIN (totalmarks) FROM student;

Database Management System(DBMS) Govt. Polytechnic Gajendragada

18 R.S.Malagitti HOD of CSE

SELECT column1 , column2 , ……… FROM table1 , table2 , …………………. [WHERE (condition) ] [GROUP BY columnname] [HAVING condition] ORDER BY column1 (values).

4.8.2 HAVING Clause Ø The HAVING statement places conditions on the group result created by

“GROUP BY” clause. Ø The HAVING key word must follow the GROUP BY clause Ø The general syntax is

Ø Example -

4.8.3 ORDER BY Clause Ø The ORDER BY clause is used to sort the values in column as per the format i.e

ascending order or descending order. Ø The general syntax is

Here, the values means “ASC” OR “DESC”

- ASC - Ascending order - DESC – Descending order

Ø Example – It sorts “dno” records in ascending order

4.9 ALIASE STATEMENT Ø The ALIASE is used for columns mentioned in select statement with help of “as”

key word.

SELECT column1 , column2, ……. FROM table1,table2, …….. [WHERE (condition) ] GROUP BY column1,column2, …. HAVING (condition)

SELECT dno, count(*) FROM student GROUP BY dno HAVING dno>2.

SELECT * FROM student ORDER BY dno ASC.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

19 R.S.Malagitti HOD of CSE

SELECT columnnames FROM tablename aliasename OR SELECT columnames FROM tablename AS aliasename

SELECT sno AS registerno, sname AS StudentName FROM student

SELECT sno AS ‘Register Number’ , sname AS ‘Student Name’ FROM student

Ø It is used for renaming the columns while displaying it. It can also used for multiple column.

Ø It is also used to temporarily rename the table. Ø The general syntax for renaming the column

If aliases name have two or words then it must be within single quotes (‘ ‘)

Ø The general syntax for tables

The column names must have aliase name with dot(.) operator.

Ø Example for columns -

Ø Example for tables

4.9 UPDATE Statement Ø The UPDATE statement is used to change or modify the records of the column

present in table. Ø The general syntax is,

ü UPDATE indicates the modifying the table records. ü SET indicates the setting the given values to the given column. ü The WHERE clause indicates the modifying the particular rows.

SELECT columnname AS aliasename FROM tablename

SELECT S.sno , S.sname FROM student S WHERE S.sno=6;

UPDATE tablename SET columnname1=’values’, columnname2=’values’, …… [WHERE condition]

Database Management System(DBMS) Govt. Polytechnic Gajendragada

20 R.S.Malagitti HOD of CSE

ü The user can update the value to any number of columns by using the multiple values and separating those columns using comma (,) operator.

ü If WHERE clause is not mentioned, it will affected to the whole table.

Ø Example -

It modify the record of the column sname as “john” where the record have

sno is 1 It modify the values of two column where condition is satisfied 4.10 DELETE Statement Ø The DELETE statement is used to delete the rows of the table. Ø The general syntax is

Ø This operation will delete all records without WHERE clause. If WHERE clause found, it will delete the particular record where the condition is satisfied.

Ø Example – It will delete all records of the STUDENT table It will delete only records those are satisfied with the WHERE condition.

4.11 VIEW Statement Ø VIEW is used to picking up some column from base table used in SELECT

statement. Ø A VIEW is virtual table. Ø The general syntax is

UPDATE student SET sname=’john’ WHERE sno=1;

UPDATE student SET sname=’john’, address=’landon’ WHERE sno=1;

DELETE FROM tablename [WHERE (condition)]

DELETE FROM student;

DELETE FROM student WHERE sno=1

CREATE VIEW viewname AS SELECT columns [ WHERE condition]

Database Management System(DBMS) Govt. Polytechnic Gajendragada

21 R.S.Malagitti HOD of CSE

Ø Example –

4.12 BETWEEN AND operator Ø The BETWEEN operator is used to select the values within the given range. Ø BETWEEN operator selects the records from the values given in left side and

right side of the AND operator. Ø The general syntax

Ø Example –

4.13 DROP Command Ø The DROP command is used to delete the complete table from memory. Ø The DROP command can also delete the database. Ø It also delete multiple tables and database at a time. Ø The general syntax for deleting table

Ø The general syntax for deleting table

Ø Example -

CREATE VIEW std AS SELECT sname, dname, sem FROM student, department WHERE student.dno=department.dno

CREATE VIEW dept AS SELECT dname FROM department

SELECT columns FROM tables WHERE columnname BETWEEN value1 AND value2.

SELECT * FROM student WHERE dno BETWEEN 1 AND 2

DROP TABLE table1, table2 …….

DROP DATABASE db1 , db2 …….

DROP TABLE student;

DROP DATABASE EMP;

Database Management System(DBMS) Govt. Polytechnic Gajendragada

22 R.S.Malagitti HOD of CSE

4.14 TRUNCATE command Ø TRUNCATE command is used to delete the complete content of the table. Ø This command doesn’t affect to the structure of the table. Ø The general syntax is

Ø Example –

4.15 NESTED QUERIES OR SUB QUERIES Ø A WHERE clause generally contains a condition but it also contains a SQL query Ø A query within a WHERE clause is called as nested query or sub query. Ø The general syntax is

ü The condition may be columns with value and relationships of tables. ü The operator may be > , < , = , IN , >= , =< , != etc

Ø Example –

4.16 TRIGGER Command Ø The TRIGGER is a database object associated with the table. It will activated

when a defined action is executed for the table. Ø The trigger can be executed when the following statements run on the table

- INSERT Statement - UPDATE Statement - DELETE Statement

Ø It can be invoked before or after the event. Ø The general syntax is

TRUNCATE tablename;

TRUNCATE student;

SELECT columns FROM tables WHERE (conditions) OPERATOR (SELECT columns FROM tables WHERE (condition) )

SELECT sno , address , sname , FROM student , dept WHERE student.dno = dept.dno AND Student.dno >=

(SELECT dno FROM dept WHERE dept.no=2)

Database Management System(DBMS) Govt. Polytechnic Gajendragada

23 R.S.Malagitti HOD of CSE

ü The “trigger_time” specifies the activation time. It has two keyword

either “BEFORE OR AFTER” - Here “BEFORE” keyword processing the action prior to change is

made on the table. - Here “AFTER” keyword processing the action after the change is

made. ü The “trigger_event” can be INSERT , UPDATE , OR DELETE

Ø Example –

4.17 IN Operator Ø “IN” operator is used to specify multiple values in a WHERE Clause condition. Ø This operator replaces the OR condition in WHERE Clause. Ø The general syntax is

Ø Example -

CREATE TRIGGER trigger_name trigger_time trigger_event ON table FOR EACH ROW BEGIN …….. END;

CREATE TRIGGER before_insert_result BEFORE INSERT ON result FOR EACH ROW BEGIN IF (new.sub1>100) then SIGNAL SQLSTATE '10000' SET MESSAGE_TEXT= 'Value must be within 100'; END IF; END;

SELECT columns FROM tables WHERE columnname IN [ (value1,value2 …..) OR (Nested query)]

Database Management System(DBMS) Govt. Polytechnic Gajendragada

24 R.S.Malagitti HOD of CSE

Unit –II Data Modeling Using Entity-Relationship Model

2.1 Database Design Phases

Figure 2.1 Database Phases Design Phases

Ø The following are the database design phases

1. Requirement collection and analysis 2. Conceptual design 3. Logical Design 4. Physical Design

1. Requirement collection and analysis ü The objective of requirement analysis is to understanding of the

requirement. ü This phase produces both data requirements and functional requirements. ü The data requirements are used as a source of database design. The

functional requirements are used as source of application software design. ü Collect the data used by the organization. ü Identify relationships in the data. ü Determine how the data is used and generated

Database Management System(DBMS) Govt. Polytechnic Gajendragada

25 R.S.Malagitti HOD of CSE

2. Conceptual Design ü This phase has to create a conceptual schema for a database. ü The conceptual schema describes the data types, relationship, constraints etc ü The result of this phase is Entity-Relationship (ER) diagram.

3. Logical Design ü In this phase, data can be arranged into logical structure and is mapped into

tables using DBMS software. ü Normalization is to eliminate redundancy.

4. Physical Design ü It deals with the physical implementation of the database in DBMS. ü It includes the specification of data elements, data types, constraints, user

access rights etc. 2.2 Entity Type, Entity set , attributes and Key

1. Entity ü An entity can be a real-world object. ü Entity may be an object with physical existence. ü Example – Employee, Student, Department etc

2. Entity Type ü An entity type is a collection of entities that have common attributes. ü In general, Entity Type means Table name

3. Entity Sets ü An entity set is a collection of similar entities. ü Example – Student set may contain all the students of a school

4. Attributes ü The properties of an entity is called “attributes”. All attributes have values. ü Example – Student entity may have name, class, register number etc ü The following are the types of attributes

1. Simple OR Atomic Attributes Ø The attributes cannot be sub-divided further. These attributes hold

single value and not composed of any other attributes. Ø Example – age, gender, semester etc

2. Composite Attribute Ø The attributes can be sub-divided into some more attributes are called

composite attributes. Ø Example – Address, Name etc

3. Single Attribute Ø The attributes have single value and composed, those are called as

single attributes. Ø Example – gender, register number etc

4. Multi-Value Attribute Ø Some attributes have more than one value, those are called multi-

value attribute.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

26 R.S.Malagitti HOD of CSE

Ø Example – Mobile number, email-ID etc 5. Stored Attribute Ø The value of some attributes cannot be obtained or derived from some

other attributes are called as stored attributes. Ø These attribute values are stored in memory Ø Example – name, register number etc

6. Derived Attribute Ø The value of some attributes can be derived from some other

attributes. These are not stored in memory. Ø Example – Average Salary from salary

7. NULL Attribute Ø Attributes may not have applicable value in such case attribute have

hold NULL value. Ø Example - mobile number have NULL value

5. Tuple ü Records or rows of the table are called as Tuple

6. Keys Ø Keys is an attribute that identifies a rows in a table. Ø Keys are used to establish and identify relation between tables. Ø It also ensures that each record or rows within table can be uniquely

identified by one or more columns. Ø The following are the types of key

1. Super Key 2. Candidate Key 3. Primary Key 4. Foreign Key 5. Composite Key

1. Super Key ü Super key is an attribute or combination of attributes in a relation that

identifies a row uniquely within a table. ü Super key is the most general type of key.

2. Candidate Key ü A candidate key is a super key that contains no extra attribute. ü Candidate key is an attribute that can act as primary key for a table to

uniquely identify each record. 3. Primary Key ü Primary key is an attribute that is uniquely identifies each rows/records in a

table ü It is main key of the table.

4. Foreign Key ü Foreign key is an attributes in a relation whose value match a primary key in

other relation.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

27 R.S.Malagitti HOD of CSE

5. Composite Key ü A primary key that consists of two or more attributes is known as

composite key.

2.3 Weak-Entity and Strong-Entity Type Weak-Entity Type Ø Entity has number of attributes without any key attributes are called as Weak-

Entity type. Ø It is represented in double rectangle box. Ø Weak-Entity type normally defines the partial key Ø In general, weak-entity type doesn’t have primary key. Ø For every weak-entity, there should be unique owner entity type. Ø Example –

Department entity is an unique owner entity and employee entity is a weak entity. Strong-Entity Type Ø Entity has number of attributes with key attributes are called as strong-entity type. Ø Strong entity type is also called as regular-entity type Ø Entity set that has primary key is strong-entity type. Ø Example –

Student entity has unique register number, it is used to identify the students so that register number must have keys.

2.4 E-R Diagram Ø E-R diagram is a visual representation of data that describes how data is related to

each other. Ø E-R diagram is graphical representation of information system that contain entity,

attributes and relationships

Database Management System(DBMS) Govt. Polytechnic Gajendragada

28 R.S.Malagitti HOD of CSE

E-R Diagram Notations or Symbols Symbols Meaning

Entity

Relationship

Attribute

Key Attribute

Weak Entity

Multi-Valued Attribute

Composite Attribute

Derived Attribute

Partial Relationship

Total Relationship

2.5 Relationship Types Ø A relationship describes relations between entities. Relationship is represented using

diamond symbol Ø There are 3 types of relationship.

1. Binary Relationship 2. Recursive Relationship 3. Ternary Relationship

Database Management System(DBMS) Govt. Polytechnic Gajendragada

29 R.S.Malagitti HOD of CSE

1. Binary Relationship ü Binary relationship means relation between two entities. There are 3 types of

binary relationship. a. One-To-One Ø When only one instance of an entity is associated with relationship. Ø It is represented as “1:1” Ø Example

b. One-To-Many Ø When more than one instance of an entity is associated with

relationship. Ø It is represented as “1:N” or “N:1” Ø Example -

c. Many-To-Many Ø When more than one instance of an entity on the left hand side and

more than one instance of an entity on the right hand side can be associated with relationship.

Ø It represented as “N:N” or “M:M”

2. Recursive Relationship Ø When an entity is related with itself is known as recursive relationship. It is

possible for same entity to participate in the relationship. Ø For Example

In employee entity, manager is also an employee. The manager number must be referenced from employee number.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

30 R.S.Malagitti HOD of CSE

3. Ternary Relationship Ø Ternary relationship is the one that involves three entity. Ø The degree of the relation is three. Ø Example -

Item - Courier - Customer

2.6 Constraints on Relationship Types Ø There are two constraints on relationship

1. Cardinality Ration Constraints 2. Participation Constraints

1. Cardinality Ratio Constraints Ø Cardinality ratio constraints specifies binary relationships on entity. Ø The cardinality of a relationship is the number of entities to which another

entity can map under that relationship Ø There are 3 types cardinality for binary relationships

a. One-To-One Mapping b. One-To-Many Mapping c. Many-To-Many Mapping

2. Participation Constraints Ø Participation constraints specifies whether the existence of an entity depends on

another entity. Ø There are two types of participation constraints

1. Total Participation ü Each entity is involved in the relationship. It is represented by double line ü Each entity must be depends on the other entity. ü Example

Database Management System(DBMS) Govt. Polytechnic Gajendragada

31 R.S.Malagitti HOD of CSE

2. Partial Participation ü All entities are not involved in the relationship. It is represented by

single line. ü Some entities may be depends on the other entity.

2.7 E-R Diagram for Company Database

Database Management System(DBMS) Govt. Polytechnic Gajendragada

32 R.S.Malagitti HOD of CSE

Unit – III

Relational Data Model and Relational Database Constraints 3.1 Relational Model Concepts ü Relational model represents the database as a collection of tables. ü This model is used for data storage and processing. ü The following are the some concepts of relational model.

1. Relation ü Relation is nothing but table. Each table has set of rows and columns. ü Each rows in relation represents a collection of related data values.

2. Domain ü Domain is defined as the set of all atomic values for an attributes. ü Domain is same as data types, it specifies which type of data can store in its

attributes. 3. Attributes ü The columns of the table are called as attributes.

4. Tuples ü Records or rows of the table are called as Tuple

5. Relation Schema ü A set of attributes is called relation schema. A description of table or relation is

called relation schema. ü It is logical definition of table.

6. Relation State ü Records of the relation may be change at any instant of time is called as relation

state. 3.1.1 Characteristics of Relation

1. Ordering of tuples in relation 2. Ordering of values within a tuple 3. Values and NULLs in the tuples 4. Interpretation of a relation 5. Duplicate tuples are not allowed

1. Ordering of tuples in relation Ø Relation is defined as set of tuples. All tuples in a relation do not have particular

order. Ø Records or tuples are physically stored on hard disk, so there is always order

among the records. Ø Tuples ordering is not part of relation definition.

2. Ordering of values within a tuple Ø Ordering of values within a tuple is irrelevant therefore column ordering is not

important.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

33 R.S.Malagitti HOD of CSE

Ø There should be one-to-one correspondence between attributes and values. 3. Values and NULLs in the tuples Ø Each value in a tuple is atomic means multi-valued and composite attributes

are not allowed in relational model. Ø Each tuple must be either “Value” or “NULL”

4. Interpretation of a relation Ø The relation schema is interpreted as a declaration. Each tuple in a relation can

be interpreted as particular instance. 5. Duplicate tuples are not allowed Ø Attributes may have constraints like primary key, unique. Those attributes

have unique or primary key constraints, must be unique values. 3.2 Relational Model Constraints 3.2.1 Relational Model Constraints Ø Constraints are restrictions on the content of the database or on database

operations. Ø It is a condition specified on the database schema that restricts the data to be

inserted in an instance of the database. Ø Constraints used for

- Maintaining relationship between relations. - Uniqueness - Consistency among tuples between two relation - Identity of attributes

Ø The following are the 3 types of constraints 1. Domain Constraints 2. Key Constraints and NOT NULL constraints 3. Integrity Constraints

3.2.2 Domain Constraints ü Domain Constraints specifies that what set of values an attribute can take. ü Domain constraints states the attribute must accept the values based on the

data type. ü The value of each attribute must be an atomic value from the domain set. ü The domain constraints indicates the data type of each attribute. ü Example –

Here “student” table has 4 attributes with data types SID INT

NAME VARCHAR

SEM VARCHAR

AGE INT

Database Management System(DBMS) Govt. Polytechnic Gajendragada

34 R.S.Malagitti HOD of CSE

3.2.3 Key Constraints AND NOT NULL Constraints 1. Key Constraints ü Keys are attributes or set of attributes that uniquely identify an entity within its

entity set. ü The following are the key Constraints

1. Primary Key 2. Foreign Key 3. Candidate Key 4. Super Key

2. NOT NULL Constraints ü The NOT NULL attribute cannot have NULL value. It enforces that attribute

must have proper value. ü Example -

Table – Student Description ATTRIBUTE NAME DATA TYPE CONSTRAINTS

SID INT PRIMARY KEY

NAME VARCHAR NOT NULL

SEM VARCHAR

AGE INT

Here “NAME” attribute have NOT NULL constraint, so that the value of this attribute cannot be NULL or empty.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

35 R.S.Malagitti HOD of CSE

3.2.4 Integrity Constraints Ø Integrity Constraints are used to ensure accuracy and consistency of a data in a

relation. Ø There are two types of integrity constraints.

1. Entity-Integrity Constraint 2. Referential-Integrity Constraint

1. Entity-Integrity Constraint Ø Entity-Integrity Constraint states that primary key attribute cannot be NULL

value. Ø The primary key attribute must have proper value because it is used to identify

individual rows in a table. If the primary key attribute have NULL value, it is difficult to identify those rows.

Ø The entity-integrity constraints assure that a specific row in a table can be identified.

Ø Example - Table – Student Description

ATTRIBUTE NAME DATA TYPE CONSTRAINTS

SID INT PRIMARY KEY

NAME VARCHAR NOT NULL

SEM VARCHAR

AGE INT

2. Referential-Integrity Constraint Ø The referential integrity constraints state that foreign key attribute of one table must

refers to the primary key attribute of another table. Ø This constraint maintain the consistency among the tuples of two relations means

that tuple in one relation must reference to an existing tuples of another relation.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

36 R.S.Malagitti HOD of CSE

Ø Examples –

Rules

1. Records cannot be inserted into a foreign key table if corresponding records does not exists in primary key table.

2. Records of the master table or Primary Table cannot be deleted or updated if corresponding records exists in the foreign key table

3.3 Relational Database Schemas ü A set of attributes is called a relation schema. ü A relation schema is used for describing the basic information of a table or

relation. ü A relational database schema defines

- Name of the table. - Set of column names of the relation - Constraints of columns in relation

ü A relational database schema represents the logical view of the entire database.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

37 R.S.Malagitti HOD of CSE

ü It shows all attributes with constraints of all relations in database. ü Example –

Company Database Schema EMPLOYEE

EMPID NAME ADDRESS GENDER DNO DEPARTMENT

DEPTID DNAME DLOCATION PROJECT

PID PNAME PLOCATION DNO DEPENDENT

EMPID DEPENDENTID DEPENDENT_NAME GENDER RELATIONSHIP 3.4 Update and Constraint Violations Ø The operations of the relational model are classified into 3 types

1. Insert Operation – The insert operation is used to insert a new tuples on relation.

2. Update Operation – The update operation is used to change or update the values of some attributes in existing tuples.

3. Delete Operation - The delete operation is used to delete existing records of the relation.

3.4. 1 Insert operation violating Constraints Ø The insert operation violating the following constraints

1. Domain constraints 2. Key Constraints 3. Entity-Integrity Constraints 4. Referential-Integrity Constraints

Ø Example – Table Details

Database Management System(DBMS) Govt. Polytechnic Gajendragada

38 R.S.Malagitti HOD of CSE

1. Domain Constraints

2. Key Constraints

3. Entity –Integrity Constraints

Database Management System(DBMS) Govt. Polytechnic Gajendragada

39 R.S.Malagitti HOD of CSE

4. Referential –Integrity Constraints

3.4. 1 Update operation violating Constraints Ø The update operation also violating the following constraints

1. Domain constraints 2. Key Constraints 3. Entity-Integrity Constraints 4. Referential-Integrity Constraints

Ø Example – Table Details

Database Management System(DBMS) Govt. Polytechnic Gajendragada

40 R.S.Malagitti HOD of CSE

1. Domain constraints

2. Key Constraints

3. Entity-Integrity Constraints

Database Management System(DBMS) Govt. Polytechnic Gajendragada

41 R.S.Malagitti HOD of CSE

4. Referential-Integrity Constraints

3.4. 1 Delete operation violating Constraints Ø The delete operation only violating referential-integrity constraint when

relationship exists between two tables. Ø The delete of the records from referencing table mean primary key table is not

possible because records of this table referenced to another table. Ø The user can delete the records from referenced table means foreign key table.

Once it is done then it is possible for deleting records from primary key table. Ø Example - Table Details

Here we applying Delete operation

Database Management System(DBMS) Govt. Polytechnic Gajendragada

42 R.S.Malagitti HOD of CSE

Database Management System(DBMS) Govt. Polytechnic Gajendragada

43 R.S.Malagitti HOD of CSE

Unit – V

Functional Dependencies & Normalization for Relational Databases 5.1 Informal Design Guidelines for relation Schemas Ø The following are the design guidelines for relation schemas

1. Semantics of the relation attributes 2. Reducing the redundant values in tuples 3. Reducing the NULL values in tuples 4. Disallowing the possibility of generating spurious tuples

1. Semantics of the relation attributes ü The semantics refers to the meaning of the attributes, means how the attribute

values in a tuple related to one another. ü Guideline -1

“ Design a relation schema so that it is easy to explain its meaning” ü It implies that do not combine attributes from multiple entity types into single

relation. 2. Reducing the redundant values in tuples ü Redundancy means minimize or reduced the storage space used by the

relations. Same information should not be kept in many places. ü Possible problems caused by redundancy are anomalies. There are 3 types of

anomalies. a. Insertion anomaly b. Update anomaly c. Deletion anomaly

ü Example – Here N students are studying in one department, The values “DNO” and “DNAME” are repeated N times which leads to redundancy. a. Insertion Anomaly

REGNO NAME ADDRESS DNO DNAME ? ? ? 3 EE

Here , cannot insert a new department without having at least one student. This will caused insertion anomaly.

REGNO NAME ADDRESS DNO DNAME 1 AA XX 1 CSE 2 BB YY 1 CSE

Database Management System(DBMS) Govt. Polytechnic Gajendragada

44 R.S.Malagitti HOD of CSE

b. Update Anomaly - During update anomaly must avoid data inconsistency. - Example – If you want move student from one department to another

department. c. Deletion Anomaly - If department closed, then it is difficult to remove the department. If

removes the department all students which are belongs to that department will be deleted.

3. Reducing the NULL values in tuples ü It says that relation must have less number of NULL values. ü Guideline – 3

“Relation should be designed such that their tuples will have few NULL values”

ü Reason for entering NULL values are - The attribute value may be unknown - The attribute value may not available

4. Disallowing the possibility of generating spurious tuples ü It says that relation should not have any undesirable tuples. ü Guideline – 4

“The relation should be designed to satisfy the lossless join condition. No spurious tuples should be generated”.

5.2 Functional Dependencies Ø Functional dependency is a relationship that exists when one attribute uniquely

determines another attribute. OR

Ø Functional dependency describes the relationship between attributes in a relation. Ø If R is a relation with attributes X and Y, a functionally dependency between the

attributes is “XàY”

- It says that “Y” is functionally dependent on “X”. - The left-hand side attribute determine the values of right-hand side

attributes. Ø Example -

Here “REGNO” is a key attributes. Non-key attributes like NAME, ADDRESS, DNO and DNAME must be dependent on the key attribute.

Ø Functional dependency maintain relationship among attributes means key attribute should determines the values of other attributes

Database Management System(DBMS) Govt. Polytechnic Gajendragada

45 R.S.Malagitti HOD of CSE

5.2.1 Types of Functional Dependencies 1. Single Valued Functional Dependency ü This functional dependency says that the attributes in a table must have

single value. ü Example – Age attribute of employee table has single value.

2. Fully Functional Dependency ü It is same as definition of functional dependency.

3. Partial Functional Dependency ü This functional dependency said that one or more non-key attributes are

functionally depending on the part of the primary key. ü Example –

4. Transitive Functional Dependency ü This functional dependency said that “one or more non-key attributes are

functionally depending on the others non-key attributes” ü Example -

5.2.2 Inference Rules for Functional Dependencies Ø There are 6 inference rules

1. Reflexive Rule 2. Augmentation Rule 3. Transitive Rule 4. Additive OR Union Rule 5. Decomposition Rule 6. Pseudo Transitive Rule

Consider the following relation R à {X,Y,Z}

1. Reflexive Rule ü If Y is subset of X, then X determines Y.

X ⸦ Y then X à Y - Y is a proper subset of X. - X can functionally determines X

2. Augmentation Rule ü If X determines Y then XZ determines YZ

X à Y then XZ à YZ It says that every non-key attributes must be fully dependent on the key attributes.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

46 R.S.Malagitti HOD of CSE

3. Transitive Rule ü If X determines Y and Y determines Z then X must also determine Z

X à Y & Y à Z then X à Z 4. Additive OR Union Rule ü If X determines Y and X determines Z then X must also determine Y and Z

X à Y & X à Z then X à YZ 5. Decomposition Rule ü It is reverse of union rule.

X à YZ then X à Y & X à Z 6. Pseudo Transitive Rule ü If X determines Y and YZ determines W(other attributes) then X and Z

determines W X à Y & YZ à W then XZ à W

5.2.3 Minimal Sets of Functional Dependencies 5.3 Normalization Ø Normalization is a process of organizing the data in database. Ø It is a systematic approach for decomposing tables to eliminate data redundancy

and anomalies like insert, update and delete anomaly. Ø Normalization is used for

- Eliminating redundant(useless) data - Ensuring data dependencies i.e data is logically stored.

Ø Generally, normalization is basic rules for applying to tables with help of normal forms.

Ø Normalization rules are divided into following normalization. 1. First Normal Form (1NF) 2. Second Normal Form (2NF) 3. Third Normal Form (3NF) 4. Boyce- Codd Normal Form (BCNF)

Database Management System(DBMS) Govt. Polytechnic Gajendragada

47 R.S.Malagitti HOD of CSE

5.3.1 First Normal Form (1NF) Ø The First Normal Form (1NF) said that “an attributes of a table cannot have

multiple values. It should have only single value”. Ø Each attributes in a table must contain single value. This rule is called First

Normal Form (1NF). Ø Example - Consider the EMPLOYEE table.

Now the below table is in 1NF because all attributes have single value.

EID ENAME DESIGNATION SALARY PNO 1 RAMESH SALES 10000 4566666 1 RAMESH SALES 10000 8777777 2 RAJU EXECUTIVE 13000 2345233 2 RAJU EXECUTIVE 13000 6553433

Ø The First Normal Form will always increases the data redundancy.

5.3.2 Second Normal Form (2NF) Ø The 2NF says that “every non-prime attribute should be fully functional

dependent on prime attribute”. - Prime attribute means attribute must be part of the primary key - Non-prime attribute means attribute is not part of primary key.

Ø Fully functional dependency is a relationship between attributes in a table. Ø Example – Consider the below EMPLOYEE table

EID ENAME SALARY ADDRESS PID PNAME PLOCATION This table is not in 2NF because all non-key attributes are not fully functional dependent on key attribute. Here “PNAME” and “PLOCATION” attributes are not functional dependent on “EID attribute.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

48 R.S.Malagitti HOD of CSE

The above table has been divided into 2 tables to meet the 2NF rules. EMPLOYEE

PROJECT

Now , these two tables are in 2NF, because all non-key attributes are fully

functional dependent on key attributes. EID à {ENAME, SALARY, ADDRESS}

PID à {PNAME, PLOCATION}

5.3.3 Third Normal Form (3NF) Ø The Third Normal Form (3NF) says that “each non- key attributes of a table

must be functionally dependent on key attribute and should not be transitive dependency”

Ø Transitive dependency means non-prime attributes functionally dependent on other non-prime attributes.

Ø If table is in 3NF then - It is in 2NF - No transitive dependency

Ø Example - Consider the EMPLOYEE table

This table follows the 2NF rules but there is transitive dependency between PINCODE, DISTRICT, TALUK and CITY. So this table is not in 3NF. This table has been divided into two tables. EMPLOYEE

EID ENAME SALARY MGRNO PINCODE ADDRESS

PINCODE CITY TALUK DISTRICT Now these two tables are in 3NF, so it follows 2NF rules and no transitive dependency

Database Management System(DBMS) Govt. Polytechnic Gajendragada

49 R.S.Malagitti HOD of CSE

5.3.4 Boyce-Codd Normal Form (BCNF) Ø BCNF is considered as a special condition of 3NF. Ø “A table is in BCNF if every determinate is a candidate key” means non-key

attributes cannot determines the key attribute i.e. primary key attribute. Ø Consider the following table

This table is in 3NF but not in BCNF because dependency exists between attribute C and B means non-key attribute C is determines the values of key attribute B. The dependencies are

AB à {CD} C à B

Converting the above table to BCNF form by dividing into 2 tables A B D

B C

In the above tables, non-key attribute cannot determine the key-attribute. Ø Example –

SNO à Student Number TNO à Teacher Number

SNO, TNO à {CLASS, DATE} It is in 3NF but this is not in BCNF because if know the CLASS, we must know who is teacher of that class.

CLASS à {TNO} “Non-key attribute determines the key – attributes”.

Ø Converting the above table into BCNF

SNO CLASS DATE

CLASS TNO

Database Management System(DBMS) Govt. Polytechnic Gajendragada

50 R.S.Malagitti HOD of CSE

Unit – VI

Fundamentals of Database Transaction Processing 6.1 Introduction Ø A transaction is a collection of operations that perform a single logical operation

in a database. “OR”

Ø The execution of a program that accesses or changes the contents of the database is called transaction.

6.1.1 Single User V/S Multi-User System Ø If single user can access the database at a time is called single-user system. Ø Example: to create a own database Ø If many users can accesses the database concurrently is called multi-user system. Ø Example: Online banking, Railway system.

6.1.2 Transaction Read and Write Operations 1. Read Operation

Ø To read data from database, first it must be brought from hard disk to buffer or RAM. After this step, the program can access the intended records from the buffer.

Steps

1. Find the address of the disk block 2. Copy the disk block into buffer in the main memory 3. Copy the item from buffer to the program variable

2. Write Operation

Ø To write the data into the database, first modification done in buffer and then content of the buffer saved to the hard disk.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

51 R.S.Malagitti HOD of CSE

Steps

1. Find the address of the hard disk block 2. Copy the hard disk into buffer in main memory 3. Copy the value of program variable into buffer. 4. Stores the updated block from buffer to hard disk.

6.1.3 Concurrency Control Ø Concurrency control is a process to ensure that data is updated correctly and

appropriately when transactions are concurrently executed in database. Ø Concurrency control is an essential part of transaction.

Example - If two users try to modify same data in different ways at the same time and then other user wants to access the data. If DBMS allows both of their actions concurrently it would be impossible to recognize which data is updated.

To solve this problem, concurrency control is needed. Ø DBMS accepts one request and blocks or rejects the other request. So that

database make concurrent.

6.1.4 Why Recovery is Needed Ø Database is very large system with lots of data and transaction. The transaction in

the database is executed at each seconds of time. Ø If there is any failure or crash while executing the transaction, then data should not

be lost. It is necessary to recent changes of transaction to previously committed point.

Ø Types of failure 1. Computer Failure 2. System Error 3. Local Errors 4. Disk Failure 5. Physical Problems

Database Management System(DBMS) Govt. Polytechnic Gajendragada

52 R.S.Malagitti HOD of CSE

1. Computer Failure Ø A hardware or software error occurs in the computer system during

transaction execution. Ø Example – Main Memory Failure

2. Transaction or System Error Ø Some operations in the transaction may fails due to errors. Ø Example – Runtime Errors

3. Local Errors Ø During the execution of transaction, some certain conditions are forcibly

cancelling the transaction. Ø Example – Data may not found

4. Disk Failure Ø During read and write operation of the transaction, disk Read/Write head crash. Ø Example – R/W head Damaged

5. Physical Problems Ø These refers the power failure or air-conditioning failure problems Ø Example – Overwriting Disk

6.2 Transaction and System Concepts 6.2.1 State Transition Diagram

1. Active State - This is the initial state, the transaction stays in this state which it is executed.

2. Partially committed - After the final statement of transaction has been executed the database has been changed temporally.

3. Failed - If the transaction is not executed due to the system errors or syntax errors then transaction has been aborted.

4. Committed - After successfully completion of transaction the database has been changed permanently.

5. End - End of the transaction.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

53 R.S.Malagitti HOD of CSE

6.3 Properties of Transactions OR ACID properties of Transaction Ø There are four important properties of transaction.

1. Atomicity 2. Consistency 3. Isolation 4. Durability

1. Atomicity ü This property ensures that either all the operations of a transaction reflected in

database or none of them will be reflected. ü Example

Here, Transaction has two operations, it must be execute both operation.

2. Consistency ü This property ensures that all transactions are executed concurrently. After

executing transactions, the content in consistent state. ü Example

3. Isolation ü This state that one transaction should start execution only when the other

transaction finished its execution. ü Example -

Database Management System(DBMS) Govt. Polytechnic Gajendragada

54 R.S.Malagitti HOD of CSE

ü

4. Durability ü Once a transaction completed successfully, the changes has to be made into the

database permanently even if the system failure. 6.3 Transaction Control Language (TCL) Command Ø Transaction control language commands are used to manage transaction in

database. 1. Commit

- Commit command is used to permanently save any transaction into database. 2. Savepoint

- It is used to temporarily save a transaction so that user can access to the old point.

3. Rollback - This command is used to restores the database. It is also use with savepoint

command to jump to savepoint in transaction.

6.4 Distributed System Ø A distributed system consists of multiple computers and software components that

communicate through a computer network Ø It is centralized system that manages the distributed database.

Advantages 1. Reliability

- If some of the machine crashed, then it will not affected to the entire system, work will continued.

2. Scalability - The distributed system can easily expanded by adding more machines.

3. Sharing Resource - In distributed system, file or resources can be easily shared.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

55 R.S.Malagitti HOD of CSE

4. Flexibility - In distributed system, it is very easy to install, implement and debug new

services. 5. Speed

- The distributed system have more computing power than other system so it’s more speed.

6. Open System - It is open system because it easy to access in very client i.e. local or

remote. 7. Performance

- The collections of processors in distributed system can provide high performance than a centralized system.

Disadvantages 1. Troubleshooting

- Troubleshooting and diagnosing problems is difficult 2. Software

- It supports less number of software’s 3. Security

- Easy access of distributed system will increase the risk of security and sharing of data generates the problem of data security.

4. Networking - The network infrastructure can create several problems such as

transmission problem, overloading and loss of messages.

6.5 NoSQL Ø NoSQL means Non-SQL or Not only SQL Ø It is non relational database system means alternative to the relational database

system. Ø It provides a mechanism for storage and retrieval of data in database within a

fraction of second. Ø It is designed for distributed system where large amount of data needs to be store

in database. Ø NoSQL database does not required fixed schema and it avoids join operation.

6.6 Why NoSQL Ø NoSQL database is used to handle the huge amount of data properly. Ø NoSQL database is used to solve the scalability and availability problem in

relational database. Ø NoSQL database uses "data first and structure later approach". Ø NoSQL provides easier way to access the database through the third party tools

like, Facebook, Whats app, Google+ etc. Ø NoSQL database are stores the data and access the data in a real time.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

56 R.S.Malagitti HOD of CSE

6.7 RDBMS V/S NoSQL RDBMS NoSQL

Structure and organize the data. It is unstructured and unpredictable data.

Is fixed schema. It is not fixed schema.

It must use SQL language No declarative query language. Low scalability and availability. High scalability and availability.

Low performance. High performance. It supports ACID transaction.

A - Atomicity C - Consistency I - Isolation D - Durability

It supports BASE transaction. B –Basically A - Available S – Soft E - Eventual

6.8 History of NoSQL Ø The NoSQL was introduced by "Carlo Strozzi" in 1998. He said that, it is open

source. Ø In 2009, “Rockspace” employees are reused the database as non relational

database. 6.9 CAP Theorem Ø NoSQL designed for distributed system. CAP Theorem provides the requirements

to distributed system. Ø CAP means Consistency, Availability and Partition Tolerance

1. Consistency - All the servers in the system will have the same data so anyone using

system will get the same copy. - Data in the database remains consistent after the execution of an operation.

2. Availability - This system will always respond to a request even if it’s not the latest data

in the system 3. Partition Tolerance

- The system continues to operate as a whole even if individual services fail of can't be reached

Ø NoSQL database follow this different combinations of CAP theorem. Like - CA à Consistency Availability - CP à Consistency Partition Tolerance - AP à Availability Partition Tolerance

6.10 NoSQL Pros / Cons Ø Pros means Advantages

1. High Scalability 2. Distributed Computing

Database Management System(DBMS) Govt. Polytechnic Gajendragada

57 R.S.Malagitti HOD of CSE

3. Low cost 4. Schema Flexibility 5. No complicated relationship

Ø Cons means Disadvantage 1. No Standardization 2. Limited query capabilities 3. Complexity

6.11 Scaling and its Types Ø Scalability is the capability of the system. It is the process of handling large

amount of data in order to accommodate the growth. Ø There are two types of scaling

1. Vertical Scaling - Vertical Scaling means adding resources to single machine in a system. - It increases the power of the single server to deal with load

2. Horizontal Scaling - Horizontal Scaling means more servers to a system.

6.12 NoSQL Categories Ø There are four types of NoSQL.

1. Key-values stores Database 2. Column-oriented Database 3. Graph database 4. Documented-oriented database

1. Key-Value Stores Database ü It is a simplest DB of No SQL and it is designed to handle huge amount of

data. ü This DB uses “hash-table” concept, it has unique key and pointer to a

particular item of data. ü In this database, all data has consists of indexed key and value. ü These databases are designed for storing data in schema, it takes less time. ü Example - Dynamo, Redis, Riak etc

Database Management System(DBMS) Govt. Polytechnic Gajendragada

58 R.S.Malagitti HOD of CSE

2. Column-oriented Database ü This database is always works on “columns” and every columns is treated as

individual. ü This database is designed for storing data as a section of columns. ü It improves the performance of queries. It can access specific column of data in

less amount of time. ü Example - Bigtable, HBase, Simple DB.

3. Graph database ü These database stores the data in graph. Each graph consists of finite set of

nodes or vertices and edges. ü Each node represent an entity, and each edge represent a connection or

relationship ü Every node and edge is defined by a unique identifier. ü Example: Oriented DB, Titan.

4. Documented-oriented database ü This database is a collection of “documents”. ü It uses documents as the central entity for storage and queries. Each document

has a unique key which is used to retrieves the documents. ü These are designed for storing, retrieving and managing document oriented

information. ü Example: Mango DB, Couch DB etc

6.13 Production Deployment Ø Google Ø Facebook Ø Adobe Ø Mozilla Ø Twitter Ø Whats app.

Database Management System(DBMS) Govt. Polytechnic Gajendragada

59 R.S.Malagitti HOD of CSE