0% found this document useful (0 votes)
13 views84 pages

Csn44 Rdbms Notes

The document provides an overview of databases and data modeling, including definitions of data, databases, and database management systems (DBMS). It discusses various DBMS architectures, database models, and the role of a database administrator (DBA), outlining their tasks and tools. Additionally, it covers concepts such as data dictionaries, entity-relationship models, and the importance of database maintenance and backup strategies.

Uploaded by

mokanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views84 pages

Csn44 Rdbms Notes

The document provides an overview of databases and data modeling, including definitions of data, databases, and database management systems (DBMS). It discusses various DBMS architectures, database models, and the role of a database administrator (DBA), outlining their tasks and tools. Additionally, it covers concepts such as data dictionaries, entity-relationship models, and the importance of database maintenance and backup strategies.

Uploaded by

mokanth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

UNIT I CONCEPTS OF DATABASES AND DATA MODELING

1.1 Basic Concepts

Data Data is a collection of facts, which is an unorganized but they can be organized
into useful form.
Two types:
1.Raw Data – It is a data which are collected from different sources and has no
meaning.
2. Derived Data - It is a data that are extracted from raw data and used for
getting useful information. Example: NAME, DOB, MARK, etc.

Database (DB) The database is a collection of inter-related data which is used to retrieve,
insert and delete the data efficiently. Example: Bank Database, Hospital
Database, etc.

DBMS Database management system is a software which is used to manage the


database. For example: MySQL, Oracle, etc. are a very popular commercial
database which is used in different applications
FEATURES OF DBMS:

 Minimum Duplication and Redundancy


 Anyone can work on it.
 Large Database Maintenance
 Provides High Level of Security
 Permanent Storage of Data
 Multi-user Access

CHARACTERISTICS OF DBMS:

Shared The data in the database are stored in such a way that it can be shared
among different users.

Security The data in the database must be protected from the unauthorized Users.
Correctness The data in the database should be correct with real world.

1
Persistence The data in the database must exist permanently.
Independent The data in the database should be independent i.e. changes made to
one data should not affect the other data.

Non redundant The database should not consist of two or more data items representing
same entity.

COMPONENTS OF DATABASE
USER n: Request
Response

USER 1: Request DBMS SOFTWARE Meta Data &


Response
End-user Data

USER 2: Request
Database Response
Administrator Database

i. User
 Users or end users are requests the needed data from the database.
 The DBMS uses query and application program.
ii. DBMS Software
 Gets the request from the users, translates and give response
 It shares the database among the multiple users.
iii. Database
 Database is a collection of interrelated data stored in database.
 Two types of data
a. End user data – raw facts of an end user
b. Meta data – data about the data
iv. Hardware
 All physical devices are called hardware. They are Processor, RAM, hard disk & I/O
Devices.
v. Database Administrator
 Database administrator is a privileged user who is in charge of the DBMS.
 This decides user privileges i.e. single user or Multiuser etc.

DATA DICTIONARY

The data dictionary stores the definition of all database tables. It is called data about
the data. This gives the description about the stored data. The content of the data dictionary are
a) What data is available?
b) Where is the data stored?
c) Who owns the data?
d) How is the data used?
e) Who can access the data?

2
DBMS ARCHITECTURE
DBMS architecture helps us to understand the components of database system and the
relationship among them. The architecture of DBMS depends on the computer system on which it
runs.
a) Overall Architecture of DBMS
Based on the computer system on which DBMS runs, the architecture of DBMS is classified
into three types. They are i) Single tier architecture
ii) Two tier architecture
iii) Three tier architecture

1-Tier Architecture

o In this architecture, the database is directly available to the user. It means the user can directly
sit on the DBMS and uses it.
o Any changes done here will directly be done on the database itself. It doesn't provide a handy
tool for end users.
o The 1-Tier architecture is used for development of the local application, where programmers
can directly communicate with the database for the quick response.

2-Tier Architecture

o The 2-Tier architecture is same as basic client-server. In the two-tier architecture, applications
on the client end can directly communicate with the database at the server side. For this
interaction, API's like: ODBC, JDBC are used.
o The user interfaces and application programs are run on the client-side.
o The server side is responsible to provide the functionalities like: query processing and
transaction management.
o To communicate with the DBMS, client-side application establishes a connection with the
server side.

3
3-Tier Architecture

o The 3-Tier architecture contains another layer between the client and server. In this
architecture, client can't directly communicate with the server.
o The application on the client-end interacts with an application server which further
communicates with the database system.
o End user has no idea about the existence of the database beyond the application server. The
database also has no idea about any other user beyond the application.
o The 3-Tier architecture is used in case of large web application.

b) Three Level Architecture


This architecture has three levels.
1. External level
2. Conceptual level
3. Internal level

1. External level
 It is also called view level. The reason this level is called “view” is because several users can
view their desired data from this level which is internally fetched from database with the help of
conceptual and internal level mapping.

4
 The user doesn’t need to know the database schema details such as data structure, table
definition etc. user is only concerned about data which is what returned back to the view level
after it has been fetched from database (present at the internal level).
 External level is the “top level” of the Three Level DBMS Architecture.
2. Conceptual level
 It is also called logical level. The whole design of the database such as relationship among
data, schema of data etc. are described in this level.
 Database constraints and security are also implemented in this level of architecture. This level
is maintained by DBA (database administrator).
3. Internal level
 This level is also known as physical level. This level describes how the data is actually stored
in the storage devices. This level is also responsible for allocating space to the data. This is the
lowest level of the architecture.

1.2 DATABASE MODEL


The structure of the database is called data model. This is used to describe the data and
the relationship among them.
Types of Database Models
The following are the different types of data models
1. Hierarchical Database Model
2. Network Database Model
3. Relational Database Model

1. Hierarchical Database Model


 This model represents database as a tree structure.
 The data organized as records and the records are stored from top to bottom as a tree.
 The highest level is named as root.
 Each level of the tree contains a row of related records.
 There are parent and child relationship among various levels.

5
Note: GM-General Manager, DGM-Deputy General manager, QC-Quality Control
In the above example, for searching the details of a employee emp4, the DBMS should start from the
root node and move downwards. EMP- Employee.
Advantage : Simple design, it is an efficient model and provides data integrity.
Dis-Advantage : Needs good knowledge to implement and Programming.

2. Network Database Model

 This model represents the database as a graph.


 Each node of the graph is called record.
 The nodes are connected by means of a relationship using linked lists
 It uses two different data structures 1) Record type and 2) Set type
 A record type is used to represent an entity.
 A set type is used to represent the relationship between the record types.

Consider the following 3 tables and records

The network model diagram to the above table as given below

A11 ANAND
CHENNAI
100 10

A11 C1 20 A11 P1 200

Advantage : Complex information can be represented


Dis-Advantage : Programming is difficult due to pointer concepts

3. Relational Database Model

 This model represents the database as the collection of relations.


 The relation is a table with rows and columns.
 The user can query the database using simple query languages.

6
Fields/Attributes/Columns

Roll No Name Age Dept


Records/
1001 Aravind 18 CSE
Tuples /Rows
3002 Karthi 19 EEE
4001 Vinod 18 MECH

Each row in the table is called tuple or record and the column headings are called attributes or
fields

Each column is called domain and it consists of related values

Advantage : Easy to understand, Simple principle

Dis-Advantage : The designer should know the structure of the database.

COMPARISON BETWEEN DATABASE MODELS

SL
NO Hierarchical Database Relational Database Network Database Model
Model Model
1 Represents database as Represents database as Represents database as
Tree structure a collection of Tables Graph
2 Simple design Simple Design Complex Design
3 Complex programming Simple programming Complex programming
4 Single data structure is Table structure is used Two data structures are
used used 1. Record type 2. Set type
5 Example Example Example
Refer its diagram, given Refer its diagram, Refer its diagram, given
above. given above above

ENTITY – RELATIONSHIP MODEL (E – R MODEL)


ER model is a high level conceptual model developed by Chen in 1976. It helps in
database design. A conceptual data model is nothing but a set of concepts that describe the structure
of database and the operations on the database. The components of ER model are
(i) Entity
(ii) Attributes
(iii) Relationships
Entity
An entity is an object or a thing that reflects the real world such as person, place, concept,
activity etc. An entity has a set of properties.
 The value of some set of properties uniquely identified as entity.
 Entities are represented by a rectangle box.
Example: customer.

7
Attributes
The attributes are the properties of an entity.
 Attributes are represented by an ellipse.
Example: Acc no, Address & Name are the attributes of entity named Customer.
The figure given below shows the entity with attributes.

RELATIONSHIPS
Relationships are defined as an association between the entities. Relationships are
represented by “diamond” symbol with the name of the relationship. The following are the
different types of relationship among the entities
1. one to one
2. one to many
3. many to one
4. many to many

1. one to one relationship


A relationship between one entity with only one entity is called “one to one”
relationship. This is represented symbolically as 1: 1.
Example : Relationship between student and register number.

One student is assigned one register no.

2. one to many relationship


A relationship between one entity with many entities is called “one to many”
relationship. This is represented symbolically as 1: m.
Example : Relationship between teacher and students.

One teacher teaches many students

3. many to one relationship


A relationship between many entities with one entity is called “many to one”
relationship. This is represented symbolically as m: 1.
Example : Relationship between students and course.

8
Many students enroll one course.

4. Many to many relationship


A relationship between many entities with many entities is called “many to many”
relationship. This is represented symbolically as m : n.
Example : Relationship between students and games.

Many students play many games.

ER DIAGRAM
The overall logical structure of a database can be expressed graphically by ER Diagram.
E –R diagram Symbols and its meaning

1. E-R Diagram for college system

9
2. E-R Diagram for customer – loan

3. E-R Diagram for library management

1.3 DATABASE ADMINISTRATOR


Server Client technology
The Client Server technology is popular technology used for distributing data on the networks.
The important components of this technology are
 Server
 Client
1. Server
Server is a powerful machine with multi-user operating system. Many users can access
the Server at the same time. Software and data that are to be shared among different users
are stored in this machine. This machine will be controlled by System Administrator or MIS
(Management Information System) staff.

Server

Client 1 Client 2 Client N

10
2. Client
Client is a low-end computer used by individual user. The client can access data and
software stored in the server. The client requests the server and the server responds to
the client.
Advantages:
i) Easy to manage and control
ii) It supports multiple users
Disadvantages
i) Server supports more time for processing
ii) This requires powerful DBMS that runs on the server.
iii) The DBMS is more complex and requires more administrative tasks than
personal computer system.

Distributed Concept
 In distributed system, the data and software are stored on several computers.
 These computers vary in size and configuration, ranging from workstations to mainframes.
 The computers communicate with one another through high speed networks or through
telephone line.
 The computers are referred by different names such as “sites” or “nodes”.

There are two types of transactions in distributed system


1. Local transaction
If the user access data in a single site, it is called local transaction.
2. Global transaction
If the user access data in multiple sites, it is called global transaction.
Advantages
1. Sharing data
User at one site shall be able to access data residing at other sites.
2. Availability of data
If one site fails, the other sites shall continue operating.
Disadvantages
1. Increased cost
Procurement and maintenance cost is higher than centralized system.
2. Security overhead
Security in distributed system cannot be enforced strictly.

11
DBA Definition

It is a process of installing, running and updating a DBMS needs a good administration. This is
done by a person called Database Administrator (DBA).
DBA TASKS
The following are the important tasks [jobs] of DBA.

1. Account Creation
DBA create the new account and password for a user or a group of users
for using the DBMS.

2. Privilege Granting
DBA grants privileges such as read or write or execute or all to the users.

3. Privilege Cancellation
DBA has the right to cancel certain privileges that were previously given to the users

4. Security Level Assignment


DBA assigns security levels to certain accounts for accessing the DBMS.

5. Maintenance
DBA is incharge of upgrading and maintaining the DBMS.

6. Co-Ordination
DBA must co-ordinate with the DBMS vendor and plans for Changes.

