--创建classinfo 表
create table classinfo
(
cid integer primary key AUTOINCREMENT,
cname text(20)
);
select * from classinfo;
--添加数据
insert into classinfo values
(null,'python3班'),
(null,'python4班'),
(null,'python5班');
---创建学生表
--drop table stuinfo 删除表结构和数据
create table stuinfo
(
sid integer primary key AUTOINCREMENT,--学生id
sno text(10),--学生编号 py05001
sname text(20),--学生姓名
sage integer,--学生年龄
sgender text(10),--学生性别
score integer,--学生成绩
cid integer ,--班级id
foreign key(cid) REFERENCES classinfo(cid)--外键
);
select * from stuinfo;
select * from classinfo;
--delete from stuinfo 删除所有数据
insert into stuinfo values
(null,'py5001','蔡文姬',14,'女',90,1);
--插入李白 到python 4班,插入赵云到python 5班
insert into stuinfo values
(null,'py4001','李白',14,'男',94,2),
(null,'py5002','赵云',14,'男',96,3);
--- 一般数据库有三种删除方式:
--1.drop table 表名 删除表结构和数据
--2.truncate from 表名 在sqlite 是不起作用
--3.delete from 表名 删除数据,保留表结构 但是原来的id会有记忆
---解决删除之后,id从之前保留的数值之后插入,清零
--select * from sqlite_sequence;
--- 找到上面的 表名
delete from stuinfo--删除所有数据
update sqlite_sequence set seq=0 where name='stuinfo';--修改保存的id记忆,从0开始
select * from stuinfo;--查看所有学员信息
insert into stuinfo values(null,'py5003','蔡文姬1',14,'女',90,1);
insert into stuinfo values(null,'py5004','蔡文姬2',14,'女',90,1);
insert into stuinfo values(null,'py5005','蔡文姬3',14,'女',90,1);
insert into stuinfo values(null,'py5006','蔡文姬4',14,'女',90,1);
---修改 update 表名 set 字段 where 条件
update stuinfo set sno='py3001' where sname='蔡文姬'
update stuinfo set sno='py3002',sname='程咬金',sgender='男' where sid=4
select * from stuinfo;
--将蔡文姬3这一行的姓名修改成 狄仁杰,性别女,cid 修改成2
update stuinfo set sname='狄仁杰',sgender='女',cid=2 where sid=6
--将将蔡文姬4 de 学号改成py4002, 分数96
update stuinfo set sno='py4002',score=96 where sid=7
-- 查询 分数在90-94之间的
SELECT * FROM stuinfo where score>=90 and score<=94
--查询性别为男的学员信息
select* from stuinfo where sgender='男'
--统计性别为女的同学个数
select count(*) as 女同学的个数 from stuinfo where sgender='女'
---查询年龄14-16之间的个数有多少个
SELECT count(*) FROM stuinfo where sage>=14 and sage<=16
--修改cid为1的年龄修改为16
update stuinfo set sage=16 where cid=1
--修改 cid为2 的 性别都改为男,分数92
update stuinfo set sgender='男',score=92 where cid=2
--修改 性别为女的,年龄在12到14岁之间,分数+3分
update stuinfo set score=score+3 where sgender='女' and sage>=12 and sage<=14
--找到性别是男,cid为1 ,年龄在12-16之间的,名字后面加上!
UPDATE stuinfo set sname=sname+'!' where cid=1 and sage>=12 and sage<=16 and sgender='男'
---姓名改为 卡卡西 ,条件是分数90,cid为1的
UPDATE stuinfo set sname='卡卡西' where score=90 and cid=1
--将姓名为0的修改为小甜甜
UPDATE stuinfo set sname='小甜甜' where sname='0'
---将姓名为赵云的年龄改为13,性别 女,分数 91
UPDATE stuinfo set sage=13,sgender='女',score=91 where sname='赵云'
--查询姓名包含s的 like 匹配查找
select * from stuinfo where sname like '%s%'
--查询姓名第一个字是卡的
select * from stuinfo where sname like '卡%'
--查询姓名最后一个是s的
select * from stuinfo where sname like '%s'
--两个表联合查询
select sno,sname,sage,sgender,score,c.cname from stuinfo s,classinfo c
where s.cid =c.cid
select * from stuinfo
---修改名字为赵云,后面加上!
update stuinfo set sname=sname||'!' where cid=3
本文暂时没有评论,来添加一个吧(●'◡'●)