您的位置:首页 > 运维架构 > Linux

【技术支持】linux(Ubuntu)下sqlite(数据)基础入门教程与练习

2012-05-19 19:42 961 查看



/*  此篇文章的是当时自己再老师上课之时自己的摘要

/*  通过自己学习之后的一些总结与整理

/*  适合初学的我们一起思考

Ubuntu安装sqlite,很简单的,网上搜下

打开sqlite的命令如下,进入一个常用的用户目录下的文件夹,因为之后会在

相应的文件下新建.sql的文件,以防.read +.sql文件会读取不到,我这里就以



建一个数据库的.db的文件



创建一个table,就像C语言的结构体一样,或者函数



给Student插入相关的数据

INSERT INTO Student(No,Name,Age,Class) VALUES('010','fox',21,'010');

查看数据库Student的情况是用select (参数)from Student,懂英语的这两句什么意思是小菜啦,贵在理解命令



刚查看出来的数据库不怎么漂亮或者相应的属性没有对齐

如果没有No|Name|Age|Class的加上一句命令“.header on”就可以了

接着是属性对齐方式的实现填写命令“.mode column”,,在select一下,看下面的效果就知道



下面是添加属性对应属性的集中情况,希望自己揣摩,一般的话添加数据命令越简单越好



修改数据库中的某个学生的class,用update,注意这里的where,这是条件的限制

修改其他的也一样的



删除是delect from Class where No=‘’这个指令是不太会用的,最好是禁用

接下来是另一部分:在自己选择的当前目录下新建一个Class.sql文件,文件中的内容为

create table Class(No,Monitor,Teacher,Adress);

.header on

.mode column

insert into Student values(‘010’,’010’,’1’,’sadcard card’);

insert into Student values(‘011’,’011’,’2’,’no news’);

这边就随便写点东西了

接下来在终端中运行

sqlite

.read Class.sql

.table

结果如下



如果要是数据库只显示几个属性的内容

在select * from Student中吧*改成相应的属性就可以了



条件语句(where)这个会在之后多处用到,可以试下下面的语句,看下效果,思考一番就知道什么用了



模糊查找 (like ‘li%’)



要是只想显示一个table中的前两个属性的信息只要加上limit限制就可以了(对了在数据库中关键词的大小写的作用是一样的)



类似:sqlite> SELECT * FROM Student WHERE Age<24 LIMIT 2;

接下来是排序,这个很好理解

desc是降序;asc是升序





类似输出指定班级的平均年龄语句:

sqlite> SELECT AVG(Age) FROM Student WHERE Class=‘012’;

搜索年龄最大、最小的语句:

sqlite> SELECT MAX(Age)FROM Student;

sqlite> SELECT MAX(Age) AS maxAge FROM Student;

分组统计:



下面是将数据库中内容输出打印到一个文件中,而不是打印到屏幕中

.ouput 5.txt是将内容打印带5.txt文件中,还要用select命令将其打印进去,不运行select命令他是不会自动输入的





子查询:查询2的班级的所有学生



类似语句:

SELECT * FROM Student JOIN Class on Student.Class=Class.NO;

JOIN 是联合查询,懂?

怎样生成创建数据库所有的命令;

看如下



好了他会生成你创建数据库的所有命令,是创建哦!!像什么select那些当然不是



之后他执行的命令所显示的东西都会显示在这个文件中



如果要重新显示到屏幕中话就应该加上.output stdout命令,这是标准的屏幕输出

创建视图

CREATE VIEW viStudent as select * from Student;

之后里面的数据时修改不了的,比如UPDATE





接下来就是练习部分

一、设有一数据库,包括四个表:学生表(Student)、课程表

(Course)、成绩表(Score)以及教师信息表(Teacher)。

四个表的结构分别如表1-1 的表(一)~表(四)所示,数据如

表1-2 的表(一)~表(四)所示。用SQL 语句创建四个表并完

成相关题目。















1、查询Student 表中的所有记录的Sname、Ssex 和Class 列。

select Sname,Ssex,Class from Student;

2、查询教师所有的单位即不重复的Depart 列。

select * from Teacher group by Depart;

3、查询Student 表的所有记录。

select *from Student;

4、查询Score 表中成绩在60 到80 之间的所有记录。

select *from Score where Score.Degree>='60' and Score.Degree<='80';

5、查询Score 表中成绩为85,86 或88 的记录。

select * from Score where Score.Degree='85' or Score.Degree='86' or Score.Degree='88';

6、查询Student 表中“95031”班或性别为“girl”的同学记录。

select *from Student where Student.Class='95031' or Student.Ssex='girl';

7、以Class 降序查询Student 表的所有记录。

select *from Student order by Class desc;

8、以Cno 升序、Degree 降序查询Score 表的所有记录。

select *from Score order by Cno asc;

select *from Score order by Degree desc;

9、查询“95031”班的学生人数。

select count(*) from Student where Student.Class='95031';

10、查询Score 表中的最高分的学生学号和课程号。(子查询或者

排序)

select *from Score where Degree=(select max(Degree) from Score);

或者select *from Score order by Degree desc limit 1;

11、查询‘105’号课程的平均分。

select avg(Degree) as avgDegree from Score where Cno='105';

12、查询Score 表中至少有5 名学生选修的并以1 开头的课程的平

均分数。

select Cno,avg(Degree) from Score where Cno like '1%' group by Cno having count(*)>5;

13、查询最低分大于70,最高分小于90 的Sno 列。

select Sno,Degree from Score group by Sno having max(Degree)<90 and min(Degree)>70;

14、查询所有学生的Sname、Cno 和Degree 列。

select Student.Sname,Score.Cno,Score.Degree from Student,Score where Student.Sno=Score.Sno;

15、查询所有学生的Sno、Cname 和Degree 列。

select Student.Sno,Course.Cname,Score.Degree from Student,Course,Score where Student.Sno=Score.Sno
and Score.Cno=Course.Cno;

16、查询所有学生的Sname、Cname 和Degree 列。

select Student.Sname,Course.Cname,Score.Degree from Student,Course,Score where Student.Sno=Score.Sno
and Score.Cno=Course.Cno;

17、查询“95033”班所选课程的平均分。

select avg(Degree) as avgDegree from Score where Sno=(select Sno from Student where
Class='95033');

18、查询选修“105”课程的成绩高于“109”号同学成绩的所有

同学的记录。

select Cno,Degree from Score where Cno='105' and Degree>(select Degree from Score where
Sno='109');

19、查询score 中选学多门课程的同学中分数为非最高分成绩的记

录。

select *from Score a where a.Sno in(select Sno from Score group by Sno having count(*)>=2)and
a.Degree <(select max(b.Degree)from Score a,Score b where b.Cno=a.Cno);

20、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所

有记录。

select * from Score where Degree>(select max(Degree) from Score where Sno='109') and
Cno<='105' and Cno>='3';

21、查询和学号为108 的同学同年出生的所有学生的Sno、Sname

和Sbirthday 列。

select Sno,Sname,Sbirthday from Student where strftime('%Y',Sbirthday)=(select strftime('%Y',Sbirthday)
from Student where Sno='108');

22、查询“zhangxu“教师任课的学生成绩。

select *from Score where Cno=(select x.Cno from Course x,teacher y where x.Tno=y.Tno);

23、查询选修某课程的同学人数多于5 人的教师姓名。

select Tname from Teacher where Tno in(select x.Tno from Course x,Score y where x.Cno=y.Cno
group by y.Cno having count(y.Cno)>5);

24、查询95033 班和95031 班全体学生的记录。

sqlite> select *from Student where Class in('95033','95031');

25、查询存在有85 分以上成绩的课程Cno.

select Cno from Score where Degree>=85 group by Cno

26、查询出“CS“教师所教课程的成绩表。

select *from Score where Cno in(select x.Cno from Course x,Teacher y where x.Tno=y.Tno
and y.Depart='CS');

27、查询“CS”与“EE“不同职称的教师的Tname 和Prof。

select Tname,Prof from Teacher where Depart='CS' and Prof not in(select Prof from Teacher
where Depart='EE');

28、查询选修编号为“105“课程且成绩至少高于选修编号为

“245”的同学的Cno、Sno 和Degree,并按Degree 从高到低次

序排序。

select *from Score where Cno='105' and Degree>(select Degree from Score where Cno='245'
order by Degree desc);

29、查询选修编号为“105”且成绩高于选修编号为“245”课程

的同学的Cno、Sno 和Degree.

select *from Score where Cno='105' and Degree>(select Degree from Score where Cno='245');

30、查询所有教师和同学的name、sex 和birthday.

select Sname,Ssex,Sbirthday from Student union select Tname,Tsex,Tbirthday from Teacher;

31、查询所有“gir”教师和“girl”同学的name、sex 和birthday.

select Sname,Ssex,Sbirthday from Student where Ssex='girl' union select Tname,Tsex,Tbirthday
from Teacher where Tsex='girl';
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: