0% found this document useful (0 votes)
33 views73 pages

RDBMS MySQL Practical Guide

The document discusses different types of databases including relational, non-relational, centralized, distributed, cloud and object-oriented databases. It provides details on the properties, advantages and disadvantages of each database type. Relational databases store data in tables using rows and columns while non-relational databases provide flexibility in data storage.

Uploaded by

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

RDBMS MySQL Practical Guide

The document discusses different types of databases including relational, non-relational, centralized, distributed, cloud and object-oriented databases. It provides details on the properties, advantages and disadvantages of each database type. Relational databases store data in tables using rows and columns while non-relational databases provide flexibility in data storage.

Uploaded by

Harman Dhanjal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PRACTICAL

FILE OF
RDBMS
(RELATIONAL DATABASE MANAGEMENT SYSTEM)

Submitted by Submitted to
Ishleen kaur Prof. Kamaljeet Kaur
22451 Computer Science Department

MATA GUJRI COLLEGE, FATEHGARH


SAHIB PUNJAB INDIA 140406

1
INDEX
SR. QUARRY PAG
NO. NAME E
NO.
1 INTRODUCTION 4-21

2 INTRODUCTION TO MYSQL 22-24

3 INSTALLATION OF MYSQL 25-36

4 CREATE DATABASE STATEMENT 37

5 CREATE TABLE STATEMENT 38-39

6 RETRIEVAL OF ROW USING SELECT STATEMENT 40-41

7 CONDITIONAL RETRIEVAL OF ROWS STATEMENT 42

8 MY SQL ALTER AND DROP STATEMENT 43-44

9 UPDATE AND DELETE STATEMENT 45-46

10 JOIN SEQUENCES 47-53

11 COMMIT AND ROLLBACK 54

12 MYSQL IS NULL CONDITION 55-56

13 MYSQL AGGREGATE FUNCTION 57-59

14 ORDERING THE RESULT OF QUERIES 60-62

15 MYSQL OPERATORS 62

16 GROUPING THE RESULT OF QUERIES 63

17 LOWER QUERY 63

18 UPPER QUERY 64

19 LENGTH QUERY 64

20 CONCAT QUERY 65

21 INSTR QUERY 65

22 SUBSTR QUERY 66

23 UNION OF ALL OPERATOR 66

24 INTERSECTION OPERATOR 65

2
25 INTERSECTION ALL STATEMENT 65

26 CREATE VIEW STATEMENT 67

27 CREATE INDEX STATEMENT 67

28 UNIQUE INDEX STATEMENT 67

29 DROP INDEX 68

30 PL/SQL ARCHITECTURE 68-70

3
INTRODUCTION

What is Database
The database is a collection of inter-related data which is used to retrieve, insert and
delete the data efficiently. It is also used to organize the data in the form of a table,
schema, views, and reports, etc.

For example: The college Database organizes the data about the admin, staff, students
and faculty etc.

Using the database, you can easily retrieve, insert, and delete the information.

Types of Databases
There are various types of databases used for storing different varieties of data:

1) Centralized Database
It is the type of database that stores data at a centralized database system. It comforts
the users to access the stored data from different locations through several
applications. These applications contain the authentication process to let users access
data securely. An example of a Centralized database can be Central Library that
carries a central database of each library in a college/university.

4
Advantages of Centralized Database

○ It has decreased the risk of data management, i.e., manipulation of data


will not affect the core data.
○ Data consistency is maintained as it manages data in a central repository.
○ It provides better data quality, which enables organizations to establish
data standards.
○ It is less costly because fewer vendors are required to handle the data sets.

Disadvantages of Centralized Database

○ The size of the centralized database is large, which increases the response time
for fetching the data.
○ It is not easy to update such an extensive database system.
○ If any server failure occurs, entire data will be lost, which could be a huge
loss.

2) Distributed Database
Unlike a centralized database system, in distributed systems, data is distributed
among different database systems of an organization. These database systems are
connected via communication links. Such links help the end-users to access the data
easily. Examples of the Distributed database are Apache Cassandra, HBase, Ignite,
etc.

We can further divide a distributed database system into:

5
○ Homogeneous DDB: Those database systems which execute on the same
operating system and use the same application process and carry the same
hardware devices.
○ Heterogeneous DDB: Those database systems which execute on different

operating systems under different application procedures, and carries


different hardware devices.

Advantages of Distributed Database

○ Modular development is possible in a distributed database, i.e., the system can


be expanded by including new computers and connecting them to the
distributed system.
○ One server failure will not affect the entire data set.

