0% found this document useful (0 votes)
4 views117 pages

Types of MySQL Variables - Local Variables

The document discusses advanced SQL topics, focusing on MySQL variables, including local, session, and global variables, and their scopes. It explains the creation and use of indexes in MySQL to enhance search speed, emphasizing the importance of selecting appropriate columns for indexing. Additionally, it highlights the distinction between user-defined and system variables, along with considerations for optimizing database performance.

Uploaded by

Kabir Singh
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)
4 views117 pages

Types of MySQL Variables - Local Variables

The document discusses advanced SQL topics, focusing on MySQL variables, including local, session, and global variables, and their scopes. It explains the creation and use of indexes in MySQL to enhance search speed, emphasizing the importance of selecting appropriate columns for indexing. Additionally, it highlights the distinction between user-defined and system variables, along with considerations for optimizing database performance.

Uploaded by

Kabir Singh
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/ 117

Advanced SQL Topics

Types of MySQL Variables – Local Variables


Types of MySQL Variables – Local Variables

scope
the region of a computer program where a phenomenon, such as a
variable, is considered valid
Types of MySQL Variables – Local Variables

scope
the region of a computer program where a phenomenon, such as a
variable, is considered valid
Types of MySQL Variables – Local Variables

scope
the region of a computer program where a phenomenon, such as a
variable, is considered valid
Types of MySQL Variables – Local Variables

scope
the region of a computer program where a phenomenon, such as a
variable, is considered valid


Types of MySQL Variables – Local Variables

scope
the region of a computer program where a phenomenon, such as a
variable, is considered valid


Types of MySQL Variables – Local Variables

scope =
the region of a computer program where a phenomenon, such as a
variable, is considered valid


Types of MySQL Variables – Local Variables

scope = visibility
the region of a computer program where a phenomenon, such as a
variable, is considered valid


Types of MySQL Variables – Local Variables

MySQL Variables
Types of MySQL Variables – Local Variables

MySQL Variables
Types of MySQL Variables – Local Variables

MySQL Variables

local
Types of MySQL Variables – Local Variables

MySQL Variables

local
Types of MySQL Variables – Local Variables

MySQL Variables

local
session
Types of MySQL Variables – Local Variables

MySQL Variables

local
session
Types of MySQL Variables – Local Variables

MySQL Variables

local global
session
Types of MySQL Variables – Local Variables

MySQL Variables

local global
session
Types of MySQL Variables – Local Variables

MySQL Variables

local global
session

local variable
a variable that is visible only in the BEGIN – END block in which it was
created
Types of MySQL Variables – Local Variables
DECLARE is a keyword that can be used when creating local variables only

v_avg_salary is visible only in the BEGIN - END block


Session Variables
Session Variables

session
a series of information exchange interactions, or a dialogue, between a
computer and a user
Session Variables

session
a series of information exchange interactions, or a dialogue, between a
computer and a user

- e.g. a dialogue between the MySQL server and a client


application like MySQL Workbench
Session Variables
a session begins at a certain point in time and terminates at another,
later point
Session Variables
a session begins at a certain point in time and terminates at another,
later point
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1

set up a
connection
Step 1
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1

set up a
connection
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1

set up a
connection
Step 2
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1

set up a
connection
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1 Step 2

set up a establish a
connection connection
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1 Step 2

set up a establish a
connection connection
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1 Step 2 Step 3

set up a establish a the Workbench


connection connection interface will
open immediately
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1 Step 2 Step 3

set up a establish a the Workbench


connection connection interface will
open immediately
Step 3
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1 Step 2 Step 3

set up a establish a the Workbench


connection connection interface will
open immediately
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1 Step 2 Step 3


session

set up a establish a the Workbench


connection connection interface will
open immediately
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1 Step 2 Step 3 Step 4


session

set up a establish a the Workbench


connection connection interface will
open immediately
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1 Step 2 Step 3 Step 4


session

set up a establish a the Workbench end a


connection connection interface will connection
open immediately
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1 Step 2 Step 3 Step 4


session

set up a establish a the Workbench end a


connection connection interface will connection
open immediately
Session Variables
a session begins at a certain point in time and terminates at another,
later point

Step 1 Step 2 Step 3 Step 4

set up a establish a the Workbench end a


connection connection interface will connection
open immediately
Session Variables
there are certain SQL objects that are valid for a specific session only
Session Variables
there are certain SQL objects that are valid for a specific session only

Step 1 Step 2 Step 3


session

set up a establish a the Workbench


connection connection interface will
open immediately
Session Variables
there are certain SQL objects that are valid for a specific session only

Step 1 Step 2
session

set up a establish a the Workbench


connection connection interface will
open immediately
Session Variables
there are certain SQL objects that are valid for a specific session only

Step 1 Step 2
session

set up a establish a the Workbench end a


connection connection interface will connection
open immediately
Session Variables
there are certain SQL objects that are valid for a specific session only

Step 1 Step 2

set up a establish a the Workbench end a


connection connection interface will connection
open immediately
Session Variables

session variable

a variable that exists only for the session in which you are operating
Session Variables

session variable

a variable that exists only for the session in which you are operating

- it is defined on our server, and it lives there


Session Variables

session variable

a variable that exists only for the session in which you are operating

- it is defined on our server, and it lives there


- it is visible to the connection being used only
Session Variables
Session Variables

= 100
Session Variables

= 100 connections = 100


Session Variables

= 100 connections = 100 sessions = 100


Session Variables

“Var”

= 100 connections = 100 sessions = 100


Session Variables

“Var”

= 100 connections = 100 sessions = 100


Session Variables

SET @var_name = value;


Global Variables
Global Variables

global variables

global variables apply to all connections related to a specific server


Global Variables
Global Variables