7. Recovery
DBA must maintain backups so as to recover from accidental damage.

8. Performance Monitoring
DBA must monitor the performance and make necessary changes to achieve maximum
performance.

DBA TOOLS / UTILITIES


Some of the tools used by DBA are
2. MYSQL
3. SQL Server Management Studio
4. Oracle RDBMS
5. Salesforce
6. Devops
7. Visual Studio Code
8. PhyMyAdmin

DATABASE MAINTENANCE

Database Maintenance is an activity designed to keep a database running smoothly and


efficiently. The maintenance of databases is generally performed by people who are familiar with the
database system.

12
Database Maintenance includes the following
1. Data and log file management

2. Index Defragmentation

3. Statistics

4. Corruption detection

5. Integrity Check

6. Backups

7. File/Data compaction

BACKUP AND RECOVERY

Backup and recovery are the operations done by DBA to protect the data

1. Backup
Backup is a process of copying the whole database onto a cheap storage medium.
Backups help to avoid the following failures:
i. System crashes
ii. Natural and physical disasters
iii. Network failure
iv. Transaction error
v. Exception condition
vi. Disk failure

Backup has 2 types


i. Physical backup
ii. Logical backup

i. Physical backup
In the type of backup, the actual physical database files are copied on a
magnetic tape or CD.

ii. Logical backup


In this type of backup, only the data are extracted from data base using
SQL statements and are stored as a binary file.

2. Recovery
 It is a process of restoring a database to the correct state in case of any failure.
 This varies with the type of the failure. The recovery procedure is given below.
 If there is any damage to the database due to disk crash, the data base is
recovered by copying the files from the backup.
 If there is damage such as transaction error, system failure etc., the database
is recovered by redoing the operations or by reversing the changes that
cause inconsistency.

13
DATABASE REPLICATION
It is the process of repetition and upholding database objects such as relations, where
numerous databases that make up a distributed database system. Changes done at one site are
captured and stored locally before being forwarded and applied at each of the database's remote
locations. Replication employs distributed database technology for sharing data among various sites,
but a replicated database and a distributed database have some differences between them. In a
distributed database, data is available at different locations, but a particular relation has to reside at
only one location.

Advantages of replication

 Replication in a database offers database users various benefits that are termed below
 Availability
 Has Reliability
 Gives high Performance
 Facilitates load reduction
 Provide disconnected computing
 Supports many users
 Supports latest versions or advanced applications

Applications of Replication

Replication maintains a diverse variety of applications that have very different requirements.
Some applications are sufficiently supported with only partial synchronization between the copies of
the database and the corporate database system, while many other applications insist continuous
synchronization between all copies of the database.

1.4 ADVANCED CONCEPTS

DATA WAREHOUSING
Data warehousing is defined as a method of collecting information from many sources and
storing it under a unique model at a single site.

The figure given below shows the overall function of the data warehouse.

Data Sources Cleaning Transformation

New Updates Data Warehouse

14
i) Gathering data from various data sources
 Data warehouse collects data from various data sources such as relational databases,
flat files and on-line records.
 The collected data are stored in databases inside the warehouse.
 The type of data collection used depends on data warehouse architecture.
 In a source the data are collected continuously or periodically.
 But in destination periodically sends request to the data source to send data.

ii) Data cleaning and transformation


 The task of correcting and preparing the data is called data cleaning.
 Data sources deliver data with many inconsistencies.
 Before storing the data into the data warehouse it should be corrected.
 For example: From collected address list removing the duplicate addresses.

iii) Updation of data


 Updates on tables at the data sources must be sent to the data warehouse.
 If the tables in data warehouse are same as sources, updating is easy.

iv) Summarizing data in Data w arehouse


 The raw data generated by a transaction may be too large to store online. Therefore, we
can use summary of transactions for easy querying.
 For example instead of storing data about every sale of clothing, we can store total sales
of clothing by item name and category.

DATA MINING CONCEPTS


The process of extracting or finding hidden knowledge (new information) from large
database is called data mining. The figure given below shows the various steps involved in data mining.

Knowledge
Patterns
3. Evaluation and Presentation

Data Mining
2. Selection and Transformation

Data warehouse
1. Cleaning and Integration

Databases Flat Files

15
(i) Data cleaning :T o r em o ve t h e no i s e a n d inconsistent data
(ii) Data integration : Data from multiple sources are combined
(iii) Data selection : The relevant data are retrieved from database for analysis.
(iv) Data transformation : The selected data are made ready for mining by performing
aggregation operations
(v) Data Mining : intelligent methods are applied to extract data patterns
(vi) Pattern evaluation : Identify the needed patterns, representing knowledge based on some
measures.
(vii) Knowledge presentation: Present mined knowledge t o t h e u s e r b y u s i n g knowledge
representation techniques.

APPLICATIONS OF DATA WAREHOUSING AND DATA MINING

1. Government and Education


 To maintain and analyze tax records, health policy records criminal database, etc.
 Universities use warehouses for
 Extracting students' information
 Understanding student demographics
 Human resource management
 Financial department of universities depends on data warehouses.
2. Health Care & Hospitality Industry
 Health Care sector uses their financial, clinical and employee records are fed to
warehouses. It helps to
 Generate patient reports
 Share data with insurance companies
 Hotel and restaurant services
 Car rental services, Holiday home services, etc.

3. Customer Goods Industry


 Prediction of customer trends
 Inventory management
 Marketing
 Advertising research
 Analysis of sales and production

4. Banking Industry
 Banking Market research (about Prof it and loss) .
 Developing marketing programs.
 Performance analysis of each product (Example: Loan, mortgage etc)
 Interchange and exchange rates for various currencies .

5. Telephone Industry
They use warehouses for
 Consolidating and integrating historical data.
 Analysis of fixed assets of industry.
 Tracking of customer queries, etc.
6. Manufacturing Industry
They utilizes for
 To predict market changes, analyze current business trends.
 Detect warning conditions, view marketing developments, etc.

16
DATA MARTS

A data mart is a subset of a data warehouse focused on a particular line of business,


department, or subject area. Data marts make specific data available to a defined group of users,
which allows those users to quickly access critical insights without wasting time searching through an
entire data warehouse. For example, many companies may have a data mart that aligns with a
specific department in the business, such as finance, sales, or marketing.

Need for Data Mart


o Creates collective data by a group of users
o Easy access to frequently needed data and Ease of creation
o Improves end-user response time
o Lower cost than implementing a complete data warehouses
o Potential clients are more clearly defined than in a comprehensive data warehouse
o It contains only essential business data and is less cluttered.

Types of Data Mart:


There are three types of data marts:
i. Dependent Data Mart

17
 Dependent Data Mart is created by extracting the data from central repository,
Datawarehouse.
 First data warehouse is created by extracting data (through ETL tool) from external sources
and then data mart is created from data warehouse.
 Dependent data mart is created in top-down approach of datawarehouse architecture.
 This model of data mart is used by big organizations.
ii. Independent Data Mart

 Independent Data Mart is created directly from external sources instead of data warehouse.
 First data mart is created by extracting data from external sources and then datawarehouse
is created from the data present in data mart.
 Independent data mart is designed in bottom-up approach of datawarehouse architecture.
 This model of data mart is used by small organizations and is cost effective comparatively.

Hybrid Data Mart

 This type of Data Mart is created by extracting data from operational source or from data
warehouse.

 1Path reflects accessing data directly from external sources and 2Path reflects dependent
data model of data mart.

18
BIG DATA

Big data is a data that are enormous in size and exceeds the processing capacity of regular or
traditional database system.
 Thus big data includes huge volume, high velocity and extensible variety of data
 Big data are produced from search engines, Social networks, weather forecast, etc.
 It is used in enterprises, organizations, companies, business, etc.
 Some of its applications are banking, agriculture, chemistry, data mining, cloud
computing, finance, marketing, stocks, health care, etc.
 Given below are some of the fields that come under the big data
 Black Box data: Airplane, jet etc. used to capture voice which helps during aircraft
 Pow er Grid Data: Holds information about a particular node to a base station.
 Social Media Data: Facebook, Twitter etc. are used big data across the globe.
 Transport data: Includes model, capacity, distance and availability of a vehicle.
 Stock Exchange Data : Hold information about the 'buy' and 'sell' decisions.
 Search Engine Data: Retrieve lots of data from different databases.

The Big data will be of three categories. They are


a) Structured data: Example: Relational data
b) Semi structured data: Example: XML data
c) Unstructured data: Example. Word, PDF, text, media logs.

CHARACTERISTICS OF BIG DATA

The characteristics of big data are given below


1. Var iet y
2. Velocity
3. Volume
4. Veracity
5. V a l u e

1. Variety
 Variety refers to the type of data that big data can contain. i.e.,
 The variety may be structured or unstructured.
 The structured data such as Table, data with its type etc.
 The unstructured data such as Word, PDF, text, media logs.

19
2. Vel oci t y
 Velocity refers to the time in which big data can be processed.
 It measures the speed of data generation, filtering and arithmetic operation.
 It is used for time sensitive processes. Ex. Fraud detection, big data flows.
3. Vol ume
 Volume refers to the quantity of data collected by a company.
 This data is used further to gain important knowledge.
 Example: Using 700 billion annual meter reading to predict power consumption.
4. Veracit y
 Veracity refers to the degree of trusted information to make a decision.
 Finding the trusted data in big data is very important for business future.
 When the number of data sources grows, the trust in big data is a big challenge.
5. Value
 Value refers the usefulness of data in making decisions.
 Data value depends on processes. It is required to collect data, stored longer.
 Hence the data value is closely related to the data volume and variety.

VARIOUS TECHNOLOGIES USED


There are various technologies available to store and analyze big data.

I. Apache Hadoop
 Hadoop can effectively store large amount of data in a cluster.
 It is java based open source framework.
 HDFS (Hadoop Distributed File System) is the storage system of Hadoop.
 HDFS splits and distributes big data across many nodes in a cluster.
 Hadoop has 3 components i.e.1. HDFS, 2. Map reduce, 3. YARN
II. Microsoft HD insight
 It is a big data solution from Microsoft.
 It is powered , by Apache Hadoop.
 It is available as a service in the cloud.
III. NoSQL [Not Only SQL]
NoSQL deals with the unstructured unpredictable kind of data.
 NoSQL gives better performance in storing massive amount of data.
 It is not based on table formats.
 It is scalable i.e., no need to expand server size.
 Here data is stored as key value or document.
 It refers to Hbase, Cassandra, Mongodb, Riak, and CouchDB.
IV. Hive
 This is a distributed data management for Hadoop.
 It supports SQL like query option Hive SQL (HSQL) to access big data.
 It is used for data mining purpose.

20
V. Sqoop
 It is a tool that connects Hadoop with various databases to transfer data.
 This is effectively used to transfer structural data to Hadoop or Hive.

VI. PIG
 PIG in Hadoop is similar to Hive developed by Yahoo.
 PIG consists of a ‘Pert-like’ Language that allows for query execution.
 It is fully open-source.

BIG DATA APPLICATIONS


 SAP: Oracle, IBM, HP and SAP jumped into the big data platform to provide purchase built
analytical systems to new and existing customers.
 Agriculture: A biotechnology firm uses sensor data (big data) is optimizes crop efficiency.
 Credit Cards: Credit card companies rely on the speed and accuracy of big data
systems to identify fraudulent transactions.
 Stock: A private stock exchange in Asia uses big data systems to detect abusive trading
patterns to detect fraud.
 F i n a n c e : Today banks perform their own credit score analysis for existing customer using a
wide range of data including checking, savings, credit cards, etc.
 Enterprise: For enterprises, big data systems are helping business people to make
decisions in real time.
 Economy: Hadoop helps organizations to transfer to low cost servers.
 Smart Phones: In smart phones, big data tools use face recognition technology for various
tasks. Eg: To unlock the device
 Data mining: It is the process of finding the hidden knowledge. They give decision about
