Mysql SQL Revision Tour-1
Mysql SQL Revision Tour-1
Santosh Chapagain
MYSQL
● It is a freely available open source Relational Database Management System
(RDBMS) that uses Structured QueryLanguage(SQL).
● In the MySQL database , information is stored in Tables. A single MySQL
database can contain many tables at once and store thousands of individual
records.
Prepared By: Er.Santosh Chapagain
SQL MySQL
It is used to query and operate the database. MySQL is used for data
handling, storing, deleting, and
updating the data in tabular
form.
SQL does not allow other processors or even its MySQL is less secure than
own binaries to manipulate data during execution. SQL, as it allows third-party
processors to manipulate data
files during execution.
Prepared By: Er.Santosh Chapagain
4. Alternate Key :
● A candidate key that is not primary key, is called an alternate key.
5. Foreign Key:
● A non-key attribute, whose values are derived from the primary key of
some other table, is known as foreign key in its current table.
● A foreign key can have a different name than the primary key it comes
from.
● Foreign key value can be null, even though primary key value can’t.
● Foreign keys don’t have to be unique in fact, they often aren’t.
REFERENTIAL INTEGRITY:
A referential integrity is a system of rules that a DBMS uses to ensure that relationships
between records in related tables are valid, and that users don’t accidentally delete or
change related data. This integrity is ensured by foreign keys.
CLASSIFICATION OF SQL STATEMENTS:
SQL commands can be mainly divided into following categories:
1. Data Definition Language(DDL) Commands:
Commands that allow you to perform task, related to data definition e.g;
● Creating, altering and dropping.
● Granting and revoking privileges and roles.
● Maintenance commands.
● DDL COMMANDS: CREATE, DROP, ALTER, TRUNCATE, RENAME.
MySQL ELEMENTS
1. Literals 2. Datatypes 3. Nulls 4. Comments
LITERALS
● It refers to a fixed data value. This fixed data value may be of character type or
numeric type. For example, ‘replay’ , ‘Raj’, ‘8’ , ‘306’ are all character literals.
● Numbers not enclosed in quotation marks are numeric literals. E.g. 22 , 18 , 1997
are all numeric literals.
● Numeric literals can either be integer literals i.e., without any decimal or be real
literals i.e. with a decimal point e.g. 17 is an integer literal but 17.0 and 17.5 are
real literals.
DATA TYPES
Data types are means to identify the type of data and associated operations for handling
it. MySQL data types are divided into three categories:
1. Numeric
2. Date and time
3. String types
1 It specifies the fixed length character It specifies the variable length character
string. string.
NULL VALUE
If a column in a row has no value, then the column is said to be null , or to contain a
null. You should use a null value when the actual value is not known or when a value
would not be meaningful.
Prepared By: Er.Santosh Chapagain
DATABASE COMMANDS
Prepared By: Er.Santosh Chapagain
Inserting Dates
Dates are by default entered in ‘YYYY-MM-DD’ format i.e first four digits depicting year,
followed by a hyphen,followed by 2 digits of month, followed by a hyphen and a two digit
day.All this is enclosed in single quotes.
Note: For this purpose table1 and table2 must be on the same database.
Note: you can also add specific data using where clause like:
Prepared By: Er.Santosh Chapagain
Note: Relational operator like =, <, > etc can’t be used with NULL
Prepared By: Er.Santosh Chapagain
Prepared By: Er.Santosh Chapagain
Prepared By: Er.Santosh Chapagain
Prepared By: Er.Santosh Chapagain
Prepared By: Er.Santosh Chapagain
Prepared By: Er.Santosh Chapagain
Prepared By: Er.Santosh Chapagain
UNIQUE CONSTRAINT
The UNIQUE constraint ensures that all values in a column are distinct. In other words,
no two rows can hold the same value for a column with UNIQUE constraint.
Prepared By: Er.Santosh Chapagain
Prepared By: Er.Santosh Chapagain
Prepared By: Er.Santosh Chapagain
Prepared By: Er.Santosh Chapagain
MYSQL FUNCTIONS:
Function:A function is a special type of predefined command set that performs some
operation and returns a single value.
Types:
1) Text/String Functions:
a) CHAR():
Syntax:
CHAR(value1,[value2,....])
Example:
Note:
b) LCASE()/LOWER():
Syntax:
LOWER(str) or LCASE(str)
Prepared By: Er.Santosh Chapagain
Example:
C) UCASE()/UPPER():
Syntax:
UPPER(str)
UCASE(str)
Example:
SELECT UPPER(‘hello’) o/p: HELLO
● Returns the argument str, with all letters in uppercase.
● The return value has the same data type as the argument str.
● UPPER() function returns NULL when the str parameter is NULL.
○ Example: SELECT UPPER(NULL) o/p: NULL
D) LENGTH():
● This function returns the length of a given string in bytes.
Syntax:
LENGTH(str)
Example:
SELECT LENGTH(‘hello’) o/p:5
● If argument str has datatype CHAR the lengths include all trailing blanks.
● If argument str is NULL , this function returns NULL.
E) LEFT():
Syntax:
LEFT(str,len)
Example:
SELECT LEFT(‘Hello’,1) o/p: H
F) RIGHT():
Syntax:
RIGHT(str,len)
Example:
SELECT Right(‘hello’,1) o/p: o
● If length exceeds the length of the string, the function returns the string.
● If length is zero or negative, the function returns an empty string.
● Returns Null if any argument is NULL.
G) INSTR():
Syntax:
INSTR(string1,string2)
Where,
String1:The string that will be searched.
String2: The string to search for in string1.
If string 2 is not found, this function returns zero.
Example:
SELECT INSTR(‘hello world”,”he”) o/p: 1
H) LTRIM():
● This function removes leading spaces i.e spaces from the left of the given string.
Syntax:
LTRIM(str)
Example:
SELECT LTRIM(‘ Hello ‘) o/p: Hello
I) RTRIM():
● This function removes leading spaces i.e spaces from the right of the given
string.
Syntax:
RTRIM(str)
J) Trim():
● TRIM() function returns a string with the specified leading and/or trailing
characters removed.
● It performs combined functions of LTRIM() and RTRIM().
k) MID()/SUBSTRING()/SUBSTR():
● MID() function returns a substring of a specified length starting from the
specified position.
Syntax:
MID(str, pos, len)
or
SUBSTRING(str,pos,len)
or
SUBSTR(str, pos, len)
Parameters
Str
Required. The original string from which to extract the substring.
Pos
Required. The start position of the substring. It can be positive or negative.
If it is negative, extract the substring from the end of the string.
Len
Optional. The length (number of characters) of the substring. If not
specified, extract a substring to the end of the original string.
Prepared By: Er.Santosh Chapagain
Return value
● The MID() function returns a substring of a specified length starting from
the specified position.
● If pos is equal to or exceeds the length of the original string, the function
will return an empty string ‘ ‘.
● If any parameter is NULL, the function will return NULL.
Examples:
MID('Hello', 1, 2) o/p: He
MID('Hello', 2, 2) o/p: el
MID('Hello', 1) o/p: Hello
MID('Hello', 1, 8) o/p: Hello
MID('Hello', -1, 2) o/p: o
MID('Hello', 8, 2) o/p:NULL
MID('Hello', 0, 2) o/p:NULL
MID('Hello', 0) o/p:NULL
MID(NULL, 1, 1) o/p: NULL
Prepared By: Er.Santosh Chapagain
a) MOD():
● This function returns modulus(i.e reminder) of given two numbers.
Syntax:
MOD(number1,number2) i.e: number1%number2
● Returns number1 if number2 is greater.
Examples:
MOD(100, 7): 2
MOD(100, 10): 0
MOD(10,100):10
MOD(0, 1): 0
MOD(1, 0): NULL
MOD(NULL, 1): NULL
b) POWER()/POW():
● This function returns mn i.e a number m raised to the nth power.
Syntax:
POWER(m,n) or POW(m,n)
● M and n can be any numbers, but if m is negative, n must be an
integer.
Examples:
POW(2, 0): 1
POW(2, 2): 4
POW(2, 4): 16
POW(2.5, 2): 6.25
Prepared By: Er.Santosh Chapagain
c) ROUND():
● ROUND() function rounds the specified number to the specified
number of decimal places.
Syntax:
ROUND(x,d)
Parameters
X: Required. The number to be rounded.
d: Required. The number of decimal places to round x to. The
default is 0.
Examples:
SELECT ROUND(1123.324,2); o/p:1123.32
3) Date Functions:
a) NOW():
● The NOW() function returns the current date and time in YYYY-MM-DD
hh:mm:ss or YYYYMMDDhhmmss format.
● NOW() returns a constant time that indicates the time at which the
statement began to execute.
Syntax:
NOW()
Example:
SELECT NOW(); o/p:today’s date
SELECT NOW()+1;
NOTE:
● The result of NOW() + 0 is in the YYYYMMDDhhmmss format.
● NOW() + N means adding N seconds to the current datetime.
b) DATE():
● The DATE() function extracts and returns the date part from a datetime
value.
Syntax:
DATE(expr)
Example:
SELECT DATE('2022-02-28'): 2022-02-28
SELECT DATE('2022-02-28 10:10:10'): 2022-02-28
SELECT DATE(NOW()): 2022-04-13
SELECT DATE('2022-02-30'): NULL
SELECT DATE('Not A Date'): NULL
Prepared By: Er.Santosh Chapagain
c) MONTH():
● The MONTH() function extracts the month part of a date and returns it as
a number.
Syntax:
MONTH(date)
Example:
SELECT MONTH('2022-02-28'); o/p: 2
SELECT MONTH('2022-02-28 10:11:12'); o/p: 2
d) MONTHNAME():
● The MONTHNAME() function returns the name of the month for a given
date.
Syntax:
MONTHNAME(date)
Example:
MONTHNAME('2021-01-01'): January
MONTHNAME('2021-12-01'): December
MONTHNAME(NULL): NULL
e) YEAR():
● The YEAR() function extracts the year part of a date and returns it as a
number.
● The return value of YEAR() is 0 or an integer number from 1000 to 9999.
Syntax:
YEAR(date)
Example:
YEAR('2022-02-28 10:11:12'): 2022
YEAR('0000-01-01'): 0
Prepared By: Er.Santosh Chapagain
f) DAY():
● This function returns the day part of a date.
Syntax:
DAY(date)
Example:
DAY('2022-02-28 10:10:10'): 28
DAY(NOW()): today’s day
DAY('2022-02-00'): 0
DAY('2022-02-30'): NULL
DAY('Not A DATE'): NULL
DAY(NULL): NULL
g) DAYNAME():
● The DAYNAME() function returns the weekday name for a given date.
● Return value will be one of the following values: Monday, Tuesday,
Wednesday, Thursday, Friday, Saturday, Sunday.
Syntax:
DAYNAME(date)
Example:
DAYNAME('2022-02-21'): Monday
DAYNAME('2022-02-00'): NULL
DAYNAME('2022-02-30'): NULL
DAYNAME('Not A DATE'): NULL
DAYNAME(NULL): NULL
Prepared By: Er.Santosh Chapagain
Prepared By: Er.Santosh Chapagain
Prepared By: Er.Santosh Chapagain
1 Single row function is applied on Multiple rows functions are applied on the
each row. group of rows.
2 Single row functions return multiple Multiple Rows Functions return only one
outputs i.e output based on each result i.e output based on a group of rows.
row.