iOS开发:SQLite使用,5年经验Android程序员面试27天

* 什么是主键

* 主键(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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值