RDBMS MySQL Practical Guide
RDBMS MySQL Practical Guide
FILE OF
RDBMS
(RELATIONAL DATABASE MANAGEMENT SYSTEM)
Submitted by Submitted to
Ishleen kaur Prof. Kamaljeet Kaur
22451 Computer Science Department
1
INDEX
SR. QUARRY PAG
NO. NAME E
NO.
1 INTRODUCTION 4-21
15 MYSQL OPERATORS 62
17 LOWER QUERY 63
18 UPPER QUERY 64
19 LENGTH QUERY 64
20 CONCAT QUERY 65
21 INSTR QUERY 65
22 SUBSTR QUERY 66
24 INTERSECTION OPERATOR 65
2
25 INTERSECTION ALL STATEMENT 65
29 DROP INDEX 68
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
○ 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.
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
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.
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:
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.
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
All modern database management systems like SQL, MS SQL Server, IBM DB2,
ORACLE, My-SQL, and Microsoft Access are based on RDBMS.
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.
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:
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:
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:
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
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.
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.
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.
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
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
○
N DBMS RDBMS
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.
stored
information.
2
and
deal with
small data.
it
supports
2
single user.
○ 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.
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.
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:
Download MySQL
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.
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:-
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:-
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:-
Example:-
1. If we want to retrieve a single column from the table, we need to execute the
below query:
Syntax:-
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:-
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:-
4
5 MySQL ALTER and DROP Statement
Syntax:
Example:-
ALTER TABLE g1
4
2 Drop column in table
Syntax:-
ALTER TABLE
4
6 Update and delete statement 1
Update statement
Syntax:-
UPDATE st
WHERE Name =
'Aman'; Example:-
4
2 Delete statement
Syntax:-
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.
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:
4
Consider two tables "officers" and "students", having the following data.
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:
Consider two tables "officers" and "students", having the following data.
5
Execute the following query:
Output:
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:
Consider two tables "officers" and "students", having the following data.
5
Execute the following query:
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
Syntax:
expression IS NULL
Parameter
● SELECT *
● FROM officers
● WHERE officer_name IS NULL;
Output:
5
5
11 MySQL Aggregate Functions
Syntax:
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:
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.
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:
6
12 Ordering the result of queries
Syntax:-
13 MySQL Operators.
In Operator
Syntax:
SELECT * FROM officers WHERE officer_name = 'Ajeet' OR
officer_name = 'Vimal' OR officer_name = 'Deepika';
6
● SELECT *
● FROM officers
● WHERE officer_name IN ('Ajeet', 'Vimal', 'Deepika');
Output:
OR Operator
● 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:
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:
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;
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;
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 Functionalities
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 :
●
PL/SQL runs on various operating systems such as windows,
Linux etc.
7
PL/SQL Architecture
1.
The Oracle Server
Advantages of PL/SQL :
●
PL/SQL provides better performance.
Disadvantages of PL/SQL :
●
PL/SQL requires high memory.
7
7