0% found this document useful (0 votes)
43 views58 pages

INFOMAN Lesson 8

The document discusses various SQL commands and functions in MySQL. It covers foreign key constraints, check constraints, indexes, operators, wildcards, aliases, aggregate functions, and subqueries. Examples are provided for many of these concepts.

Uploaded by

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

INFOMAN Lesson 8

The document discusses various SQL commands and functions in MySQL. It covers foreign key constraints, check constraints, indexes, operators, wildcards, aliases, aggregate functions, and subqueries. Examples are provided for many of these concepts.

Uploaded by

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

Lesson 8

More Commands in MySQL


FOREIGN KEY Constraint
▪ The FOREIGN KEY constraint is used to prevent
actions that would destroy links between tables.
▪ A FOREIGN KEY is a field (or collection of fields)
in one table, that refers to the PRIMARY KEY in
another table.
▪ The table with the foreign key is called the child
table, and the table with the primary key is
called the referenced or parent table.
FOREIGN KEY

PK

FK A FOREIGN KEY is a field (or collection of


PK fields) in one table, that refers to the PRIMARY
KEY in another table.

The table with the foreign key is called the child


table, and the table with the primary key is
called the referenced or parent table.
FOREIGN KEY
CHECK on CREATE TABLE
▪ The CHECK constraint is used to limit the value
range that can be placed in a column.
▪ If you define a CHECK constraint on a column it
will allow only certain values for this column.
▪ If you define a CHECK constraint on a table it can
limit the values in certain columns based on
values in other columns in the row.
CHECK on CREATE TABLE
CHECK on ALTER TABLE
DROP a CHECK Constraint
CREATE INDEX
▪ The CREATE INDEX statement is used to create indexes in
tables.
▪ Indexes are used to retrieve data from the database
more quickly than otherwise. The users cannot see the
indexes, they are just used to speed up
searches/queries.
▪ Note: Updating a table with indexes takes more time
than updating a table without (because the indexes also
need an update). So, only create indexes on columns
that will be frequently searched against.
CREATE INDEX
Duplicate values
are allowed.

Creates a unique index on a table. Duplicate values are not


allowed.
CREATE INDEX Example
DROP INDEX Example
MYSQL Operators
Arithmetic Operators
MYSQL Operators
Comparison Operators
MYSQL Operators
Logical Operators
WILDCARD Characters
▪ A wildcard character is used to substitute one or more
characters in a string.
▪ Wildcard characters are used with the LIKE operator. The
LIKE operator is used in a WHERE clause to search for a
specified pattern in a column.
WILDCARD Characters
CREATE ALIASES
▪ Aliases are used to give a table, or a column in a
table, a temporary name.
▪ Aliases are often used to make column names
more readable.
▪ An alias only exists for the duration of that
query.
▪ An alias is created with the AS keyword.
CREATE ALIASES
Alias Column Syntax
CREATE ALIASES
Alias Table Syntax
CREATE ALIASES
Aliases can be useful when:
• There are more than one table involved in a
query
• Functions are used in the query
• Column names are big or not very readable
• Two or more columns are combined together
SELECTING DATA
Function
A function is a block of code that does a certain job
for us. For an analogy, think of the mathematical
functions available in MS Excel. To add numbers,
we can use the sum() function and type
sum(A1:A5) instead of typing
A1+A2+A3+A4+A5.
SELECTING DATA
The full list of MySQL functions can be found at
https://dev.mysql.com/doc/refman/8.0/en/func-
op-summary-ref.html
MySQL Function
CONCAT()
The first is the CONCAT() function. This function
allows us to combine two or more strings into a
single string. This is known as concatenating the
strings.
MySQL Function
SUBSTRING()
This function allows us to extract a substring from a specified
string. A substring refers to a portion of a longer string.

The SUBSTRING() function requires us to pass in a string, the


starting position to extract the substring, and the desired length
of the substring (in that order). If the desired length is not
specified, the function extracts from the starting position to the
end of the string.
MySQL Function
SUBSTRING()

Output:
MySQL Function
SUBSTRING()

Output:

substring that starts from position 2, with a length of 6 characters.


MySQL Function
NOW()
This function gives us the current date and time
whenever that function is being used. It is
commonly used to record the date and time that a
particular record is inserted into a table.
MySQL Function
CURDATE()
This gives us the current date.

CURTIME()
This gives us the current time.
MySQL Aggregate Function
An aggregate function is a function that performs
calculation on a set of values and returns the result
of the calculation as a single value.
Table Name: employees
MySQL Aggregate Function
COUNT()
• Returns the number of the rows in the table.
• If we pass in * to the function, it returns the total number of rows
in the table.
• If we pass in a column name instead, it returns the number of non
NULL values in that column (NULL values are ignored).
• If we want to remove duplicates, we add the DISTINCT keyword
before the column name.
MySQL Aggregate Function
COUNT()
MySQL Aggregate Function
COUNT()
MySQL Aggregate Function
COUNT()
MySQL Aggregate Function
AVERAGE()
• Function returns the average of a set of values.
MySQL Aggregate Function
ROUND()
• Returns the number to round off and the number of
decimal places we want it rounded off to.
MySQL Aggregate Function
MAX()
• Returns the maximum of a set of values.
MySQL Aggregate Function
MIN()
• Returns the minimum of a set of values.
MySQL Aggregate Function
SUM()
• Returns the sum of a set of values.
MySQL Aggregate Function
GROUP BY()
• MySQL allows us to group data when performing
calculations.
MySQL Aggregate Function
GROUP BY()
• MySQL allows us to group data when performing
calculations.
MySQL Aggregate Function
IF()
• MySQL IF() takes three expressions and if the first
expression is true, not zero and not NULL, it returns the
second expression. Otherwise, it returns the third
expression.
MySQL Aggregate Function
IF()
• Function returns the maximum of a set of values.
MySQL Aggregate Function
IF()
MySQL Subqueries
• A subquery is a SQL query nested inside a larger
query.
MySQL Subqueries
MySQL Subqueries
MySQL Subqueries
MySQL Subqueries
End of
Lesson 8
44

You might also like