iOS数据库学习(2)-基础SQL语句

/* 1. 创建一个数据表 */
CREATE TABLE IF NOT EXISTS t_dog (name text, age integer);
CREATE TABLE IF NOT EXISTS t_cat (name,age);
CREATE TABLE IF NOT EXISTS t_student (name text NOT NULL, age integer);
CREATE TABLE IF NOT EXISTS t_student (name text NOT NULL UNIQUE, age integer NOT NULL);
CREATE TABLE IF NOT EXISTS t_student (name text, age integer DEFAULT 1); /* 指定年龄默认为1 */
CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text, age integer); /* SQLite中AUTOINCREMENT可以省略 */


/* 2. 删除表 */
DROP TABLE 表名
DROP TABLE IF EXISTS 表名

/* 3. 插入数据 */
INSERT INTO t_user (username,age,height) VALUES (‘赵六’,20,1.90);
INSERT INTO t_user(username,age) VALUES(‘陈七’,21);

/* 4. 更新数据 */
UPDATE t_user SET height = 1.65, age = 14;
UPDATE t_user SET left_count = 0 WHERE price < 1000;

/* 5. 删除数据 */
DELETE FROM t_user 
DELETE FROM t_shop WHERE left_count = 0;
DELETE FROM t_shop WHERE left_count < 500 AND price < 2000;
DELETE FROM t_shop WHERE left_count < 500 OR price < 2000;

/* 6.查询数据 */
SELECT name,price FROM t_shop;
SELECT * FROM t_shop;
SELECT * FROM t_shop left_count > 800;
SELECT * FROM name shop_name, price AS shop_price, left_count 库存 FROM t_shop s;
SELECT s.name, price, s.left_count FROM t_shop s;
SELECT count(name) FROM t_shop;
SELECT count(name) FROM t_shop WHERE left_count > 800;
SELECT count(*) FROM t_shop;
SELECT count(*) 剩余数量 FROM t_shop;
SELECT * FROM t_shop ORDER BY left_count ASC;
SELECT * FROM t_shop ORDER BY left_count DESC, price ASC;
SELECT * FROM t_shop LIMIT 4,8;
SELECT * FROM t_shop ORDER BY price DESC LIMIT 0,10;
SELECT * FROM t_shop LIMIT 7 /* 等价于LIMIT 0,7 */

 

posted @ 2016-12-15 21:26  夜行过客  阅读(189)  评论(0编辑  收藏  举报