disease risk, fraud risk, purchases, etc.
 Banking: In banking, for storing and retrieve customer data.
 Marketing: Big data systems help marketers to create ads which go "viral" and
improve sales.

OVERVIEW OF NOSQL
 NoSQL deals with the unstructured unpredictable kind of data.
 NoSQL is defined as a database which has a set of common observations such as
 NoSQL also called non-relational storage systems.
 In this not using the relational model
 It is running well on clusters
 Execution steps are very less (i.e. Schema)
 Mostly open-source
 Reason for using NoSQL database as follows
 Avoid complexity in relational databases.
 No complexity in cost.
 High throughput(Passing higher volume of data)
 Running on any hardware easily.

21
DIFFERENCES BETWEEN RDBMS AND NOSQL
S.No RDBMS NoSQL
1 It is called Relational Database It is called non- relational database
2 It is table based It is document based
3 It is used as SQL - Structured Query Language It is used as un structured query
[Structured Means length & type of data] language
4 It is Vertically Scalable It is horizontally scalable
5 It is fit for Complex queries It is not fit for Complex queries
6 It is not fit for hierarchical database It is fit for hierarchical database
7 Example: MySQL, Oracle, SQLite, Postgres, Example: MongoDB, Big Table, Redis,
MS-SQL Server 2008 HBase, CouchDB and Neo4

TOOLS USED IN BIG DATA

1. Hadoop: Apache tool, used by many large corporations. This handles huge amount
of data sets in cluster based.

2. Cloudera: It is the most popular provider and supporter of Apache Hadoop. It reduces
business risks and gives the business a competitive advantage. This tool is used by all
corporate users who use the data for different purposes.

3. MongoDB: It is an alternative to relational databases. These changes the data in structured


or semi structured. Users: Mobile App, Catalog, Multiple platforms.

4. S t o r m : Storm does data processing capabilities in real time. It integrates with many other
tools such as Apache slider to manage and secure the data.
5. Cassandra: It is widely used tool, Manages large amount of data, Fault tolerance,
Durability and scalability. Users: eBay, Netflix

6. Open Refine: Open Refine is used to organize data in the data-base.

7. Rapid miner: It is an open source science platform. If functions through visual


programming. It allows us to manipulate, analyze, create models and integrate the data
into business processes.

SCALABILITY OF BIG DATA


The constant increase in the volume of big data from worldwide, we need to
manage the overflow. The method of managing this overflow called scalability. A scalable
application platform accommodates
 Rapid growth in traffic and data volume (scaling up)
 Adapts to decrease in demand (scaling down)
 Scalable platform is an elastic on-demand database. Example. Gaming application

22
UNDERSTANDING STORAGE ARCHITECTURE

 Big data architecture is the logical and physical layout of how big data will be stored,
accessed and managed within a big data or IT environment.
 The storage architecture design is shown in fig.

Access-Request
Client Network

Message Sync.

Master Node

Local Balancing

Slave Node Slave Node Slave Node


Data Block 0 Data Block 0 Data Block 0

Data Block 1 Data Block 1 Data Block 1

Data Block N Data Block N Data Block N

 This architecture avoids data loss and damage.


 Virtualized resources are integrated into the master node .
 It is responsible for monitoring the daily operation of the slave nodes.
 Multiple slave nodes achieve load balanced distribution of stored data.
 Data may be stored in different data nodes and blocks.

Storage System Access

 Storage system access flow begins with ‘access request’ sent by ‘client’.
 Then the message accesses near ‘master node’ server through network.
 Now the ‘master node’ locates ‘data block’ in specified slave node depending on the client
request.
 As long as connecting to the Internet, client terminal can access the system.
 The terminal can be hardware, mobile devices, fixed devices, embedded devices, etc.

23
UNIT II RELATIONAL DATA MODEL & MYSQL ADMINISTRATION

2.1 Relational Data Model


A Relational Database Management System (RDBMS) is a database management system
based on relational model introduced by E.F.Codd. RDBMS is used to manage relational database.

Relational database is a collection of organized set of tables from which data can be accessed
easily.

CODD’S Rules

1. High-level insert, update and delete


RDBMS must support insertion, updating and deletion at the table level.
2. Information rule
Data are stored in the relational database in the form of tables with information like
attributes and constraints.
3. Non-subversion rule
When information is accessed by the other language, it should not change integrity
constraints. This is necessary for data integrity.
4. Distributive Independence rule
The end-user must not be able to see that the data is distributed over various locations.
Users should always get the impression that the data is located at one site only.
5. Integrity independence
Like table and view definitions, integrity constraints must be stored in the database. If
any constraints are changed, this should not affect the application programs.
6. View updating rule
Any views that are theoretically seen can be updated by using the RDBMS.
7. Comprehensive data sub-language rule
The RDBMS may support several languages. These languages should allow the users to
define tables, views, and update database.
8. Database description rule
The description of database and its contents are maintained in the tables in data
dictionary. These can be queried by the user using data manipulation language if they
have the privilege.
9. Guaranteed access rule
Each and every data must be accessed by using the table name and the primary key
or column name.
10. Physical data independence
The changes made in the physical data storage should not affect the application
programs.

1
11. Logical data independence
Logical changes in the tables such as adding or deleting columns or changing field
lengths should not require modification on the program.
12. Systematic treatment / Null rule:
RDBMS distinguishes between zeros, blanks and nulls in the records and handles it. If
any constraints or rule is not provided, RDBMS must provide null value for the missed
values.
Components of RDBMS

1. Table Structure

Tables are the basic building blocks of relational database system. All the data are stored in
the tables in rows and columns. Headings are called attributes. Table can have zero or more rows
of data values. For example employee table is given below.

Explanation

 The above table contains four columns and three rows.


 Each column name is called attribute and have many rows of values.
 Each row of value is called record or tuple.
 The set of all values in an attribute is called the domain of the attribute.

1. Domain is a set of all data values in a particular column of the table.


Example: The domain for the column name in the employee table are:

BOSE, MANI, RAHUL, VINOD

2. Attributes are column names which describe the table.


Example: The different attributes in the given employee relation is

ROLL, NAME, AGE, DEPT.

3. Tuples: A table consists of number of rows. These rows are called tuples.

Example: 1099, VINOD, 18, IT

2
4. Degree: The number of attributes in a relation is called degree of the relation.
Example: The degree of the STUDENT table is 4 (columns).

5. Cardinality: The number of tuples (rows) in a relation is called cardinality of the


Relation. This cardinally changes when more rows are added or deleted.

Example: The cardinality of STUDENT table is 4 (Rows).

6. Keys: Key is defined as an attribute or group of attributes are used to uniquely identify a tuple or
row in a relation.

Keys

Key is defined as an attribute or group of attributes that is used to uniquely identify a row in a
relation. The different types of keys are

(1) Primary key


(2) Composite key
(3) Unique key
(4) Foreign key
1) Primary key

Primary key is a column or a set of columns that is used to uniquely identify a row in a
relation. The primary key column values should be unique and not null. If more than one
column is used in primary key, it is called “Composite key”.

Syntax: Creating primary key with “create table” command

create table tablename ( columnname1 datatype primary key , columnname2


datatype,……. ) ;

where

tablename - name of the table

columnname - name of the columns

keycolumn - column name(s) on which primary key is created

Example:

mysql > create table employee (empid int primary key , name varchar(40) , deptno int ,

salary int);

3
2) Composite key

If more than one column is used in primary key, it is called “Composite key”.

Syntax : Creating candidate key with “create table” command

create table table_name ( columnname1 datatype, columnname2 datatype, … ,


primary key(keycolumn1, keycolumn2) );

where
tablename - name of the table

columnname - name of the columns

keycolumn1 and 2 - column name(s) on which composite key is created

Example:

mysql > create table employee (empno int , name varchar(40) , deptno int ,

salary int ,primary key (empno,name) ) ;

3) Unique key

Unique key is a column or a set of columns that is used to uniquely identify a row in a
relation. It is similar to primary key in enforcing values to be unique, but allows null values.

Syntax : Creating Unique key with “create table” command

create table tablename ( columnname1 datatype, columnname2 datatype, … ,


unique key (keycolumn) ) ;

where

tablename - name of the table

columnname - name of the columns

keycolumn - column name(s) on which unique key is created

Example:

mysql > create table employee (empno int , name varchar(40) , deptno int ,

mobile int , unique key (mobile) ) ;

4
4) Foreign Key

A foreign key is a column or set of columns in one table whose values must have matching
values in the primary key column of another table.

Consider a table “t1” has a column “c1” which is the primary key in another table “t2”. Then
the column “c1” in table “t1” which refers to the primary key of table “t2” is called “foreign
key”. Here, the table “t1” is called “child table” and the table “t2” is called “parent table”. The
data type and size of the foreign key should be same as that of the referring primary key.

Syntax :

