데이터베이스 연습 문제 6- 데이터베이스 복잡한 쿼리 + 인덱스

1. 실험 목적
1. SELECT 문의 기본 구문과
질의 조건 의 표현 방법 습득 2. 질의 조건의 종류와 표현 방법 습득
3. 조인 질의
의 표현과 사용 습득 4. 중첩 질의의 표현과 사용
5 컬렉션 쿼리의 표현과 사용을 이해합니다.
6. 관리 인덱스 생성 사용법을 익히십시오.

(1) 'DB_'로 시작하고 마지막 세 번째 문자가 's'인 코스의 세부 정보를 쿼리합니다.

select * from course
where cname like 'DB\_%s_'

(2) 이름에서 두 번째 단어가 'Yang'인 학생의 이름과 학생 번호, 선택 과목 번호 및 과목 이름을 조회합니다.

select sname,student.sno,sc.cno,cname
from student,sc,course
where sname like '_阳%' 
and student.sno=sc.sno
and sc.cno=course.cno

(3) '수학'또는 '대학 영어'를 수강 한 학생의 학생 ID, 이름, 부서, 선택 과목 번호 및 성적을 기재하십시오.

select student.sno,sname,sdept,sc.cno,grade
from student,sc,course
where student.sno=sc.sno 
and sc.cno=course.cno
and cname in ('数学','大学英语')

(4) 성적이 부족한 모든 학생의 세부 정보를 쿼리합니다.

select * from student,sc,course 
where student.sno=sc.sno 
and sc.cno=course.cno 
and grade is null

(5) '량'의 나이와 다른 모든 학생의 정보를 조회합니다 (이름 만 있다고 가정).

select * from student
where sage<>(
select sage from student 
where sname='张力'
)

(6) 선택한 과정의 평균 점수가 Zhang Li보다 높은 학생의 학생 ID, 이름 및 평균 점수를 쿼리합니다.

select student.sno,sname,平均成绩=AVG(grade)
from student,sc
where student.sno=sc.sno
group by student.sno,sname 
having AVG(grade)>(
	select AVG(grade)
	from student,sc
	where student.sname='张力' and student.sno=sc.sno
	group by student.sno,sname
)

(7) 학생이 이수한 학점을 "학생 번호, 이름, 교수진, 이수 학점"순으로 나열하십시오. 이수한 학점은 시험에 합격 한 과목의 학점을 합한 것입니다.

select student.sno,sname,sdept,已修学分=SUM(ccredit)
from student,course,sc
where student.sno=sc.sno and sc.cno=course.cno
group by student.sno,sname,sdept 

(8) 한 과목 만 수강하는 학생의 ID, 이름, 부서 및 성적을 기재하십시오.

select student.sno,sname,sdept,grade
from sc,student
where student.sno=sc.sno and student.sno in (
select student.sno
from sc,student
where student.sno=sc.sno
group by student.sno
having COUNT (sc.cno)=1) 

(9) 긴장 과정과 동일한 선택 과정을 하나 이상 수강 한 학생의 학생 ID, 이름 및 과정 ID를 찾습니다.

select student.sno,sname,cno
from student,sc
where student.sno=sc.sno and cno in (
select cno
from sc,student
where student.sno=sc.sno and student.sname='张力'
)

(10) "데이터베이스"와 "데이터 구조"의 두 과목 만 수강하는 학생의 기본 정보;

select student.sno,sname,Ssex,Sage,Sdept
from student,sc,course
where student.sno=sc.sno and
sc.cno=course.cno and
sc.sno in(select sc.sno from sc,course
where (cname='数据库'or cname='数据结构')and 
sc.cno=course.cno
group by sc.sno
having COUNT(*)=2)
group by student.sno,sname,Ssex,Sage,Sdept
having COUNT(*)=2

(11) 최소한 "데이터베이스"또는 "데이터 구조"과정을 수강 한 학생의 기본 정보;

select student.sno,sname,sdept,sc.Cno,cname,grade
from student,sc,course
where student.sno=sc.sno 
and sc.Cno=course.cno and
sc.sno in(
select sc.sno from sc,course 
where (cname='数据库'or cname='数据结构')
and sc.Cno=course.cno )

(12) 코스 번호, 코스 이름, 학생 번호, 이름 및 성적을 포함하여 선택한 모든 코스의 세부 사항을 나열합니다.

select course.cno,cname,student.sno,sname,grade
from student,sc,course
where student.sno=sc.sno
and sc.cno=course.cno
order by course.cno

(13) * 한 명의 학생 만 선택한 과정의 과정 번호와 과정 이름을 조회합니다.

select sc.cno,cname
from sc,course
where sc.cno=course.cno
group by sc.cno,cname 
having COUNT(sc.sno)=1

(14) * 학습 과정 검색에는 학생 ID와 학생 'Zhang Xiangdong'이 연구 한 과정 이름이 포함됩니다.

select Student.Sno,Student.Sname
from Student,SC
where Student.Sno=SC.Sno 
and SC.Cno in (
select SC.Cno
from Student,SC
where Student.Sno=SC.Sno 
and Student.Sname='张向东')

2. T-SQL 문을 사용하여 다음 작업을 수행하십시오.
(1) 학생 테이블의 sno 열에 고유 클러스터형 인덱스 index_sno를 만듭니다.

create unique clustered index index_sno on student (sno)

(2) 학생 테이블의 sname 열에 고유 한 비 클러스터형 인덱스 index_sname 생성

create unique NONCLUSTERED index index_sname on student (sname)

(3) 학생 테이블의 sage 열에 클러스터되지 않은 인덱스 index_sage를 만듭니다.

CREATE NONCLUSTERED INDEX index_sage ON student(sage)

(4) sc 테이블의 sno 열과 cno 열에 복합 비 클러스터형 인덱스 index_sno_cno를 만듭니다.

CREATE NONCLUSTERED INDEX index_sno_cno ON sc(sno,cno)

(5) 위 인덱스 index_snocno 삭제

DROP index index_sno_cno ON sc

추천

출처blog.csdn.net/ssdssa/article/details/109039136