IOS理论知识OC基础iOS之数据库及数据结构与算法

iOS数据存储之SQL语句的基本使用

2015-12-27  本文已影响2481人  芝麻绿豆

SQL语句的种类

DDL语句的基本使用

DROP TABLE IF EXISTS '表名';
CREATE TABLE IF NOT EXISTS '表名' 
( '字段名' 类型(INTEGER, REAL, TEXT, BLOB) 
NOT NULL 不允许为空 PRIMARY KEY 主键 AUTOINCREMENT 自增长, 
'字段名2' 类型, ... )
CREATE TABLE IF NOT EXISTS 't_student' 
( "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
"name" TEXT, "age" INTEGER, "height" REAL
)
 - 语句说明
- CREATE TABLE:创建一张表
- IF NOT EXISTS:不存在则创建
- 't_student':表的名称
- NOT NULL:不允许为空
- PRIMARY KEY:主键
- AUTOINCREMENT:自动增加
- 'id' INTEGER:有一个ID字段,类型是INTEGER

DML语句的基本使用

INSERT INTO 't_student' (name, age, height) VALUES ('why', 18, 1.88);
UPDATE 't_student' SET 字段 = '值' WHERE 条件判断;
UPDATE t_student SET name = 'me' WHERE age = 14;
UPDATE t_student SET name = 'liu' WHERE age is 20;
UPDATE t_student SET name = 'yy' WHERE age < 20;
UPDATE t_student SET name = 'A' WHERE age < 100 and score > 60;
UPDATE t_student SET name = 'ly';
DELETE FROM t_student;
DELETE FROM t_student WHERE age < 30;

DQL语句的基本使用

SELECT * FROM t_student;
SELECT name, age FROM t_student;
SELECT * FROM t_student WHERE age >= 18;
SELECT * FROM t_student WHERE name like '%i%';
SELECT count(*) FROM t_student;
- 计算某一个列个数
SELECT count(age) FROM t_student;
SELECT * FROM t_student ORDER BY age;
- 降序 DESC
SELECT * FROM t_student ORDER BY age DESC;
- 按照年龄升序排序,如果年龄相同,按照名字的降序排列
SELECT * FROM t_student ORDER BY age,name DESC;
SELECT name AS myName, age AS myAge FROM t_student;
- 给表起别名
SELECT s.name, s.age FROM t_student as s;
SELECT * FROM t_student LIMIT 数字1,数字2;
- 跳过前9条数据,再查询3条数据
SELECT * FROM t_student LIMIT 9, 3;
- 跳过0条数据,取5条数据
SELECT * FROM t_student LIMIT 5; 
上一篇下一篇

猜你喜欢

热点阅读