首页 > 数据库 >sql 记录

sql 记录

时间:2023-02-13 20:39:38浏览次数:39  
标签:name 记录 teacher course sql where id select


--查询‘张老师’教师,代课总课时的sql语句。
select SUM(courseHour)
from teacher t ,Timetable t2 ,course c
where t.id = t2.teacher and c.id=t2.course and t.name like N'迪%'

select * from timetable_view

create view timetable_view(教师姓名,教师编号,课程编号,课程名,课时)
AS
select t.name,t.id,c.id,c.name,t2.courseHour
from teacher t ,course c ,Timetable t2
where t2.course = c.id and t2.teacher = t.id

--查询课有一门或以上课程的课时超过80的教师的id和name
--1
select t.id,t.name
from teacher t ,Timetable t2
where t.id = t2.teacher
group by t.id,t.name
having sum(iif(t2.courseHour > 80,1,0)) >= 1
--2 仅当条件为一或者更多一下语句可以实现
select distinct t.id,t.name
from teacher t ,Timetable t2
where t.id = t2.teacher and t2.courseHour > 80

select s.name,t.name,c.name,sc.grade ,tt.courseHour
from SelectCourse sc ,teacher t ,student s ,Timetable tt,course c
where tt.teacher = t.id
and tt.course = c.id
and sc.course = c.id
and s.id = sc.student
and tt.courseHour > 80
order by s.name


insert into SelectCourse(student,course,grade)
values ((select id from student where name = N'七七'),(select id from course where name = N'社会安全'),20),
((select id from student where name = N'甘雨'),(select id from course where name = N'社会安全'),100),
((select id from student where name = N'尤拉'),(select id from course where name = N'社会安全'),80),
((select id from student where name = N'甘雨'),(select id from course where name = N'格斗'),60),
((select id from student where name = N'尤拉'),(select id from course where name = N'历史'),80),
((select id from student where name = N'甘雨'),(select id from course where name = N'历史'),80),
((select id from student where name = N'工子'),(select id from course where name = N'格斗'),100),
((select id from student where name = N'甘雨'),(select id from course where name = N'经济'),80),
((select id from student where name = N'工子'),(select id from course where name = N'经济'),10),
((select id from student where name = N'可莉'),(select id from course where name = N'茶艺'),20),
((select id from student where name = N'七七'),(select id from course where name = N'茶艺'),20)

((select id from student where name = N'可莉'),(select id from course where name = N'社会安全'),30)

alter table SelectCourse
add student uniqueidentifier references student(id)

insert into Timetable (course,teacher,courseHour)
values((select id from course where name = N'社会安全'),(select id from teacher where name = N'琴'),50),
((select id from course where name = N'格斗'),(select id from teacher where name = N'迪卢克'),200),
((select id from course where name = N'调酒'),(select id from teacher where name = N'迪卢克'),50),
((select id from course where name = N'历史'),(select id from teacher where name = N'钟离'),200),
((select id from course where name = N'茶艺'),(select id from teacher where name = N'钟离'),150),
((select id from course where name = N'经济'),(select id from teacher where name = N'凝光'),150)

alter table course
drop column teacher

alter table course
drop constraint FK__course__teacher__32E0915F

insert into course(name,teacher)
values(N'社会安全',(select id from teacher where name = N'琴')),
(N'调酒',(select id from teacher where name = N'迪卢克')),
(N'格斗',(select id from teacher where name = N'迪卢克')),
(N'历史',(select id from teacher where name = N'钟离')),
(N'茶艺',(select id from teacher where name = N'钟离')),
(N'经济',(select id from teacher where name = N'凝光'))

insert into teacher(name)
values(N'琴'),(N'迪卢克'),(N'钟离'),(N'凝光')

insert into student (name)
values(N'可莉'),(N'七七'),(N'工子'),(N'甘雨'),(N'尤拉')


sp_rename 'Timetable.keshi','courseHour','column'
sp_rename 'CourseHour','Timetable'

create table CourseHour(
course uniqueidentifier references course(id),
teacher uniqueidentifier references teacher(id),
keshi int NOT NULL
)


create table SelectCourse (
id uniqueidentifier primary key default newid(),
course uniqueidentifier references course(id),
grade int NULL
)

create table course(
id uniqueidentifier primary key default newid(),
name nvarchar(10) not null,
teacher uniqueidentifier references teacher(id)
)

sp_rename 'tercher','teacher'

create table tercher (
id uniqueidentifier primary key default newid(),
name nvarchar(10) not null
)

alter table student
add constraint id_def_val default (newid()) for id

alter table student
add name nvarchar(10) not null

sp_rename 'aa','student'

create table aa(
id uniqueidentifier primary key
)


标签:name,记录,teacher,course,sql,where,id,select
From: https://blog.51cto.com/u_15964288/6054676

相关文章

  • node mysql 增删改查 demo
    前端原生jsJquery后端Node数据库MySQL​​​http://hongbin.xyz:8080/​​​​github仓库​​​​演示视频地址​​node增删改查TODO:删除用户数据库需要触发器将......
  • 记录一下2023.02.13
        今天是开学第一天,下午就进行了javaweb的测试,考试内容跟期末考试的差不多,都是实现增删改查,连接多个数据库,实现多个用户的操作。JDBC.Toolspackageutil;im......
  • 关于我忘记MySQL root 密码那件事
    登录MySQL时,忘记密码,百度一番找到合适的方法问题已解决,记录一下。该篇文章仅做学习用,方便自己下次查找。1、先把MySQL的服务为停了,可以直接按win键搜服务,找到MySQL服务,......
  • 记录--数组去重的五种方法
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助前言您或许会疑惑,网上那么多去重方法,这篇文章还有什么意义?别着急,这篇文章只节选了简单的,好玩的,古老的,有......
  • mysql查询语句case及时间操作
    1、获取系统当前时间戳SELECT unix_timestamp(now())*1000 --乘以1000,为了得到毫秒时间戳   2、时间戳转年月日SELECTFROM_UNIXTIME(1676280033000/1000,......
  • sql中 exists的用法
    现有:班级表(A_CLASS) 学生表(STUDENT)注:学生表(STUDENT)的classId关联班级表(A_CLASS)的主键ID  代码:select*fromSTUDENTsWHEREexists(select1fromA_ClA......
  • mysql怎么删除重复数据并且保留其中一条
    现在有张加班表,一个人同一天只允许申请一次加班,就是加班日期和userid相同的数据应该只有一条,但是现在由于之前没有做限制,导致很多数据重复怎么通过sql删掉重复数据思路:......
  • 自定义注解记录日志
    1.自定义注解2.切面类3.具体方法3.具体方法二4.具体方法  5.具体方法三6.具体方法7 ......
  • SQL执行慢的原因分析以及调优手段
    目录开发规范※谨慎使用MySQL分区表※经常一起使用的列放到一个表中※禁止在数据库中存储文件(比如图片)这类大的二进制数据所有表必须使用InnoDB存储引擎数据库和表的字......
  • 更全面的记录缺陷,你需要了解这些
     很多朋友在软件测试这条路上,都具有很强的业务逻辑分析能力,甚至具有多门语言的编码能力,认为bug找到了,开发也确诊了,记录bug就不那么重要了,可事实是真的如此么?你是否:在测......