数据库基本知识
2017-11-03 11:03
239 查看
数据库:存储时间的仓库
数据存储:
Plist :(NSArray\NSDictionary)
Preference(偏好设置\NSUserDefaults)
NSCoding(NSKeyedArchiver\NSKeyedUnarchiver)
不能存储大数据
SQLite3:数据库,存C语言,轻量级(只适用于存储用户个人数据,几百k的内存就够了) 大批量数据缓存:微博,网易新闻
core data:数据库,基于SQLite3,OC版本,庞大,不好操作
新浪服务器存储的数据库
常用的关系型数据库
PC端:Oracle,MySQL,SQL Server,Access,DB2
嵌入式\移动端:sqlite
服务器数据库
Navicat\PLSQL\Toad
序列号:serial number简称 SN
SQL:(structured query language):结构化查询语言(增删改查,CRUD)
SQL语句特点:
1 不区分大小写(比如数据库认为user和User是一样的)
2 每条语句都必须以分号;结尾
SQL关键字:
select insert update delete from create where desc order by table alter view index等等
3 数据库中不可以使用关键字来命名表,字段
数据定义语句:(DDL:Data Definition Lanaguage) 包括create 和drop等操作
在数据库中创建新表或者删除表(create table或者drop table)
数据操作语句:(DML:Data Manipulation Language)
包括insert, update, delete等操作
数据查询语句:(DQL:Data Query Language)
create table t_student (name text, age integer);
或者写下面的语句
creat table if not exists t_student(name text, age integer);
drop table if exists t_student
数据库中的字符串值用单引号:
insert into t_student (name , price, left_count) values (’手机’,2000,500);
update t_student set name = ‘小三’, age = 5 ;
update t_student set age = 5 where age > 10 and name != ‘jack’;
update t_student set name = age where name = ‘jack’;
删除数据(delete)
delete from t_student where name = ‘111’ and age < 6;删除所有记录
delete from t_student where name = ‘222’ or age = 6;
条件语句常见的格式:
where 字段 = 某个值;
where 字段 is 某个值; // is 相当于 =
where 字段 != 某个值;
where 字段 is not 某个值; // is not相当于 !=
查询
select name, price from t_shop;
select * from t_shop where left_count > 800;
select * from t_food where name = ‘虾’;//搜索条件名称完全为’虾’的.
select * from t_food where name like ‘%%虾%%’;//,两个%代表一个%号,左边%表示任何东西不放东西也可以,右边%表示右边可以放任何东西也可以不放东西.
算下有多少条名字的记录count(字段)函数
select count(name) from t_shop;
select count(name) from t_shop where left_count > 800;
select count()from t_shop where left_count > 800; //号表示有多少条记录
select *from t_shop order by left_count asc;// asc 升序 desc 降序
select *from t_shop order by left_count desc,price asc;//默认升序
select * from t_shop order by price desc limit 10,10;//取出第二页价格最高的10条 limit可以分页查询
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;// limit 7 == limit 0, 7
联合外键
select id from t_food_type where name = ‘鲁菜’;// 这条语句结果返回为3
// 嵌套查询
select * from t_food where food_type_id = (select id from t_food_type where name = ‘鲁菜’);
//联合查询如下:
select f.name,ft.name from t_food f, t_food_type ft where f.food_type_id = ft.id and ft.name = ‘粤菜’;
Plist存储方式
// 先取出来
//缺点:每次把旧数据读出来,新数据添加,才能存储
// 检索数据方面
SQLite3 :数据库 轻款的,这个用户的数据。只用在移动端
大批量数据缓存 用sqlite3
Core Data: 数据库,基于SQLite3, OC版本,
关系型数据库:
PC端:Oracle MySQL SQL Server Access DB2 Sybase
嵌入式\移动客户端:SQLite
表table
4个字段或者4列 column 或者叫属性
2条记录\行\row\record
商品名称 商品价格 商品的库存树 商品的日期
水杯 10 200 2011/9/9
枕头 200
数据库存储数据的步骤:
新建一张表
添加多个字段
添加多行记录
数据库软件:Navicat\PLSQL
Fields属性
表名以t开头t_shop
字段名称 字段类型
name text
price real 浮点型
left_count integer
增删改查 CRUD DDL
SQL语句 不区分大小写,每天语句以分号结尾
create drop insert update delete select
where order by group by having
创表:[create table 表名(字段1 字段类型1,字段2 字段类型2)]
CREATE TABLE IF NOT EXISTS t_student (name text,age integer);
删表:drop table if exists 表名
DML
插入数据 :数据库的字符串用单引号而不是双引号
insert into 表名(name,price) values(‘手机’,2000.1);
更新数据:update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值
删除数据:delete * from 表名;
条件语句的常见格式
where 字段 = 某个值;// 不能用两个 =
// 检索数据方面
当价格等于1000元的库存等号用一个=
update t_shop set left_count = 0 where price = 1000
delete from t_shop where left_count = 0;
删除库存《200且 价格低于2000的手机 不是用&& 而是用and 或 不是用||而是用or
delete from t_shop where left_count < 500 AND price < 2000
条件语句的常见格式
where字段 = 某个值;//不能用两个=
where字段 is 某个值;//is相当于=
where 字段 != 某个值;
where 字段 is not 某个值;// is not 相等于 !=
where 字段1 = 某个值 and 字段2 > 某个值;// and相当于C语言中的&&
where 字段1 = 某个值 or 字段2 = 某个值;// or相当于C语言中的||
练习
将t_student表中年龄大于10并且姓名不等于jack的记录,年龄都改为5
update t_student set age = 5 where age > 10 and name != ‘jack’;
删除t_student表中年龄小于等于10或者年龄大于30的记录
delete from t_student where age <= 10 or age > 30;
update t_student set score = age where name = ‘jack’;
将t_student表中名字等于jack的记录,score字段的值都改为age字段的值
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 name shop_name, price As shop_price,left_count FROM t_shop;
// 其中As可以省略,shop_name 和shop_price分别是name和price的别名。
// 查询表中有多少条东西用count()这个函数
select count(name) from t_shop;// count()是函数的意思
select count(*) from t_shop where left_count > 800;
排序:
select * from t_shop ORDER BY left_count;// 默认升序
select * from t_shop ORDER BY left_count DESC/ASC;
// 先按库存排再按照价格排 谁写到前面,谁的优先级高
select * from t_shop ORDER BY left_count DESC,price ASC;
limit使用limit可以精确地查询结果的数量,比如每次只查询10条数据
格式:select* from 表名 limit 数值1,数值2;//数值1决定从哪里开始查,数值2决定查出来的是多少条
示例
select * from t_student limit 4,8;
// 可以理解为 :跳过最前面的4条语句(0,1,2,3)从4条开始,然后取8条记录
//取出价格最高的10条
select * from t_shop order by price DESC limit 0,10;
//第2页的最高10条
select * from t_shop order by price DESC limit 10,10;
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;
简单约束 (尽力添加约束,避免脏数据)
建表时可以给特定的字段设置一些约束条件,常见的约束有:
not null;规定字段的值不能为null
unique:规定字段的值必须唯一
default:制定字段的默认值。
约束都是在创建表的时候指定的:
create table if not exists t_student(name,txt,age integer default 1);
主键:(Primary Key,简称PK)用来唯一标识某一条记录
例如:t_student可以增加一个id字段作为主键,相等于人的身份证
主键可以是一个字段或多个字段。
create table if not exists t_student(id integer PRIMARY KEY autoincrement,name text, age integer)
班级和学生的关系是1对多 外键加在多的那端。
利用外键约束可以用来建立表与表之间的联系
外键一般情况下是:一张表的某个字段,引用着另一张表的主键字段
// 班级表
;
select * from t_food where food_type_id = select id from t_food_type where name = ‘粤菜’;
select f.name, ft.name from t_food f ,t_food_type ft where f.food_type_id = ft.id and ft.name = ‘粤菜’;
// 查询安卓班的学生
select * from t_student t where class = select id from t_class where name = android;
select s.name sname,c.name cname from t_student s,t_class c where s.class = c.id and c.name = ‘android’;
数据存储:
Plist :(NSArray\NSDictionary)
Preference(偏好设置\NSUserDefaults)
NSCoding(NSKeyedArchiver\NSKeyedUnarchiver)
不能存储大数据
SQLite3:数据库,存C语言,轻量级(只适用于存储用户个人数据,几百k的内存就够了) 大批量数据缓存:微博,网易新闻
core data:数据库,基于SQLite3,OC版本,庞大,不好操作
新浪服务器存储的数据库
常用的关系型数据库
PC端:Oracle,MySQL,SQL Server,Access,DB2
嵌入式\移动端:sqlite
服务器数据库
Navicat\PLSQL\Toad
序列号:serial number简称 SN
SQL:(structured query language):结构化查询语言(增删改查,CRUD)
SQL语句特点:
1 不区分大小写(比如数据库认为user和User是一样的)
2 每条语句都必须以分号;结尾
SQL关键字:
select insert update delete from create where desc order by table alter view index等等
3 数据库中不可以使用关键字来命名表,字段
数据定义语句:(DDL:Data Definition Lanaguage) 包括create 和drop等操作
在数据库中创建新表或者删除表(create table或者drop table)
数据操作语句:(DML:Data Manipulation Language)
包括insert, update, delete等操作
数据查询语句:(DQL:Data Query Language)
create table t_student (name text, age integer);
或者写下面的语句
creat table if not exists t_student(name text, age integer);
drop table if exists t_student
数据库中的字符串值用单引号:
insert into t_student (name , price, left_count) values (’手机’,2000,500);
update t_student set name = ‘小三’, age = 5 ;
update t_student set age = 5 where age > 10 and name != ‘jack’;
update t_student set name = age where name = ‘jack’;
删除数据(delete)
delete from t_student where name = ‘111’ and age < 6;删除所有记录
delete from t_student where name = ‘222’ or age = 6;
条件语句常见的格式:
where 字段 = 某个值;
where 字段 is 某个值; // is 相当于 =
where 字段 != 某个值;
where 字段 is not 某个值; // is not相当于 !=
查询
select name, price from t_shop;
select * from t_shop where left_count > 800;
select * from t_food where name = ‘虾’;//搜索条件名称完全为’虾’的.
select * from t_food where name like ‘%%虾%%’;//,两个%代表一个%号,左边%表示任何东西不放东西也可以,右边%表示右边可以放任何东西也可以不放东西.
算下有多少条名字的记录count(字段)函数
select count(name) from t_shop;
select count(name) from t_shop where left_count > 800;
select count()from t_shop where left_count > 800; //号表示有多少条记录
select *from t_shop order by left_count asc;// asc 升序 desc 降序
select *from t_shop order by left_count desc,price asc;//默认升序
select * from t_shop order by price desc limit 10,10;//取出第二页价格最高的10条 limit可以分页查询
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;// limit 7 == limit 0, 7
联合外键
select id from t_food_type where name = ‘鲁菜’;// 这条语句结果返回为3
// 嵌套查询
select * from t_food where food_type_id = (select id from t_food_type where name = ‘鲁菜’);
//联合查询如下:
select f.name,ft.name from t_food f, t_food_type ft where f.food_type_id = ft.id and ft.name = ‘粤菜’;
Plist存储方式
NSAarry* data = @[@”jack”,@”rose”]; [data writeToFile:@”/user/apple/desktop/names.plist” atomically:YES];
// 先取出来
NSMutableArray* data = [NSMuatbleArray arrayWithContentsOfFile:@”/user/apple/desktop/names.plist”]; [data addOject:@"jim"]; [data writeToFile:@”/user/apple/desktop/names.plist” atomically:YES];
//缺点:每次把旧数据读出来,新数据添加,才能存储
// 检索数据方面
SQLite3 :数据库 轻款的,这个用户的数据。只用在移动端
大批量数据缓存 用sqlite3
Core Data: 数据库,基于SQLite3, OC版本,
关系型数据库:
PC端:Oracle MySQL SQL Server Access DB2 Sybase
嵌入式\移动客户端:SQLite
表table
4个字段或者4列 column 或者叫属性
2条记录\行\row\record
商品名称 商品价格 商品的库存树 商品的日期
水杯 10 200 2011/9/9
枕头 200
数据库存储数据的步骤:
新建一张表
添加多个字段
添加多行记录
数据库软件:Navicat\PLSQL
Fields属性
表名以t开头t_shop
字段名称 字段类型
name text
price real 浮点型
left_count integer
增删改查 CRUD DDL
SQL语句 不区分大小写,每天语句以分号结尾
create drop insert update delete select
where order by group by having
创表:[create table 表名(字段1 字段类型1,字段2 字段类型2)]
CREATE TABLE IF NOT EXISTS t_student (name text,age integer);
删表:drop table if exists 表名
DML
插入数据 :数据库的字符串用单引号而不是双引号
insert into 表名(name,price) values(‘手机’,2000.1);
更新数据:update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值
删除数据:delete * from 表名;
条件语句的常见格式
where 字段 = 某个值;// 不能用两个 =
// 检索数据方面
NSMutableString* sql = [NSMutableString string]; for(int i = 0; i< 1000;i++){ NSString* name = [NSString stringWithFormat:@"iPhone%d",i]; double price = arc4random()%10000 + 100 ; int leftCount = arc4random()%10000 ; [sql appendFormat:@"insert into t_shop(name,price,left_count) values ('%@', %f,%d);\n",name, price,leftCount]; } NSLog(@"\n%@",sql); [sql writeToFile:@"/Users/apple/Desktop/shops.sql" atomically:YES encoding:NSUTF8StringEncoding error:nil];
当价格等于1000元的库存等号用一个=
update t_shop set left_count = 0 where price = 1000
delete from t_shop where left_count = 0;
删除库存《200且 价格低于2000的手机 不是用&& 而是用and 或 不是用||而是用or
delete from t_shop where left_count < 500 AND price < 2000
条件语句的常见格式
where字段 = 某个值;//不能用两个=
where字段 is 某个值;//is相当于=
where 字段 != 某个值;
where 字段 is not 某个值;// is not 相等于 !=
where 字段1 = 某个值 and 字段2 > 某个值;// and相当于C语言中的&&
where 字段1 = 某个值 or 字段2 = 某个值;// or相当于C语言中的||
练习
将t_student表中年龄大于10并且姓名不等于jack的记录,年龄都改为5
update t_student set age = 5 where age > 10 and name != ‘jack’;
删除t_student表中年龄小于等于10或者年龄大于30的记录
delete from t_student where age <= 10 or age > 30;
update t_student set score = age where name = ‘jack’;
将t_student表中名字等于jack的记录,score字段的值都改为age字段的值
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 name shop_name, price As shop_price,left_count FROM t_shop;
// 其中As可以省略,shop_name 和shop_price分别是name和price的别名。
// 查询表中有多少条东西用count()这个函数
select count(name) from t_shop;// count()是函数的意思
select count(*) from t_shop where left_count > 800;
排序:
select * from t_shop ORDER BY left_count;// 默认升序
select * from t_shop ORDER BY left_count DESC/ASC;
// 先按库存排再按照价格排 谁写到前面,谁的优先级高
select * from t_shop ORDER BY left_count DESC,price ASC;
limit使用limit可以精确地查询结果的数量,比如每次只查询10条数据
格式:select* from 表名 limit 数值1,数值2;//数值1决定从哪里开始查,数值2决定查出来的是多少条
示例
select * from t_student limit 4,8;
// 可以理解为 :跳过最前面的4条语句(0,1,2,3)从4条开始,然后取8条记录
//取出价格最高的10条
select * from t_shop order by price DESC limit 0,10;
//第2页的最高10条
select * from t_shop order by price DESC limit 10,10;
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;
简单约束 (尽力添加约束,避免脏数据)
建表时可以给特定的字段设置一些约束条件,常见的约束有:
not null;规定字段的值不能为null
unique:规定字段的值必须唯一
default:制定字段的默认值。
约束都是在创建表的时候指定的:
create table if not exists t_student(name,txt,age integer default 1);
主键:(Primary Key,简称PK)用来唯一标识某一条记录
例如:t_student可以增加一个id字段作为主键,相等于人的身份证
主键可以是一个字段或多个字段。
create table if not exists t_student(id integer PRIMARY KEY autoincrement,name text, age integer)
外键约束: t_student t_class id name age class id name 1 jack 20 2 1 android 2 rose 23 1 2 iOS 3 kate 18 2 4 jim 25 1 android rose jim iOS jack kate b182 t_class_student描述班级和学生的关系 id class student 1 1 2 2 1 4 3 2 1 4 2 3
班级和学生的关系是1对多 外键加在多的那端。
利用外键约束可以用来建立表与表之间的联系
外键一般情况下是:一张表的某个字段,引用着另一张表的主键字段
// 班级表
;
select * from t_food where food_type_id = select id from t_food_type where name = ‘粤菜’;
select f.name, ft.name from t_food f ,t_food_type ft where f.food_type_id = ft.id and ft.name = ‘粤菜’;
// 查询安卓班的学生
select * from t_student t where class = select id from t_class where name = android;
select s.name sname,c.name cname from t_student s,t_class c where s.class = c.id and c.name = ‘android’;
相关文章推荐
- 数据库基本知识梳理
- 【转】数据库基本知识:(十)数据操作 · 查 · (三)使用子查询访问和修改数据
- MySQL_知识总结01(基本数据库语句+关于Mysql面试题+select语句的练习)
- 有关数据库的一些基本知识
- 数据库的一些基本知识(1)------数据库引擎
- 【转】数据库基本知识:(七)函数和表达式的使用
- 数据库基本知识
- 数据库知识基本概念
- 【转】数据库基本知识:(八)数据操作 · 查 · (一)常规型
- 【黑马程序员】数据库基本知识
- 数据库基本知识
- 数据库的基本知识
- NOSQL数据库基本知识(二)
- 数据库的基本知识
- 数据库的基本知识之基本select语句、比较运算和多表查询
- oracel数据库基本知识和基本查询方法
- 数据库的字段基本知识
- 数据库基本知识篇二:关系数据库那些事儿
- 数据库开发基本操作-数据库基础知识
- 3.22数据库基础知识:①数据类型②基本规则③数据库基本操作,增删改库,表