數(shù)據(jù)持久化: 數(shù)據(jù)永久的保存起來
1.文件
2.cookie
3.數(shù)據(jù)庫
根據(jù)處理數(shù)據(jù)的能力, 可分為:
1.大型數(shù)據(jù)庫: Oracle
2.中型數(shù)據(jù)庫: MySQL, SQLServer
3.小型數(shù)據(jù)庫: Access
4.輕量級數(shù)據(jù)庫: SQLite
數(shù)據(jù)庫的組成
1.一個數(shù)據(jù)庫系統(tǒng)管理著多個數(shù)據(jù)庫
2.一個數(shù)據(jù)庫中可以存放多張表
3.每張表都有字段(比如姓名, 年齡)
4.表中會有一個特殊的字段(主鍵), 用于保證數(shù)據(jù)的唯一性
MySQL的管理系統(tǒng): phpMyAdmin
通過代碼操作數(shù)據(jù)庫, 使用SQL(structure query language, 結(jié)構(gòu)化查詢語言)
CURD
1.增(insert)
2.刪(delete)
3.改(update)
4.查(select)
注: SQL語句中的關(guān)鍵詞, 不區(qū)分大小寫
一.查詢語句
1.查詢所有數(shù)據(jù)
select * from 表名
例如: select * from student/SELECT * FROM student
2.查詢所有數(shù)據(jù), 只顯示某些字段
select 字段1, 字段2, ..., 字段n from 表名
例如: select name, gender from student
3.根據(jù)某個條件進行查找
select * from 表名 where 字段 = 值
例如: select * from student where gender = '女'
4.根據(jù)多個條件進行查找
select * from 表名 where 字段1 = 值1 and 字段2 = 值2
例如: select * from student where name = ‘you’ and age = 2
5.根據(jù)范圍進行查找
select * from 表名 where 字段 > 值
例如: select * from student where age >= 18
select * from 表名 where 字段 between 值1 and 值2
例如: select * from student where age between 24 and 25
6.反向查找
select * from 表名 where 字段 not between 值1 and 值2
例如: select * from student where age not between 24 and 25
7.根據(jù)多個條件中的某個條件, 進行查找
select * from student where 字段1 = 值1 or 字段2 = 值2
例如: select * from student where name = ‘hou’ or age = 18
8.模糊查詢, 以什么開頭
select * from student where 字段 like 值%
例如: select * from student where name like '張%'
9.模糊查詢, 以什么結(jié)尾
select * from student where 字段 like %值
例如: select * from student where name like '%張'
10.模糊查詢, 包含某個內(nèi)容
select * from student where 字段 like %值%
例如: select * from student where name like '%張%'
11.不重復(fù)查找
select distinct 字段 from 表名
例如: select distinct gender from student
12.限制查詢的條數(shù)
select * from 表名 limit 條數(shù)
例如: select * from student limit 2
13.對查詢的結(jié)果進行排序
升序: select * from 表名 order by 字段 asc
降序: select * from 表名 order by 字段 desc
例如: select * from student order by age asc
二: 插入語句
insert into 表名 (字段1, 字段2, ..., 字段n) values (值1, 值2, ..., 值3)
例如: insert into student (name, gender, age) values (‘keke’, '女', 38)
三: 修改語句
update 表名 set 字段1 = 值1, ..., 字段n = 值n where 主鍵 = 值
例如: update student set name = '經(jīng)紀人' where id = 6
四.刪除語句
delete from 表名 where 主鍵 = 值
例如: delete from student where id = 6
五.新建表
create table 表名(字段1 類型1, ..., 字段n 類型n)
例如: create table if not exists cat(id int primary key auto_increment, nickname text)
六.刪除表
drop table 表名
例如: drop table cat