create table childtablename ( columnname1 datatype, columnname2 datatype, … ,

foreign key (fkeycolumn) references parenttablename (pkeycolumn) ;

where

childtablename - name of the child table

parenttablename - name of the parent table

fkeycolumn - foreign key column name in child table

pkeycolumn - -primary key column name in parent table

Example:

Creating parent table “department” with primary key:

mysql > create table department (deptno int, dname varchar(40));

Creating child table “employee” with foreign key:

mysql > create table employee (empno int , name varchar(40) , deptno int ,

salary int ,foreign key (deptno) references department (deptno) ) ;

Meta Data

The data about data called Meta data.


This normally includes name, type, range of values, Source and access authorization.
This indicates which application programs use the data.
Suppose, if the data is altered then its affected programs can be generated.

5
Data Dictionary

The Data Dictionary holds the definitions of all the data tables. The data dictionary is also
called as catalog.

The contents of the Data Dictionary are:


 What data is available?
 Where is the data stored?
 How is the data used?
 Who can access the data?

Data Integrity

The following categories of Data Integrity exist in the DBMS.

1. Entity Integrity: No Duplicate rows in the table.

2. Domain Integrity: Enforces valid entries for the given column by type format or range of
values.

3. Referential Integrity: Rows can’t be deleted which are used by other records.

4. User Defined Integrity: Enforces some business rules that don’t fall into entity, domain or
referential integrity.

Data Constraints and Validation

Constraints are policies used to maintain the accuracy and integrity of data in the database.
Any one of the following constraints can be applied to the table.

Types of constraints

1. Referential integrity constraints (PRIMARY KEY, FOREIGN KEY)


2. Entity integrity constraints (UNIQUE , PRIMARY KEY)
3. Domain integrity constraints (NOT NULL & CHECK)

1. Referential Integrity Constraints


It is used to establish a parent-child relationship between 2 tables having a common column.
Assume that x is primary key (parent) and y is foreign key(child), then it is said to be x y. Example:
Consider the relations given below

6
Table Name:

Using the above diagram, creating of 2 tables with constraint and without constraint

Syntax(Primary key):

Create table tablename(column1 datatype primary key,column2 datatype,…);

Syntax(foreign key):

Create table tablename(column1 datatype,column2 datatype,…., foreign key(column1)


references parenttablename(column1));

Example:

Parent table:

create table customer(custid int primary key, name varchar(25), address varchar(5));

Child Table:

create table accounts(accid int primary key, balance float(8,2), custid int, foreign key(custid)
references customer(custid));

2. Entity Integrity Constraints

It is used to enforce consistency in the database. It has two commands.

i. UNIQUE KEY ii. PRIMARY KEY

I. UNIQUE Key constraint:


It is used to prevent the duplication of data in a column/group of column. If UNIQUE is more

than one column then it is called composite constraints.

A. Column level constraint: Constraint applying to a particular column

Syntax:

Create table tablename(column1 datatype unique,column2 datatype,…..);

7
Example:

Create table student (rollno int unique, name varchar(15),address varchar(15));

B. Table level constraint: Constraint applying to a complete table

Syntax:

Create table tablename(column1 datatype ,column2 datatype,…..,unique(column1));

Example:

Create table student (rollno int , name varchar(15),address varchar(15),unique(rollno));

II. PRIMARY KEY constraint:


It is used to identify each row uniquely (individually). This does not allow NULL and
duplicate.

A. Column level constraint


Syntax:

Create table tablename(column1 datatype primary key,column2 datatype,…);

Example:

create table student(rollno int primary key, name char(25), address varchar(5));

B. Table level constraint


Syntax:
Create table tablename(column1 datatype ,column2 datatype,…,primary key(column1));

Example:

Create table student (rollno int, name varchar(25), address varchar(5),primary key(rollno));

3. Domain Integrity Constraints

It is used to verify whether the data entered is in proper form and range. It has

2 commands.

I. NOT NULL constraint:


It is used to make the user to enter values to the field compulsory. If not entered, record
will not be validated.

A. Column level constraint


Syntax:

Create table tablename(column1 datatype not null,column2 datatype,…);

Example:

create table student(rollno int not null, name varchar(25), address varchar(5));

B. Table level constraint

Syntax:

Create table tablename(column1 datatype,column2 datatype,…,not null(columnname));


8
Example:

create table student(rollno int, name varchar(25), address varchar(5), not null(rollno));

II. CHECK constraint:


It is used to enforce an integrity rule based on a logical expression. Some of the operators
are given below.

Syntax: check columnname operator (set of permitted values);

Operators are: IN, NOT IN, BETWEEN, LIKE

Examples:

i. IN: CHECK (gender IN(‘Male’, ‘Female’));

ii. NOT IN: CHECK(name NOT IN(‘Raja’, ‘Ravi’, ‘Akshaya’));

iii. BETWEEN: CHECK(roll BETWEEN 1001 AND 1100);

iv. LIKE: This is used to perform pattern matching. They are Percentage (%) &
underscore(_).

Example1: CHECK(roll LIKE ‘1%’); // ROLL starts with 1

Example2: CHECK(name LIKE ‘R_j’); // Name; 1st letter is R & 3rd letter is J

A. Column level constraint


Example:

Create table student(rollno int check(roll between 1001 and 1100), name varchar(25),

address varchar(10));

B. Table level constraint

Example:

Create table student (rollno int, name varchar(15),address varchar(10), check(name

like ‘A%’));

9
Difference between MySQL & SQL
MySQL SQL

1 It uses the concept of Relational Database It is structured Query Language


and management system.

2 It is declarative computer language aimed at It is a relational database - a piece of software


querying relational databases. optimized for data storage and retrieval.

3 It is open source It is cost based.

4 Selected data can be displayed, update and Selected data can be displayed, updated and
saved again with different name. saved with same name.

5 More Features like stored procedures, Features of procedures, triggers and cursor are
triggers and cursors are with sample in PL/SQL only.
procedure.

6 It has some constraints to update and delete No restrictions for Update and delete data.
data.

2.2 Normalization

Normalization
It is a process of building relational database structures without data redundant or duplicate

data. It was developed by E.F. CODD.

Benefits

i. To maintain redundancy and inconsistency in database


ii. To avoid wrong data during insert/update/delete
iii. To reorganize after modification/delete
iv. To reduce the loss of data.

Types of Normalization

1. First normal Form (1NF)


2. Second Normal Form (2NF)
3. Third Normal Form (3NF)

1. First Normal Form (1NF)

A relation is said to be in “first normal form”,

• If every record is unique.

• If every attribute of the table is atomic, that is the values should not be multi-
valued or composite.

10
Multi-valued attribute

Multi-valued attribute is the attribute that can have more than one value in a single
record. Consider the table “employee” where an employee can work in more than one
project as given below

emp_id emp_name proj_name

1 arun Bank, SCHOOL

2 babu web

Here, “proj_name” attribute is multi-valued and the table is not in 1NF. To conform to
1NF, the table is split into two as given below

Employee1: Employee2:

emp_id emp_name emp_id proj_name


1 arun 1 bank
2 Babu 1 School
2 web

Composite attribute

Composite attribute is an attribute that follows some structure and can be split further
into meaningful data.

Consider the following table “employee”

emp_id emp_name address

1 arun 10, gandhi street, puducherry

2 babu 20, nehru street, cuddalore

Here, “address” attribute is composite attribute and hence the table is not in 1NF. To
conform to 1NF, the structure of the table is changed as given below

emp_id emp_name door_no street city

1 arun 10 Gandhi street puducherry

2 babu 20 nehru street cuddalore

11
2. Second Normal Form (2NF)

A relation is said to be in “second normal form” if both the following conditions hold

• table is in 1NF

• If every non-key attribute is dependent on the entire primary key.


Consider a table “company”

emp_id emp_name emp_address proj_id proj_name proj_months

E1 arun puducherry P1 bank 24

E1 arun puducherry P2 school 12

E2 babu cuddalore P3 web 6

The table is in 1NF because each attribute has atomic values. Here, (emp_id,
proj_id) is the primary key. The non-key attributes emp_name and emp_address are
dependent only on emp_id and not on proj_id. The non-key attributes proj_name and
proj_months are dependent only on proj_id and not on emp_id. So the table is not in 2NF.
To conform to 2NF, the table is split into 3 tables as follows

Table “employee”

emp_id emp_name emp_address

E1 arun puducherry

E2 babu cuddalore

Table “project”

proj_id proj_name proj_months

P1 bank 24

P2 school 12

P3 web 6

12
Table “emp_project”

emp_id proj_id

E1 P1

E1 P2

E2 P3

Now, the tables are in 2NF.

3. Third Normal Form (3NF)

A relation is said to be in “third normal form” if both the following conditions hold

• Table is in 2NF.

• It has no transitive functional dependency of non-key attribute on super key,


that is, every non-key attribute is directly dependent on the entire primary key.

Transitive dependency is dependency as follows

If X -> Y and Y -> Z, then X -> Z.

Consider table “employee”

emp_id name city state


E1 arun puducherry puducherry
E2 babu cuddalore tamilnadu
E3 chitra chennai tamilnadu
E4 daniel chennai tamilnadu

The primary key is “emp_id”. Here, the non-key attributes “name” and “city” are
directly dependent on the primary key “emp_id”; the non-key attribute “state” is dependent
on “city” and transitively dependent on “emp_id”. This violates the rule of 3NF. To conform to
3NF, the table is split into two table as follows.

Table “employee”

emp_id name city


E1 arun puducherry
E2 babu cuddalore
E3 chitra chennai
E4 daniel Chennai

13
Table “city_state”

CITY STATE
Puducherry Puducherry

Cuddalore Tamilnadu

chennai Tamilnadu

2.3 MYSQL Installation

MYSQL FEATURES
 Relational Database Management System (RDBMS)
 Easy to use
 It is secure
 Client/ Server Architecture
 Free to download
 Compatible on many operating systems
 Allows roll-back
 High Performance
 High Flexibility
 High Productivity

DOWNLOADING MYSQL

Follow these steps:


 Go to MySQL official website http://www.mysql.com/downloads/
 Choose the version number for MySQL community server which you want.
Installing, Configuring and Test MYSQL on Windows

To install MySQL server version 8.0 on windows, the minimum operating system requirements is
windows 10.
The installation procedure is given below.
1) Execute MySQL installer.
2) In the License Agreement window, enable I accept the license terms and click it.
3) In the Choosing a setup type window, choose the appropriate setup type for your system.
i) Choose Developer default to install MySQL server and other MySQL tools
related to MySQL development, helpful tools like MySQL workbench.
ii) Or choose the custom setup type to manually select your desired MySQL
products.
Click next.
4) Next installation window appears. It shows the products to be installed. Click Execute.
5) After the products are installed, installation window shows next button. Click it.
6) Next Product Configuration window appears. Click next.
7) Next Type and Networking window appears. In the Config Type, Select Development Machine
and Click Next.

14
8) Next Accounts and Role Window appears. Here give MySQL root password. If necessary, add
one user and click next.
9) In the windows service window, keep the default options as it is and click next.
10) Now Plugins and Extensions window appears. Click next.
11) In the Apply server configuration window, with Configuration steps tab chosen by default, click
Execute.
12) After the configuration is completed, click Finish.
13) Finally, connect to server window appears which indicates that MySQL server 8.0 is
successfully installed. Now to check whether the main user root is connected successfully with
the server, click the button check. Connection successful message appears. Click next.
14) Next Apply server configuration window with log tab enabled appears. Click Execute.
15) After the configuration is completed, finish button appears, Click it.
16) Now in the product configuration window, click next.
17) And finally installation complete window appears. Click finish for completion.

Starting and Stopping Connections to the MYSQL Server

Click start All programs MYSQL From that choose MYSQL Administrator Give Password for
root.
Enter password: ****

The welcome screen appears as shown in Fig.

After connecting to the MySQL server, the mysql> prompt appears. Now user can
enter the sql commands. To see the command line help type ‘\h’ on the mysql prompt.

mysql>

To exit MySQL just type QUIT or EXIT

mysql> QUIT

mysql> exit

15
2.4 Working with MYSQL Admin

1. Creating Database
Create database command is used to create a new database.

Syntax1 Create database databasename;


Example Create database polytechnic;

Syntax2 Create database if not exists databasename;


Example Create database if not exists polytechnic;

Creates if ‘polytechnic’ database does not exist.


2. Dropping Database
Drop command is used to delete an existing database.

Syntax1 Drop database databasename;


Example Drop database polytechnic;

Syntax2 Drop database if exists databasename;


Example Drop database if exists polytechnic;
Polytechnic database has been dropped.
3. Selecting Database

Once the database has been created, you will need to select it in order to begin working.
i. Use()

Syntax Use databasename;


Example Use polytechnic;

ii. Select()

Syntax Select database ();


Example Select database ();

The current database has been shown.

Output:
DATABSES()
polytechnic

4. Showing Database

Show database command is used to list all the existing databases in the server.

Syntax Show databases;


Example Show databases;

16
Output
Databases
------------------------
Information_schema
Mysql
Performance_schema
Test
Polytechnic_college

5. Describing Database

It describes the structure of table.

Syntax Describe tablename;


Example Describe student;

Backing up Databases

MySQL dump utility programs is used to backup databases.


For windows, i) Start a new ‘cmd’.
ii) Changes to bin directory.
iii) Give MySQL dump command to backup i.e. Assume that the backup file is to be
used stored in d:\and the name of the backup file is backup_poytechnic.sql.
Syntax
mysqldump –u root –p database name > drivename:\backupfilename.sql.
Example
C:\program files\MySQL\MySQL server 8.0\bin\
mysqldump –u root –p polytechnic>d:\backup_polytechnic.sql.

Restore Database

To restore the database MPC available in the file d:\backupfile.sql, give the
following command.

Syntax
mysql –u root –p database name < drivename:\backupfilename.sql.

Example

C:\program files\MySQL\MySQL server 8.0\bin> Mysql –u root –p Polytechnic <


d:\backup_polytechnic.sql

Enter Password: ****

17
UNIT –III INTERACTIVE MYSQL

3.1 Introduction to MYSQL

MySQL Data Types

MySQL uses many different data types broken into four categories

 Numeric data Types


 String data Types
 Date and Time data Types
 Spatial data Types
1. Numeric Data Types
Numeric data types represent numbers.

Type Name Meaning


TINYINT (1 byte) A very small integer
SMALLINT (2 bytes) A small integer
MEDIUMINT (3 bytes) A Medium-sized integer
INT(4 bytes) A Standard integer
BIGINT (8 Bytes) A large integer
DECIMAL A Fixed point number. (The maximum
number of digits for DECIMAL is 65)
FLOAT A Single precision floating point number.
(A precision from 0 to 23 results in a four-
byte single-precision FLOAT column)
DOUBLE A double-precision floating point number.
(A precision from 24 to 53 results in an
eight-byte double-precision)
BIT A bit field. (A type of BIT(N) enables
storage of N-bit values. N can range from
1 to 64.)

