数据库

Posted ice-k

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库相关的知识,希望对你有一定的参考价值。

数据库的增删改查:
查:show databases;
增:create database z_0222 charset utf8;
删:drop database z_0222;
改:alter database 数据库名 character set=新字符集 collate=新校对集;
操作表:进入数据库 use 库名;


表的增删改查:
查:show tables
增:drop table login;
create table login(
id int auto_increment primary key,
name varchar(10) comment ‘这是姓名‘,
age char(1) default ‘1‘,
foreign key (name) referendes user(name1)cr
);
删:drop table login(表名);
create table [if not exists] 表名(
字段列表, [约束或索引列表],
字段列表, [约束或索引列表],
索引
约束
) [表选项列表];
查看表结构:desc 表名;

表内增删改字段:
增:alter table 表名 add [column] 字段名 字段类型 字段属性;
删: alter table 表名 drop 字段名
改:alter table 表名 change 原字段名 新字段名 新字段类型 新字段属性;


数据的操作:
增:insert into login(表名) values();
删:delete from student where s_no=101 or s_no = ‘107‘;//where后面写条件。
改:updata student set s_sex = ‘10‘ where s_no = ‘107‘;//where后面写条件。
查:select * from student;

插入数据:insert into student values(‘108‘,‘曾华‘,‘男‘,‘1997-09-01‘,‘95033‘);

只插入几个数据(建立约束):
insert into student(s_no,s_name,s_sex) values(‘108‘,‘曾华‘,‘男‘,‘1997-09-01‘,‘95033‘);

批量插入数据:insert into student values
(‘108‘,‘曾华‘,‘男‘,‘1997-09-01‘,‘95033‘),
(‘108‘,‘曾华‘,‘男‘,‘1997-09-01‘,‘95033‘),
(‘108‘,‘曾华‘,‘男‘,‘1997-09-01‘,‘95033‘),
(‘108‘,‘曾华‘,‘男‘,‘1997-09-01‘,‘95033‘);
查询建表时候的SQL 语句:show create table (表名);

set character_set_client=gbk;
set character_set_results=gbk;

 ********************例题******************

创建表:

create table course (
c_no varchar(20) not null comment ‘课程号‘ primary key,
c_name varchar(20) not null comment ‘课程名称‘,
t_no varchar(20) not null comment‘教工编号‘
);
create table score (
id int not null comment ‘主键‘ auto_increment primary key,
s_no varchar(20) not null comment ‘学号‘,
c_no varchar(20) not null comment ‘课程号‘,
degree decimal comment‘成绩‘
);

create table teacher(
t_no varchar(20) not null comment ‘教工编号‘ primary key,
t_name varchar(20) not null comment ‘教工姓名‘,
t_sex varchar(20) not null comment ‘教工性别‘,
t_birthday datetime comment ‘教工出生年月‘,
p_rof varchar(20) comment ‘职称‘,
depart varchar(20) not null comment‘教工所在部门‘

);

create table student(
s_no varchar (20) not null comment ‘学号‘ primary key,
s_name varchar(20) not null comment ‘学生姓名‘,
s_sex varchar (20) not null comment ‘学生姓名‘,
s_birthday datetime comment ‘学生出生年月‘,
class varchar(20) comment ‘学生所在班级‘
);

查询联系:

1.查询Student表中的所有记录的Sname、Ssex和Class列
select sname,ssex,class from student

2.查询教师所有的单位即不重复的Depart列
select distinct depart from teacher

3.查询Student表的所有记录
select * from student

4.查询Score表中成绩在60到80之间的所有记录
select * from score where degree>60 and degree<80

5.查询Score表中成绩为85,86或88的记录
select * from score where degree=85 or degree=86 or degree=88

6.查询Student表中“95031”班或性别为“女”的同学记录
select * from student where class=‘95031‘ and ssex=‘女‘

7.以Class降序查询Student表的所有记录
select * from student order by class desc

8.以Cno升序、Degree降序查询Score表的所有记录
select * from score order by cno asc,degree desc

9.查询“95031”班的学生人数
select count(*) as ‘95031班人数‘ from student where class=‘95031‘

10.查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
select sno,cno from score order by degree desc limit 1

11.查询每门课的平均成绩
select avg(degree) from score group by cno

12.查询Score表中至少有5名学生选修的并以3开头的课程的平均分数
select avg(degree) from score where cno like ‘3%‘ group by cno having count(cno)>=5

13.查询分数大于70,小于90的Sno列
select sno from score where degree>70 and degree<90

14.查询所有学生的Sname、Cno和Degree列
select sname,cno,degree from student join score on student.sno=score.sno