3) Relational Database
This database is based on the relational data model, which stores data in the form of
rows(tuple) and columns(attributes), and together forms a table(relation). A relational
database uses SQL for storing, manipulating, as well as maintaining the data. E.F.
Codd invented the database in 1970. Each table in the database carries a key that
makes the data unique from others. Examples of Relational databases are MySQL,
Microsoft SQL Server, Oracle, etc.

6
Properties of Relational Database

There are following four commonly known properties of a relational model known as
ACID properties, where:

A means Atomicity: This ensures the data operation will complete either with success
or with failure. It follows the 'all or nothing' strategy. For example, a transaction will
either be committed or will abort.

C means Consistency: If we perform any operation over the data, its value before and
after the operation should be preserved. For example, the account balance before and
after the transaction should be correct, i.e., it should remain conserved.

I means Isolation: There can be concurrent users for accessing data at the same time
from the database. Thus, isolation between the data should remain isolated. For
example, when multiple transactions occur at the same time, one transaction effects
should not be visible to the other transactions in the database.

D means Durability: It ensures that once it completes the operation and commits the
data, data changes should remain permanent.

4) NoSQL Database
Non-SQL/Not Only SQL is a type of database that is used for storing a wide range of
data sets. It is not a relational database as it stores data not only in tabular form but in
several different ways. It came into existence when the demand for building modern
applications increased. Thus, NoSQL presented a wide variety of database
technologies in response to the demands. We can further divide a NoSQL database
into the following four types:

7
a. Key-value storage: It is the simplest type of database storage where it
stores every single item as a key (or attribute name) holding its value,
together.
b. Document-oriented Database: A type of database used to store data as JSON-
like document. It helps developers in storing data by using the same
document-model format as used in the application code.
c. Graph Databases: It is used for storing vast amounts of data in a graph-like
structure. Most commonly, social networking websites use the graph database.
d. Wide-column stores: It is similar to the data represented in relational
databases. Here, data is stored in large columns together, instead of
storing in rows.

Advantages of NoSQL Database

○ It enables good productivity in the application development as it is not


required to store data in a structured format.
○ It is a better option for managing and handling large data sets.
○ It provides high scalability.
○ Users can quickly access data from the database through key-value.

5) Cloud Database
A type of database where data is stored in a virtual environment and executes over the
cloud computing platform. It provides users with various cloud computing services
(SaaS, PaaS, IaaS, etc.) for accessing the database. There are numerous cloud
platforms, but the best options are:

○ Amazon Web Services(AWS)


○ Microsoft Azure
○ Kamatera
○ PhoenixNAP
○ ScienceSoft

8
○ Google Cloud SQL, etc.

6) Object-oriented Databases
The type of database that uses the object-based data model approach for storing data
in the database system. The data is represented and stored as objects which are
similar to the objects used in the object-oriented programming language.

7) Hierarchical Databases
It is the type of database that stores data in the form of parent-children relationship
nodes. Here, it organizes data in a tree-like structure.

Data get stored in the form of records that are connected via links. Each child record
in the tree will contain only one parent. On the other hand, each parent record can
have multiple child records.

8) Network Databases
It is the database that typically follows the network data model. Here, the
representation of data is in the form of nodes connected via links between them.
Unlike the hierarchical database, it allows each record to have multiple children and
parent nodes to form a generalized graph structure.

9
9) Personal Database
Collecting and storing data on the user's system defines a Personal Database. This
database is basically designed for a single user.

Advantage of Personal Database

○ It is simple and easy to handle.


○ It occupies less storage space as it is small in size.

10) Operational Database


The type of database which creates and updates the database in real-time. It is
basically designed for executing and handling the daily data operations in several
businesses. For example, An organization uses operational databases for managing
per day transactions.

11) Enterprise Database


Large organizations or enterprises use this database for managing a massive amount
of data. It helps organizations to increase and improve their efficiency. Such a
database allows simultaneous access to users.

Advantages of Enterprise Database:

○ Multi processes are supportable over the Enterprise database.


○ It allows executing parallel queries on the system.

Database Management System


○ 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.

1
○ DBMS provides an interface to perform various operations like database
creation, storing data in it, updating data, creating a table in the database
and a lot more.
○ It provides protection and security to the database. In the case of multiple

users, it also maintains data consistency.

DBMS allows users the following tasks:

○ Data Definition: It is used for creation, modification, and removal of


