基本SQL语句

来源:互联网 发布:上海人工智能展览会 编辑:程序博客网 时间:2024/05/23 13:15

SQL语句

  • DDL 数据定义语句。包括 createdrop...
  • DML 数据操作语句。包括insertupdatedelete等操作。
  • DQL 数据查询语句。包括 select等。DDL常用关键字有where,order by, having

创表

格式:

  • create table 表名(字段名1 字段类型1字段名2 字段类型2...
  • create table if not exists 表名(字段名1 字段类型1 字段名2 字段类型2...

示例:

create table if not exists t_student(id integer, name text, age integer, score real);

字段类型

  • integer: 整型值
  • real: 浮点值
  • text: 文本字符串
  • blob: 二进制数据

实际上SQLite是无类型的

删表

格式:

  • drop table 表名;
  • drop table if exists 表名;

示例:

drop table if exists t_students;

插入数据

格式:

  • insert into 表名(字段1字段2, ...) values(字段1的值,字段2的值, ...);

示例:

insert into t_students(name, age) values('gy', 10);

注意 数据库中的字符串内容用单引号

删表

格式:

  • drop table 表名;
  • drop table if exists 表名;

示例:

drop table if exists t_students;

更新数据

格式:

  • update 表名 set 字段1 =字段1的值, 字段2 =字段2的值, ...);

示例:

update t_students set name = 'jack' age = 10;

注意 上面的示例会将t_students表中的所有记录的name都改为jack,所有年纪都改为10

删除数据

格式:

  • delete from 表名;

示例:

delete from t_students;

注意 上面的示例会将t_students表中的所有记录都删掉。

条件语句

如果只想更新或删除某些固定的记录,那就必须在DML上加一些条件。条件语句的常见格式:

  • where 字段 = 某个值;
  • where 字段 is 某个值;
  • where 字段 != 某个值;
  • where 字段 is not 某个值;
  • where 字段 > 某个值;
  • where 字段1 = 某个值 and where 字段2 = 某个值;
  • where 字段1 = 某个值 or where 字段2 = 某个值;

DQL语句

  • select 字段1 字段2 ... from 表名;
  • select * from 表名;

示例:

select name, age from t_students;

select * from t_students;

select * from t_students where age > 10;

起表名

格式(字段和表都可以起表名)

  • select 字段1 别名,字段2别名,... from 表名 别名;
  • select 字段1 别名,字段2 as别名,... from 表名 别名;
  • select 别名.字段1 别名.字段2 别名,... from 表名 别名;

示例:

select name myname, age myage from t_students;

select s.name, s.age from t_students s;

计算记录的数量

格式(字段和表都可以起表名)

  • select count(字段) from 表名;
  • select count(*) from 表名;

示例:

select count(age) from t_students;

select count(*) from t_students where score >= 60;

排序

查询的结果可以用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_students limit 4, 8;

可以理解为跳过前面4条语句,然后取8条记录

limit常用来做分页查询,比如每页固定显示5条数据,如:

  • 第一页:limit 0, 5
  • 第二页:limit 5, 5
  • 第三页:limit 10, 5
  • ...
  • n页:limit 5 * (n - 1), 5

简单约束

建表时可以给特定的字段设置一些约束条件,常见的约束有:

  • not null : 规定字段的值不恩哪个为null
  • unique : 规定字段的值必须唯一
  • default : 指定字段的默认值

建议 尽量给字段设定严格的约束,可以保证数据的规范性

示例:

  • create table t_student(id integer, name text not null unique, age integer not null default 1);

主键约束

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

主键用来唯一地标识某一条记录,主键可以是一个字段或多个字段。

主键的设计原则

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

主键的声明

在创表的时候用primary key声明一个主键

create table if not exists t_student(id integer primary key, name text, age integer);

主键字段主要声明为primary key, 其说明是一个主键字段,主键字段默认就包含了not null unique 两个约束。

如果想要主键自动增长(必须是integer类型),应该增加autoincrement

create table t_student(id integer primary key autoincrement, name text, age integer);

外键约束

利用外键约束可以用来建立表和表之间的联系。外键的一般情况是:一张表的某个字段,引用着另一张表的主键字段

新建一个外键:

create table t_student(id integer primary key autoincrement, name text, age integer, class_id integer, constraint fk_student_class foreign key(class_id) reference t_class(id))

0 0