1、
1.一个SQL数据库是表(Table)的集合,它由一个或多个SQL模式定义。
2.一个SQL表由行集构成,一行是列的序列(集合),每列与行对应一个
数据项。
3.一个表或者是一个基本表或者是一个视图。基本表是实际存储在数据库的表,而视图是由若干基本表或其他视图构成的表的定义。
4.一个基本表可以跨一个或多个存储文件,一个存储文件也可存放一个或多个基本表。每个存储文件与外部存储上一个物理文件对应。
5.用户可以用SQL语句对视图和基本表进行查询等操作。在用户角度来看,视图和基本表是一样的,没有区别,都是关系(表格)。
6.SQL用户可以是应用程序,也可以是终端用户。SQL语句可嵌入在
宿主语言的程序中使用,宿主语言有FORTRAN,COBOL,PASCAL,PL/I,C和Ada语言等。SQL用户也能作为独立的
用户接口,供交互环境下的终端用户使用。
primary key 唯一且非空。
Autoinc 自动增长
建表
Create Table "main"."Student"
SQL:
CREATE TABLE "main"."Student"
("stuID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "stuName" VARCHAR(20), "stupswd" VARCHAR(20))
添加
INSERT INTO "main"."Student" ("stuName","stupswd") VALUES (?1,?2)
Parameters:
param 1 (text): tian
param 2 (text): asdfggh
删除
Delete 1 records from Student where stuid=1
改
UPDATE "main"."Student"
SET "stuName" = ?1 WHERE "stuID" = 1
Parameters:
param 1 (text): tian
--创建数据库
create database ios
--使用数据库
use ios
--创建数据表
create table student
(
stuid int primary key autoincrement,
stuname varchar(20),
stupwd varchar(20)
)
--查询所有信息
select * from student
--增加信息
insert into student(stuname,stupwd)
values ('lisi','123456')
--修改
update student set stuname = '李四'
where stuid = 1
update student set stuname = '李四',stupwd='Sd123'
where stuid = 1
--删除
delete from student where stuid=1
返回信息 对表信息的反馈
创建数据表
create table class
(
cid integer primary key autoincrement ,
cname varchar(20)
)
查询语句
select * from class
增加语句
insert into class (cname) values('IOS实训班')
修改语句
update class set cname='物联网' where cid=2
删除语句
delete from class where cid=2
查询表中的记录数
select count(*) from class
select count(*) from class where cname='数据库'
select * from class where cname='数据库'
select cid from class where cname='数据库'
select cid,cname from class where cname='数据库'
select cid,cname from class where cid>=5 and cid<=9
select cid,cname from class where cid between 5 and 9
select cid,cname from class where cid in(5,6,7,8,9)
降序排列
select * from class order by cid desc
升序排列
select * from class order by cid asc
select * from class where cname='数据库' order by cid desc
select * from class where cname like '%库' order by cid desc
select * from class where cname like '数_库' order by cid desc
分组
select count(*) from class group by cname
select count(*) count from class group by cname order by count desc
外键
create table class
(
cid integer primary key autoincrement,
cname varchar(20),
schid int ,
foreign key(schid) references sch(schid)
)
主外键
create table class
(
cid integer primary key autoincrement,
cname varchar(20)
)
create table student
(
stuid integer primary key autoincrement,
stuname varchar(20),
stupwd varchar(20),
stuaddr varchar(200),
cid int ,
foreign key (cid) references class(cid)
)
增加
insert into student(stuname,stupwd,stuaddr,cid)values('zhangsan','123','aaa',1)
联合查询
select * from class,student where class.cid=student.cid
左外连接
select s.stuid,s.stuname,s.stupwd,s.stuaddr,c.cid
from class c,student s where c.cid=s.cid
select s.stuid,s.stuname,s.stupwd,s.stuaddr,c.cid
from class c left join student s on c.cid=s.cid
交叉连接
select s.stuid,s.stuname,s.stupwd,s.stuaddr,c.cid
from student s cross join class c
创建视图
create view v_class_student
as
select s.stuid,s.stuname,s.stupwd,s.stuaddr,c.cid
from student s left join class c on s.cid=c.cid
查询视图
select * from v_class_student where stuid=1
登录查询语句
select count( *) from student where stuname='zhangsan' and stupwd='123456'
select stupwd from student where stuid=1 and stuname='zhangsan'
三表联合查询
select stu.stuname,c.cname,s.sname from school s left join class c on s.sid=c.sid left join student stu on stu.cid=c.cid
select * from student stu,class c,school s
where stu.cid=c.cid and s.sid=c.sid
select stu.stuname,c.cname,s.sname from student stu,class c,school s
where stu.cid=c.cid and s.sid=c.sid
select stu.stuname,c.cname,s.sname from student stu left join class c
on stu.cid=c.cid left join school s on s.sid=c.sid
select * from student where cid in
(select cid from class where cname='php')
select * from student where cid in
(select cid from class where sid in
(select sid from school where sname='myschool'))
update student set stuname='qq' ,stuaddr='bj'
,stupwd='123456789' where stuid=1
删除数据表
drop table student
先删除子表 再删除 主表
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [self login];
// [self Add];
[self showAll];
}
//显示数据表中的所有信息
-(void)showAll
{
NSLog(@"显示数据");
// 数据库
sqlite3 *db;
// 数据库文件存储路径
NSString *path =[[NSBundle mainBundle] pathForResource:@"sasa" ofType:@"sqlite"];
// 根据指定的数据库文件路径 打开数据库
int result = sqlite3_open([path UTF8String], &db);
// 创建执行命令对象
sqlite3_stmt *stmt;
// 打开数据库成功
if (result==SQLITE_OK) {
NSLog(@"连接成功");
// 执行预处理命令
int rst=sqlite3_prepare_v2(db, "select * from student", -1, &stmt, nil);
if (rst==SQLITE_OK) {
// 循环遍历数据表中整型列的信息
while (sqlite3_step(stmt)==SQLITE_ROW) {
int stuid=sqlite3_column_int(stmt, 0);
NSLog(@"stuid is %d",stuid);
// 获取数据表中 字符型的列信息
NSLog(@"%@",[NSString stringWithFormat:@"%s",sqlite3_column_text(stmt, 1)]);
NSLog(@"%@",[NSString stringWithFormat:@"%s",sqlite3_column_text(stmt, 2)]);
}
}
}
}
-(void)login
{
sqlite3 *db;
sqlite3_stmt *stmt;
sqlite3_open([[[NSBundle mainBundle]pathForResource:@"sasa" ofType:@"sqlite"]UTF8String],&db );
// 通过 ? 充当站位符
int rst =sqlite3_prepare_v2(db, "select * from student where stuname=? and stupwd =? ", -1, &stmt, nil);
// 绑定数据信息 注意 : 1 表示的是 ? 的位置
sqlite3_bind_text(stmt, 1, "lisi", -1, nil);
sqlite3_bind_text(stmt, 2, "1234", -1, nil);
if (rst==SQLITE_OK) {
if (SQLITE_ROW==sqlite3_step(stmt)) {
NSLog(@"login ok");
}
else
{
NSLog(@"sorry");
}
}
}
-(void)Add
{
sqlite3 *db;
sqlite3_stmt *stmt;
sqlite3_open([[[NSBundle mainBundle]pathForResource:@"sasa" ofType:@"sqlite"]UTF8String],&db );
int rst=sqlite3_prepare_v2(db, "insert into student (stuname,stupwd)values(?,?) ", -1, &stmt, nil);
sqlite3_bind_text(stmt, 1, "lisi", -1, nil);
sqlite3_bind_text(stmt, 2 , "lisi", -1, nil);
if (rst==SQLITE_OK) {
// 判断是否增加成功
if (SQLITE_DONE==sqlite3_step(stmt))
{
NSLog(@"ADD ok");
}
else
{
NSLog(@"add sorry");
}
}
}