definition that defines the organization of data in the database.
○ Data Updation: It is used for the insertion, modification, and deletion of the
actual data in the database.
○ Data Retrieval: It is used to retrieve the data from the database which can be
used by applications for various purposes.
○ User Administration: It is used for registering and monitoring users,
maintain data integrity, enforcing data security, dealing with concurrency
control, monitoring performance and recovering information corrupted by
unexpected failure.

What is RDBMS (Relational Database


Management System)
RDBMS stands for Relational Database Management System.

All modern database management systems like SQL, MS SQL Server, IBM DB2,
ORACLE, My-SQL, and Microsoft Access are based on RDBMS.

It is called Relational Database Management System (RDBMS) because it is based on


the relational model introduced by E.F. Codd.

How it works
Data is represented in terms of tuples (rows) in RDBMS.

1
A relational database is the most commonly used database. It contains several tables,
and each table has its primary key.

Due to a collection of an organized set of tables, data can be accessed easily in RDBMS.

Brief History of RDBMS


From 1970 to 1972, E.F. Codd published a paper to propose using a relational database
model.

RDBMS is originally based on E.F. Codd's relational model invention. Following

are the various terminologies of RDBMS:

What is table/Relation?
Everything in a relational database is stored in the form of relations. The RDBMS
database uses tables to store data. A table is a collection of related data entries and
contains rows and columns to store data. Each table represents some real- world
objects such as person, place, or event about which information is collected. The
organized collection of data into a relational table is known as the logical view of the
database.

1
Properties of a Relation:

○ Each relation has a unique name by which it is identified in the database.


○ Relation does not contain duplicate tuples.
○ The tuples of a relation have no specific order.
○ All attributes in a relation are atomic, i.e., each cell of a relation contains
exactly one value.

A table is the simplest example of data stored in RDBMS. Let's

see the example of the student table.

ID Name AGE COURSE

1 Ajeet 24 [Link]

2 aryan 20 C.A

3 Mahesh 21 BCA

4 Ratan 22 MCA

5 Vimal 26 BSC

1
What is a row or record?
A row of a table is also called a record or tuple. It contains the specific information of
each entry in the table. It is a horizontal entity in the table. For example, The above
table contains 5 records.

Properties of a row:

○ No two tuples are identical to each other in all their entries.


○ All tuples of the relation have the same format and the same number of
entries.
○ The order of the tuple is irrelevant. They are identified by their content, not
by their position.

Let's see one record/row in the table.

ID Name AGE COURSE

1 Ajeet 24 [Link]

What is a column/attribute?
A column is a vertical entity in the table which contains all information associated
with a specific field in a table. For example, "name" is a column in the above table
which contains all information about a student's name.

Properties of an Attribute:

○ Every attribute of a relation must have a name.


○ Null values are permitted for the attributes.

1
○ Default values can be specified for an attribute automatically inserted if no
other value is specified for an attribute.
○ Attributes that uniquely identify each tuple of a relation are the primary
key.

Name

Ajeet

Aryan

Mahesh

Ratan

Vimal

What is data item/Cells?


The smallest unit of data in the table is the individual data item. It is stored at the
intersection of tuples and attributes.

Properties of data items:

○ Data items are atomic.

1
○ The data items for an attribute should be drawn from the same domain.

In the below example, the data item in the student table consists of Ajeet, 24 and Btech,
etc.

ID Name AGE COURSE

1 Ajeet 24 [Link]

Degree:

The total number of attributes that comprise a relation is known as the degree of the
table.

For example, the student table has 4 attributes, and its degree is 4.

ID Name AGE COURSE

1 Ajeet 24 [Link]

2 aryan 20 C.A

3 Mahesh 21 BCA

1
4 Ratan 22 MCA

5 Vimal 26 BSC

Cardinality:

The total number of tuples at any one time in a relation is known as the table's
cardinality. The relation whose cardinality is 0 is called an empty table.

For example, the student table has 5 rows, and its cardinality is 5.

ID Name AGE COURSE

1 Ajeet 24 [Link]

2 aryan 20 C.A

3 Mahesh 21 BCA

4 Ratan 22 MCA

5 Vimal 26 BSC

1
Domain:

The domain refers to the possible values each attribute can contain. It can be specified
using standard data types such as integers, floating numbers, etc. For example, An
attribute entitled Marital_Status may be limited to married or unmarried values.

NULL Values

The NULL value of the table specifies that the field has been left blank during record
creation. It is different from the value filled with zero or a field that contains space.

Data Integrity

There are the following categories of data integrity exist with each RDBMS: Entity

