pragma mark - DDL語句
pragma mark create(創(chuàng)建表)
// 格式
create table 表名 (字段名1 字段類型1, 字段名2 字段類型2, …) ;
create table if not exists 表名 (字段名1 字段類型1, 字段名2 字段類型2, …) ;
// 示例
create table t_student (id integer, name text, age inetger, score real) ;
CREATE TABLE IF NOT EXISTS t_student(id INTEGER , name TEXT, age , score REAL);
pragma mark create+(簡單約束)
// 建表時可以給特定的字段設置一些約束條件,常見的約束有
not null // 規(guī)定字段的值不能為null
unique // 規(guī)定字段的值必須唯一
default // 指定字段的默認值
//(建議:盡量給字段設定嚴格的約束,以保證數據的規(guī)范性)
// 示例
create table t_student (id integer, name text not null unique, age integer not null default 1) ;
// name字段不能為null些椒,并且唯一
// age字段不能為null府适,并且默認為1
pragma mark create + id(添加主鍵)
create table t_student (id integer primary key autoincrement, name text, age integer) ;
pragma mark create (新建一個外鍵)
create table t_student (id integer primary key autoincrement, name text, age integer, class_id integer, constraint fk_t_student_class_id_t_class_id foreign key (class_id) references t_class (id)) ;
pragma mark drop(刪除表)
// 格式
drop table 表名 ;
drop table if exists 表名 ;
// 示例
drop table t_student ;
DROP TABLE IF EXISTS t_student;
pragma mark - DML語句
pragma mark insert(插入數據)
// 格式
insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ;
// 示例
insert into t_student (name, age) values (‘lnj’, 10) ; //數據庫中的字符串內容應該用單引號 ’ 括住
INSERT INTO t_student(age, score, name) VALUES ('28', 100, 'jonathan');
INSERT INTO t_student(name, age) VALUES ('lee', '28');
INSERT INTO t_student(score) VALUES (100);
pragma mark update(更新數據)
// 格式
update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;
// 示例
update t_student set name = ‘jack’, age = 20 ;//會將t_student表中所有記錄的name都改為jack纲仍,age都改為20
UPDATE t_student SET name = 'MM' WHERE age = 10;
UPDATE t_student SET name = 'WW' WHERE age is 7;
UPDATE t_student SET name = 'XXOO' WHERE age < 20;
UPDATE t_student SET name = 'NNMM' WHERE age < 50 and score > 10;
pragma mark delete(刪除數據)
// 格式
delete from 表名 ;
// 示例
delete from t_student ;//將t_student表中所有記錄都刪掉
pragma mark where(條件語句)
// 如果只想更新或者刪除某些固定的記錄包斑,那就必須在DML語句后加上一些條件
// 條件語句的常見格式
where 字段 = 某個值 ; // 不能用兩個 =
where 字段 is 某個值 ; // is 相當于 =
where 字段 != 某個值 ;
where 字段 is not 某個值 ; // is not 相當于 !=
where 字段 > 某個值 ;
where 字段1 = 某個值 and 字段2 > 某個值 ; // and相當于C語言中的 &&
where 字段1 = 某個值 or 字段2 = 某個值 ; // or 相當于C語言中的 ||
// 示例
// 將t_student表中年齡大于10 并且 姓名不等于jack的記錄骑素,年齡都改為 5
update t_student set age = 5 where age > 10 and name != ‘jack’ ;
// 刪除t_student表中年齡小于等于10 或者 年齡大于30的記錄
delete from t_student where age <= 10 or age > 30 ;
// 將t_student表中名字等于jack的記錄鲫忍,score字段的值 都改為 age字段的值
update t_student set score = age where name = ‘jack’ ;
pragma mark - DQL語句
pragma mark select(查詢)
// 格式
select 字段1, 字段2, … from 表名 ;
select * from 表名; // 查詢所有的字段
// 示例
select name, age from t_student ;
select * from t_student ;
select * from t_student where age > 10 ; // 條件查詢
pragma mark select(別名)
// 格式(字段和表都可以起別名)
select 字段1 別名 , 字段2 別名 , … from 表名 別名 ;
select 字段1 別名, 字段2 as 別名, … from 表名 as 別名 ;
select 別名.字段1, 別名.字段2, … from 表名 別名 ;
// 示例
select name myname, age myage from t_student ;//給name起個叫做myname的別名逐抑,給age起個叫做myage的別名
select s.name, s.age from t_student s ;//給t_student表起個別名叫做s鸠儿,利用s來引用表中的字段
SELECT s.name myName, s.age myAge, s.score myScore FROM t_student s WHERE s.age > 50;
pragma mark select(計算記錄的數量)
// 格式
select count (字段) from 表名 ;
select count ( * ) from 表名 ;
// 示例
select count (age) from t_student ;
select count ( * ) from t_student where score >= 60;
pragma mark select(排序)
// 格式
// 查詢出來的結果可以用order by進行排序
select * from t_student order by 字段 ;
select * from t_student order by age ;
// 示例
// 默認是按照升序排序(由小到大),也可以變?yōu)榻敌颍ㄓ纱蟮叫厕氨。?br>
select * from t_student order by age desc ; //降序
select * from t_student order by age asc ; // 升序(默認)
// 多字段進行排序
select * from t_student order by age asc, height desc ;
先按照年齡排序(升序)进每,年齡相等就按照身高排序(降序)
pragma mark select+limit(分頁)
// 使用limit可以精確地控制查詢結果的數量,比如每次只查詢10條數據
// 格式
select * from 表名 limit 數值1, 數值2 ;
// 示例
select * from t_student limit 4, 8 ;//可以理解為:跳過最前面4條語句命斧,然后取8條記錄
// limit常用來做分頁查詢田晚,比如每頁固定顯示5條數據,那么應該這樣取數據
第1頁:limit 0, 5
第2頁:limit 5, 5
第3頁:limit 10, 5
…
第n頁:limit 5*(n-1), 5
select * from t_student limit 7 ;
// 相當于select * from t_student limit 0, 7 ;
// 表示取最前面的7條記錄
pragma mark select (表連接查詢)
// 表連接的類型
// 內連接:inner join 或者 join (顯示的是左右表都有完整字段值的記錄)
// 左外連接:left outer join (保證左表數據的完整性)
// 示例
// 查詢0316iOS班的所有學生
select s.name,s.age from t_student s, t_class c where s.class_id = c.id and c.name = ‘0316iOS’;