2020
Working with Databases and MySQL
2020
Working with Databases and MySQL
2
Loading of Data
2020
LOAD command lets you load the data from a file unto
your table
Syntax:
LOAD DATA INFILE “<file>” INTO TABLE
<table>;
mysql> LOAD DATA INFILE “student_file.txt”
-> INTO TABLE tblstudent;
3
Example
2020
4
SELECT Statement
2020
• SELECT clause is used to display the column content
in a table.
Syntax:
• SELECT <column expression> FROM <tablename>;
mysql> SELECT * FROM tblStudent;
mysql> SELECT name FROM tblStudent;
mysql> SELECT name, gender FROM tblStudent;
5
In Writing MySQL Statement
2020
• MySQL statements are not case sensitive.
• MySQL statement can be on one or more lines.
• Keywords cannot be abbreviated or split across lines.
• Clauses are usually placed on separate lines.
• Indents are used to enhance readability.
6
Limiting the Rows that are 2020
Selected
• Restrict the rows that are returned by using the
WHERE clause:
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)];
7
Using the WHERE clause
2020
mysql> SELECT id, name, gender
-> FROM tblstudent
-> WHERE gender = ‘F’ ;
Character strings and date values are enclosed in
single quotation marks.
Character values are case sensitive, and date values
are format sensitive.
8
MySQL Operators
2020
• Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
9
Example
2020
mysql> SELECT name, tuition + 500 FROM
tblStudent WHERE id=10001;
10
Defining ‘Alias’
2020
• A column alias:
• renames a column heading
• is useful with calculations
• immediately follows the column name ( there can be an
optional AS keyword between the column name and alias.
• requires double qoutation marks if it contains spaces or
special characters or if it is case-sensitive.
11
Example
2020
mysql> SELECT name, tuition + 500 as “Fee”
FROM tblStudent WHERE id=10001;
12
Logical Operators
2020
• AND or &&
mysql> SELECT * FROM tblStudent WHERE id=10001
AND name=‘Juan Dela Peña’;
mysql> SELECT * FROM tblStudent WHERE id=10001
&& name=‘Juan Dela Peña’;
13
Logical Operators
2020
• OR or ||
mysql> SELECT * FROM tblStudent WHERE id=10001
OR name=‘Juan Dela Peña’;
mysql> SELECT * FROM tblStudent WHERE id=10001
|| name=‘Juan Dela Peña’;
14
Comparison Operators
2020
• Used to compare values
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
BETWEEN Between two values (inclusive)
...AND...
IN(set) Match any of a list of values
LIKE Match a character pattern
IS NULL Is a null value 15
Example
2020
mysql> SELECT * FROM tblStudent WHERE id <>
10001;
mysql> SELECT * FROM tblStudent WHERE id !=
10001;
16
Using the LIKE Condition
2020
• Use the LIKE condition to perform wildcard searches
of valid search string values.
• Search conditions can contain either literal characters
or numbers:
• % denotes zero or many characters.
• _ denotes one character..
mysql> SELECT name FROM tblstudent
-> WHERE name LIKE ‘A%’;
mysql> SELECT name FROM tblstudent
-> WHERE name LIKE '_o%' ;
17
The ORDER BY Clause
2020
• Sort retrieved rows with the ORDER BY clause:
• ASC: ascending order, default
• DESC: descending order
• The ORDER BY clause comes last in the SELECT
statement
mysql> SELECT * FROM tblStudent ORDER BY name;
mysql> SELECT * FROM tblStudent ORDER BY name
DESC;
18
Example
2020
19
2020
Questions??