integrity: It specifies that there should be no duplicate rows in a table.

Domain integrity: It enforces valid entries for a given column by restricting the type,
the format, or the range of values.

Referential integrity specifies that rows cannot be deleted, which are used by other
records.

User-defined integrity: It enforces some specific business rules defined by users. These
rules are different from the entity, domain, or referential integrity.

1

○ Difference between DBMS and RDBMS


○ Although DBMS and RDBMS both are used to store information in physical
database but there are some remarkable differences between them.
○ The main differences between DBMS and RDBMS are given below:

N DBMS RDBMS

1 DBMS RDBMS applications store data in a


tabular form.
applications store
data as file.

2 I DBMS, data is In RDBMS, the tables have an


n
generally stored in identifier called primary key and
either the data values are stored in the
form of tables.
a
hierarchical form
or a navigational
form.

3 Normalization is Normalization is present in RDBMS.


not
present in DBMS.

1
DBMS does not apply RDBMS defines the integrity constraint
4 any security for the purpose of ACID
with (Atomocity,

2
) regards to Consistency, Isolation
data and Durability)
manipulation. property.

5 DBMS uses file in RDBMS, data values are stored in


system to store the form of tables, so a relationship
data, so there will between these data values will be
be no relation stored in the form of a table as well.
between the
tables.

6 DBMS has to provide RDBMS system supports a tabular


some uniform structure of the data and a
methods to access relationship between them to access
the the stored information.

stored
information.

7 DBMS does not RDBMS supports distributed database.


support
distributed
database.

8 DBMS is meant to be RDBMS is designed to handle large


for amount of data. it supports multiple
users.
small
organization

2
and
deal with
small data.
it
supports

2
single user.

9 Examples of DBMS Example of RDBMS are mysql, postgre,


are file systems, sql server, oracle etc.
xml etc.

○ After observing the differences between DBMS and RDBMS, you can say that
RDBMS is an extension of DBMS. There are many software products in the
market today who are compatible for both DBMS and RDBMS. Means today a
RDBMS application is DBMS application and vice-versa.

2
INTRODUCTION TO MYSQL

MySQL is currently the most popular database management system software used for
managing the relational database. It is open-source database software, which is
supported by Oracle Company. It is fast, scalable, and easy to use database
management system in comparison with Microsoft SQL Server and Oracle Database.
It is commonly used in conjunction with PHP scripts for creating powerful and
dynamic server-side or web-based enterprise applications.

It is developed, marketed, and supported by MySQL AB, a Swedish company, and


written in C programming language and C++ programming language. The official
pronunciation of MySQL is not the My Sequel; it is My Ess Que Ell. However, you can
pronounce it in your way. Many small and big companies use MySQL. MySQL
supports many Operating Systems like Windows, Linux, MacOS, etc. with C, C++,
and Java languages.

MySQL is a Relational Database Management System (RDBMS) software that


provides many things, which are as follows:

○ It allows us to implement database operations on tables, rows, columns, and


indexes.
○ It defines the database relationship in the form of tables (collection of rows and
columns), also known as relations.
○ It provides the Referential Integrity between rows or columns of various
tables.
○ It allows us to updates the table indexes automatically.
○ It uses many SQL queries and combines useful information from multiple
tables for the end-users.

How MySQL Works?


MySQL follows the working of Client-Server Architecture. This model is designed for
the end-users called clients to access the resources from a central computer known as
a server using network services. Here, the clients make requests through a graphical
user interface (GUI), and the server will give the desired output as soon as the
instructions are matched. The process of MySQL environment is the same as the
client-server model.

2
The core of the MySQL database is the MySQL Server. This server is available as a
separate program and responsible for handling all the database instructions,
statements, or commands. The working of MySQL database with MySQL Server are
as follows:

1. MySQL creates a database that allows you to build many tables to store
and manipulate data and defining the relationship between each table.
2. Clients make requests through the GUI screen or command prompt by
using specific SQL expressions on MySQL.
3. Finally, the server application will respond with the requested expressions
and produce the desired result on the client-side.

A client can use any MySQL GUI. But, it is making sure that your GUI should be
lighter and user-friendly to make your data management activities faster and easier.
Some of the most widely used MySQL GUIs are MySQL Workbench, SequelPro,
DBVisualizer, and the Navicat DB Admin Tool. Some GUIs are commercial, while
some are free with limited functionality, and some are only compatible with MacOS.
Thus, you can choose the GUI according to your needs.

Reasons for popularity


MySQL is becoming so popular because of these following reasons:

