美文网首页
7-各种存储方式的优缺点

7-各种存储方式的优缺点

作者: AlanGe | 来源:发表于2017-08-27 14:40 被阅读102次

    各种存储方式的优缺点

    • 1、Plist(NSArray\NSDictionary),只能存储数组,字典,但是数组和字典里面不能有自定义对象

    • 2、偏好设置:Preference(偏好设置\NSUserDefaults)也不能存储自定义对象

    • 3、归档:NSCoding(NSKeyedArchiver\NSkeyedUnarchiver):存储自定义对象,局限:一次性读取和存取操作。

    • 4、SQLite3:
      1.操作数据比较快,读取比较方便
      2.可以局部的读取
      3.比较小型,占用的内存资源比较小
      什么是SQLite
      SQLite是一款轻型的嵌入式数据库
      它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了
      它的处理速度比Mysql、PostgreSQL这两款著名的数据库都还快

    • 5、FMDB
      FMDB的优点
      使用起来更加面向对象,省去了很多麻烦、冗余的C语言代码
      对比苹果自带的Core Data框架,更加轻量级和灵活
      提供了多线程安全的数据库操作方法,有效地防止数据混乱

    • 6、Core Data:

    数据库操作

    操作数据库的步骤:

    1、打开数据库
    2、创建数据库表,表名,通常以 t_ 开头
    3、设计表的字段,看下需要存储什么数据,就添加什么样的字段,字段必须要搞一个主键,记录数据的唯一标识符。
    4、记录数据

    DDL:数据定义语句

    • 创建表格:
      create table if not exists t_student (id integer primary key autoincrement,name text,age integer);
      command + r:刷新表格

    • 删除表格
      drop table if exists t_class;

    DML:数据操作语句

    • 插入数据
      insert into t_class (name) values ('a');

    • 更新数据
      update t_class set name = 'jack’;

    • 删除数据
      delete from t_class;

    • 注意:只会删除记录,并不是删除表(drop才是删除表)
      delete from t_student where name = 'a1';

    条件语句

    • 条件语句的常见格式
      where 字段 = 某个值 ; // 不能用两个 =
      where 字段 is 某个值 ; //is 相当于 =
      where 字段 != 某个值 ;
      where 字段 is not 某个值 ; //is not 相当于 !=
      where 字段 > 某个值 ;
      where 字段1 = 某个值 and 字段2 > 某个值 ; // and相当于C语言中的 &&
      where 字段1 = 某个值 or 字段2 = 某个值 ; // or 相当于C语言中的 ||

    • 示例
      update t_class set name = 'aa' where name = 'a';
      update t_student set name = 'q' where age < 10 and id < 50;

    DQL:数据查询语句

    • 格式
      select 字段1, 字段2, … from 表名;
      select * from 表名; // 查询所有的字段

    • 示例
      select name, age from t_student;
      select * from t_student;
      select * from t_student where age > 10 ; // 条件查询

    排序

    • 按照某个字段的值,进行排序搜索
      select * from t_student order by 字段;
      select * from t_student order by age;

    • 默认是按照升序排序(由小到大),也可以变为降序(由大到小)
      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;
      先按照年龄排序(升序),年龄相等就按照身高排序(降序)

    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条记录
      select * from t_student where age > 50 order by age limit 3;
      查询年龄大于50的前三条数据

    简单约束

    • 建表时可以给特定的字段设置一些约束条件,常见的约束有
      not null :规定字段的值不能为null
      unique :规定字段的值必须唯一
      default :指定字段的默认值
      (建议:尽量给字段设定严格的约束,以保证数据的规范性)

    • 示例
      create table t_student (id integer, name text not null unique, age integer not null default 1);
      name字段不能为null,并且唯一
      age字段不能为null,并且默认为1

    主键约束

    • 如果t_student表中就name和age两个字段,而且有些记录的name和age字段的值都一样时,那么就没法区分这些数据,造成数据库的记录不唯一,这样就不方便管理数据

    • 良好的数据库编程规范应该要保证每条记录的唯一性,为此,增加了主键约束
      也就是说,每张表都必须有一个主键,用来标识记录的唯一性

    • 什么是主键
      主键(Primary Key,简称PK)用来唯一地标识某一条记录
      例如t_student可以增加一个id字段作为主键,相当于人的身份证
      主键可以是一个字段或多个字段

    主键的设计原则

    • 主键应当是对用户没有意义的
    • 永远也不要更新主键
    • 主键不应包含动态变化的数据
    • 主键应当由计算机自动生成

    主键的声明

    • 在创表的时候用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 if not exists t_student (id integer primary key autoincrement, name text not null unique, age integer not null default 1) ;

    外键约束

    • 利用外键约束可以用来建立表与表之间的联系

    • 外键的一般情况是:一张表的某个字段,引用着另一张表的主键字段

    • 新建一个外键
      create table if not exists t_student (id integer primary key autoincrement, name text, age integer, class_id integer,constraint fk_student_class foreign key (class_id) references t_class(id));
      t_student表中有一个叫做fk_student_class的外键
      这个外键的作用是用t_student表中的class_id字段引用t_class表的id字段

    表连接查询

    • 什么是表连接查询
      需要联合多张表才能查到想要的数据

    • 表连接的类型
      内连接: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’;
      查询学生对应的班级
      xiaoming 18 1
      xiaoli 19 2
      xiaozhang 20 3
      xiaozhao 21 1
      xiaowang 22 1

    • 起别名,不添加s.class_id = c.id,查询结果会不对,查询的结果个数 = student个 * class个
      select s.name sName,c.name cName from t_student s,t_class c where s.class_id = c.id;
      select s.name sName(别名),c.name cName(别名) from t_student s(别名),t_class c(别名) where s.class_id = c.id;
      select s.name sName,s.id sID,c.name cName,c.id cID from t_student s,t_class c where s.class_id = c.id and c.name = '2班';

    模糊查询:

    • like:表示模糊查询
    • %:表示任意
      select *from t_contact where name = 'q1';
      select *from t_contact where name like '%q';//前面任意,最后一个必须为q
      select *from t_contact where name like 'q%';// 后面任意,第一个必须为q
      select *from t_contact where name like '%q%';// 前后任意,中间带q

    相关文章

      网友评论

          本文标题:7-各种存储方式的优缺点

          本文链接:https://www.haomeiwen.com/subject/pmgbdxtx.html