15.查询所有学生的Sno、Cname和Degree列
select sno,cname,degree from score join course on course.cno=score.cno

16.查询所有学生的Sname、Cname和Degree列
select sname,cname,degree from student,score,course where
student.sno=score.sno and course.cno=score.cno

17.查询“95033”班学生的平均分
select avg(degree) from student join score on student.sno=score.sno where
class=‘95033‘

18.查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录
select * from score where
degree>(select degree from score where sno=‘109‘ and cno=‘3-105‘) and cno=‘3-105‘

19.查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录
select * from student,score,course where
student.sno=score.sno and score.cno=course.cno and
degree>(select degree from score where sno=‘109‘ and cno=‘3-105‘)

20.查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列
select sno,sname,year(sbirthday) from student where
sbirthday=(select sbirthday from student where sno=‘108‘)

21.查询“张旭“教师任课的学生成绩(姓名)
select sname,degree from student,score,course,teacher where
student.sno=score.sno and score.cno=course.cno and course.tno=teacher.tno
and tname=‘张旭‘

22.查询考计算机导论的学生成绩
select degree from course join score on course.cno=score.cno where cname=‘计算机导论‘

23、查询李诚老师教的课程名称
select cname from course join teacher on course.tno=teacher.tno where tname=‘李诚‘

24、教高等数学的老师是哪个系的
select depart from course join teacher on course.tno=teacher.tno where cname=‘高等数学‘

25、查询选修某课程的同学人数多于5人的教师姓名。
select tname from score,course,teacher where
score.cno=course.cno and course.tno=teacher.tno group by score.cno having count(*)>5

select tname from teacher where tno=(select tno from course where cno=(select cno from score group by cno having count(*)>5));
26、查询95033班和95031班全体学生的记录。
select * from student where class=‘95033‘ or class=‘95031‘

27、查询成绩表中存在有85分以上成绩的课程Cno.
select cno from score where degree>85

28、查询出“计算机系“教师所教课程的成绩表。
select degree from score,course,teacher where
score.cno=course.cno and course.tno=teacher.tno and depart=‘计算机系‘

29、查询所有教师和同学的name、sex和birthday
select sname,ssex,sbirthday from student
union
select tname,tsex,tbirthday from teacher

30.查询所有“女”教师和“女”同学的name、sex和birthday
select sname,ssex,sbirthday from student where ssex=‘女‘
union
select tname,tsex,tbirthday from teacher where tsex=‘女‘

31.查询所有成绩比3-105课程平均成绩低的同学的成绩表
select degree from score where degree<(select avg(degree) from score where cno=‘3-105‘)

32.查询所有任课教师的Tname和Depart
select tname,depart from teacher

33.查询所有未讲课的教师的Tname和Depart
select tname,depart from teacher

34.查询至少有2名男生的班号
select class from student where ssex=‘男‘ group by ssex having count(*)>=2

35.查询Student表中不姓“王”的同学记录
select * from student where sname not like ‘王%‘

36.查询Student表中每个学生的姓名和年龄
select sname as ‘姓名‘,year(now())-year(sbirthday) as ‘年龄‘ from student

37.查询Student表中最大和最小的Sbirthday日期值
select max(sbirthday) as ‘生日最大的‘,min(sbirthday) as ‘生日最小的‘ from student

38.以班号和年龄从大到小的顺序查询Student表中的全部记录
select * from student order by class desc,year(now())-year(sbirthday) desc

39.查询“男”教师及其所上的课程
select tname,cname,tsex from teacher as t join course as c on t.tno=c.tno where tsex=‘男‘

40.查询最高分同学的Sno、Cno和Degree列
select sno,cno,degree from score where degree=(select max(degree) from score)

41.查询和“李军”同性别的所有同学的Sname
select sname from student where ssex in(select ssex from student where sname=‘李军‘)

42.查询和“李军”同性别并同班的同学Sname
select sname from student where ssex in(select ssex from student where sname=‘李军‘) and
class in(select class from student where sname=‘李军‘)

43.查询所有选修“计算机导论”课程的“男”同学的成绩表
select degree from student,score,course where student.sno=score.sno and score.cno=course.cno
and course.cname=‘计算机导论‘ and student.ssex=‘男‘

以上是关于数据库的主要内容,如果未能解决你的问题,请参考以下文章

数据库类型——系统数据库/用户数据库

关系型数据库与文档型数据库的差别?

数据库数据库管理系统数据库系统

mysql数据库的基本操作:创建数据库查看数据库修改数据库删除数据库

如何清理mysql数据库缓存数据?

JDBC访问数据库的步骤