2. String Data Types

Strings can have anything, even arbitrary binary data such as images or sounds.

Type Name Meaning


CHAR A FIXED length character string
VARCHAR A variable length character string
BINARY A fixed-length binary string
VARBINARY A variable length binary string
TINYBLOB A very small BLOB(Binary Large Object)
BLOB A small BLOB
1
MEDIUMBLOB A medium-sized BLOB
LONGBLOB A large BLOB
TINYTEXT A very small non-binary string
TEXT A small non-binary string
MEDIUMTEXT A medium-sized non-binary string
LONGTEXT A large non-binary string
ENUM An Enumeration; each column value may be assigned one
enumeration member.
SET A set; each column value may be assigned zero or more
set member.

3. Date and Time Types

Type Name Meaning


Date A date value, in ‘YYYY-MM-DD’ format
Time A time value, in ‘hh:mm:ss’ format
Date time A date and time value, in ‘yyyy-mm-dd hh:mm:ss’ format
Timestamp A timestamp value, in ‘yyyy-mm-dd hh:mm:ss’ format
year A year value, in YYYY or YY format

4. Spatial Types

Type Name Meaning


Geometry A spatial value of any type
Point A point (a pair of X,Y coordinates)
Linestring A curve( one or more POINT values)
Polygon A polygon
Geometry Collection A collection of Geometry values

DDL (Data Definition Language) Commands

Data Definition commands are used to do the following operations on the database objects. The
operations are,
1. Creating tables
2. Altering tables
3. Renaming tables
4. Copying tables
5. Deleting tables

2
1. Creating Table
The MySQL CREATE TABLE command is used to create a new table into the
database. A table creation command requires three things:
 Name of the table
 Names of fields
 Definitions for each field

Syntax
CREATE TABLE tablename (columnname1 datatype, columnname2 datatype ...);

Example
Here, we will create a table named as "stud" in the database "student".

Create table stud (rollno int, name varchar (15));

Create table stud (id int primary key, name varchar (50));

2. Altering Table

 ALTER statement is used to change the structure of the table.


 The ALTER statement is always used with "ADD", "DROP",”CHANGE” and "MODIFY"
commands according to the situation.

I. New columns can be added

Syntax

Alter table tablename ADD newcolumnname datatype;

Example

Alter table student ADD address varchar (50);

It adds a new column address at the end of the table student.

II. Width of the column can be changed

Syntax

Alter table tablename MODIFY oldcolumnname datatype;

Example

Alter table student MODIFY address varchar (60);

It changes the width of the address field from 50 to 60 in the table student.

3
III. Datatype of the column can be dropped

Syntax

Alter table tablename DROP COLUMN columnname;

Example

Alter table student DROP COLUMN address;

It drops the column address from the table student.

IV. Name of Column can be changed

Syntax

Alter table tablename CHANGE COLUMN oldcolumnname newcolumnname datatype;

Example

Alter table student CHANGE COLUMN dateofbirth dob date;

It changes the column dateofbirth in table student to dob with the column definition date;

3. Renaming Table

RENAME COMMAND is used to change the name of the table.

Syntax
RENAME TABLE oldtablename TO newtablename;

Example Rename table friends to user friends;


This will rename the friends table to user friends.

4. Copying Table

To copy data from an existing table to a new table.

Syntax1 CREATE TABLE newtablename LIKE oldtablename;

Example1 CREATE TABLE stud LIKE student;

Syntax2 CREATE TABLE newtablename select col1, col2, col3 from existing table;

Example2 CREATE TABLE stud select * from student;

4
5. Deleting Tables

Truncate

 TRUNCATE statement removes the complete data without removing its structure.

Syntax Truncate table tablename;

Example Truncate table stud;

Drop
 It is used to delete a structure of table permanently.

Syntax Drop table tablename;

Example Drop table stud;

Data Manipulation (DML)Commands

1. Insert
The Insert command is used to add one or more rows to a table.

Different properties of Insert commands are

i. Inserting values for all the columns in a table

Syntax insert into tablename values (value1, value2 ...);

Example insert into student values (1,'anu', 98);

Output
ROLLNO NAME M1
1 ANU 98

ii. Inserting values only for some particular columns

Syntax INSERT into tablename (col1, col2,...) values (value1, value2,...);

Example insert into student(rollno,name) values(2,'balu');

Output
ROLLNO NAME M1
1 Anu 98
2 Balu

iii. Inserting multiple values

Syntax Insert into table name (col1, col2, ..) Values (value1, value2...), (value1, value2...),

(value1, value2...);
Example insert into student (rollno, name, m1) values (3,'banu',100), (4,'pavi',79), (5,'senthil',99);

5
Output
ROLLNO NAME M1
1 Anu 98
2 Balu
3 Banu 100
4 Pavi 79
5 Kumar 99
2. Update

 It is used to update the rows in a table.


 It can be changing values in one or two columns of a row.
 The specific rows can also be updated based on some condition.

Syntax update tablename SET columnname=value [wHERE condition];

Example update student set m1 = 100 where rollno = 2;

Output
ROLLNO NAME M1
1 Anu 98
2 Balu 100
3 Banu 97
4 Pavi 79
5 Kumar 99
3. Delete

The delete command is used to delete rows from a table. To delete a row

Syntax Delete from tablename [WHERE condition];

Example
i. To delete a specified row: Delete from student where rollno =1;

Output
ROLLNO NAME M1
2 Balu 100
3 Banu 97
4 Pavi 79
5 Kumar 99

ii. To delete all the rows: Delete from student;

6
Data retrieval commands

The SELECT command is used to retrieve the stored data from the table.

Different use of select commands

1. Simply to show all the rows and columns of a table

Syntax select * from tablename;

Example select * from student;

Output
ROLLNO NAME M1
2 Balu 100
3 Banu 97
4 Pavi 79
5 Kumar 99

2. Selects only the specified columns from the table

Syntax select column 1, column 2, … FROM tablename;

Example select rollno, name from student;

ROLLNO NAME
Output 2 Balu
3 Banu
4 Pavi
5 Kumar

3. Selecting specified rows by WHERE command.

Syntax select columnnames from tablename where condition;

Example select * from students where m1>=100;

Output ROLLNO NAME M1


2 BALU 100

4. Selecting distinct rows from the table to avoid duplicate values

Syntax select distinct columnname from tablename ;

Example select distinct grade from students;

7
5. Selects rows in ascending / descending order

Syntax select col1,col2,…. from tablename order by columnname [desc / asc];

Example select * from students order by rollno desc ;

3.2 MYSQL Operators and Expressions

MySQL Operators
Generally there are three types of operators in SQL:
1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators

MYSQL ARITHMETIC OPERATORS

Let's assume two variables "a" and "b". Here "a" is valued 50 and "b" valued 100.

Operators Descriptions Examples

+ It is used to add containing values of both operands a+b will give 150

- It subtracts right hand operand from left hand operand a-b will give -50

* It multiply both operand?s values a*b will give 5000

/ It divides left hand operand by right hand operand b/a will give 2

% It divides left hand operand by right hand operand and b%a will give 0
returns reminder

MYSQL COMPARISON OPERATORS

Let's take two variables "a" and "b" that are valued 50 and 100.

Operator Description Example

= Examine both operands value that are equal or not,if yes condition (a=b) is not true
become true.

!= This is used to check the value of both operands equal or not,if not (a!=b) is true
condition become true.

<> Examines the operand?s value equal or not, if values are not equal (a<>b) is true
condition is true

> Examine the left operand value is greater than right Operand, if yes (a>b) is not true
condition becomes true

< Examines the left operand value is less than right Operand, if yes (a<="" td="">
condition becomes true
8
>= Examines that the value of left operand is greater than or equal to (a>=b) is not true
the value of right operand or not,if yes condition become true

<= Examines that the value of left operand is less than or equal to the (a<=b) is true
value of right operand or not, if yes condition becomes true

MYSQL LOGICAL OPERATORS

This is the list of logical operators used in SQL.

Operator Description

AND This operator allows the existence of multiple conditions in an SQL statement.

NOT the NOT operator reverse the meaning of any logical operator

OR this operator is used to combine multiple conditions in SQL statements

Expressions
Expressions are formed using operands and operators. Mostly expressions are used in select
commands after where clause.
Example 1 select * from student where dept=’cse’;
Example 2 select * from student where dept=’cse’ and marks > =75;

Pattern Matching

We can perform pattern matching using operator LIKE with wildcard characters. The wildcard ‘_‘
matches any single character, ‘%’ matches any number of characters.

Example 1 select * from pet where name like 'b%';

Name owner species


Buffy Harold dog
Bowser Diane dog

To find names ending with fy

Example 2 select * from pet where name like '%fy';

Name owner species


Buffy Harold dog
Fluffy Diane cat

To find names containing exactly five characters, use five instances of the _ pattern character:

Example 3 select * from pet where name like '- - - - -';

Name Owner species


Buffy Harold bird
Claws Diane cat

9
Importing Data

 We can keep our raw data in a text file, and load them into the table via the LOAD DATA
command.
 For example, use a text editor to create a new file called “production.CSV” under
“d:\mysqlprograms”.
 The file contains the following records.
 Here the values are separated by ‘,’.
 The file extension of “.CSV” stands for Comma Separated Value text file.
Pnc, pencil 3B, 600,0.52
Pnc, pencil 4B, 300,0.62
Pnc, pencil 5B, 200, 0.73
Pnc, pencil 6B, 600, 0.47
 We can load (import) the raw data into the products table as follows.
Example
load data local infile ‘d:/mysqlprograms/productin.csv’ into table products
columns terminated by ‘,’ lines terminated by ‘\r\n’;
Exporting Data

We can use select ….. into outfile filename from tablename to export data from a table to a text
file.
Example
Select * from products into outfile ‘d: /mysqlprograms/productin.csv’
columns terminated by ‘,’ lines terminated by ‘\r\n’;
3.3 Built-in Functions

Two types of functions

1. Single row / scalar functions


2. Group function / Aggregate functions.

1. Single Row Functions

i. Numeric Functions
ii. STRING Functions
iii. Date and Time Functions
iv. Conversions functions

10
i. Numeric Functions

S. Function
Meaning Example Result
No Name
1 ABS(X) Returns the absolute value of x Abs(-12.5) 12.5
2 Exp(x) Returns ex Exp(1) 2.718
3 Log 10(x) Returns the logarithm of x to the base 10 Log10(10) 1

4 Log(x) Returns the natural (base e) logarithm of x Log(1) 0

5 Mod(m,n) Performs m%n Mod(10,3) 1

Returns the value of pi Pi() 3.14


6 Pi()

Returns xy Pow(2,3) 8
7 Pow(x,y)
Returns the largest integer not greater than
Floor(3.8) 3
8 Floor(x) x
Returns the smallest integer not greater
Ceil(3.6) 4
9 Ceil(x) than x

Returns sine of x. x should be in radians Sin(0) 0


10 Sin(x)
Returns cosine of x. x should be in
Cos(0) 1
11 Cos(x) radians
Returns tangent of x. x should be in
Tan(0) 0
12 Tan(x) radians

Returns the non-negative square root of x Sqrt(25) 5


13 Sqrt(X)
Returns -1,0 or 1, depending on whether Sign(-25) -1
14 SIGN(X) the value of x is negative zero or positive Sign(25) 1

ii. String Functions

S.No Function Name Meaning Example Result


1 ascii(str) Returns the integer Ascii(‘a’)
value of the left most 97
byte of the string str.
2 Char(number) Returns the character Char(66)
B
of the number given.
3 Char_length(str) Returns the number of Char_length(‘rdbms’)
characters in the string 5

11
concatenates two concat(‘poly’,’technic’)
4 Concat(str1,str2)
strings str1 and str2
‘polytechnic’
and returns the
resultants string
Returns the Hex(16)
5 hex(n)
hexadecimal
10
representation of the
number n.
Converts str into Lcase(‘COLLEGE’)
6 Lcase(str) college
lowercase letters
Converts str into ucase(‘college’)
7 Ucase(str) COLLEGE
uppercase letters
Reverses the string str Reverse(‘college’)
egelloc
8 Reverse(str)
Returns a substring Substr(‘polytechnic’,5)
9 Substr(str,pos)
from the string str,
Technic
beginning at position
pos.
Returns the right most Right(‘polytechnic’,7)
10 Right(str,len)
len characters from the Technic
string str
Returns the left most Left(‘polytechnic’,4)
11 Left(str,len)
len characters from the poly
string str

iii) Date and Time Functions

