select
、insert
、update
、delete
、from
、create
、where
、desc
、order
、by
、group
、table
、alter
、view
、index
等等create table 表名 (字段名1 字段类型1, 字段名2 字段类型2, …) ;
create table if not exists 表名 (字段名1 字段类型1, 字段名2 字段类型2, …) ;
create table t_student (id integer, name text, age inetger, score real) ;
create table t_student(name, age);
drop table 表名 ;
drop table if exists 表名 ;
drop table t_student ;
insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ;
insert into t_student (name, age) values (‘mj’, 10) ;
注意:数据库中的字符串内容应该用单引号 ’ 括住
update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;
update t_student set name = ‘jack’, age = 20 ;
注意:上面的示例会将t_student表中所有记录的name都改为jack,age都改为20
delete from 表名 ;
delete from t_student ;
注意:上面的示例会将t_student表中所有记录都删掉
where 字段 = 某个值 ; // 不能用两个 =
where 字段 is 某个值 ; // is 相当于 =
where 字段 != 某个值 ;
where 字段 is not 某个值 ; // is not 相当于 !=
where 字段 > 某个值 ;
where 字段1 = 某个值 and 字段2 > 某个值 ; // and相当于C语言中的 &&
where 字段1 = 某个值 or 字段2 = 某个值 ; // or 相当于C语言中的 ||
select 字段1, 字段2, … from 表名;
select * from 表名; // 查询所有的字段
select name, age from t_student;
select * from t_student;
select * from t_student where age > 10; // 条件查询
select 字段1 别名 , 字段2 别名 , … from 表名 别名 ;
select 字段1 别名, 字段2 as 别名, … from 表名 as 别名 ;
select 别名.字段1, 别名.字段2, … from 表名 别名 ;
select name myname, age myage from t_student;
//给name起个叫做myname的别名,给age起个叫做myage的别名
select s.name, s.age from t_student s;
//给t_student表起个别名叫做s,利用s来引用表中的字段
select count (字段) from 表名 ;
select count ( * ) from 表名 ;
select count (age) from t_student ;
select count ( * ) from t_student where score >= 60;
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 ;
先按照年龄排序(升序),年龄相等就按照身高排序(降序)
select * from 表名 limit 数值1, 数值2 ;
select * from t_student limit 4, 8 ;
可以理解为:跳过最前面4条语句,然后取8条记录
第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条记录
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
字段作为主键,相当于人的身份证create table t_student (id integer primary key, name text, age integer) ;
integer类型的id作为t_student表的主键
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) references t_class (id));
t_student
表中有一个叫做fk_t_student_class_id_t_class_id
的外键 这个外键的作用是用t_student
表中的class_id
字段引用t_class
表的id
字段
查询03班的所有学生
select s.name,s.age from t_student s, t_class c where s.class_id = c.id and c.name = ‘03’;