* 什么是主键
* 主键(Primary Key,简称PK)用来唯一地标识某一条记录
* 例如t_student可以增加一个id字段作为主键,相当于人的身份证
* 主键可以是一个字段或多个字段
16.主键的设计原则
* 主键应当是对用户没有意义的
* 永远也不要更新主键
* 主键不应包含动态变化的数据
* 主键应当由计算机自动生成
17.主键的声明
* 在创表的时候用primary key声明一个主键
* create table t_student (id integer primary key, name text, age integer) ;
* integer类型的id作为t_student表的主键
* 主键字段
* 只要声明为primary key,就说明是一个主键字段
* 主键字段默认就包含了not null 和 unique 两个约束
* 如果想要让主键自动增长(必须是integer类型),应该增加autoincrement
* create table t_student (id integer primary key autoincrement, name text, age integer) ;
18.外键约束
* 利用外键约束可以用来建立表与表之间的联系
* 外键的一般情况是:一张表的某个字段,引用着另一张表的主键字段
* 新建一个外键
* 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)) ;
* t_student表中有一个叫做fk_t_student_class_id_t_class_id的外键
* 这个外键的作用是用t_student表中的class_id字段引用t_class表的id字段
19.表连接查询
* 什么是表连接查询
* 需要联合多张表才能查到想要的数据
* 表连接的类型
* 内连接: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’;
七、SQLite编码
1.创建、打开、关闭数据库
* 创建或打开数据库
path是数据库文件的存放路径
sqlite3 *db = NULL;
int