5. Database
TABLE
How Does My Data Get Stored? Ans: tables
Databases use a series of Tables to store the
data.
first
name
last
name
email
Benson Lu
llwbenson@g
mail.com
Cherry Lin
cherry@gmail.
com
#_asukademy_users
6. Database
id
How Do I Organize My Data? Ans: id
For each record, we give a
id
first
name
last
name
email
1 Benson Lu
llwbenson
@gmail.co
m
2 Cherry Lin
cherry@g
mail.com
#__asukademy_users
unique id (Primary key)
7. Database
One database may have multiple tables
id
first
name
last
name
email
1 Benson Lu
llwbenson@
gmail.com
2 Cherry Lin
cherry@gma
il.com
#__asukademy_users
id city area
3 Taichung South
4 Taipei Xinyi
#__asukademy_address
8. Database
Table may have relation with other tables
id
first
name
last
name
email
addres
s_id
1
Benso
n
Lu
llwbenso
n@gmail.
com
4
2 Cherry Lin
cherry@g
mail.com 3
#__asukademy_users
id city area
3 Taichung South
4 Taipei Xinyi
#__asukademy_addresses
10. Create your Database
Plan your table
1. decide what information you want to
store
2. decide what TYPE is that data
ex: date, varchar, char,
3. create table
4. create table structure
12. Table data TYPE
Type
型態
空間需
求
範圍
TINYINT 1
Signed: -128 to 127
Unsigned: 0 to 255
INT 4
Signed: -2147683648~2147683647
Unsigned: 0~4294967295
DATE 3 1000-01-01~9999-12-31
16. Read from Database
SELECT * FROM user
1. Make connection
2. SELECT * from table_name
3. execute query
4. parse result($result->num_rows)
5. Close connection
18. Create from Database
INSERT INTO table (column1, column2, …)
VALUE(NULL, val2, val3, …)
1. Make connection
2. INSERT INTO FOO
3. execute query
4. Close connection
19. Update from Database
1. We must have ID to determine which record to
modified
2. We must know the what columns to update
3. Make connection
4. UPDATE table SET foo=bar ……..WHERE id=?
5. execute query
6. Close Connection
UPDATE table_name SET `column1`=`val1` … WHERE `id`=3
21. Delete from Database
1. We must have ID to determine which
record to delete
2. Make connection
3. DELETE FROM users WHERE id = ?
4. execute query
5. Close Connection