美文网首页
Sqlite3约束

Sqlite3约束

作者: 动感新势力fan | 来源:发表于2016-04-21 15:08 被阅读82次

DDL:

create table if not exists t_class (id integer primary key autoincrement,name text);

drop table t_class;

DML:

insert into t_student (name,age) values ('chenrong',18);

delete from t_class where id=1;

update t_class set name = 'rachel' where id=2;
update t_class set name = 'rachel' , age = '18' where id=2;

SQL:

select * FROM t_student;

select * from t_student order by age desc ; //降序
select * from t_student order by age asc ; // 升序(默认)

imit常用来做分页查询,比如每页固定显示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条记录

普通约束 , 主键约束:

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_class (id integer primary key autoincrement,name text);

学生表:

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字段

表连接查询:

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

1:

select s.name sName,c.name cName from t_student s,t_class c;
会把所有结果查出来

2正确的察法:

select s.name sName,c.name cName from t_student s,t_class c where s.class_id = c.id;

多表查询

***连接不需要外键约束,这不是必要条件 ***

****left join(表名) on 条件等式

select * from t_person p left join t_book b on p.book_id= b.id


****代码意义

左连接,把t_person表跟t_book表联系起来,而查询的结果的条件是t_person.book_id= t_book.id


****关于Left Join跟Join的选择:
left join

如果要查询左边表中的所有符合条件的数据,使用left jion
通常查询出来的结果会多,因为右边表不存在的记录,同样可能会被查询出来,查询出来之后,右边表不存在的记录,全部为NULL

<br />join

如果要两个表中同时存在的符合条件的数据,使用jion
通常查询出来的结果会比左连接少,因为右边表不存在的记录,不会显示出来


相关文章

  • Sqlite3约束

    DDL: create table if not exists t_class (id integer prima...

  • sqlite3 简介

    初次接触sqlite3,她就给了我一种小巧,轻便,易上手的感觉。没有那么多的约束,也没有那么让人琢磨不透。 大致语...

  • 使用sqlite3存储奥斯卡金像奖提名信息

    SQLite3 可使用 sqlite3 模块与 Python 进行集成。sqlite3 模块是由 Gerhard ...

  • sqlite的用法

    SQLite3 可使用 sqlite3 模块与 Python 进行集成。sqlite3 模块是由 Gerhard ...

  • SQLite3深入浅出

    文章目录: sqlite3 基础语句 sqlite3 API sqlite3 线程安全 FMDB 基础语句: 创建...

  • sqlite3简单命令行

    sqlite3简单命令行1、输入" sqlite3 + 数据库名.db " (如: " sqlite3 Book...

  • Python数据分析基础----第二十二天

    数据库 Python内置的sqlite3模块 import sqlite3 创建sqlite3内存数据库 创建带有...

  • sqlite3

    来源sqlite3 进入sqlite3数据库命令行 .exit/.quit 退出sqlite3命令行 sqlite...

  • sqlite3 与 pandas

    对于一个 sqlite3 的小白,通过 sqlite3 的表头使用 pandas 来获取 sqlite3 的数据内...

  • sqlite操作简介

    进入sqlite3数据库命令: sqlite3 退出sqlite3命令行: .exit/.quit 创建一个新的数...

网友评论

      本文标题:Sqlite3约束

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