S.No Function Name Meaning Example Result


1 Adddate(date, Takes a date or date and time Adddate (‘2005-12- 2006-12-01
expr) value date, adds a temporal 01’,365)
value representing number of
days to it and returns the result
2 Addtime(expr1, Adds the two expressions and Addtime(‘2005-01-01 2005-01-01
expr2) returns the result. 00:00:00’, ’12:30:00’) 12:30:00
3 Curdate() Returns the current date YYYY- Curdate() ‘2017-07-13’
MM-DD’ format (Today’s date)
4 Curtime() Returns the current time Curtime() 12:30:34
HH:MM:SS format

12
5 Date(expr) Returns the date part of the Date(‘2017-06-12 2017-06-12
expression 13:14:22’)
6 Day(date) Returns the numeric value of 12
Day(‘2017-06-12’)
the day of the month for date
7 Last_day(date) Returns the date for the last Last_day(‘2017-06-12’) 2017-06-30
day of the month in which the
argument falls
8 Minute(time) Returns the numeric value of Minute(’12:31:58’) 31
the minute for the time value
time, in the range from 0 to 59
9 Month(date) Returns the numeric value of Month(‘2003-12-01’) 12
the month of the year for date
10 Time(expr) Returns the time part of expr. Time(‘2006-03-11, 16:15:05
16:15:05’)

IV) Conversion Functions

S.
Function Name Meaning Example Result
No
1 Cast(expr as type) Converts an expression Cast(404 as binary) ‘404’
value expr to a given Cast(13 as decimal (5,2)) 013.00
type Cast(20080202 as date) 2008:02:02
2 Convert(expr,type) Same a cast(), but Convert(304,binary) ‘304’
different syntax Convert(13,decimal(5,2)) 13.00
Convert(20080202,date) 2008:02:02
2. Aggregate / Group Functions

Aggregate functions calculate a single value base on a group of value.

S.No Function Name Meaning Example Result


1 Avg(expr) Returns average value of expr in Select avg(mark) from 75
the selected rows for non-Null student;
values.
2 Count(expr) Returns the count of number of Select count(mark) 10
non-NULL value of expr. from student
3 Max(expr) Returns the maximum value of Select max(mark) from 73
expr student;
4 Min(expr) Returns the minimum value of Select min(mark) from 30
expr student;

13
5 Std(expr) Returns the standard deviation of Select std(mark) from 2.5
expr for all non-NULL values student;
6 Sum(expr) Returns the sum of expr for all Select sum(mark) from 750
non-NULL values student;
7 Variance(expr) Returns the variance of expr for all Select variance(mark) 6.29
non-NULL values from student;

3.4 Querying the Table


Let us consider the following students table to understand the following queries

Example

SNO SNAME BRANCH GRADE


10 KUMAR CSE A
20 INDIRA CSE B
30 POOJA IT A
40 RAJ ECE B
50 REKHA EEE C

1. WHERE Clause

The WHERE clause is used to select some specific rows from table according to a given condition.
Syntax Select columnname from tablename where condition;

Example Select * from students where grade=’c’;


SNO SNAME BRANCH GRADE

50 REKHA EEE C

2. GROUP BY Clause

 This clause is used to group the rows from a table based on some condition.

Syntax Select columnname from tablename group by columnname;

Example Select grade from students group by grade;

GRADE
A
B
C
3. HAVING Clause

 The HAVING clause is used in the select statement to specify filter conditions for a group of
rows.
 This clause is often used with the group by clause.
Syntax Select columnname from tablename group by columnname having condition;

14
Example Select branch from students group by branch having count (*) =2;
Explanation This query groups the 2 CSE rows from the specified table

BRANCH
CSE
4. ORDER BY Clause

This query is used to display the rows from the table in the ascending order or
descending order.
Syntax Select columnname from tablename order by columnname [desc/asc];
To display the selected row in descending order:

Example1 Select * from students order by sno desc;

SNO SNAME
50 REKHA
40 RAJ
30 POOJA
20 INDIRA
10 KUMAR
To order the student table by displaying name starting from R

Example2 Select sno, sname from students where sname like ‘r%’ order by sno desc;

SNO SNAME
50 REKHA
40 RAJ

MYSQL Subquery
 Sub-query is defined as a query within a query.
 That is one select query contains another select query.
 The inner query will be executed first.
Operators Used in Sub-Query

1) ANY
2) ALL
3) SOME
4) IN
5) NOT IN
6) EXISTS
7) NOT EXISTS

Syntax

Outerquery subquery-operator (inner query);

Let us consider the table’s students & marks to understand sub queries.

15
Students table

SNO SNAME GRADE


10 Kumar A
20 Indira B
30 Pooja A
40 Raj B
50 Rekha C
Marks Table
NO NAME TOTAL
10 Kumar 400
15 Kannan 450
20 Indira 300
25 Selvi 350

1. IN
It compares two tables and lists the common records.
Example
Select sno, sname from students where sno IN (Select no from marks);
Output
Sno Sname
10 Kumar
20 Indira
Explanation
The outer query displays the ‘sno, sname’ of the students from the table students whose
sno is present in marks table also.
2. NOT IN
It compares two tables and lists the common records.
Example Select sno, sname from students where sno NOT IN (Select no from marks);
Output
Sno Sname
30 Pooja
40 Raj
50 Rekha
Explanation
The outer query displays the ‘sno, sname’ of the students from the table students whose
sno is not present in marks table .

3. ANY
It compares two tables and selects any value based on some condition.
Example Select sno, sname from students where sno>=ANY (Select no from marks where
no>=40);
Output
Sno Sname
40 Raj
50 Rekha
Explanation
The outer query displays the ‘sno, sname’ of the students whose no>=40.
16
4. ALL

It compares two tables and lists all the fields under the given condition.
Example Select * from students where sno ALL (select no from marks where total>=400);
Output

S.NO SNAME GRADE


10 KUMAR A

Correlated Sub-queries

Correlated sub-query is a query within a query.

Difference between sub query and correlated sub query

Sub Query Correlated sub Query

Inner query is evaluated first and then the outer query is evaluated first and then the
outer query inner query is evaluated

The following are the steps involved in the correlated sub-query

 The outer query evaluates a row called candidate row.


 For each candidate row, the inner query is executed
 The inner query result decides whether the candidate row is included or not

3.5 Flow Control


Flow control statements can be used in stored procedures and custom functions to control the
flow of the program.
The flow control statements in MySQL include

1. IF()
2. IF NULL()
3. CASE
4. LOOP
5. LEAVE
6. ITERATE
7. REPEAT
8. WHILE

1. IF()

Syntax if (expr1,expr2,expr3)

Explanation

 If expr1 is TRUE then it returns expr2;


 otherwise it returns expr3.
17
Example

mysql> select if(1>2,'yes','no');


-> 'no’

2. IF NULL()

Syntax ifnull(expr1,expr2)

 If expr1 is not NULL, it returns expr1;


 otherwise it returns expr2.
Example
mysql> Select Ifnull(1,0);
-> 1

3. CASE
Syntax
case casevalue
when compare_value1 then statementlist1
when compare_value2 then statementlist2
...
else statement listN
end case
 Here, casevalue is an expression.
 This value is compared to the comparevalue expression in each When clause until one of
them is equal.
 When an equal comparevalue is found the corresponding Then clause Statementlist executes.
 If no comparevalue is equal, the Else clause Statementlist execute.

Example
Begin
Declare V int default 3 ;
Case V
When 1 THEN 'one'
When 2 THEN 'two'
else 'invalid input'
END case;
END;
Output 'invalid input'

18
4. LOOP

Syntax [begin_label:] LOOP


statement_list
END LOOP [end_label]
 LOOP implements a simple loop construct, enabling repeated execution of the statement
list, which consists of one or more statements, each terminated by a semicolon (;) statement
delimiter. The statements within the loop are repeated until the loop is terminated.

Example

BEGIN
Declare p1 int default 1;
start: LOOP
SET p1 = p1 + 1;
IF p1 < 10 THEN
ITERATE start;
END IF;
LEAVE start;
END LOOP start;
END;

5. REPEAT

Syntax
REPEAT
statement_list
UNTIL searchcondition
END REPEAT;
 The statement list within a REPEAT statement is repeated until
the search_condition expression is true.
 Thus, a REPEAT always enters the loop at least once.
 statement_list consists of one or more statements, each terminated by a
semicolon (;) statement delimiter.
 A REPEAT statement can be labeled.

19
Example

BEGIN
Declare x int default 1;
REPEAT
SET x = x + 1;
UNTIL x <5
END REPEAT;
END

6. WHILE
Syntax
WHILE condition DO
statement_list
END WHILE
 The statement list within a WHILE statement is repeated as long as the
condition expression is true.
 statement_list consists of one or more SQL statements, each terminated by a
semicolon (;) statement delimiter.
 A WHILE statement can be labeled.

Example
BEGIN
DECLARE v1 INT DEFAULT 5;
WHILE v1 > 0 DO
SET v1 = v1 - 1;
END WHILE;
END;

7. LEAVE

Syntax LEAVE label

 This statement is used to exit the flow control construct that has the given label.
 If the label is for the outermost stored program block, LEAVE exits the program.
 LEAVE can be used within BEGIN ... END or loop constructs (LOOP, REPEAT,WHILE).

8. ITERATE

Syntax ITERATE label

 ITERATE can appear only within LOOP, REPEAT, and WHILE statements.
 ITERATE means “start the loop again.”

20
UNIT IV: MYSQL PERFORMANCE TUNING

4.1 INDEXES & SEQUENCES

4.1.1 Indexes

MySQL uses the indexes to select exact physical corresponding rows of the table.
 Index uses pointer that points a particular record directly
 Indexes are used to speed up execution of SQL statements on a table
 It is used to search and find a particular row in a table faster.

Creating Index
Indexes can be created on selected column to facilitate fast search.

Syntax create index indexname on basetablename (column1, column2…columnn);

Example create index stud on student (rollno);

TYPES OF INDEX

The index can be created on selected columns to facilitate fast search. But it requires more
memory. The types are
1. Simple Index (or UNIQUE Index)
2. Composite Index (or Compound Index)

1. Simple index

This index has with single column to prevent duplicate and null value.

Syntax Create unique index index name on basetablename (columnname));

Example Create unique index idx1 on student (rollno));

[Now the object idx1 (user defined) is applied on rollno column of student table]

2. Composite Index

This index has with more than one column to prevent duplicates and null values.

Syntax create index indexname on basetablename (column1, column2...));

Example create index idx1 on student (rollno, regno));

[Now the object idx1 (user defined) is applied on rollno & regno columns of student table]

Dropping Index

The index can be removed permanently

Syntax drop index indexname;

Example drop index idx1;

1
4.1.2 Sequences

The sequences are list of integers generated in the ascending order i.e. 1.2.3..
Example: CustomerId, RegisterNumber, RollNumber, TransactionNo
Rules
- It must be integers only
- In MYSQL the sequences uses auto_increment keyword
- It must be indexed by PRIMARY KEY or UNIQUE
- Generally NOT NULL keyword is Implicit or we can represent explicitly.

1. Creating sequences
Sequence can be created in the create table with auto_increment and primary key.

Syntax create table tablename (column1 datatype auto_increment primary key,


Column2 datatype…);