○ MySQL is an open-source database, so you don't have to pay a single penny to


use it.
○ MySQL is a very powerful program that can handle a large set of
functionality of the most expensive and powerful database packages.
○ MySQL is customizable because it is an open-source database, and the
open-source GPL license facilitates programmers to modify the SQL
software according to their own specific environment.

2
○ MySQL is quicker than other databases, so it can work well even with the
large data set.
○ MySQL supports many operating systems with many languages like PHP,
PERL, C, C++, JAVA, etc.
○ MySQL uses a standard form of the well-known SQL data language.
○ MySQL is very friendly with PHP, the most popular language for web
development.
○ MySQL supports large databases, up to 50 million rows or more in a table.
The default file size limit for a table is 4GB, but you can increase this (if your
operating system can handle it) to a theoretical limit of 8 million terabytes
(TB).

2
INSTALLATION OF
MYSQL
How to install MySQL
MySQL is one of the most popular relational database management software that is
widely used in today's industry. It provides multi-user access support with various
storage engines. It is backed by Oracle Company. In this section, we are going to learn
how we can download and install MySQL for beginners.

Prerequisites

The following requirements should be available in your system to work with MySQL:

○ MySQL Setup Software


○ Microsoft .NET Framework 4.5.2
○ Microsoft Visual C++ Redistributable for Visual Studio 2019
○ RAM 4 GB (6 GB recommended)

Download MySQL

Follow these steps:

Step 1: Go to the official website of MySQL and download the community server
edition software. Here, you will see the option to choose the Operating System, such as
Windows.

Step 2: Next, there are two options available to download the setup. Choose the
version number for the MySQL community server, which you want. If you have good
internet connectivity, then choose the mysql-installer-web-community. Otherwise,
choose the other one.

2
Installing MySQL on Windows

Step 1: After downloading the setup, unzip it anywhere and double click the MSI
installer .exe file. It will give the following screen:

Step 2: In the next wizard, choose the Setup Type. There are several types available,
and you need to choose the appropriate option to install MySQL product and features.
Here, we are going to select the Full option and click on the Next button.

2
This option will install the following things: MySQL Server, MySQL Shell, MySQL
Router, MySQL Workbench, MySQL Connectors, documentation, samples and
examples, and many more.

Step 3: Once we click on the Next button, it may give information about some features
that may fail to install on your system due to a lack of requirements. We can resolve
them by clicking on the Execute button that will install all requirements automatically
or can skip them. Now, click on the Next button.

Step 4: In the next wizard, we will see a dialog box that asks for our confirmation of a
few products not getting installed. Here, we have to click on the Yes button.

After clicking on the Yes button, we will see the list of the products which are going to
be installed. So, if we need all products, click on the Execute button.

2
Step 5: Once we click on the Execute button, it will download and install all the
products. After completing the installation, click on the Next button.

Step 6: In the next wizard, we need to configure the MySQL Server and Router. Here,
I am not going to configure the Router because there is no need to use it with MySQL.
We are going to show you how to configure the server only. Now, click on the Next
button.

3
Step 7: As soon as you will click on the Next button, you can see the screen below.
Here, we have to configure the MySQL Server. Now, choose the Standalone MySQL
Server/Classic MySQL Replication option and click on Next. Here, you can also
choose the InnoDB Cluster based on your needs.

Step 8: In the next screen, the system will ask you to choose the Config Type and other
connectivity options. Here, we are going to select the Config Type as 'Development
Machine' and Connectivity as TCP/IP, and Port Number is 3306, then click on Next.

3
Step 9: Now, select the Authentication Method and click on Next. Here, I am going to
select the first option.

Step 10: The next screen will ask you to mention the MySQL Root Password. After
filling the password details, click on the Next button.

3
Step 11: The next screen will ask you to configure the Windows Service to start the
server. Keep the default setup and click on the Next button.

Step 12: In the next wizard, the system will ask you to apply the Server Configuration.
If you agree with this configuration, click on the Execute button.

3
Step 13: Once the configuration has completed, you will get the screen below. Now,
click on the Finish button to continue.

Step 14: In the next screen, you can see that the Product Configuration is completed.
Keep the default setting and click on the Next-> Finish button to complete the MySQL
package installation.

3
Step 15: In the next wizard, we can choose to configure the Router. So click on Next-
>Finish and then click the Next button.

Step 16: In the next wizard, we will see the Connect to Server option. Here, we have to
mention the root password, which we had set in the previous steps.

