Professional Documents
Culture Documents
CHAPTER 09
DATABASE CONNECTIVITY: MYSQL AND SQL
COMMANDS
9.1 Understanding
The Database
Learning Outcome :
Design
Process
Some advantages to good database design
9.1 Understanding
The Database
Topics covered in this lesson are.
Design Process
4
Chapter 15: Understanding the Database Design Process
9.1 The Importance of Good Database Design
9.2 Types of Table Relationships
9.3 Understanding Normalization
9.4 Following the Design Process
5
Chapter 16: Learning Basic SQL Commands
9.5 Learning the MySQL Data Types
9.6 Learning the Table Creation Syntax
9.7 Using the INSERT Command
9.8 Using the SELECT Command
9.9 Using WHERE in Your Queries
9.10
9.11
9.12
9.13
9.14
9.15
Optimized
efficiently
relationships,
perform
Types
One-to-one relationships
One-to-many relationships
Many-to-many relationships
One-to-one relationships
example
One-to-many relationships
example
10
Many-to-many relationships
example
11
9.3 Understanding
Normalization
12
9.3 Understanding
Flat Table
Normalization
(cont)
A spreadsheetit has many, many columns
13
Example
StudentName
CourseID1
CourseDescrip
tion1
CourseInstruct
or1
CourseID2 - ID
CourseDescrip
tion2
CourseInstruct
or2
9.3 Understanding
Normalization (cont)
Rules
Example
14
9.3 Understanding
Normalization (cont)
Rules
Example
15
9.3 Understanding
Normalization (cont)
Rules
Example
16
Steps
discern relationships
17
18
Outcome
How to use basic functions, the WHERE clause, and the GROUP
BY clause in SELECT expressions
Three Categories
numeric
string
19
20
Lists
range
is
from
2147483648
to
21
22
23
list
1000-01-01
00:00:00
and
9999-12-31
24
25
for
TIMESTAMP
is
14,
26
list
CHAR(M) - fixed-length string between 1
and 255 characters in length
example :
CHAR(5); defining a length is not
required, but the default is 1
VARCHAR(M)
variable-length
string
between 1 and 255 characters in length
example : VARCHAR(25); must define a length
when creating a VARCHAR field
27
28
29
length
with
LONGBLOB
or
30
Creating Database
1. Open any bowser, type localhost in search box and press enter
3. Click SQL
4. Type CREATE DATABASE 101 in Run SQL query/queries on server localhosts text area and click Go.
31
Creating Database
2nd option to create database
32
names of fields
CREATE TABLE
column_type);
table_name
(column_name
33
34
35
36
37
38
39
Ordering
Example
SELECT id, item_name, curr_qty FROM grocery_inventory
ORDER BY item_name DESC;
40
Limiting
requirements needed
Example
SELECT id, item_name, curr_qty FROM grocery_inventory
ORDER BY item_name DESC LIMIT 0, 2;
41
SELECT expressions_and_columns
FROM table_name [WHERE
some_condition_is_true]
42
43
operator
meaning
=
Equal to
!=
Not equal to
<=
<
>=
>
Less than
Greater than or equal to
Greater than
44
45
comparing strings
Example
SELECT * FROM grocery_inventory WHERE item_name LIKE 'A
%' ;
46
Example
47
48
49
tablename.fieldname
50
an ambiguity error
Example
SELECT id, fruit_name, colour_name FROM fruit, colour
WHERE fruit.id = colour.id;
51
Example
SELECT fruit.id, fruit_name, colour_name FROM fruit,
colour WHERE fruit.id = colour.id;
52
Types
INNER JOIN
LEFT JOIN
RIGHT JOIN
53
54
55
56
Example
SELECT student_name.id, first_name, last_name, email
FROM student_name LEFT JOIN student_email ON
student_name.id = student_email.id ORDER BY
student_name.id;
57
Example
SELECT student_name.id, first_name, last_name, email
FROM student_name RIGHT JOIN student_email ON
student_name.id = student_email.id ORDER BY
student_name.id;
58
extremely useful
SELECT
expressions_and_columns
FROM
table_name WHERE somecolumn = (SUBQUERY);
59
60
UPDATE
table_name
SET
column1=new
value,
column2=new value2 [WHERE some_condition_is_true]
61
62
Example
UPDATE fruit SET fruit_name = 'Grape';
63
Example
64
65
To modifying records,
INSERT command
similar
66
to
the
(column
list)
67
Example
DELETE FROM fruit;
68
Example
DELETE FROM fruit WHERE fruit_name = 'Anggur';
69
9.14 Frequently
Used String
MySQLs built-in string-related functions
Functions
can be used several in
ways MySQL
70
9.14 Frequently
Used String
Length and Concatenation Functions
Functions in MySQL
(cont)
LENGTH()
OCTET_LENGTH()
CHAR_LENGTH()
CHARACTER_LENGTH(),
Example
SELECT LENGTH('This is cool!');
71
9.14 Frequently
Used String
CONCAT() function
Functions in MySQL
(cont)
Example
72
9.14 Frequently
Used String
Using CONCAT() function with a table
Functions
containing names in MySQL
(cont)
Instead of using two strings, use two field
names to concatenate
73
9.14 Frequently
Used String
Example
Functions in MySQL
(cont)
74
9.14 Frequently
Used String
CONTACT_WS()
Functions in MySQL
(cont)
example : whitespace
Example
SELECT CONCAT_WS(' ', first_name,
last_name) FROM student_name;
75
9.14 Frequently
Used String
Shorten the width of result table
Functions in MySQL
(cont)
Example
76
9.14 Frequently
Used String
Trimming and Padding Functions
Functions in MySQL
Adding and removing extra characters
(cont)
(including whitespace) from strings
77
9.14 Frequently
Used String
Example
Functions in MySQL
(cont)
78
9.14 Frequently
Used String
TRIM() function
Functions in MySQL
(cont)
Example
79
9.14 Frequently
Used String
Functions in MySQL
(cont)
80
9.14 Frequently
Used String
RPAD() and LPAD() functions
Functions in MySQL
(cont)
Example
SELECT RPAD('needle', 10, 'X');
81
9.14 Frequently
Used String
Location and Position Functions
Functions in MySQL
(cont)
LOCATE() function
Example
SELECT LOCATE('needle', 'haystackneedlehaystack');
82
9.14 Frequently
Used String
Substring Functions
Functions in MySQL
(cont)
SUBSTRING() function
Example
SUBSTRING
83
9.14 Frequently
Used String
Functions in MySQL
(cont)
SELECT LEFT('MySQL', 2)
84
9.14 Frequently
Used String
String Modification Functions
Functions in MySQL
Example
(cont)
SELECT LCASE('MYSQL');
85
9.14 Frequently
Used String
Functions in MySQL
(cont)
86
9.14 Frequently
Used String
Functions in MySQL
(cont)
SELECT REPEAT('bowwow');
SELECT REPLACE('bowwowbowwowbowwowbowwow',
'wow', 'WoW');
87
specifying a table
88
DAYOFMONTH () function
DAYOFYEAR() function
DAYNAME() function
89
SELECT DAYOFWEEK('2015-10-28');
SELECT WEEKDAY('2015-10-28');
90
SELECT DAYOFMONTH('2015-10-28');
SELECT DAYOFYEAR('2015-12-31');
91
92
93
Example
SELECT MONTH('2015-01-01'), MONTHNAME('2012-01-01');
94
95
96
Example
SELECT DISTINCT YEAR(date_of_order) FROM orders;
97
WEEK ()
Example
SELECT WEEK('2015-12-31');
98
99
100
Example
SELECT CONCAT_WS(':',HOUR('2015-10-28 14:30:26'),
MINUTE('2015-10-28 14:30:26')) AS sample_time;
101
DATE_FORMAT() function
DATE_FORMAT(date,format);
102
103
104
105
106
107
108
109
synonymous
Example
SELECT CURDATE(), CURRENT_DATE();
110
Example
SELECT CURTIME(), CURRENT_TIME();
111
Example
SELECT NOW(), SYSDATE(), CURRENT_TIMESTAMP();
112
Example
SELECT UNIX_TIMESTAMP();
113
Example
SELECT FROM_UNIXTIME('1445751492');
114
Example
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(), '%D %M %Y
at %h:%i:%s');
115
9.16 Summary
116
Normalization
Summary (cont)
117
INSERT command
Summary (cont)
WHERE clauses
118
JOIN clauses
selecting records
statement
from
multiple
tables
within
one
Summary (cont)
119
DELETE command
Summary (cont)
120
Summary (cont)
121