Example Create table student (rollno int auto_increment primary key, name
varchar (20), address varchar (20));
For testing
insert into student (name, address) values ('balan', 'chennai'), ('saravanan', 'trichy');
Output
Select * from student;
Rollno Name Address
1 Balan Chennai
2 Saravanan trichy

[In the output we didn’t input the Roll number but it is automatically generated]

2. Altering Sequences

The sequence values can be altered.


Syntax alter table tablename auto_increment=starting value;

Example alter table student auto_increment =1001;

For testing

insert into student (name, address) values ('raj', 'avadi');


Output
Select * from student;

Rollno Name Address


1 Balan Chennai
2 Saravanan trichy
1001 Raj avadi

In this output, the starting value was generated from 1001 by auto_increment.

2
3. Deleting sequences

Truncate Table & Drop Table command deletes the sequence.

Syntax: drop table tablename; Example: drop table student;


Syntax: truncate table tablename; Example: truncate table student;

4.2 VIEWS

View is an imaginary table derived from the base table.


The advantages are
 It provides table security by restricting access to the base table
 It simplifies the data access from multiple table
 This stores the output in a derived table
 It is possible to update view
 It is possible to remove view

1. Creating views

 A view is an imaginary table derived from base tables.


 It contains the definition of the base table with no data.
 But data are derived from the base table.
 View can be created by CREATE VIEW command.

Syntax Create view viewname as select column1, column2… from basetablename


[Where condition];

Assume the following college Table.

Rollno Name Total


100 Raj 500
101 Suresh 450
102 Mala 333
103 Siva 600

Example Create view stud_view as select rollno, total from college where total>=500;

[From College base table we select only Rollno & Total Columns about those are taken
total>=500. Now the output ‘2 rows returned’ will get. ]

Output select * from stud_view;

Rollno Total
100 500
103 600

3
2. Updating Views

We can modify the existing view.

Syntax alter view viewname as select column1, column2 … from basetablename


where condition;
Example alter view stud_view as select rollno, name, total from college where total>=500;

Output select * from stud_view;

Rollno Name Total


100 Raj 500
103 Siva 600

Now the Name field is updated in the same View name ‘stud_view’

3. Deleting (Dropping) Views

We can delete or remove an existing view using DROP VIEW command.

Syntax: drop view viewname;

Example: drop view stud_view;

4.3 Joins & Unions


4.3.1 Joins
 It is a query that combines data from more than one table
 It uses SELECT statement
 By joining of 2 or more tables, we can retrieve some common information.
Example The ‘Student’ and ‘Mark’ Tables gives a complete information about a
particular student
 It uses ALIAS: Means the columns are specified along with their table name
Syntax TableName.ColumnName.
Example Student.Rollno

Types of Joins
1. Natural join
2. Inner join
3. Left join
4. Right join
5. Self-join

Student Table
Rollno Name
100 Abi
101 Bala
102 Chiti

4
Mark Table
Rollno Name Marks
100 Abi 500
101 Bala 400
103 Dinesh 300

1. Natural Join

 Columns with same name of multiple tables will appear once only.
 ON clause should not be used.
 The identical columns should have same data types.

Syntax Select columnnames from table1 natural join table2;

Example Select student.rollno, marks from student natural join mark;

Output Rollno Marks


100 500
101 400
2. Inner Join
It is used to return all rows from multiple tables where the join condition is satisfied.
Image representation

Syntax Select columnnames from table1 inner join table2 on table1.column=table2.column;

Example Select student.rollno, mark.name from student inner join mark on


student.rollno=mark.rollno;
Output
Rollno Name
100 Abi
101 Bala

3. Left join
It returns all records from the left hand side table of LEFT JOIN command and the
matched records from the right table.
Image representation

Syntax Select columnnames from table1 left join table2 on table1.column=table2.column;

Example Select student.rollno, mark.name from student left join mark on student.rollno=mark.rollno;

Output
Rollno Name
100 Abi
101 Bala
102 null

5
4. Right Join
It returns all records from the right hand side table of RIGHT JOIN command and the
matched records from the left table.
Image representation

Syntax Select columnnames from table1 right join table2 on table1.column=table2.column;

Example Select student.rollno, mark.name from student right join mark on student.rollno=mark.rollno;

Output Rollno Name


100 Abi
101 Bala
Null Dinesh

5. Self Join

Joining a table itself called self join. This combines the rows with other rows of the same table.
It uses both table for left hand and right hand side.
Syntax select columnnames from table1 as object1 join table1 as object2 on
object1.column=object2.column;

Example select a.rollno, a.name,b.name from sample as a join sample as b on


a.friend_code=b.rollno;
Alias ‘a’ Table
Rollno Name Friend_code
10 Kumar 20
15 Kannan 20
20 Indira 15
25 selvi 15
Alias ‘b’ Table
Rollno Name Friend_code
10 Kumar 20
15 Kannan 20
20 Indira 15
25 selvi 15

Output
Rollno Name Name
10 Kumar Indra
15 Kannan Indra
20 Indira Kannan
25 selvi Kannan

6
4.3.2 UNIONS

It is a set operator used to combine the result of two queries into a single one.
The types are
1. Union [or Union Distinct]
2. Union All

Student Table

rollno name
100 raj
101 nagaraj
102 mala
103 raj
104 mala

Mark Table

rollno name total


200 raj 333
201 suresh 444
202 mala 333

1. Union or Union Distinct

This operator combines rows of data from two SELECT statements and returns all
distinct values.
Syntax select columnames from tablename1 union select columnnames from
tablename2;
Example select name from student union select name from mark;

Output
name
raj
nagaraj
mala
suresh

i) Union or Union Distinct with Order by clause

Order by clause is used to order the results of the query.

Syntax select columnames from tablename1 union select columnnames from


tablename2 order by columnname asc/desc;

Example select name from student union select name from mark order by name asc;

Output
name
mala
nagaraj
raj
suresh

7
ii) Union or Union Distinct with LIMIT handling

Limit clause is used to limit the number of rows displayed.

Syntax Select columnames from tablename1 union select columnnames from


tablename2 limit number;

Example Select name from student union select name from mark limit 2;

Output
name
raj
nagaraj

2. Union all

This operator combines rows of data from two select statements and Returns all the
rows including duplicate rows.
Syntax Select columnames from tablename1 union all select columnnames from
tablename2;

Example Select name from student union all select name from mark;

Output name
raj
nagaraj
mala
raj
mala
raj
suresh
mala

4.4 USER & TRANSACTION MANAGEMENT

4.4.1 USER

The USER is actually a record in the 'USER' table of the MySQL server which contains the
following.
 Login information (username & password).
 The corresponding account privileges.
 Client information for MySQL account.
 We can use the MySQL USER for the web applications also.
1. Creating Users

This command is used to create a new user.

Syntax Create user username identified by `password';

Example Create user student identified by ‘admin123’;

Now the user can login into the user name: ‘student’ and password: ‘admin123’
8
2. Showing Users

To show all MySQL users from the user table.

Example Select user from MySQL. User;

Output User
Staff
Student
root

3. Renaming Users

This command is used to rename the existing user name.

Syntax Rename user olduser to newuser;

Example Rename user student to kumar;

4. Dropping Users

This command is used to remove or delete the user name permanently.

Syntax Drop user username;

Example Drop user Kumar;

4.4.2 GRANT Command

Generally the new users have no any permission to do the operations. Hence the GRANT
command is used to give the rights to the new users.
It has 2 types.
1. Grant all privileges
2. Grant specific privileges.

1. Grant all privileges


It gives complete rights to the new users.

Syntax Grant all privileges on *.* to newuser;

Where,

*.* : refers the database and table

New user : refers the name of the user which is already exists.

Grant all privileges on, to : Predefined keywords

Example Grant all privileges on *.* to Kumar;

9
2. Grant Specific Privileges

It gives some specific rights to the user.

Syntax Grant specificprivileges on *.* to newuser;


`
Specific privileges such as CREATE, INSERT, DELETE, DROP, UPDATE, GRANT
OPTION

Example1 Grant create on polytechnic.* to kumar;

Where the user ‘kumar’ has create rights to ‘polytechnic’ database only

Example2 Grant create on *.* to kumar;

Where the user ‘kumar’ has create rights to any database.

Example3 Grant insert, grant option on *.* to kumar;

Where the user ‘kumar’ has insert and granting rights to someone else

Once we have finalized the permissions that we want to set up for the new users, always we
have to be sure to reload all the privileges.

i.e. FLUSH Privileges;

Now our changes will be in effect.

4.4.3 REVOKE Command

The REVOKE command is used to cancel the rights from the users.
It has 2 types.
1. Revoke all privileges
2. Revoke specific privileges.

1. Revoke all privileges:

It cancels all granted rights from the users.

Syntax Revoke all privileges on *.* from newuser;

Example Revoke all privileges on *.* from kumar;

2. Revoke specific privileges:

It cancels few granted rights from the user.

Syntax Revoke specificprivileges on *.* from newuser;

Specific privileges such as CREATE, INSERT, DELETE, DROP, UPDATE, GRANT

OPTION

Example1 Revoke create on polytechnic.* from kumar;

Where the user ‘kumar’ cancels his create rights from ‘polytechnic’ database.
10
Example2 Revoke create on *.* from kumar;

Where the user ‘kumar’ cancels his create rights from any database.

Example3 Revoke insert, grant option on *.* from kumar;

Where the user ‘kumar’ cancels his insert and grant option rights from all databases

4.4.4 TRANSACTION COMMANDS


 Changes made to a database are called transaction control.
 By default, the auto commit mode and transactions are implicit.
 START TRANSACTION command is used for disable the ‘auto commit mode’ so that the
transaction becomes explicit.
They are two important transaction commands.
1. Commit
2. Rollback

1. COMMIT
It is used to end the transactions and make them permanent of changes.

Syntax commit;

Example

Creating a table

MYSQL> create table test (id int primary key) ;

Starting the transaction

MYSQL> start transaction;

Inserting rows into the table

MYSQL> insert into test values (1);

MYSQL> insert into test values (2);

Using Commit

MYSQL> commit;

Viewing the table

MYSQL> select * from test;

Output
id
1
2

11
2. ROLLBACK
It is used to undo changes made during transaction.
Syntax rollback;
Example
Starting transaction:
MYSQL> start transaction;
Inserting rows into the table test:

MYSQL> insert into test values (3);

Using rollback to undo changes in the current transaction:

MYSQL> rollback;

Querying the table ‘test’ to view the effect of rollback:

MYSQL> select * from test;

Output id
1
2

SAVEPOINTS
 MySQL enables us to perform a partial rollback of a transaction.
 Save point statement is used within the transaction to set a marker.
Example
Creating a table
MYSQL> create table stud (regno int);
Starting transaction

MYSQL> start transaction;

Inserting rows into the table ‘stud’

MYSQL> insert into stud values (219061);


Setting savepoint marker

MYSQL> savepoint sample;


Again inserting a row

MYSQL> insert into stud values (219062);


Using savepoint marker to rollback

MYSQL> rollback to savepoint sample;


Again inserting a row

MYSQL> insert into stud values (219063);


Verifying the effect of using savepoint

MYSQL> select * from stud;


Output regno
219061
219063

12
UNIT V: STORED PROGRAM CONCEPTS AND DEVELOPMENT

5.1 Stored Procedures & Functions

Stored Procedure

 Stored procedure is a group of query that are compiled, verified and Stored in the server data
base.
 Stored Procedures do not return a result directly but can be used to perform general computations
or produce result sets.
 Then the results are passed back to the client.

 A stored procedure can be invoked by triggers, other stored procedures and applications such as Java,
Python, PHP, etc,.

Creating stored procedure

Syntax:

MYSQL> delimiter $$
MYSQL> create procedure procedurename (arguments)
begin
Declaration statements
Executable statements

end $$

MYSQL> delimiter ;

A procedure has two parts. They are

a) Specification

b) Body