In this screen, it is also required to check about the connection is successful or not by
clicking on the Check button. If the connection is successful, click on the Execute
button. Now, the configuration is complete, click on Next.

3
Step 17: In the next wizard, select the applied configurations and click on the Execute
button.

Step 18: After completing the above step, we will get the following screen. Here, click on
the Finish button.

3
Step 19: Now, the MySQL installation is complete. Click on the Finish button.

Verify MySQL installation

Once MySQL has been successfully installed, the base tables have been initialized, and
the server has been started, you can verify its working via some simple tests.

Open your MySQL Command Line Client; it should have appeared with a mysql>
prompt. If you have set any password, write your password here. Now, you are
connected to the MySQL server, and you can execute all the SQL command at mysql>
prompt as follows:

For example: Check the already created databases with show databases command:

3
3
QUERIES
1 MySQL Create Database Statement

Here, we are going to create a database name "employeedb" using the following
statement:

Syntax:-

mysql> CREATE DATABASE

charan3; Example:-

3
2 MySQL CREATE TABLE Statement

MySQL allows us to create a table into the database by using the CREATE
TABLE command. Following is a generic syntax for creating a MySQL table
in the database.

Syntax:-

1. CREATE TABLE [IF NOT EXISTS] table_name(


2. column_definition1,
3. column_definition2,
4 ,
5. table_constraints
6. );

Example:-

We need to use the following command to see the newly created table: mysql>

SHOW TABLES;

output:

4
MySQL Desc table statement

We can use the following command to see the information or structure of the
newly created table:

Syntax:-

mysql> DESCRIBE

employee_table; Example:-

4
3 Retrieval of rows using select statement

It is the most commonly used MySQL query. The general syntax of this
statement to fetch data from tables st is as follows:

Syntax:-

SELECT Name FROM st;

Example:-

1. If we want to retrieve a single column from the table, we need to execute the
below query:

Syntax:-

SELECT Name FROM st;

We will get the below output where we can see only one column records.

4
2. If we want to query multiple columns from the table, we need to execute the
below query:

Syntax:-

SELECT Name, Email, City FROM employee_detail;

We will get the below output where we can see the Roll_No,name of st.

Example:-

4
4 conditional retrieval of rows
Following syntax is used to retrieve particular row from the table ➖

Syntax:-

Select * from st where Roll_NO=107

4
5 MySQL ALTER and DROP Statement

1) ADD a column in the table

Syntax:

ALTER TABLE table_name

ADD new_column_name column_definition ;

Example:-

ALTER TABLE g1

ADD class varchar(12) ;

4
2 Drop column in table

Syntax:-

ALTER TABLE table_name

drop new_column_name column_definition ;

ALTER TABLE

table_name Drop class ;

4
6 Update and delete statement 1

Update statement

Syntax:-

UPDATE st

SET Name = 'Raj'

WHERE Name =

'Aman'; Example:-

4
2 Delete statement

Syntax:-

DELETE FROM st WHERE Roll_NO=107;

Example:-

4
7 Join sequences

MySQL JOINS are used with SELECT statement. It is used to retrieve data
from multiple tables. It is performed whenever you need to fetch records from
two or more tables.

There are three types of MySQL joins:

○ MySQL INNER JOIN (or sometimes called simple join)


○ MySQL LEFT OUTER JOIN (or sometimes called LEFT JOIN)
○ MySQL RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)

MySQL Inner JOIN (Simple Join)

The MySQL INNER JOIN is used to return all rows from multiple
tables where the join condition is satisfied. It is the most common type of join.

Syntax:

1. SELECT columns
2. FROM table1
3. INNER JOIN table2
4. ON [Link] = [Link];

Image representation:

Let's take an example:

4
Consider two tables "officers" and "students", having the following data.

Execute the following query:

1. SELECT officers.officer_name, [Link],


students.course_name
2. FROM officers

3. INNER JOIN students


4. ON officers.officer_id = students.student_id;

Output:

5
MySQL Left Outer Join

The LEFT OUTER JOIN returns all rows from the left hand table specified in
the ON condition and only those rows from the other table where the join
condition is fulfilled.

Syntax:

1. SELECT columns
2. FROM table1
3. LEFT [OUTER] JOIN table2
4. ON [Link] = [Link];

Image representation:

Let's take an example:

Consider two tables "officers" and "students", having the following data.

5
Execute the following query:

1. SELECT officers.officer_name, [Link],


students.course_name
2. FROM officers
3. LEFT JOIN students
4. ON officers.officer_id = students.student_id;

