SQL - MySQL For Data Analytics and Business Intelligence
SQL - MySQL For Data Analytics and Business Intelligence
SQL’s syntax
Data Definition Language
SQL’s syntax
comprises several types of statements that allow you to perform various
commands and operations
Data Definition Language
SQL’s syntax
comprises several types of statements that allow you to perform various
commands and operations
SQL’s syntax
comprises several types of statements that allow you to perform various
commands and operations
SQL’s syntax
comprises several types of statements that allow you to perform various
commands and operations
SQL’s syntax
comprises several types of statements that allow you to perform various
commands and operations
sales
purchase_number
Data Definition Language
sales
purchase_number
the table name can coincide with the name assigned to the database
Data Definition Language
- ADD
- REMOVE
- RENAME
Data Definition Language
sales
purchase_number
Data Definition Language
sales
purchase_number
Data Definition Language
sales
purchase_number date_of_purchase
Data Definition Language
customers
customer_id first_name
Data Definition Language
customers
customer_id first_name
Data Definition Language
customers
customer_id first_name
Data Definition Language
customer_id first_name
Data Definition Language
customer_data
customer_id first_name
Data Definition Language
customers
customer_id first_name
Data Definition Language
Keywords:
- ADD
Keywords
Keywords:
- ADD
- CREATE
- ALTER
- etc.
Keywords
Keywords:
- ADD
- CREATE
- ALTER
- etc.
Keywords:
- ADD
- CREATE
- ALTER
- etc.
CREATE, ALTER:
Keywords
CREATE, ALTER:
alter
purchase_number
Data Definition Language
ADD
Data Definition Language
ADD
sales
purchase_number date_of_purchase
Data Definition Language
ADD, ALTER
sales
purchase_number date_of_purchase
Keywords
sales
purchase_number
Data Manipulation Language
sales
purchase_number
Data Manipulation Language
sales
purchase_number
Data Manipulation Language
sales
purchase_number
Data Manipulation Language
sales
purchase_number
Data Manipulation Language
sales
purchase_number date_of_purchase
Data Manipulation Language
sales
purchase_number date_of_purchase
1 2017-10-11
Data Manipulation Language
sales
purchase_number date_of_purchase
1 2017-10-11
Data Manipulation Language
sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language
sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language
UPDATE sales
SET date_of_purchase = ‘2017-12-12’
WHERE purchase_number = 1;
sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language
UPDATE sales
SET date_of_purchase = ‘2017-12-12’
WHERE purchase_number = 1;
sales
purchase_number date_of_purchase
1 2017-12-12
2 2017-10-27
Data Manipulation Language
vs.
with DELETE, you can specify precisely what you would like to be removed
Data Manipulation Language
sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language
sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language
sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language
sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language
sales
purchase_number date_of_purchase
1 2017-10-11
2 2017-10-27
Data Manipulation Language
users
Data Control Language
big companies and corporations don’t use this type of server, and their
databases lay on external, more powerful servers
Database administrators
Data Control Language
Database administrators
people who have complete rights to a database
Data Control Language
Database administrators
people who have complete rights to a database
- they can grant access to users and can revoke it
Data Control Language
Database administrators
people who have complete rights to a database
- they can grant access to users and can revoke it
Database administrators
people who have complete rights to a database
- they can grant access to users and can revoke it
Database administrators
people who have complete rights to a database
- they can grant access to users and can revoke it
Next:
Data Control Language
Next:
Transaction Control Language (TCL)
Transaction Control Language (DCL)
Transaction Control Language
DB administrator
Transaction Control Language
DB administrator
- Change the last name of the 4th customer from ‘Winnfield’ to ‘Johnson’
Transaction Control Language
DB administrator
- Change the last name of the 4th customer from ‘Winnfield’ to ‘Johnson’
Transaction Control Language
DB administrator
- Change the last name of the 4th customer from ‘Winnfield’ to ‘Johnson’
Transaction Control Language
DB administrator
Transaction Control Language
DB administrator
UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4;
Transaction Control Language
DB administrator
UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4;
Transaction Control Language
DB administrator
UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4;
Transaction Control Language
DB administrator
Problem: users
Transaction Control Language
DB administrator
UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4;
Transaction Control Language
DB administrator
UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4
COMMIT;
Transaction Control Language
DB administrator
users
Transaction Control Language
DB administrator
UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4
COMMIT;
Transaction Control Language
DB administrator
UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4
COMMIT;
ROLLBACK;
Transaction Control Language
DB administrator
UPDATE customers
SET last_name = ‘Johnson’
WHERE customer_id = 4
COMMIT;
ROLLBACK;
Transaction Control Language
primary key
a column (or a set of columns) whose value exists and is unique for
every record in a table is called a primary key
Relational Schemas: Primary Key
Primary Key
Relational Schemas: Primary Key
Primary Key
a column (or a set of columns) whose value exists and is unique for
every record in a table is called a primary key
Relational Schemas: Primary Key
Primary Key
a column (or a set of columns) whose value exists and is unique for
every record in a table is called a primary key
- each table can have one and only one primary key
Relational Schemas: Primary Key
Primary Key
a column (or a set of columns) whose value exists and is unique for
every record in a table is called a primary key
- each table can have one and only one primary key
- in one table, you cannot have 3 or 4 primary keys
Relational Schemas: Primary Key
Primary Key
a column (or a set of columns) whose value exists and is unique for
every record in a table is called a primary key
- each table can have one and only one primary key
- in one table, you cannot have 3 or 4 primary keys
Relational Schemas: Primary Key
Primary Key
may be composed of a set of columns
Relational Schemas: Primary Key
Primary Key
may be composed of a set of columns
e.g. “purchase_number” + “date_of_purchase”
Relational Schemas: Primary Key
Primary Key
may be composed of a set of columns
e.g. “purchase_number” + “date_of_purchase”
Relational Schemas: Primary Key
Primary Key
one-column primary key = all purchases will be recorded under a
different number
Relational Schemas: Primary Key
Primary Key
a column (or a set of columns) whose value exists and is unique for
every record in a table is called a primary key
- each table can have one and only one primary key
- in one table, you cannot have 3 or 4 primary keys
Relational Schemas: Primary Key
Primary Key
a column (or a set of columns) whose value exists and is unique for
every record in a table is called a primary key
- each table can have one and only one primary key
- in one table, you cannot have 3 or 4 primary keys
- primary keys are the unique identifiers of a table
Relational Schemas: Primary Key
Primary Key
a column (or a set of columns) whose value exists and is unique for
every record in a table is called a primary key
- each table can have one and only one primary key
- in one table, you cannot have 3 or 4 primary keys
- primary keys are the unique identifiers of a table
- cannot contain null values!
Relational Schemas: Primary Key
Primary Key
Relational Schemas: Primary Key
Primary Key
Relational Schemas: Primary Key
Relational Schemas: Primary Key
Relational Schemas: Primary Key
=
Relational Schemas: Primary Key
Primary Key
a column (or a set of columns) whose value exists and is unique for
every record in a table is called a primary key
- each table can have one and only one primary key
- in one table, you cannot have 3 or 4 primary keys
- primary keys are the unique identifiers of a table
- cannot contain null values!
Relational Schemas: Primary Key
Primary Key
a column (or a set of columns) whose value exists and is unique for
every record in a table is called a primary key
- each table can have one and only one primary key
- in one table, you cannot have 3 or 4 primary keys
- primary keys are the unique identifiers of a table
- cannot contain null values!
- not all tables you work with will have a primary key
Relational Schemas: Primary Key
relational schema
Relational Schemas: Primary Key
database schema
Relational Schemas: Primary Key
database schema
(relational schemas)
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
ERROR
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
Relational Schemas: Foreign Key
foreign key
identifies the relationships between tables, not the tables themselves
Relational Schemas: Foreign Key
Relational Schemas: Unique Key & Null Values
Relational Schemas: Unique Key & Null Values
Relational Schemas: Unique Key & Null Values
?
Relational Schemas: Unique Key & Null Values
Relational Schemas: Unique Key & Null Values
?
Relational Schemas: Unique Key & Null Values
unique key
used whenever you would like to specify that you don’t want to see
duplicate data in a given field
Relational Schemas: Unique Key & Null Values
ERROR
Relational Schemas: Unique Key & Null Values
NUMBER OF KEYS 1 0, 1, 2…
Relational Schemas: Unique Key & Null Values
NUMBER OF KEYS 1 0, 1, 2…
APPLICATION TO
yes yes
MULTIPLE COLUMNS
Relational Schemas: Unique Key & Null Values
Relational Schemas: Unique Key & Null Values
Relational Schemas: Unique Key & Null Values
Relationships
Relationships
Relationships
Relationships
Relationships
relationships tell you how much of the data from a foreign key field can
be seen in the primary key column of the table the data is related to
and vice versa
Relationships
Relationships
Relationships
unique values
Relationships
unique values
Relationships
unique values
repeated values
Relationships
unique values
repeated values
1 customer
Relationships
1 purchase 1 customer
Relationships
1 purchase 1 customer
or >1 purchase
Relationships
1 purchase 1 customer
or >1 purchase
Relationships
1 purchase 1 customer
or >1 purchase
1 purchase 1 customer
or >1 purchase
1
minimum # of instances of the
“Customers” table that can be
associated with the “Sales” entity
Relationships
1 purchase 1 customer
or >1 purchase
Relationships
1 purchase 1 customer
or >1 purchase
1 purchase 1 customer
or >1 purchase
1 purchase
Relationships
1 purchase 1 customer
Relationships
cardinality constraints
Relationships
M N
cardinality constraints
Relationships
Relationships
relationships tell you how much of the data from a foreign key field can
be seen in the primary key column of the table the data is related to
and vice versa
Relationships
Relationships
relationships tell you how much of the data from a foreign key field can
be seen in the primary key column of the table the data is related to
and vice versa
types of relationships
Relationships
Relationships
relationships tell you how much of the data from a foreign key field can
be seen in the primary key column of the table the data is related to
and vice versa
types of relationships
- one-to-many (many-to-one)
Relationships
Relationships
relationships tell you how much of the data from a foreign key field can
be seen in the primary key column of the table the data is related to
and vice versa
types of relationships
- one-to-many (many-to-one)
- one-to-one
Relationships
Relationships
relationships tell you how much of the data from a foreign key field can
be seen in the primary key column of the table the data is related to
and vice versa
types of relationships
- one-to-many (many-to-one)
- one-to-one
- many-to-many
Relationships
Relational schemas
Relationships
Relational schemas
- represent the concept database administrators must implement
Relationships
Relational schemas
- represent the concept database administrators must implement
- depict how a database is organized
Relationships
Relational schemas
- represent the concept database administrators must implement
- depict how a database is organized
= blueprints, or a plan for a database
Relationships
Relational schemas
- represent the concept database administrators must implement
- depict how a database is organized
= blueprints, or a plan for a database
write code
(SQL query)
Client Program:
MySQL Server MySQL Workbench
2 provided by
obtain results
The Client-Server Model
provided by
The Client-Server Model
You could also wonder why we would need a server. Sticking to the basic
theory of operation of computer networks, MySQL Workbench acts as a
client program – a client of a MySQL Server.
Client Program:
MySQL Server MySQL Workbench
provided by
The Client-Server Model
The server is, practically, nothing more than a machine that provides
data and services to the same or other computers.
MySQL Server
Client Program:
MySQL Server MySQL Workbench
provided by
Briefly, the server will perform all calculations and operations you execute in
Workbench. You will be writing queries through the Workbench interface, in the
form of raw code, which MySQL server understands and processes.
The Client-Server Model
write code
(SQL query)
Client Program:
MySQL Server MySQL Workbench
provided by
Briefly, the server will perform all calculations and operations you execute in
Workbench. You will be writing queries through the Workbench interface, in the
form of raw code, which MySQL server understands and processes.
The Client-Server Model
Client Program:
MySQL Server MySQL Workbench
2 provided by
The Client-Server Model
Client Program:
MySQL Server MySQL Workbench
2 provided by
obtain results
The Client-Server Model
write code
(SQL query)
2
obtain results
The Client-Server Model
write code
(SQL query)
Client Program:
MySQL Server MySQL Workbench
2 provided by
obtain results
First Steps in SQL
Creating a Database – Part I
Creating a Database – Part I
So far:
Creating a Database – Part I
So far:
So far:
So far:
Sales
Creating a Database – Part I
Sales
Creating a Database – Part I
CREATE DATABASE
Creating a Database – Part I
CREATE DATABASE
creates a database as an abstract unit
Creating a Database – Part I
Creating a Database – Part I
database_name
Creating a Database – Part I
database_name
give a name that is short but at the same time as related to the content
of the data as possible
Creating a Database – Part I
Sales
Creating a Database – Part I
database_name
give a name that is short but at the same time as related to the content
of the data as possible
Creating a Database – Part I
database_name
give a name that is short but at the same time as related to the content
of the data as possible
- the SQL code is not case sensitive
Creating a Database – Part I
database_name
give a name that is short but at the same time as related to the content
of the data as possible
- the SQL code is not case sensitive
- in this element the quotes are optional
Creating a Database – Part I
Surname of a
person:
string ‘James’
Introduction to Data Types
Surname of a
person:
string ‘James’
length
Surname of a
person: length
‘Jackson’ 7 symbols
length
size
- measured in bytes
- 1 byte ~ 1 symbol
Introduction to Data Types
Surname of a
person: length size
storage
the physical space in the computer drive’s memory, where the data is
being saved or stored
String Data Types
String Data Types
Example
string data type Storage
Example
string data type Storage length size
(symbols) (bytes)
CHAR(5)
5 represents the maximum number of symbols you are
allowed to use in writing a value in this format
String Data Types
Example
string data type Storage length size
(symbols) (bytes)
Maximum size
string data type
(bytes)
Maximum size
string data type
(bytes)
company CHAR(3)
Password:
the symbols cannot be more than password VARCHAR(10)
10 characters
String Data Types
integers
whole numbers with no decimal point
INTEGER INT
Integers
-128 127
TINYINT 1
0 255
-32,768 32,767
SMALLINT 2
0 65,535
-8,388,608 8,388,607
MEDIUMINT 3
0 16,777,215
-2,147,483,648 2,147,483,647
INT 4
0 4,294,967,295
-9,223,372,036,854,775,808 9,223,372,036,854,775,807
BIGINT 8
0 18,446,744,073,709,551,615
Integers
signed ≠ unsigned
-128 127
TINYINT 1
0 255
-32,768 32,767
SMALLINT 2
0 65,535
-8,388,608 8,388,607
MEDIUMINT 3
0 16,777,215
-2,147,483,648 2,147,483,647
INT 4
0 4,294,967,295
-9,223,372,036,854,775,808 9,223,372,036,854,775,807
BIGINT 8
0 18,446,744,073,709,551,615
Integers
signed ≠ unsigned
-128 127
TINYINT 1
0 255
-32,768 32,767
SMALLINT 2
0 65,535
-8,388,608 8,388,607
MEDIUMINT 3
0 16,777,215
-2,147,483,648 2,147,483,647
INT 4
0 4,294,967,295
-9,223,372,036,854,775,808 9,223,372,036,854,775,807
BIGINT 8
0 18,446,744,073,709,551,615
Integers
-128 127
TINYINT 1
0 256
-32,768 32,767
SMALLINT 2
0 65,535
-8,388,608 8,388,607
MEDIUMINT 3
0 16,777,215
-2,147,483,648 2,147,483,647
INT 4
0 4,294,967,295
-9,223,372,036,854,775,808 9,223,372,036,854,775,807
BIGINT 8
0 18,446,744,073,709,551,615
Integers
-128 127
TINYINT 1
0 256
-32,768 32,767
SMALLINT 2
0 65,535
-8,388,608 8,388,607
MEDIUMINT 3
0 16,777,215
-2,147,483,648 2,147,483,647
INT 4
0 4,294,967,295
-9,223,372,036,854,775,808 9,223,372,036,854,775,807
BIGINT 8
0 18,446,744,073,709,551,615
Integers
e.g. if you are sure that, in a certain column, you won’t need an
job perfectly and you would not need more storage space per data point
Integers
e.g. if you are sure that, in a certain column, you won’t need an
job perfectly and you would not need more storage space per data point
number: precision
10.523 5
precision
10.523 5 3
scale
10.523 5 3
36.875 5 3
e.g. DECIMAL ( 5 , 3 )
Fixed- and Floating-Point Data Types
text ≠ numbers
DECIMAL ( 5 , 3 ) 10.523
10.5 10.500
10.5236789 10.524
Fixed- and Floating-Point Data Types
DECIMAL ( 7 )
1234567
DECIMAL ( 7, 0 )
Fixed- and Floating-Point Data Types
DECIMAL = NUMERIC
Fixed- and Floating-Point Data Types
text ≠ numbers
INTEGER DECIMAL
NUMERIC
Fixed- and Floating-Point Data Types
DECIMAL = NUMERIC
e.g. salaries
NUMERIC ( p , s )
precision: p = 7
scale: s = 2
FLOAT ( 5 , 3 ) 10.523
10.5236789 10.524
the main difference between the fixed- and the floating-point type
is in the way the value is represented in the memory of the computer
DECIMAL ( 5 , 3 )
10.5236789 10.524
FLOAT ( 5 , 3 )
10.5236789 10.524
Fixed- and Floating-Point Data Types
text ≠ numbers
NUMERIC DOUBLE
Fixed- and Floating-Point Data Types
FLOAT 4 single 23
DOUBLE 8 double 53
Other Useful Data Types
Other Useful Data Types
DATE
DATE DATETIME
0 – 23:59:59.999999
DATETIME
represents the date shown on the calendar and the time shown on the
clock
vs.
TIMESTAMP
used for a well-defined, exact point in time
Other Useful Data Types
TIMESTAMP
TIMESTAMP
- representing a moment in time as a number allows you to easily
obtain the difference between two TIMESTAMP values
5,400 TIMESTAMP
Other Useful Data Types
CHAR INTEGER
VARCHAR DECIMAL
DATE NUMERIC
DATETIME FLOAT
TIMESTAMP DOUBLE
BLOB
*.jpg
Other Useful Data Types
CHAR INTEGER
VARCHAR DECIMAL
DATE NUMERIC
DATETIME FLOAT
TIMESTAMP DOUBLE
Creating a Table
Creating a Table
Sales
Creating a Table
AUTO_INCREMENT
frees you from having to insert all purchase numbers manually through
the INSERT command at a later stage
Creating a Table
AUTO_INCREMENT
frees you from having to insert all purchase numbers manually through
the INSERT command at a later stage
AUTO_INCREMENT
sales
purchase_number
1
2
3
4
…
n
Creating a Table
AUTO_INCREMENT
sales
purchase_number
1
2
3
4
…
n
Creating a Table
Sales
Using Databases and Tables
Using Databases and Tables
queries
one of their main features is to manipulate data within a database
Using Databases and Tables
Sales
Using Databases and Tables
queries
one of their main features is to manipulate data within a database
e.g.
SQL Objects:
- SQL table
Using Databases and Tables
SQL Objects:
- SQL table
- views
- stored procedures
- functions
Using Databases and Tables
USE sales;
SELECT * FROM customers;
Using Databases and Tables
database_object . sql_object
Using Databases and Tables
database_object . sql_object
. – “dot operator”
signals the existence of a connection between the two object types
Using Databases and Tables
database_object . sql_object
. – “dot operator”
signals the existence of a connection between the two object types
Additional Notes on Using Tables
Additional Notes on Using Tables
query
query
So far:
PRIMARY KEY Constraint
So far:
So far:
So far:
constraints
constraints
constraints
Sales
FOREIGN KEY Constraint
FOREIGN KEY Constraint
Sales
FOREIGN KEY Constraint
Sales
FOREIGN KEY Constraint
foreign key
points to a column of another table and, thus, links the two tables
FOREIGN KEY Constraint
FOREIGN KEY Constraint
child table
FOREIGN KEY Constraint
parent table
child table
FOREIGN KEY Constraint
parent table
child table
FOREIGN KEY Constraint
Sales
FOREIGN KEY Constraint
Sales
FOREIGN KEY Constraint
Sales
FOREIGN KEY Constraint
Sales
FOREIGN KEY Constraint
Sales
Sales
the foreign key maintains the referential integrity within the database
FOREIGN KEY Constraint
ON DELETE CASCADE
if a specific value from the parent table’s primary key has been
deleted, all the records from the child table referring to this value
will be removed as well
FOREIGN KEY Constraint
FOREIGN KEY Constraint
FOREIGN KEY Constraint
FOREIGN KEY Constraint
ON DELETE CASCADE
UNIQUE Constraint
UNIQUE Constraint
unique key
used whenever you would like to specify that you don’t want to see
duplicate data in a given field
UNIQUE Constraint
unique key
used whenever you would like to specify that you don’t want to see
duplicate data in a given field
ERROR
UNIQUE Constraint
Sales
UNIQUE Constraint
UNIQUE Constraint
Indexes
UNIQUE Constraint
Indexes
unique keys in MySQL have the same role as indexes
UNIQUE Constraint
Indexes
unique keys in MySQL have the same role as indexes
Indexes
unique keys in MySQL have the same role as indexes
index of a table
Indexes
unique keys in MySQL have the same role as indexes
index of a table
- it takes more time to update a table because indexes must be updated, too, and
that’s time consuming
UNIQUE Constraint
UNIQUE Constraint
DEFAULT Constraint
DEFAULT Constraint
DEFAULT Constraint
helps us assign a particular default value to every row of a column
DEFAULT Constraint
DEFAULT Constraint
helps us assign a particular default value to every row of a column
- a value different from the default can be stored in a field where the
DEFAULT constraint has been applied, only if specifically indicated.
DEFAULT Constraint
DEFAULT Constraint
2
DEFAULT Constraint
2 Peter Figaro M
Data Definition Language
the “not null” restriction is applied through the NOT NULL Constraint
NOT NULL Constraint
the “not null” restriction is applied through the NOT NULL Constraint
- when you insert values in the table, you cannot leave the respective
field empty
NOT NULL Constraint
the “not null” restriction is applied through the NOT NULL Constraint
- when you insert values in the table, you cannot leave the respective
field empty
- if you leave it empty, MySQL will signal an error
ERROR
NOT NULL Constraint
NOT NULL Constraint
NOT NULL
NOT NULL Constraint
0 NONE NULL
NULL
the value
of 0,
NOT NULL
NOT NULL Constraint
NULL
NOT NULL Constraint
NULL
NOT NULL Constraint
NULL
Next:
Coding Techniques and Best Practices
Data Manipulation
SQL Best Practices
Coding Techniques and Best Practices
Coding Techniques and Best Practices
clean code
good practice implies you will choose the version that will be easiest
to read and understand
Coding Techniques and Best Practices
good practice implies you will choose the version that will be easiest
to read and understand
assumption:
at your workplace, you will always type code cleanly –
as simple as possible, perfectly organized, maintaining a steady logical flow
Coding Techniques and Best Practices
when assigning names to variables or SQL objects,
Coding Techniques and Best Practices
when assigning names to variables or SQL objects,
always chose shorter, meaningful names, conveying specific information
Coding Techniques and Best Practices
when assigning names to variables or SQL objects,
always chose shorter, meaningful names, conveying specific information
customer_purchase_unique_number
Coding Techniques and Best Practices
when assigning names to variables or SQL objects,
always chose shorter, meaningful names, conveying specific information
customer_purchase_unique_number
Coding Techniques and Best Practices
when assigning names to variables or SQL objects,
always chose shorter, meaningful names, conveying specific information
customer_purchase_unique_number
Coding Techniques and Best Practices
when assigning names to variables or SQL objects,
always chose shorter, meaningful names, conveying specific information
purchase_number
Coding Techniques and Best Practices
purchase_number
PurchaseNumber
Coding Techniques and Best Practices
purchase_number
PurchaseNumber
purchase number
Coding Techniques and Best Practices
purchase_number
PurchaseNumber
purchase number
ERROR
Coding Techniques and Best Practices
readability
Coding Techniques and Best Practices
readability
readability
- time is a factor
Coding Techniques and Best Practices
- time is a factor
- time is a factor
comments
lines of text that Workbench will not run as code; they convey a
message to someone who reads our code
Coding Techniques and Best Practices
comments
lines of text that Workbench will not run as code; they convey a
message to someone who reads our code
comments
lines of text that Workbench will not run as code; they convey a
message to someone who reads our code
So far:
SELECT… FROM…
So far:
= equal operator
AND
= equal operator
in SQL, there are many other linking keywords and symbols, called
operators, that you can use with the WHERE clause
AND
= equal operator
in SQL, there are many other linking keywords and symbols, called
operators, that you can use with the WHERE clause
- AND
- OR
- IN - NOT IN
- LIKE - NOT LIKE
- BETWEEN… AND…
AND
= equal operator
in SQL, there are many other linking keywords and symbols, called
operators, that you can use with the WHERE clause
AND
AND
AND
allows you to logically combine two statements in the condition code
block
AND
AND
allows you to logically combine two statements in the condition code
block
AND
allows you to logically combine two statements in the condition code
block
- allows us to narrow the output we would like to extract from our data
OR
OR
AND
AND binds SQL to meet both conditions enlisted in the WHERE clause
simultaneously
AND
conditions set on different columns
OR
AND
conditions set on different columns
OR
conditions set on the same column
Operator Precedence
Operator Precedence
So far:
Operator Precedence
So far:
- AND
- OR
Operator Precedence
- AND
- OR
Operator Precedence
AND > OR
Operator Precedence
AND > OR
regardless of the order in which you use these operators, SQL will
always start by reading the conditions around the AND operator
Wildcard Characters
Wildcard Characters
wildcard characters
% _ *
BETWEEN… AND…
helps us designate the interval to which a given value belongs
BETWEEN… AND…
SELECT
*
FROM
employees
WHERE
hire_date BETWEEN '1990-01-01' AND '2000-01-01';
BETWEEN… AND…
SELECT
*
FROM
employees
WHERE
hire_date BETWEEN '1990-01-01' AND '2000-01-01';
SELECT
*
FROM
employees
WHERE
hire_date NOT BETWEEN '1990-01-01' AND '2000-01-01';
BETWEEN… AND…
SELECT
*
FROM
employees
WHERE
hire_date NOT BETWEEN '1990-01-01' AND '2000-01-01';
SELECT
*
FROM
employees
WHERE
hire_date NOT BETWEEN '1990-01-01' AND '2000-01-01';
SELECT
*
FROM
employees
WHERE
hire_date NOT BETWEEN '1990-01-01' AND '2000-01-01';
BETWEEN… AND…
BETWEEN… AND…
BETWEEN… AND…
BETWEEN… AND…
IS NOT NULL
IS NOT NULL / IS NULL
IS NOT NULL
used to extract values that are not null
IS NOT NULL / IS NULL
IS NOT NULL
used to extract values that are not null
IS NULL
used to extract values that are null
So far:
Other Comparison Operators
So far:
- BETWEEN… AND…
- LIKE - NOT LIKE
- IS NOT NULL - IS NULL
Other Comparison Operators
- BETWEEN… AND…
- LIKE - NOT LIKE
- IS NOT NULL - IS NULL
Other Comparison Operators
SQL
= equal to
different from
SELECT DISTINCT
SELECT DISTINCT
SELECT DISTINCT
selects all distinct, different data values
SELECT DISTINCT
SELECT DISTINCT
selects all distinct, different data values
aggregate functions
COUNT()
Introduction to Aggregate Functions
COUNT()
counts the number of non-null records in a field
Introduction to Aggregate Functions
COUNT()
counts the number of non-null records in a field
SUM()
Introduction to Aggregate Functions
COUNT()
counts the number of non-null records in a field
SUM()
sums all the non-null values in a column
Introduction to Aggregate Functions
COUNT()
counts the number of non-null records in a field
SUM()
sums all the non-null values in a column
MIN()
Introduction to Aggregate Functions
COUNT()
counts the number of non-null records in a field
SUM()
sums all the non-null values in a column
MIN()
returns the minimum value from the entire list
Introduction to Aggregate Functions
COUNT()
counts the number of non-null records in a field
SUM()
sums all the non-null values in a column
MIN()
returns the minimum value from the entire list
MAX()
Introduction to Aggregate Functions
COUNT()
counts the number of non-null records in a field
SUM()
sums all the non-null values in a column
MIN()
returns the minimum value from the entire list
MAX()
returns the maximum value from the entire list
Introduction to Aggregate Functions
COUNT()
counts the number of non-null records in a field
SUM()
sums all the non-null values in a column
MIN()
returns the minimum value from the entire list
MAX()
returns the maximum value from the entire list
AVG()
Introduction to Aggregate Functions
COUNT()
counts the number of non-null records in a field
SUM()
sums all the non-null values in a column
MIN()
returns the minimum value from the entire list
MAX()
returns the maximum value from the entire list
AVG()
calculates the average of all non-null values belonging to a certain
column of a table
Introduction to Aggregate Functions
COUNT()
counts the number of non-null records in a field
Introduction to Aggregate Functions
COUNT()
counts the number of non-null records in a field
COUNT()
SELECT COUNT(column_name)
FROM table_name;
Introduction to Aggregate Functions
COUNT()
SELECT COUNT(column_name)
FROM table_name;
the parentheses after COUNT() must start right after the keyword, not
after a whitespace
Introduction to Aggregate Functions
COUNT(DISTINCT )
aggregate functions
GROUP BY
When working in SQL, results can be grouped according to a specific
field or fields
GROUP BY
GROUP BY
When working in SQL, results can be grouped according to a specific
field or fields
GROUP BY
When working in SQL, results can be grouped according to a specific
field or fields
GROUP BY
SELECT column_name(s)
FROM table_name
WHERE conditions
GROUP BY column_name(s)
ORDER BY column_name(s);
GROUP BY
GROUP BY
in most cases, when you need an aggregate function, you must add a
GROUP BY clause in your query, too
Always include the field you have grouped your results by in the
SELECT statement!
HAVING
HAVING
HAVING
HAVING
HAVING
HAVING
refines the output from records that do not satisfy a certain condition
SELECT column_name(s)
FROM table_name
WHERE conditions
GROUP BY column_name(s)
HAVING conditions
ORDER BY column_name(s);
HAVING
SELECT column_name(s)
FROM table_name
WHERE conditions
GROUP BY column_name(s)
HAVING conditions
ORDER BY column_name(s);
WHERE
WHERE vs HAVING
WHERE
allows us to set conditions that refer to subsets of individual rows
WHERE vs HAVING
WHERE
allows us to set conditions that refer to subsets of individual rows
WHERE
WHERE vs HAVING
WHERE
re-organizing the
output into groups
(GROUP BY)
WHERE vs HAVING
WHERE
re-organizing the
output into groups
(GROUP BY)
WHERE
re-organizing the
output into groups
(GROUP BY)
HAVING
WHERE vs HAVING
WHERE
re-organizing the
output into groups
(GROUP BY)
HAVING
WHERE vs HAVING
WHERE
re-organizing the
output into groups
(GROUP BY)
HAVING
ORDER BY…
WHERE vs HAVING
HAVING
- you cannot have both an aggregated and a non-aggregated condition in
the HAVING clause
WHERE vs HAVING
SELECT column_name(s)
FROM table_name
WHERE conditions
GROUP BY column_name(s)
HAVING conditions
ORDER BY column_name(s);
LIMIT
LIMIT
SELECT column_name(s)
FROM table_name
WHERE conditions
GROUP BY column_name(s)
HAVING conditions
ORDER BY column_name(s)
LIMIT number ;
The SQL INSERT Statement
The INSERT Statement
The INSERT Statement
Sales
The INSERT Statement
COMMIT;
1
TCL’s COMMIT and ROLLBACK
COMMIT; COMMIT;
1 2
TCL’s COMMIT and ROLLBACK
COMMIT; COMMIT;
1 2 …
TCL’s COMMIT and ROLLBACK
1 2 … 10
TCL’s COMMIT and ROLLBACK
1 2 … 10 1
TCL’s COMMIT and ROLLBACK
- ROLLBACK will have an effect on the last execution you have performed
1 2 … 10 1
TCL’s COMMIT and ROLLBACK
- ROLLBACK will have an effect on the last execution you have performed
1 2 … 10 1 2
TCL’s COMMIT and ROLLBACK
- ROLLBACK will have an effect on the last execution you have performed
1 2 … 10 1 2 …
TCL’s COMMIT and ROLLBACK
- ROLLBACK will have an effect on the last execution you have performed
1 2 … 10 1 2 … 20
TCL’s COMMIT and ROLLBACK
- ROLLBACK will have an effect on the last execution you have performed
1 2 … 10 1 2 … 20
The UPDATE Statement
The UPDATE Statement
UPDATE table_name
SET column_1 = value_1, column_2 = value_2 …
WHERE conditions;
The UPDATE Statement
UPDATE table_name
SET column_1 = value_1, column_2 = value_2 …
WHERE conditions;
UPDATE table_name
SET column_1 = value_1, column_2 = value_2 …
WHERE conditions;
UPDATE table_name
SET column_1 = value_1, column_2 = value_2 …
WHERE conditions;
The UPDATE Statement
UPDATE table_name
SET column_1 = value_1, column_2 = value_2 …
WHERE conditions;
- if you don’t provide a WHERE condition, all rows of the table will be
updated
The SQL DELETE Statement
The DELETE Statement
The DELETE Statement
ON DELETE CASCADE
if a specific value from the parent table’s primary key has been
deleted, all the records from the child table referring to this value
will be removed as well
DROP vs TRUNCATE vs DELETE
DROP vs TRUNCATE vs DELETE
column_1
1
2
3
4
…
10
DROP vs TRUNCATE vs DELETE
DROP
column_1
1
2
3
4
…
10
DROP vs TRUNCATE vs DELETE
DROP
column_1
1 indexes
2
3
4
+ + constraints
…
…
10
DROP vs TRUNCATE vs DELETE
DROP
column_1
1 indexes
2
3
4
+ + constraints
…
…
10
DROP vs TRUNCATE vs DELETE
DROP
- you won’t be able to roll back to its initial state, or to the last
COMMIT statement
use DROP TABLE only when you are sure you aren’t going to use the table
in question anymore
DROP vs TRUNCATE vs DELETE
TRUNCATE
column_1
1
2
3
4
…
10
DROP vs TRUNCATE vs DELETE
column_1
1
2
3
4
…
10
DROP vs TRUNCATE vs DELETE
column_1
1
2
3
4
+
…
10
DROP vs TRUNCATE vs DELETE
column_1
1
2
3
4
+
…
10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset
column_1
1
2
3
4
…
10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset
column_1
1
2
3 TRUNCATE
4
…
10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset
column_1 column_1
1
2
3 TRUNCATE
4
…
10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset
column_1 column_1
1 11
2
3 TRUNCATE
4
…
10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset
column_1 column_1
1 11
2
3 TRUNCATE
4
…
10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset
column_1 column_1
1 11 1
2
3 TRUNCATE
4
…
10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset
column_1 column_1
1 11 1
2 12
3 TRUNCATE
4
…
10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset
column_1 column_1
1 11 1
2 12 2
3 TRUNCATE
4
…
10
DROP vs TRUNCATE vs DELETE
TRUNCATE
when truncating, auto-increment values will be reset
column_1 column_1
1 1
2 2
3 TRUNCATE 3
4 4
… …
10 10
DROP vs TRUNCATE vs DELETE
DELETE
DROP vs TRUNCATE vs DELETE
DELETE
removes records row by row
DROP vs TRUNCATE vs DELETE
DELETE
removes records row by row
DELETE
removes records row by row
column_1
1
2
3
4
…
10
DROP vs TRUNCATE vs DELETE
column_1
1
2
3 DELETE
4
…
10
DROP vs TRUNCATE vs DELETE
column_1 column_1
1
2
3 DELETE
4
…
10
DROP vs TRUNCATE vs DELETE
column_1 column_1
1 11
2 12
3 DELETE 13
4 14
… …
10 20
Next:
Next:
SQL Functions
MySQL Aggregate Functions
COUNT()
COUNT()
So far:
COUNT()
So far:
So far: Next:
So far: Next:
aggregate functions
they gather data from many rows of a table, then aggregate it into a
single value
COUNT()
aggregate functions
they gather data from many rows of a table, then aggregate it into a
single value
INPUT
COUNT()
aggregate functions
they gather data from many rows of a table, then aggregate it into a
single value
INPUT
the information contained
in multiple rows
COUNT()
aggregate functions
they gather data from many rows of a table, then aggregate it into a
single value
INPUT
the information contained
in multiple rows
COUNT()
aggregate functions
they gather data from many rows of a table, then aggregate it into a
single value
INPUT OUTPUT
the information contained
in multiple rows
COUNT()
aggregate functions
they gather data from many rows of a table, then aggregate it into a
single value
INPUT OUTPUT
the information contained the single value they
in multiple rows provide
COUNT()
COUNT()
COUNT()
COUNT()
SUM()
COUNT()
COUNT()
SUM()
MIN()
COUNT()
COUNT()
SUM()
MIN()
MAX()
COUNT()
COUNT()
SUM()
MIN()
MAX()
AVG()
COUNT()
COUNT()
SUM()
MIN()
MAX()
AVG()
COUNT()
COUNT()
SUM()
MAX()
AVG()
COUNT()
COUNT()
SUM()
MAX()
AVG()
COUNT()
COUNT()
applicable to both numeric and non-numeric data
COUNT()
COUNT(DISTINCT )
helps us find the number of times unique values are encountered in a
given column
COUNT()
Alternatively:
COUNT()
Alternatively:
COUNT(*)
COUNT()
Alternatively:
COUNT(*)
* returns the number of all rows of the table, NULL values included
COUNT()
COUNT()
COUNT()
COUNT()
the parentheses and the argument must be attached to the name of the
aggregate function
COUNT()
COUNT()
the parentheses and the argument must be attached to the name of the
aggregate function
COUNT()
the parentheses and the argument must be attached to the name of the
aggregate function
COUNT()
COUNT()
COUNT()
the parentheses and the argument must be attached to the name of the
aggregate function
COUNT() COUNT ()
COUNT()
COUNT()
the parentheses and the argument must be attached to the name of the
aggregate function
COUNT()
the parentheses and the argument must be attached to the name of the
aggregate function
COUNT(*)
SUM()
COUNT(*)
* returns all rows of the table, NULL values included
SUM()
COUNT(*)
* returns all rows of the table, NULL values included
SUM(*)
SUM()
COUNT(*)
* returns all rows of the table, NULL values included
SUM(*)
SUM()
COUNT(*)
* returns all rows of the table, NULL values included
SUM(*)
COUNT()
SUM()
SUM()
MIN()
MAX()
AVG()
SUM()
SUM()
MIN()
MAX()
AVG()
SUM()
SUM()
MIN()
- work only with numeric data
MAX()
AVG()
MIN() and MAX()
MIN() and MAX()
MAX()
returns the maximum value of a column
MIN() and MAX()
MAX()
returns the maximum value of a column
MIN()
returns the minimum value of a column
AVG()
AVG()
AVG()
extracts the average value of all non-null values in a field
AVG()
COUNT()
SUM()
MIN()
MAX()
AVG()
AVG()
COUNT()
MIN()
MAX()
AVG()
AVG()
COUNT()
MIN()
MAX()
AVG()
AVG()
COUNT()
MIN()
MAX()
AVG()
AVG()
COUNT()
MIN()
frequently used together with a GROUP BY clause
MAX()
AVG()
ROUND()
ROUND()
ROUND(#,decimal_places)
ROUND()
ROUND(#,decimal_places)
numeric, or math, function you can use
ROUND()
ROUND(#,decimal_places)
numeric, or math, function you can use
IF NULL() and COALESCE() are among the advanced SQL functions in the
toolkit of SQL professionals. They are used when null values are
dispersed in your data table and you would like to substitute the
null values with another value.
So, let’s adjust the “Departments” duplicate in a way that suits the
purposes of the next video, in which we will work with IF NULL() and
COALESCE().
First, let’s look at our table and see what we have there.
COALESCE() - Preamble
Nine departments, with their department numbers and names provided. Ok!
COALESCE() - Preamble
Now, with the ALTER TABLE statement and the CHANGE COLUMN command,
we will modify this constraint and allow null values to be
registered in the “department name” column.
Right after that, we will insert into the department number column
of this table a couple of data values – D-10 and D-11, the numbers
of the next two potential departments in the “Departments Duplicate”
table.
By running this SELECT query over here, you can see whether this
operation was carried out successfully.
SELECT
*
FROM
departments_dup
ORDER BY dept_no ASC;
COALESCE() - Preamble
We have the two new department numbers listed below, and in the
“Department name” column we can see two null values. The latter
happened because we allowed for null values to exist in this field,
“Department name”. Thus, Workbench will indicate that a value in a
cell is missing by attaching a “null” label to it. Great!
COALESCE() - Preamble
SELECT
*
FROM
departments_dup
ORDER BY dept_no ASC;
COALESCE() - Preamble
COMMIT;
Here we’ll end the setup for the video about IF NULL() and
COALESCE().
Good luck!
IFNULL() and COALESCE()
IFNULL() and COALESCE()
IFNULL(expression_1, expression_2)
IFNULL() and COALESCE()
IFNULL(expression_1, expression_2)
returns the first of the two indicated values if the data value found in
the table is not null, and returns the second value if there is a null
value
IFNULL() and COALESCE()
IFNULL(expression_1, expression_2)
returns the first of the two indicated values if the data value found in
the table is not null, and returns the second value if there is a null
value
- prints the returned value in the column of the output
IFNULL() and COALESCE()
joins
the SQL tool that allow us to construct a relationship between objects
Introduction to Joins
Introduction to Joins
Introduction to Joins
a join shows a result set, containing fields derived from two or more tables
Introduction to Joins
joins
- we must find a related column from the two tables that contains the
same type of data
Introduction to Joins
Introduction to Joins
joins
- we must find a related column from the two tables that contains the
same type of data
- we will be free to add columns from these two tables to our output
Introduction to Joins
Introduction to Joins
joins
- the columns you use to relate tables must represent the same object,
such as id
- the tables you are considering need not be logically adjacent
Introduction to Joins
Introduction to Joins
- ‘departments_dup’
- ‘dept_manager_dup’
Next:
INNER JOIN
INNER JOIN
INNER JOIN
INNER JOIN
INNER JOIN
INNER JOIN
INNER JOIN
INNER JOIN
Venn diagram
INNER JOIN
INNER JOIN
Venn diagram
a mathematical tool representing all possible logical relations
between a finite collection of sets
INNER JOIN
Venn diagram
a mathematical tool representing all possible logical relations
between a finite collection of sets
INNER JOIN
Venn diagram
a mathematical tool representing all possible logical relations
between a finite collection of sets
INNER JOIN
the area that belongs to both circles, which is filled with red, represents all
records belonging to both the “Department Manager Duplicate” and the
“Departments Duplicate” tables
INNER JOIN
result set
the area that belongs to both circles, which is filled with red, represents all
records belonging to both the “Department Manager Duplicate” and the
“Departments Duplicate” tables
INNER JOIN
INNER JOIN
can help us extract this result set
result set
INNER JOIN
INNER JOIN
can help us extract this result set
result set
INNER JOIN
dept_no CHAR(4)
INNER JOIN
dept_no CHAR(4)
INNER JOIN
SELECT
table_1.column_name(s), table_2.column_name(s)
FROM
table_1
JOIN
table_2 ON table_1.column_name = table_2.column_name;
INNER JOIN
INNER JOIN
SELECT
t1.column_name, t1.column_name, …, t2.column_name, …
FROM
table_1 t1
JOIN
table_2 t2 ON t1.column_name = t2.column_name;
INNER JOIN
INNER JOIN
M D
INNER JOIN
inner joins extract only records in which the values in the related
columns match. Null values, or values appearing in just one of the two
tables and not appearing in the other, are not displayed
- only non-null matching values are in play
INNER JOIN
Simply, the result set will be empty. There will be no link between the
two tables.
Duplicate Records
Duplicate Records
LEFT JOIN
LEFT JOIN
LEFT JOIN
LEFT JOIN
LEFT JOIN
LEFT JOIN
2 1
LEFT JOIN
when working with left joins, the order in which you join tables matters
LEFT JOIN
LEFT JOIN
LEFT JOIN
LEFT JOIN
INNER join
LEFT JOIN
INNER join
the result set is in the inner part of the Venn diagram
LEFT JOIN
LEFT join
LEFT JOIN
LEFT join
in the output obtained you have data from the
outer part of the Venn diagram too
LEFT JOIN
SELECT
t1.column_name, t1.column_name, …, t2.column_name, …
FROM
table_1 t1
JOIN
table_2 t2 ON t1.column_name = t2.column_name
WHERE
column_name … IS NULL;
RIGHT JOIN
RIGHT JOIN
RIGHT JOIN
RIGHT JOIN
RIGHT JOIN
Yes, we will!
RIGHT JOIN
RIGHT JOIN
RIGHT JOIN
dept_no CHAR(4)
RIGHT JOIN
dept_no CHAR(4)
dept_no CHAR(4)
RIGHT JOIN
RIGHT JOIN
RIGHT JOIN
when applying a RIGHT JOIN, all the records from the right table will be
included in the result set
RIGHT JOIN
RIGHT JOIN
when applying a RIGHT JOIN, all the records from the right table will be
included in the result set
RIGHT JOIN
RIGHT JOIN
when applying a RIGHT JOIN, all the records from the right table will be
included in the result set
values from the left table will be included only if their linking column
contains a value coinciding, or matching, with a value from the
linking column of the right table
RIGHT JOIN
RIGHT JOIN
when applying a RIGHT JOIN, all the records from the right table will be
included in the result set
values from the left table will be included only if their linking column
contains a value coinciding, or matching, with a value from the
linking column of the right table
RIGHT JOIN
LEFT and RIGHT joins are perfect examples of one-to-many relationships
RIGHT JOIN
LEFT and RIGHT joins are perfect examples of one-to-many relationships
RIGHT JOIN
LEFT and RIGHT joins are perfect examples of one-to-many relationships
1 manager 1 department
or >1 managers
RIGHT JOIN
LEFT and RIGHT joins are perfect examples of one-to-many relationships
RIGHT JOIN
LEFT and RIGHT joins are perfect examples of one-to-many relationships
1 manager 1 department
The New and the Old Join Syntax
The New and the Old Join Syntax
Next:
- Relational Database Essentials
- MySQL Constraints
- SELECT, INSERT, UPDATE, DELETE
- MySQL Aggregate Functions
- INNER JOIN, LEFT JOIN, RIGHT JOIN
The New and the Old Join Syntax
Next:
- Relational Database Essentials - Subqueries, Self-joins
- MySQL Constraints - Stored Routines
- SELECT, INSERT, UPDATE, DELETE - Advanced SQL Tools
- MySQL Aggregate Functions
- INNER JOIN, LEFT JOIN, RIGHT JOIN
The New and the Old Join Syntax
The New and the Old Join Syntax
The New and the Old Join Syntax
The New and the Old Join Syntax
The New and the Old Join Syntax
The New and the Old Join Syntax
connection points
The New and the Old Join Syntax
connection points
The New and the Old Join Syntax
connection points
The New and the Old Join Syntax
connection points
The New and the Old Join Syntax
connection points
The New and the Old Join Syntax
connection points
The New and the Old Join Syntax
SELECT
t1.column_name, t1.column_name, …, t2.column_name, …
FROM
table_1 t1,
table_2 t2
WHERE
t1.column_name = t2.column_name;
The New and the Old Join Syntax
JOIN or WHERE?
The New and the Old Join Syntax
JOIN or WHERE?
JOIN or WHERE?
JOIN or WHERE?
JOIN or WHERE?
- the JOIN syntax allows you to modify the connection between tables
easily
The New and the Old Join Syntax
JOIN (the New Join Syntax) vs WHERE (the Old Join Syntax)
JOIN and WHERE Used Together
JOIN and WHERE Used Together
JOIN + WHERE
JOIN and WHERE Used Together
JOIN + WHERE
JOIN:
- used for connecting the “employees” and “salaries” tables
JOIN and WHERE Used Together
JOIN + WHERE
JOIN:
- used for connecting the “employees” and “salaries” tables
WHERE:
- used to define the condition or conditions that will determine which will be
the connecting points between the two tables
JOIN and WHERE Used Together
FRAGILE
HANDLE WITH CARE
JOIN More Than Two Tables in SQL
JOIN More Than Two Tables in SQL
when creating a query that joins multiple tables, you must back it with
strong intuition and a crystal-clear idea of how you would like the
tables to be connected
JOIN More Than Two Tables in SQL
first_name last_name
JOIN More Than Two Tables in SQL
dept_name
Tips and Tricks for Joins
dept_name average_salary
Tips and Tricks for Joins
dept_name average_salary
Tips and Tricks for Joins
dept_name average_salary
Tips and Tricks for Joins
JOINs
Tips and Tricks for Joins
JOINs
- one should look for key columns, which are common between the tables
involved in the analysis and are necessary to solve the task at hand
Tips and Tricks for Joins
JOINs
- one should look for key columns, which are common between the tables
involved in the analysis and are necessary to solve the task at hand
- these columns do not need to be foreign or private keys
Tips and Tricks for Joins
dept_name average_salary
UNION vs UNION ALL
UNION vs UNION ALL
UNION ALL
UNION vs UNION ALL
UNION ALL
used to combine a few SELECT statements in a single output
UNION vs UNION ALL
UNION ALL
used to combine a few SELECT statements in a single output
UNION ALL
used to combine a few SELECT statements in a single output
SELECT
N columns
FROM
table_1
UNION ALL SELECT
N columns
FROM
table_2;
UNION vs UNION ALL
UNION ALL
used to combine a few SELECT statements in a single output
UNION ALL
used to combine a few SELECT statements in a single output
UNION ALL
used to combine a few SELECT statements in a single output
UNION ALL
used to combine a few SELECT statements in a single output
_dup
UNION vs UNION ALL
UNION
SELECT
N columns
FROM
table_1
UNION SELECT
N columns
FROM
table_2;
UNION vs UNION ALL
- use UNION
UNION vs UNION ALL
- use UNION
- use UNION
subqueries
queries embedded in a query
SQL Subqueries with IN Nested Inside WHERE
subqueries
SQL Subqueries with IN Nested Inside WHERE
subqueries
= outer select
SQL Subqueries with IN Nested Inside WHERE
_dup
SQL Subqueries with IN Nested Inside WHERE
subqueries
SQL Subqueries with IN Nested Inside WHERE
subqueries
- a subquery should always be placed within parentheses
SQL Subqueries with IN Nested Inside WHERE
1) the SQL engine starts by running the inner query
SQL Subqueries with IN Nested Inside WHERE
1) the SQL engine starts by running the inner query
_dup
SQL Subqueries with IN Nested Inside WHERE
- you can have a lot more than one subquery in your outer query
SQL Subqueries with IN Nested Inside WHERE
- you can have a lot more than one subquery in your outer query
- it is possible to nest inner queries within other inner queries
SQL Subqueries with IN Nested Inside WHERE
- you can have a lot more than one subquery in your outer query
- it is possible to nest inner queries within other inner queries
in that case, the SQL engine would execute the innermost query
first
SQL Subqueries with IN Nested Inside WHERE
- you can have a lot more than one subquery in your outer query
- it is possible to nest inner queries within other inner queries
in that case, the SQL engine would execute the innermost query
first, and then each subsequent query
SQL Subqueries with IN Nested Inside WHERE
- you can have a lot more than one subquery in your outer query
- it is possible to nest inner queries within other inner queries
in that case, the SQL engine would execute the innermost query
first, and then each subsequent query, until it runs the outermost
query last
SQL Subqueries with EXISTS-NOT EXISTS
Nested Inside WHERE
SQL Subqueries with EXISTS-NOT EXISTS Nested
Inside WHERE
EXISTS
SQL Subqueries with EXISTS-NOT EXISTS Nested
Inside WHERE
EXISTS
EXISTS
SQL Subqueries with EXISTS-NOT EXISTS Nested
Inside WHERE
EXISTS IN
SQL Subqueries with EXISTS-NOT EXISTS Nested
Inside WHERE
EXISTS IN
EXISTS IN
EXISTS IN
quicker in retrieving
large amounts of data
SQL Subqueries with EXISTS-NOT EXISTS Nested
Inside WHERE
EXISTS IN
this is true particularly for inner queries using the WHERE clause
SQL Subqueries with EXISTS-NOT EXISTS Nested
Inside WHERE
subqueries:
SQL Subqueries with EXISTS-NOT EXISTS Nested
Inside WHERE
subqueries:
- allow for better structuring of the outer query
SQL Subqueries with EXISTS-NOT EXISTS Nested
Inside WHERE
subqueries:
- allow for better structuring of the outer query
- thus, each inner query can be thought of in isolation
SQL Subqueries with EXISTS-NOT EXISTS Nested
Inside WHERE
subqueries:
- allow for better structuring of the outer query
- thus, each inner query can be thought of in isolation
- hence the name of SQL – Structured Query Language!
SQL Subqueries with EXISTS-NOT EXISTS Nested
Inside WHERE
subqueries:
- allow for better structuring of the outer query
- thus, each inner query can be thought of in isolation
- hence the name of SQL – Structured Query Language!
- many users prefer subqueries simply because they offer enhanced code
readability
SQL Subqueries Nested in SELECT and FROM
SQL Subqueries Nested in SELECT and FROM
In this lecture:
SQL Subqueries Nested in SELECT and FROM
In this lecture:
challenging
task
SQL Subqueries Nested in SELECT and FROM
In this lecture:
challenging
task
SQL Subqueries Nested in SELECT and FROM
In this lecture:
challenging exercise
task
SQL Self Join
SQL Self Join
SQL Self Join
self join
SQL Self Join
self join
applied when a table must join itself
SQL Self Join
self join
applied when a table must join itself
- if you’d like to combine certain rows of a table with other rows of the
same table, you need a self-join
INNER JOIN
in a self-join statement, you will have to comply with the same logical and
syntactic structure
SQL Self Join
in a self-join statement, you will have to comply with the same logical and
syntactic structure
SQL Self Join
in a self-join statement, you will have to comply with the same logical and
syntactic structure
SQL Self Join
- the 2 tables will be identical to the table you’ll be using in the self-join
SQL Self Join
- the 2 tables will be identical to the table you’ll be using in the self-join
SQL Self Join
=
- the 2 tables will be identical to the table you’ll be using in the self-join
SQL Self Join
=
- the 2 tables will be identical to the table you’ll be using in the self-join
- you can think of them as virtual projections of the underlying, base table
SQL Self Join
=
- the self-join will reference both implied tables and will treat them as two
separate tables in its operations
SQL Self Join
=
- the data used will come from a single source, which is the underlying table
that stores data physically
INNER JOIN
INNER JOIN
M D
SQL Self Join
=
- using aliases is obligatory
SQL Self Join
e1 = e2
e1 = e2
- these references to the original table let you use different blocks of the
available data
SQL Self Join
e1 = e2
- you can either filter both in the join, or you can filter one of them in the
WHERE clause, and the other one – in the join
SQL Self Join
emp_manager
e1 = e2
SQL Views
Using SQL Views
Using SQL Views
view
a virtual table whose contents are obtained from an existing table or
tables, called base tables
Using SQL Views
view
a virtual table whose contents are obtained from an existing table or
tables, called base tables
A view acts as a shortcut for writing the same SELECT statement every time a new
request has been made
Using SQL Views
SQL View
- saves a lot of coding time
Using SQL Views
SQL View
- saves a lot of coding time
- occupies no extra memory
Using SQL Views
SQL View
- acts as a dynamic table because it instantly reflects data and
structural changes in the base table
Using SQL Views
SQL View
- acts as a dynamic table because it instantly reflects data and
structural changes in the base table
‘dept_emp’ (table)
‘dept_emp’ (table)
‘dept_emp’ (table)
query output
Introduction to Stored Routines
Introduction to Stored Routines
stored routine
an SQL statement, or a set of SQL statements, that can be stored on the
database server
algorithm
1) checks all monthly sales
generated throughout a
query calendar year
2) returns the lowest of these
values
stored routine
Introduction to Stored Routines
= 100
Introduction to Stored Routines
stored routine
Introduction to Stored Routines
stored routine
Introduction to Stored Routines
stored routine
= 100 - this routine can bring the desired result multiple times
Introduction to Stored Routines
stored routines
≠ built-in functions
(aggregate functions,
datetime functions)
Introduction to Stored Routines
stored routines
DELIMITER $$
The MySQL Syntax for Stored Procedures
Query #1 ;
Query #2 ;
call stored_procedure_1 ;
p_Query #1 ;
p_Query #2 ;
stored procedure
The MySQL Syntax for Stored Procedures
Query #1 ;
Query #2 ;
call stored_procedure_1 ;
p_Query #1 ;
p_Query #2 ;
Query #4 ;
Query #5 ;
… ; stored procedure
The MySQL Syntax for Stored Procedures
Query #1 ; $$ or //
Query #2 ;
DELIMITER $$
call stored_procedure_1 ;
p_Query #1 ;
p_Query #2 ;
stored procedure
The MySQL Syntax for Stored Procedures
Query #1 ;
Query #2 ;
DELIMITER $$
call stored_procedure_1 ;
p_Query #1 ;
p_Query #2 ;
Query #4 ;
Query #5 ;
… ; stored procedure
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name(param_1, param_2)
DELIMITER $$
CREATE PROCEDURE procedure_name() A procedure can be created
without parameters too!
Nevertheless, the parentheses
must always be attached to
its name
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
body of the procedure
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
+ BEGIN
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
query
END$$
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
;
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
;
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000$$
END$$
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000$$
END$$
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
DELIMITER ;
The MySQL Syntax for Stored Procedures
DELIMITER $$
CREATE PROCEDURE procedure_name()
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
From this moment on, $$ will not act as a
DELIMITER ; delimiter
Stored Procedures with an Input Parameter
Stored Procedures with an Input Parameter
a stored routine can perform a calculation that transforms an input
value in an output value
Stored Procedures with an Input Parameter
a stored routine can perform a calculation that transforms an input
value in an output value
stored procedures can take an input value and then use it in the query,
or queries, written in the body of the procedure
Stored Procedures with an Input Parameter
a stored routine can perform a calculation that transforms an input
value in an output value
stored procedures can take an input value and then use it in the query,
or queries, written in the body of the procedure
DELIMITER $$
CREATE PROCEDURE procedure_name(in parameter)
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
DELIMITER ;
Stored Procedures with an Input Parameter
a stored routine can perform a calculation that transforms an input
value in an output value
stored procedures can take an input value and then use it in the query,
or queries, written in the body of the procedure
DELIMITER $$
CREATE PROCEDURE procedure_name(in parameter, out parameter)
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
DELIMITER ;
Stored Procedures with an Output Parameter
DELIMITER $$
CREATE PROCEDURE procedure_name(in parameter, out parameter)
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
DELIMITER ;
Stored Procedures with an Output Parameter
DELIMITER $$
CREATE PROCEDURE procedure_name(in parameter, out parameter)
- BEGIN
SELECT * FROM employees
LIMIT 1000;
END$$
DELIMITER ;
Stored Procedures with an Output Parameter
DELIMITER $$
CREATE PROCEDURE procedure_name(in parameter, out parameter)
- BEGIN
SELECT * FROM employees
it will represent the variable
LIMIT 1000; containing the output value of
the operation executed by the
END$$
query of the stored procedure
DELIMITER ;
Stored Procedures with an Output Parameter
every time you create a procedure containing both an IN and an OUT
parameter, remember that you must use the SELECT-INTO structure in the
query of this object’s body!
Variables
Variables
when you are defining a program, such as a stored procedure for
instance, you can say you are using ‘parameters’
Variables
when you are defining a program, such as a stored procedure for
instance, you can say you are using ‘parameters’
- ‘parameters’ are a more abstract term
Variables
when you are defining a program, such as a stored procedure for
instance, you can say you are using ‘parameters’
- ‘parameters’ are a more abstract term
DELIMITER $$
CREATE PROCEDURE procedure_name (in , out )
Variables
when you are defining a program, such as a stored procedure for
instance, you can say you are using ‘parameters’
- ‘parameters’ are a more abstract term
DELIMITER $$
CREATE PROCEDURE procedure_name (in parameter , out )
Variables
when you are defining a program, such as a stored procedure for
instance, you can say you are using ‘parameters’
- ‘parameters’ are a more abstract term
DELIMITER $$
CREATE PROCEDURE procedure_name (in parameter , out parameter )
Variables
once the structure has been solidified, then it will be applied to the
database. The input value you insert is typically referred to as the
‘argument’, while the obtained output value is stored in a ‘variable’
Variables
once the structure has been solidified, then it will be applied to the
database. The input value you insert is typically referred to as the
‘argument’, while the obtained output value is stored in a ‘variable’
CREATE PROCEDURE …
Variables
once the structure has been solidified, then it will be applied to the
database. The input value you insert is typically referred to as the
‘argument’, while the obtained output value is stored in a ‘variable’
input:
input:
input: output:
DELIMITER $$
CREATE PROCEDURE procedure_name (in parameter , out parameter )
≠
input: output:
CREATE PROCEDURE …
Variables
IN-OUT parameters
input:
input:
input:
input = output
DELIMITER $$
CREATE FUNCTION function_name(parameter data_type) RETURNS data_type
DECLARE variable_name data_type
- BEGIN
SELECT …
RETURN variable_name
END$$
DELIMITER ;
User-Defined Functions in MySQL
DELIMITER $$
CREATE FUNCTION function_name(parameter data_type) RETURNS data_type
DECLARE variable_name data_type
- BEGIN here you have no OUT parameters to
SELECT … define between the parentheses after
the object’s name
RETURN variable_name
END$$
DELIMITER ;
User-Defined Functions in MySQL
DELIMITER $$
CREATE FUNCTION function_name(parameter data_type) RETURNS data_type
DECLARE variable_name data_type
- BEGIN here you have no OUT parameters to
SELECT … define between the parentheses after
the object’s name
RETURN variable_name
all parameters are IN, and since this
END$$ is well known, you need not explicitly
indicate it with the word, ‘IN’
DELIMITER ;
User-Defined Functions in MySQL
DELIMITER $$
CREATE FUNCTION function_name(parameter data_type) RETURNS data_type
DECLARE variable_name data_type
- BEGIN
although there are no OUT
SELECT … parameters, there is a
RETURN variable_name ‘return value’
END$$
DELIMITER ;
User-Defined Functions in MySQL
DELIMITER $$
CREATE FUNCTION function_name(parameter data_type) RETURNS data_type
DECLARE variable_name data_type
- BEGIN
although there are no OUT
SELECT … parameters, there is a
RETURN variable_name ‘return value’
it is obtained after running the
END$$
query contained in the body of
DELIMITER ; the function
User-Defined Functions in MySQL
DELIMITER $$
CREATE FUNCTION function_name(parameter data_type) RETURNS data_type
DECLARE variable_name data_type
- BEGIN it can be of any data type
SELECT …
RETURN variable_name
END$$
DELIMITER ;
User-Defined Functions in MySQL
we cannot call a function!
User-Defined Functions in MySQL
we cannot call a function!
we can select it, indicating an input value within parentheses
User-Defined Functions in MySQL
we cannot call a function!
we can select it, indicating an input value within parentheses
SELECT function_name(input_value);
Stored Routines - Conclusion
Stored Routines - Conclusion
TECHNICAL DIFFERENCES
Stored Routines - Conclusion
TECHNICAL DIFFERENCES
stored procedure
Stored Routines - Conclusion
TECHNICAL DIFFERENCES
stored procedure
Stored Routines - Conclusion
TECHNICAL DIFFERENCES
user-defined
stored procedure function
Stored Routines - Conclusion
TECHNICAL DIFFERENCES
user-defined
stored procedure function
returns a value
Stored Routines - Conclusion
TECHNICAL DIFFERENCES
user-defined
stored procedure function
TECHNICAL DIFFERENCES
user-defined
stored procedure function
CALL procedure;
Stored Routines - Conclusion
TECHNICAL DIFFERENCES
user-defined
stored procedure function
CONCEPTUAL DIFFERENCES
Stored Routines - Conclusion
CONCEPTUAL DIFFERENCES
user-defined
stored procedure function
Stored Routines - Conclusion
CONCEPTUAL DIFFERENCES
user-defined
stored procedure function
CONCEPTUAL DIFFERENCES
user-defined
stored procedure function
CONCEPTUAL DIFFERENCES
user-defined
stored procedure function
CONCEPTUAL DIFFERENCES
user-defined
stored procedure function
- in those cases, the operation performed will apply changes to the data in your
database
Stored Routines - Conclusion
how about involving an INSERT, an UPDATE, or a DELETE statement?
- in those cases, the operation performed will apply changes to the data in your
database
- there will be no value, or values, to be returned and displayed to the user
Stored Routines - Conclusion
CONCEPTUAL DIFFERENCES
user-defined
stored procedure function
CONCEPTUAL DIFFERENCES
user-defined
stored procedure function
INSERT UPDATE
DELETE
Stored Routines - Conclusion
CONCEPTUAL DIFFERENCES
user-defined
stored procedure function
INSERT UPDATE
DELETE
Stored Routines - Conclusion
CONCEPTUAL DIFFERENCES
user-defined
stored procedure function
DELETE DELETE
Stored Routines - Conclusion
CONCEPTUAL DIFFERENCES
user-defined
stored procedure function
DELETE DELETE
Stored Routines - Conclusion
user-defined
stored procedure function
Stored Routines - Conclusion
user-defined
stored procedure function
TECHNICAL DIFFERENCE
Stored Routines - Conclusion
user-defined
stored procedure function
TECHNICAL DIFFERENCE
user-defined
stored procedure function
TECHNICAL DIFFERENCE
CONCEPTUAL DIFFERENCE
Stored Routines - Conclusion
user-defined
stored procedure function
TECHNICAL DIFFERENCE
CONCEPTUAL DIFFERENCE
- you can easily include
a function as one of the
columns inside a SELECT
statement
Stored Routines - Conclusion
user-defined
stored procedure function
TECHNICAL DIFFERENCE
CONCEPTUAL DIFFERENCE
- including a procedure - you can easily include
in a SELECT statement a function as one of the
is impossible columns inside a SELECT
statement
Stored Routines - Conclusion
once you become an advanced SQL user, and have gained a lot of practice,
you will appreciate the advantages and disadvantages of both types of
programs
Stored Routines - Conclusion
once you become an advanced SQL user, and have gained a lot of practice,
you will appreciate the advantages and disadvantages of both types of
programs
- you will encounter many cases where you should choose between procedures and
functions
Stored Routines - Conclusion
what we did in this section was to lay the foundation of the relevant
syntax, as well as performing exercises on the practical aspects of
these tools
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
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
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
session
Step 1 Step 2
session
Step 1 Step 2
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
session variable
a variable that exists only for the session in which you are operating
= 100
Session Variables
“Var”
“Var”
global variables
“Var” “Var”
“Var” “Var”
“Var” “Var”
“Var”
“Var” “Var”
Global 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_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
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
system
User-Defined vs System Variables
variables in MySQL can be characterized according to the way they have
been created
user-defined
system
user-defined
system
user-defined
system
user-defined
system
user-defined
system
user-defined
system
user-defined
system
user-defined *
system *
user-defined *
system *
user-defined *
system *
- 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