Specification part begins with the keyword create procedure and ends with the parameter list or
parameter name.

Body part begins with the keyword begin and ends with the keyword end.

1
MYSQL stored procedures variables

1. To declare a variable inside a stored procedure DECLARE statement is used.


Syntax:
DECLARE variablename datatype default defaultvalue;
Example
DECLARE x int default 1;
2. Values can be assigned to variables
Declare sum int default 0;
Set sum=10;
3. Select into statement can be used to assign the result of a query into a variable
Declare total int default 0;
Select count(*) into total from products;
Executing or Invoking stored Procedures
It is used to execute the stored procedure.
Syntax Call ProcedureName(Parameter);
Example

MYSQL>delimiter $$
MYSQL> create procedure getoffice (countryname varchar(10))
begin
select * from emp where country=countryname;
end $$
MYSQL> delimiter;
Suppose, we want to get all offices in ‘India’, we just need to pass a value i.e. India to the
stored procedure as follows:
Mysql> call getoffice(‘India’);

Advantages of stored procedure


1. Increase the performance
2. It is reusable
3. It is portable
4. Reduce the traffic between application and database server
5. It has more security

Modes of Parameter

Procedure has 3 parameters.


They are 1.IN
2. OUT
3. INOUT

2
Employee table
EID EMP_NAME
101 ADITHIYA
102 BAKYA
103 DEEPA
104 SARAVANAN
105 YUVARAJ

1. IN Parameter
This passes the value by IN parameter and display the output directly.
Example
MYSQL> delimiter$$
MYSQL> create procedure empdet (IN id int)
begin
select * from employee where eid=id;
end $$
MYSQL>delimiter;

CALL Procedure call empdet (102);

Output
EID EMP_NAME
102 BAKYA

2. OUT Parameter
This passes the value by IN parameter and stores its results by OUT parameter and
displays the output.
Example
MYSQL> delimiter$$
MYSQL> create procedure empdet (in id int, out name varchar (20))
begin
select emp_name into name from employee where eid =id;
end $$
MYSQL> delimiter ;

CALL Procedure call empdet (102, @name);


View the output Select @name;

Output
EMP_NAME
BAKYA

3
3. IN OUT Parameter
This passes the value and stores its results by a single parameter called IN OUT.
Example
MYSQL> delimiter$$
MYSQL> create procedure empdet(in out rollno int)
begin
select count(*) into rollno from employee where emp_name like 'a%';
end $$
MYSQL> delimiter ;

CALL the Procedure call empdet(@rollno);


View the output Select @rollno;

Output
@rollno
101

Dropping stored Procedures


The ‘drop’statement used to drop or delete a stored procedure.
Syntax drop procedure procedurename;

Example drop procedure empdet;

Stored Functions

A stored function is a special kind of stored program that returns a single value.
Creating Function
Syntax
MYSQL> delimiter $$
MYSQL> create function functionname(parameters)
returns datatype
[NOT] [deterministic]
begin
Declaration part;
Execution part;
return (value);
end $$
MYSQL> delimiter ;
4
The function has;

 Function specification: It begins with CREATE FUNCTION and ends with RETURNS data
type.
 Function body: It begins with BEGIN and ends with the END
 In function, all parameters are IN as default parameter.
 If the function returns the result then it is called deterministic
Otherwise non deterministic.

Calling functions
Used to execute the stored function.
Syntax function_name(Parameter value);

‘User’ table
Username Phonenumber Address
Kumar 9841412545 Chennai
Kalai 6369401526 Trichy
Prabha 9445789642 Tirunelveli

Example to create function: This is used to find the address of a phone no.

MYSQL> delimiter$$
MYSQL> create function findaddress (phno varchar (20))
returns varchar (20)
begin
declare addr varchar (20);
select address into addr from user where phonenumber =phno;
return addr;
end $$
MYSQL> delimiter;

Calling function: select findaddress (9841412545);

Output Findaddress(9841412545)
Chennai

5
Deleting stored Functions
The statement used to delete a stored function.

Syntax drop function functionname;


Example drop function findaddress;

Advantages
1. Stored functions that are reusable among SQL statements or stored programs.
2. Stored functions can be used in SQL statements wherever an expression is used.
3. This improves readability and maintainability of the procedural code.

5.2. MYSQL TRIGGER & CURSOR

5.2.1 TRIGGERS
Trigger is a statement that is executed automatically by the system as a side effect
of a modification to the database.

Advantages of triggers
 Triggers are used to check the changes in the databases
 They can be used to record every change to every record in the table.
 They are used to ensure the integrity of data.
 Triggers can undo an attempt to change data.
 They can undo an attempt to change the structure of the database.
Creating Trigger
delimiter$$
create trigger triggername {before/after} (insert/update/delete}
on tablename for each row
begin
trigger body ;
end $$
delimiter ;

Where
CREATE...TRIGGER is a statement. Anyone within{} is must.
Parts of the Trigger:
a) Trigger statement:
The DML statement (INSERT/UPDATE/DELETE) is called the
trigger statement. It fires the trigger body.
b) Trigger body:
Stores the logical part of program;
c) FOR EACH ROW:
The trigger body will be fired once for each row.
6
Types of triggers

a) BEFORE: trigger fires before executing triggering statement i.e. Insert….

b) AFTER: trigger fires after executing triggering statement i.e. Insert/update...

Example for trigger

Using the database:

MYSQL> use polytechnic;

1. Creating a table authors

MYSQL> create table authors (authorid int,name varchar(20),titlecount int);

2. Inserting rows into the table authors:

MYSQL>insert into authors values(301,’R.K.Selvakumar’,1);

MYSQL>insert into authors values(302,’EdwinDayanand’,1);

MYSQL>insert into authors values(303,’A.Balakrishnan’,1);

3. Querying the table authors:

MYSQL>select * from authors;

authorid name titlecount


301 R.K.Selvakumar 1
302 EdwinDayanand 1
303 A.Balakrishnan 1

4. Creating another table titles

MYSQL>create table titles(authorid int,titlename varchar(512));

5. Inserting rows into the table titles

MYSQL>insert into titles values(301,’C Programming’);

MYSQL>insert into titles values(302,’RDBMS’);

MYSQL>insert into titles values(303,’Computer Architecture’);

6. Querying the table titles

MYSQL>select * from titles;

Output:
authorid titlename
301 C Programming
302 RDBMS
303 Computer Architecture

7
7. Creating a trigger to update the titlecount of authors table when inserting into titles table

MYSQL>delimiter $$

MYSQL>create trigger inscount after insert on titles for each row

begin

update authors set titlecount=titlecount+1 where authorid=new.authorid;

end $$

MYSQL>delimiter ;

8. Verifying trigger event

MYSQL>insert into titles values (303,’Operating Systems’);

MYSQL>select * from authors;

authorid name titlecount


301 R.K.Selvakumar 1
302 EdwinDayanand 1
303 A.Balakrishnan 2

Deleting Triggers
To delete or drop the trigger, DROP TRIGGER statement is used.

Syntax Drop trigger triggername;

Example Drop trigger mytrig;

5.2.2 CURSORS
Cursor is a temporary work area used by the MYSQL server to execute MYSQL statements and
to store the processed information. The cursor commands are DECLARE, OPEN, FETCH and CLOSE.
They are explained below

1. DECLARE statement
This statement is used to declare the cursor before use.

Syntax Declare cursorname cursor for select statement;

Example Declare cursoremp cursor for select empid from employee;

8
2. OPEN statement

This statement is used to open the existing cursor.

Syntax Open cursorname;

Example: OPEN cursoremp;


3. FETCH statement
We can use the FETCH statement to retrieve the next row pointed by the cursor and move
the cursor to the next row in the result set.
Syntax Fetch cursorname into variablelist;

Example Fetch cursoremp into id;

4. CLOSE statement
Finally we can use the CLOSE statement to deactivate the cursor and release the memory associated with it.

Syntax close cursorname;

Example close cursoremp;

Not Found Handler

When working with MYSQL cursor, we must also declare a NOT FOUND handler to handle
the situation when the cursor could not find any row. Because each time we call the FETCH
statement, the cursor attempts to read the next row in the result set. When the cursor reaches the
end of the result set, it will not be able to get data, and a condition is raised. The handler is used to
handle this condition.

To declare a NOT FOUND handler, the following syntax is used

Declare continue handler for not found set finished=1;

Where finished is a variable to indicate that the cursor reached the end of the result set. The
handler
declaration must appear after variable and cursor declaration inside the stored procedures.

Example program

‘Employee ‘Table
Empno Empname Designation Netsalary
100 Rani Lecturer 50000
101 Kumar Lecturer 45000
102 Deepa Lecturer 40000
103 Muthu Lecturer 45000

9
mysql> delimiter$$
mysql> create procedure cursoremp (in empid int)
begin
declare finished int(1);
declare eno int;
declare ename varchar(25);
declare netsal decimal(6,2);
declare salarycursor cursor for select empno, empname, netsalary from
employee;

declare continue handler for Not Found set finished=1;


begin
open salarycursor;
g1: loop
fetch salarycursor into eno, ename, netsal;
if finished=1 then
leave g1;
end if;
end loop g1;
update employee set netsalary= netsalary + 3000 where empno=empid;
close salaraycursor;
end;
end $$
mysql> delimiter ;

Using the Cursor to update the employee Net_salary

MYSQL> call cursoremp (101);

MYSQL> select * from employee;

Empno Empname Designation Netsalary


100 Rani Lecturer 50000
101 Kumar Lecturer 48000
102 Deepa Lecturer 40000
103 Muthu Lecturer 45000

Deleting cursor
It is used to delete the cursor completely.
Syntax Drop cursor cursorname;
Example Drop cursor cursoremp;
10
5.3 MYSQL Web

MYSQL distributions include a set of client programs. For example, the client program permits us
to interact with the server to execute SQL statements.

Need For Own MySQL Programs

1. Customize input handling

 In MySQL our own program can prompt input from the user and reads a value
 These methods are easier to use.
 Users need not know SQL.

2. Validate input provided by the user.


 We can check whether certain fields are empty
 We can check some field’s data type. Ex. Checks Date format dd/mm/yy
 This enhances the safety and security of our applications.

3. Generate input automatically


 Without human intervention, input for MYSQL can be generated by another program.

4. Customize the output


 MYSQL output is unformatted, but we can make it formatted.
 This can be done by writing our own programs.

5. Integrate MYSQL into any application

 MYSQL databases can be accessed by any application programs.


 For eg: an application that verifies a student register number for result.

MYSQL'S Application Programming Interfaces (API)


There are3 most popular API for MYSQL. They are

1. The Cclient library API,


2. The DBI(Data base Interface)API for perl
3. The PHP API

1. The Cclient library API

 It is the primary programming interface to MYSQL.


 The CAPI is used within compiled C programs.
 It is used to implement the clients such as my sql, my sql admin, dumb etc.,
 It is a client library that provides an interface with MYSQL server.
 It gives the facilities for establishing a connection with the server.

11
2. The DBI API for perl: The figure shows about the perl functions

Explanation
 DBI is implemented as a Perl script module.
 This module interfaces with other modules at the DBD(Database Driver)level.
 This provides access to a specific data base engine.
 MYSQL can be used with DBI to create scripts. These scripts can be invoked from the
command line or by web browser.
 DBI API is used within the applications written for the perl scripting Language.
 DBI works with data bases using perl modules that work in a two-level architecture.

3. The PHP API

 The PHP (Personal Home Page) is a server- side scripting language.


 It provides a convenient way of embedding programs in web pages.
 This page is processed by PHP on the server before sent to the client.
 PHP enables the script to generate dynamic content with the help of MYSQL.
 PHP supports accessing several database engines in addition to MYSQL.
 Client browser sends a request for a PHP page to the Web server.
 PHP in the web server executes the script it finds in the page and replaces it.
 The result is sent to the browser.
 PHP scripts typically look like HTML pages. Ex<? php and?>tags.

12

You might also like