Output:

MySQL Right Outer Join

5
The MySQL Right Outer Join returns all rows from the RIGHT-hand table
specified in the ON condition and only those rows from the other table where he
join condition is fulfilled.

Syntax:

1. SELECT columns
2. FROM table1
3. RIGHT [OUTER] JOIN table2
4. ON [Link] = [Link];

Image representation:

Let's take an example:

Consider two tables "officers" and "students", having the following data.

5
Execute the following query:

1. SELECT officers.officer_name, [Link],


students.course_name, students.student_name
2. FROM officers
3. RIGHT JOIN students
4. ON officers.officer_id = students.student_id;

Output:

5
8 Commit and Rollback statement

1 Commit statement

Syntax:-

Commit;

Example:-

5
2 Rollback statement

Syntax:-

ROLLBACK;

Example:-

ROLLBACK;

output

5
9 MySQL IS NULL Condition

MySQL IS NULL condition is used to check if there is a NULL value in the


expression. It is used with SELECT, INSERT, UPDATE and DELETE
statement.

Syntax:

expression IS NULL

Parameter

expression: It specifies a value to test if it is NULL value.

Consider a table "officers" having the following data.

Execute the following query:

● SELECT *
● FROM officers
● WHERE officer_name IS NULL;

Output:

5
5
11 MySQL Aggregate Functions

MySQL's aggregate function is used to perform calculations on multiple values


and return the result in a single value like the average of all values, the sum of
all values, and maximum & minimum value among certain groups of values.
We mostly use the aggregate functions with SELECT statements in the data
query languages.

Syntax:

mysql> SELECT COUNT(name) FROM employee;

Count() Function

MySQL count() function returns the total number of values in the expression.
This function produces all rows or only some rows of the table based on a
specified condition, and its return type is BIGINT. It returns zero if it does not
find any matching rows. It can work with both numeric and non-numeric data
types.

Example

Suppose we want to get the total number of employees in the employee table, we
need to use the count() function as shown in the following query:

mysql> SELECT COUNT(name) FROM employee;

Output:

After execution, we can see that this table has six employees.

5
Sum() Function

The MySQL sum() function returns the total summed (non-NULL) value of an
expression. It returns NULL if the result set does not have any rows. It works
with numeric data type only.

Suppose we want to calculate the total number of working hours of all


employees in the table, we need to use the sum() function as shown in the
following query:

mysql> SELECT SUM(working_hours) AS "Total working hours" FROM


employee;

Output:

AVG() Function

MySQL AVG() function calculates the average of the values specified in the
column. Similar to the SUM() function, it also works with numeric data type
only.

Suppose we want to get the average working hours of all employees in the table,
we need to use the AVG() function as shown in the following query:

syntax:-

6
mysql> SELECT AVG(working_hours) AS "Average working hours" FROM
employee;

Output:

Min and Max Statement.


The MIN () function returns the smallest value of the selected
column. The MAX () function returns the largest value of the selected
column. Syntax: SELECT MIN (column name)
FROM table name
WHERE condition;
Syntax: SELECT MAX(column name)
FROM table name
WHERE condition;
Example:

6
12 Ordering the result of queries

Syntax:-

select * from student order by Roll_No desc;

13 MySQL Operators.

The MySQL IN condition is used to reduce the use of multiple OR conditions in


a SELECT, INSERT, UPDATE and DELETE statement.

In Operator

Syntax:
SELECT * FROM officers WHERE officer_name = 'Ajeet' OR
officer_name = 'Vimal' OR officer_name = 'Deepika';

Execute the following query:

6
● SELECT *
● FROM officers
● WHERE officer_name IN ('Ajeet', 'Vimal', 'Deepika');

Output:

OR Operator

Execute the following query:

● SELECT *
● FROM officers
● WHERE officer_name = 'Ajeet'
● OR officer_name = 'Vimal'
● OR officer_name =

'Deepika'; Output:

6
3 Between operator

● SELECT *
● FROM officers
● WHERE officer_id BETWEEN 1 AND

3; Output:

14 Grouping the result of


queries Syntax:-
Select Name From st group by Name;

6
15 Lower.
The SQL LOWER () function is used to convert all characters of a string to
lower case.
Syntax: LOWER (SQL course)
Example:

16 UPPER.
The SQL UPPER () function is used to convert all characters of a string to
uppercase.
Syntax: UPPER (SQL course)
Example:

17 Length.
This function returns the length of the input string.
Syntax: LENGTH (Column | Expression)
Example:

6
18 CONCAT.
This function always appends (concatenates) string2 to the end of string1.
Syntax: CONCAT ('String1', 'String2')
Example:

19 INSTR.
This function returns numeric position of a character or a string in a given
string.
Syntax: INSTR (Column | Expression, 'String', [,m], [n])
Example:

6
20 SUBSTR.
This function returns a portion of a string from a given start point to an end
point.
Syntax: SUBSTR ('String', start-index, length of extracted string)
Example:

21 Union ALL operator


A union is used for extracting rows using the conditions
specified in the query while Union All is used for extracting
all the rows from a set of two tables.
Syntax: select column name(s) from
table1 UNION ALL
Select column name(s)from table2;

6
22 Intersection operator
The UNION ALL command combines the result set of two or more SELECT
statements (allows duplicate values).
Syntax: select column name(s) from table1
Intersect
Select column name(s)from table2;

23 Intersection ALL statement


Syntax: select column name(s) from
table1 Intersect
Select column name(s)from table2;

6
24 Create view statement
A view contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database.
Syntax: Create view view name AS
Select column1, column2
From table name
Where
condition;

25 Create index statement


Indexes are used to retrieve data from the database more quickly than
otherwise. The users cannot see the indexes, they are just used to speed up
searches/queries.
Syntax: Create index index name
ON table name (column1,
column2);

26 Unique index statement


Creates a unique index on a table. Duplicate values are not allowed
Syntax: Unique index index name
ON table name (column1, column2);

6
27 Drop index statement
The DROP INDEX statement is used to delete an index in a table.
Syntax: Drop index index name
ON table name;

28 PL/SQL Architecture

PL/SQL is a block structured language. The programs of PL/SQL are logical


blocks that can contain any number of nested sub-blocks. Pl/SQL stands for
"Procedural Language extension of SQL" that is used in Oracle. PL/SQL is
integrated with Oracle database (since version 7). The functionalities of
PL/SQL usually extended after each release of Oracle database. Although
PL/SQL is closely integrated with SQL language, yet it adds some
programming constraints that are not available in SQL.

PL/SQL Functionalities

PL/SQL includes procedural language elements like conditions and loops. It


allows declaration of constants and variables, procedures and functions, types
and variable of those types and triggers. It can support Array and handle
exceptions (runtime errors). After the implementation of version 8 of Oracle
database have included features associated with object orientation. You can
create PL/SQL units like procedures, functions, packages, types and triggers,
etc. which are stored in the database for reuse by applications.

With PL/SQL, you can use SQL statements to manipulate Oracle data and flow
of control statements to process the data.

The PL/SQL is known for its combination of data manipulating power of SQL
with data processing power of procedural languages. It inherits the robustness,
security, and portability of the Oracle Database.

PL/SQL is not case sensitive so you are free to use lower case letters or upper
case letters except within string and character literals. A line of PL/SQL text
contains groups of characters known as lexical units. It can be classified as
follows:

○ Delimeters

7
○ Identifiers
○ Literals
○ Comments

Features of PL/SQL :

The various features of PL/SQL are given below –


PL/SQL runs on various operating systems such as windows,
Linux etc.

● PL/SQL have an error-checking facility and displays user-


friendly messages when an error occurs in a program.

What do you mean by PL/SQL Architecture?

The PL/SQL runtime system is a technology and not an independent product.


This technology is actually like an engine that exhibits PL/SQL blocks,
subprograms like functions and procedures. This engine can be installed in an
Oracle Server or in application development tools such as Oracle Form Builder,
Oracle Reports Builder etc.

7
PL/SQL Architecture

PL/SQL can reside in two environments –

1.
The Oracle Server

2. The Oracle tools

These two environments are independent of each other. In either environment,


the PL/SQL engine accepts any valid PL/SQL block as input. The PL/SQL
engine executes the procedural part of the statements and sends the SQL
statement executer in the Oracle Server. A single transfer is required to send
the block from the application to the Oracle Server, thus improving
performance, especially in a Client-Server network. PL/SQL code can also be
stored in the Oracle server as subprograms that can be referenced by any
number of applications connected to the database.

Advantages of PL/SQL :


PL/SQL provides better performance.

● PL/SQL has high Productivity.

● It supports Object-Oriented Programming concepts.

● It has Scalability and Manageability.

● PL/SQL supports various Web Application Development tools.

Disadvantages of PL/SQL :


PL/SQL requires high memory.

● Lacks of functionality debugging in stored procedures.

7
7

You might also like