语法
1.创建数据库
sqlite3 DatabaseName.db
2.创建表
create table tablename(col_name type constraint, . . . );
如:create table student(stuNo varchar(20) primary key,----->主键约束
stuPwd varchar(20),
stuName varchar(20) not null,------>非空约束
gender varchar(2) check(gender='男'or gender='女'),------->检查约束
address varchar(18) default(‘北京’));------>默认约束
3.删除表
drop table tablename
如:drop table student;
4.修改表
4.1 添加一列
alter table student add column email text;
4.2 不能删除一列,sqlite不支持
5.添加数据
5.1 添加一条数据
insert into student (stuNo,stuPwd,stuName) values ('s111','123456','小明');
5.2 添加多条数据
insert into student (stuNo,stuPwd,stuName) values ('s113','123456','小明'),('s114','123456','小亮');
6.删除数据
6.1 删除表中所有数据
Delete from student;
6.2 删除id为111的学生
Delete from student where id=111;
7.更新
Update student set stuName='丽丽' where id='s111';
8.单表查询
8.1 查询所有列
Select * from student;
8.2 查询部分列
Select stuNo,stuPwd,stuName from student;
8.3 查询学号为's111'的学生信息
Select * from student where stuNo='s111';
8.4 查询姓名为'zhang'的学生(模糊查询like,like不支持中文)
Select * from student where stuName like 'zhang%';
8.5 对学生信息按照学号排序
Select * from student order by stuNo asc; 升序
Select * from student order by stuNo desc; 降序
8.6 分组 group by
1) 按照学生的所属地分组
Select address from student group by address;
2) 按照学生性别分组
Select gender from student group by gender;
8.7 筛选 having
Select address from student group by address having address='北京市';
8.8 去掉学生姓名相同的信息 (distinct去重关键字)
Select distinct stuName from student;