“Var” “Var”
“Var” “Var”
“Var” “Var”
“Var”
“Var” “Var”
Global Variables

SET GLOBAL var_name = value;


Global Variables

SET GLOBAL var_name = value;

SET @@global.var_name = value;


Global Variables
you cannot set just any variable as global
Global Variables
you cannot set just any variable as global
- a specific group of pre-defined variables in MySQL is suitable for
this job. They are called system variables
Global Variables
you cannot set just any variable as global
- a specific group of pre-defined variables in MySQL is suitable for
this job. They are called system variables
Global Variables
you cannot set just any variable as global
- a specific group of pre-defined variables in MySQL is suitable for
this job. They are called system variables

.max_connections()
Global Variables
you cannot set just any variable as global
- a specific group of pre-defined variables in MySQL is suitable for
this job. They are called system variables

.max_connections()

.max_join_size()
Global Variables
you cannot set just any variable as global
- a specific group of pre-defined variables in MySQL is suitable for
this job. They are called system variables

.max_connections() - indicates the maximum number of connections


to a server that can be established at a
certain point in time

.max_join_size()
Global Variables
you cannot set just any variable as global
- a specific group of pre-defined variables in MySQL is suitable for
this job. They are called system variables

.max_connections() - indicates the maximum number of connections


to a server that can be established at a
certain point in time

.max_join_size() - sets the maximum memory space allocated for


the joins created by a certain connection
User-Defined vs System Variables
User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created
User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

user-defined
User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

user-defined

system
User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

user-defined variables that can be set by the user manually

system
User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

user-defined variables that can be set by the user manually

system variables that are pre-defined on our system –


the MySQL server
User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

local session global

user-defined

system

- local variables can be user-defined only


User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

local session global

user-defined

system

- local variables can be user-defined only


User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

local session global

user-defined

system

- only system variables can be set as global


User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

local session global

user-defined

system

- only system variables can be set as global


User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

local session global

user-defined

system

- both user-defined and system variables can be set as session variables


User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

local session global

user-defined

system

- both user-defined and system variables can be set as session variables


User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

local session global

user-defined

system

- both user-defined and system variables can be set as session variables


there are limitations to this rule!
User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

local session global

user-defined *

system *

- both user-defined and system variables can be set as session variables


there are limitations to this rule!
User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

local session global

user-defined *

system *

- some of the system variables can be defined as global only; they


cannot be session variables
User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created

local session global

user-defined *

system *

- some of the system variables can be defined as global only; they


cannot be session variables (e.g. .max_connections() )
MySQL Indexes
MySQL Indexes
MySQL Indexes
MySQL Indexes
the index of a table functions like the index of a book
MySQL Indexes
the index of a table functions like the index of a book
- data is taken from a column of the table and is stored in a certain
order in a distinct place, called an index
MySQL Indexes
the index of a table functions like the index of a book
- data is taken from a column of the table and is stored in a certain
order in a distinct place, called an index

your datasets will typically contain 100,000+ or even 1,000,000+ records


MySQL Indexes
the index of a table functions like the index of a book
- data is taken from a column of the table and is stored in a certain
order in a distinct place, called an index

your datasets will typically contain 100,000+ or even 1,000,000+ records

- the larger a database is, the slower the process of finding the record
or records you need
MySQL Indexes
we can use an index that will increase the speed of searches related to
a table
MySQL Indexes
we can use an index that will increase the speed of searches related to
a table

CREATE INDEX index_name


ON table_name (column_1, column_2, …);
MySQL Indexes
we can use an index that will increase the speed of searches related to
a table

CREATE INDEX index_name


ON table_name (column_1, column_2, …);

the parentheses serve us to indicate the column


names on which our search will be based
MySQL Indexes
we can use an index that will increase the speed of searches related to
a table

CREATE INDEX index_name


ON table_name (column_1, column_2, …);

these must be fields from your data table you


will search frequently
MySQL Indexes
composite indexes
MySQL Indexes
composite indexes
- applied to multiple columns, not just a single one
MySQL Indexes
composite indexes
- applied to multiple columns, not just a single one

CREATE INDEX index_name


ON table_name (column_1, column_2, …);
MySQL Indexes
composite indexes
- applied to multiple columns, not just a single one

CREATE INDEX index_name


ON table_name (column_1, column_2, …);

- carefully pick the columns that would optimize your search!


MySQL Indexes
primary and unique keys are MySQL indexes
MySQL Indexes
primary and unique keys are MySQL indexes
- they represent columns on which a person would typically base their
search
MySQL Indexes
MySQL Indexes
SQL specialists are always aiming for a good balance between the
improvement of speed search and the resources used for its execution
MySQL Indexes
SQL specialists are always aiming for a good balance between the
improvement of speed search and the resources used for its execution

the costs of having an index might


small datasets be higher than the benefits
MySQL Indexes
SQL specialists are always aiming for a good balance between the
improvement of speed search and the resources used for its execution

the costs of having an index might


small datasets be higher than the benefits

a well-optimized index can make a


large datasets positive impact on the search
process
The CASE Statement
Condition #1
Condition #1
Condition #1
Condition #1 Output #1
Output #1

Condition #1 Output #2
Output #1

Condition #1 Output #2

COALESCE()
Output #1

Condition #1 Output #2

COALESCE()

IFNULL()
Output #1

Condition #1 Output #2

COALESCE()

IFNULL()

The CASE Statement


THE CASE STATEMENT

SELECT
column_name(s)
CASE
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2

ELSE
END AS
FROM
table_name;
THE CASE STATEMENT

SELECT
column_name(s)
CASE condition_field
WHEN condition_field_value_1 THEN result_1
WHEN condition_field_value_2 THEN result_2

ELSE
END AS
FROM
table_name;

You might also like