在SQL中如何利用學生表和課程表創建一個選課表
選課表要求每個學生至少選兩門課,至少有三到四個學生不及格,每個學生必須有成績。在線等~~~ 回答得好追加500!!!!!!假如已有學生表和課程表的話,再加一個選課表就可以了。選課表與學生表,課程表都是1對N的關系,這樣學生表和課程表就可以是N對N的關系了。
學生表-----studentid-----選課表-----courseid-----課程表
Create table StudentCourse (
id int Indentity(1,1) not null,
StudentId varchar(10) not null,
CourseId varchar(10) not null,
Score int
)
然后依次用insert to 插入數據
至于樓主的那些要求都是數據上的問題,自行控制吧
CREATE TABLE sc(
sno VARCHAR(12) NOT null,//學生編號
cno VARCHAR(4) NOT NULL,//課程編號
cname varchar(10),//數據類型要跟你的學生表和課程表都一樣 課程名
PRIMARY KEY(sno,cno),//設置聯合主鍵
FOREIGN KEY (sno) REFERENCES student(sno),//設置外鍵
FOREIGN KEY (cno) REFERENCES course(cno)//設置外鍵
);
你是要做一個查詢呢,還是要做什么,看你寫的象是要做一個程序。
用SQL語句實現:學生表、課程表、選課表三張表中的問題:
學生表:student(學號:sno、姓名:sname、性別:sgender、年齡:sage)rn課程表:couse (課程編號:cno、課程名稱:cname)rn選課表:sc(學號:sno、課程編號:cno、成績:marks)rn問題一:查詢選了”計算機原理“的學生學號和姓名?rn問題二:查詢”周星馳“同學選修的課程名字?rn問題三:選秀了5門課程的學生學號和姓名?問題一:select sno, sname from student
where sno in(select sno from sc
where cno =(select cno from couse
where cname="計算機原理"))
問題二:select cname from couse
where cno in(select cno from sc
where sno=(select sno from student
where sname ="周星馳"))
問題三:select sno, sname from student
where sno in(select sno from sc
where count(cno)=5
第一個
select s.sno, s.sname
from student s, sc t
where s.sno = t.sno
and t.cno = (
select c.cno
from couse c
where c.cname = '計算機原理'
);
第二個:
select c.cname
from student s, couse c, sc t
where c.cno = t.cno and t.sno = s.sno and s.sname = '周星馳';
第三個:
select s.sno, s.sname
from student s
where s.sno in (
select t.sno
from sc t
group by t.sno
having count(t.cno) = 5
);
我已經在本地數據庫建表測試過了,如果有什么問題,可以再聯系我。
你那個couse表名字錯誤了吧,應該是course才對吧?答案如下
問題一:查詢選了”計算機原理“的學生學號和姓名?
SELECT student.sno, student.sname
FROM student, sc, couse
WHERE student.sno = sc.sno
AND couse.cname = '計算機原理'
AND couse.cno = sc.cno
問題二:查詢”周星馳“同學選修的課程名字?
SELECT cname
FROM couse
WHERE cno IN (SELECT sc.cno FROM sc, student
WHERE sc.sno = student.sno
AND student.sname = '周星馳')
問題三:選秀了5門課程的學生學號和姓名?
SELECT sno, sname
FROM student
WHERE sno IN (SELECT sno FROM sc
GROUP BY sno
HAVING COUNT(cno) = 5)
加油!取得答案后好好研究下自己掌握才是最重要的。
這些問題都不難的,在企業管理器里面稍微動手寫一下,相信花點時間不難出來結果
問題一:
select sno,sname from student where sno in
(
select sno from sc where cno =
(
select cno from course where cname='計算機原理'
)
)
問題二:
select cname from course where cno in
(
select cno from sc where sno =
(
select sno from student where sname = '周星弛'
)
)
問題三:
select sno,sname from student where sno in
(
select sno from sc group by sno having count(*) = 5
)
用子查詢
select 姓名
from 學生,選課,課程
where 學生.學號=選課.學號 and 選課.課程號=課程.課程號 and 課程.課程名='數據庫'
and 學生.學號 in
(select 學生.學號
from 學生,選課,課程
where 學生.學號=選課.學號 and 選課.課程號=課程.課程號 and 課程.課程名='操作系統';
}
sql中創建課程表,學生表,選課表,執行錯誤,求高手改正下~
create table student1 --學生表rn(rn Sno varchar(20) not null primarykey, rn Sname varchar(50),rn Sgender varchar(4), --男 女rn Sbirthday datetime,rn Sdept varchar(50) default x27軟件學院x27,rn SSQL int --0-100rn)rnrncreate table course --課程表rn(rn Cno varchar(20) not null primarykey,rn Cname varchar(50),rn Cscore intrn)rnrncreate table xuanke --選課表rn(rn Id int identify(1,1),rn Cno varchar(20), --關聯 課程表的Cnorn Sno varchar(20), --關聯 學生表的Snorn Date varchar(50) rn)您好,從您的SQL語法中猜測,你是使用T-SQL在SQL SERVER中建立數據庫。
您的前兩個SQL中把primarykey 寫成primary key中間加個空格。
第三個語句,把identify換成大寫,即可。我已經在 sqlserver2000中測試通過了語句。呵呵。
希望能給你提供點信息。祝你好運。
11
SQL語句的一道題 三個基本表:學生表(Student)、課程表(Course)、學生選課表(SC)
1.查詢學生選課表中的全部數據。
2.查詢計算機系學生的姓名、年齡。
3.查詢成績在70~80分之間的學生的學號、課程號和成績。
4.查詢計算機系年齡在18~20之間且性別為“男”的學生的姓名和年齡。
5.查詢課程號為“C01”的課程的最高分數。
6.查詢計算機系學生的最大年齡和最小年齡。
7.統計每個系的學生人數。
8.統計每門課程的選課人數和考試最高分。
9.統計每個學生的選課門數和考試平均成績,并按學號的升序顯示結果。
10.查詢總成績超過200分的學生,要求列出學號、姓名、總成績。
11.查詢選修了課程“C02”的學生的姓名和所在系。
12.查詢成績在80分以上的學生的姓名、課程號和成績,并按成績的降序排列結果。
13.查詢哪些課程沒有人選修、要求列出課程號和課程名。
14.用子查詢實現如下查詢:
(1)查詢選修了課程“C01”的學生的姓名和所在系。
(2)查詢信息系成績在80分以上的學生的學號、姓名。
(3)查詢計算機系考試成績最高的學生的姓名。
15.刪除成績小于50分的選課記錄。
16.將所有選修了課程“C01”的學生的成績加10分:
17.將計算機系所有選修了課程“計算機文化基礎”課程的學生的成績加10分。
18.創建查詢學生的學號、姓名、所在系、課程號、課程名、課程學分的視圖。
19.創建查詢每個學生的平均成績的視圖,要求列出學生學號及平均成績。
20.創建查詢每個學生的選課學分的視圖,要求列出學生學號及總學分。
21. 創建一個唯一的、非聚集的索引,該索引依賴于數據表Student的Sname字段
22.用SQL語句創建一個名為f_1的函數,該函數能夠求出3到100之間的所有素數之和。
用SQL語句創建一個名為pro_get_stu_gra的存儲過程,該存儲過程能夠根據用戶指定的 Sno(學號)求出與該學號對應的學生姓名、課程名、成績。如果不指定學號,則顯示所有學生的姓名、課程名、成績。
用SQL語句創建一個名為pro_get_stu_gra的存儲過程,該存儲過程能夠求出選修指定課程的最高分及對應的學生學號。
1. select * from SC
2. select Sname,Sage from Student where Sdept = '計算機'
3. select Sno,Cno,Grade from SC where Grade >= 70 and Grade <= 80
4. select Sname,Sage from Student where Sage between 18 and 20 and Ssex = '男'
5. select top 1 Grade from SC where Cno = 'C01'
6. select max(Sage),min(Sage) from Student
7. select Sdept,sum(Sno) from Student group by Sdept
8. select course.Cname,sum(sc.Sno),max(Grade) from SC
join studet on Student.Sno = SC.Sno
join Course on Course.Cno = SC.Cno
group by course.cname,max(grade)
9. select sum(Cno),avg(Grade) from SC
join Course on Course.Cno = SC.Cno
join Student on Student.Sno= SC.Sno
order by SC.Sno
10. select Stuent.Sno,Stuent.Sname,sum(Grade) A from SC
join Student on Student.Sno = SC.Sno
group by sc.Sno,student.Sname
having A > 200
11. select Student.Sname,Student.Sdept from Student
join Course on Course.Cno = SC.Cno
join SC on SC.Sno = Student.Sno
where SC.Cno = 'C02'
12. select Student.sname,course.cno,sc.grade from sc
join student on student.sno = sc.sno
join course on course.cno = sc.cno
where sc.grade >= 80
order by sc.grade desc
13. select cno,cname from
(
select course.cno,course.cname,sun(sno) from student
join course on course.cno = sc.cno
join sc on sc.sno = student.sno
group by cno,cname
having sun(sno) > 0
)
14. ① select student.sname,student.sdept from
(
select student.sname,student.sdept,course.cname from student
join sc on sc.sno = student.sno
join course on course.cno = sc.cno
where course.cname = 'C01'
)
② select student.sno,student.sname from
(
select student.sno,student.sname,student.sdept,sc.grade from sc
join student on student.sno = sc,sno
where student.sdept = '信息' and sc.grade >= 80
)
③ select top 1 student.sname from
(
select student.sname,student.sdept,sum(sc.grade) from sc
join student on student.sno = sc.sno
where student.sdept = '計算機'
group by student.sname,student.sdept
order by
)
15. delete from sc where grade < 50
16. update sc set grade += 5 from sc
join course on course.cno = sc.cn
join student on student.sno = sc.sno
where student.sno =
(
select student.sno from sc
join course on course.cno = sc.cno
where course.cname = 'c01'
)
17. update sc set grade += 10 from sc
join student on student.sno = sc.sno
where student.sno =
(
select student.sno from student
join sc on sc.sno = student.sno
join course on course.cno = sc.cno
where student.sdept = '計算機' and course.cname = '計算機文化基礎'
)
18. create view [A] as
select student.sno,student.sname,student.sdept,course.cno,course.cname,sc.grade from sc
join student on student.sno = sc.sno
join course on course.cno = sc.cno
19. create view [A] as
select student.sno,avg(sc.grade) from sc
join student on student.sno = sc.sno
group by student.sno
20. create view [A] as
select student.sno,sum(sc.grade) from sc
join student on student.sno = sc.sno
group by student.sno
21. create index A on student(sname)
22. 不會
select * from course
select Sname,Sage from Student where sdept = '計算機系'
select * from sc where 70 <= grade and grade =>80
select Sname,Sage from Student where sdept = '計算機系' and Ssex='男'
。。。。
自己看手冊把,都很簡單的,題太多,不好回答,,
先做題,后看答案哦! 相信乖乖做完的你肯定會有不小的收獲的~
SQL代碼如下:
CREATE TABLE student
(Sno varchar(20) NOT NULL,
Sname varchar(20) NOT NULL,
sex varchar(20) NOT NULL,
age INT NOT NULL,
dept varchar(20) NOT NULL,
PRIMARY KEY (Sno)
);
CREATE TABLE course
(Cno varchar(20) NOT NULL,
Cname varchar(20) NOT NULL,
hours VARCHAR(20) NOT NULL,
PRIMARY KEY (Cno)
);
CREATE TABLE SC
(Sno varchar(20) NOT NULL,
Cno varchar(20) NOT NULL,
grade INT ,
PRIMARY KEY (Sno,Cno)
);
INSERT INTO student VALUES ('9512101','李勇','男',19,'計算機系');
INSERT INTO student VALUES ('9512102','劉晨','男',20,'計算機系');
INSERT INTO student VALUES ('9512103','王敏','女',20,'計算機系');
INSERT INTO student VALUES ('9521101','張立','男',22,'信息系');
INSERT INTO student VALUES ('9521102','吳賓','女',21,'信息系');
INSERT INTO student VALUES ('9521103','張海','男',20,'信息系');
INSERT INTO student VALUES ('9531101','錢小力','女',18,'數學系');
INSERT INTO student VALUES ('9531102','王大力','男',19,'數學系');
INSERT INTO course VALUES ('C01','計算機文化學','70');
INSERT INTO course VALUES ('C02','VB','90');
INSERT INTO course VALUES ('C03','計算機網絡','80');
INSERT INTO course VALUES ('C04','數據庫基礎','108');
INSERT INTO course VALUES ('C05','高等數學','180');
INSERT INTO course VALUES ('C06','數據結構','72');
INSERT INTO SC VALUES ('9512101','C01',90);
INSERT INTO SC VALUES ('9512101','C02',86);
INSERT INTO SC VALUES ('9512101','C06',NULL);
INSERT INTO SC VALUES ('9512102','C02',78);
INSERT INTO SC VALUES ('9512102','C04',66);
INSERT INTO SC VALUES ('9521102','C01',82);
INSERT INTO SC VALUES ('9521102','C02',75);
INSERT INTO SC VALUES ('9521102','C04',92);
INSERT INTO SC VALUES ('9521102','C05',50);
INSERT INTO SC VALUES ('9521103','C02',68);
INSERT INTO SC VALUES ('9521103','C06',NULL);
INSERT INTO SC VALUES ('9531101','C01',80);
INSERT INTO SC VALUES ('9531101','C05',95);
INSERT INTO SC VALUES ('9531102','C05',85);
-- 1.分別查詢學生表和學生修課表中的全部數據
SELECT * FROM student
SELECT * FROM course
-- 2.查詢成績在70-80分之間的學生的學號、課程號和成績
SELECT *
FROM SC
WHERE grade > 70 and grade < 80
-- 3.查詢C01號課程成績最高的分數
SELECT max(grade)
FROM SC
WHERE Cno = 'C01'
GROUP BY Cno
-- 4.查詢學生都選修了哪些課程,要求列出課程號
SELECT Cno
FROM SC
GROUP BY Cno
-- 5.查詢C02號課程的所有學生的平均成績、最高成績和最低成績。
SELECT AVG(grade),MAX(grade),MIN(grade)
FROM SC
WHERE Cno = 'C02'
group BY Cno
-- 6.統計每個系的學生人數
SELECT dept,count(*) as `人數`
FROM student
GROUP BY dept
-- 7.統計每門課程的修課人數和考試最高分
SELECT Cno, count(*),max(grade)
FROM SC
group BY Cno
-- 8.統計每個學生的選課門數,并按選課門數的遞增順序顯示結果
SELECT Sno, count(*)
FROM SC
GROUP BY Sno
ORDER BY count(*)
-- 9.統計選修課的學生總數和考試的平均成績
SELECT Cno, count(*), avg(grade)
FROM SC
GROUP BY Cno
-- 10.查詢選課門數超過2門的學生的平均成績和選課門數
SELECT Sno, count(*),avg(grade)
FROM SC
GROUP BY Sno
having count(*) > 2
-- 11.列出總成績超過200分的學生,要求列出學號、總成績
SELECT Sno,sum(grade)
FROM SC
GROUP BY Sno
HAVING sum(grade) > 200
-- 12.查詢選修了C02號課程的學生的姓名和所在系。
SELECT student.Sname,student.dept,SC.Cno
FROM student join SC ON student.Sno = SC.Sno -- 選取內容在兩張表中,第一種方法將兩張表聯結起來
WHERE Cno = 'C02'
SELECT Sname,dept
FROM student
WHERE Sno in (SELECT Sno
FROM SC
WHERE Cno = 'C02') -- 第二種方法,采用子查詢法。
-- 13.查詢成績80分以上的學生的姓名、課程號和成績,并按成績的降序排列結果。
SELECT S.Sname,SC.Cno,SC.grade
FROM student AS S JOIN SC ON S.Sno = SC.Sno
WHERE SC.grade > 80
ORDER BY SC.grade DESC
-- 14.查詢計算機系男生修了‘數據庫基礎’的學生姓名、性別、成績
SELECT S.Sname,S.sex,SC.grade
FROM student AS S JOIN SC ON S.Sno = sc.Sno
JOIN course ON sc.Cno = course.Cno
WHERE S.dept = '計算機系' AND S.sex = '男' AND course.Cname = '數據庫基礎'
SELECT Sname,sex,SC.Grade
FROM student
inner join SC ON Cno IN(SELECT Cno FROM course WHERE Cname='數據庫基礎') -- 顯示成績的條件,僅聯結了兩張表,On 后面也可以跟不同的聯結條件。
AND student.Sno=SC.Sno -- 顯示成績的學生的學號
WHERE dept='計算機系' AND sex='男'
---------------------
-- 15.查詢哪些學生的年齡相同,要求列出年齡相同的學生的姓名和年齡。
SELECT S1.Sname,S1.sex
FROM student AS S1, student AS S2
WHERE S1.age = S2.age AND S1.Sname <> S2.Sname
GROUP BY S1.Sname
ORDER BY S1.age
-- 16.查詢哪些課程沒有人選,要求列出課程號和課程名。
SELECT Cno,Cname
FROM course
WHERE Cno NOT in (SELECT Cno
FROM sc
GROUP BY Cno)
-- 17.查詢有考試成績的所有學生的姓名、修課名稱及考試成績。
-- 要求將查詢結果放在一張新的永久表(假設新表名為new-sc)中。
CREATE TABLE new_sc AS -- 將查詢結果放在新表中的方法。
SELECT S.Sname,course.Cname,sc.grade
FROM student AS S inner JOIN sc ON S.Sno = sc.Sno
INNER JOIN course ON course.Cno = sc.Cno
WHERE grade IS NOT NULL
-- 18.分別查詢信息系和計算機系的學生姓名、性別、修課名稱、修課成績
SELECT S.Sname,S.sex,course.Cname,sc.grade
FROM student AS S inner JOIN sc ON S.Sno = sc.Sno
INNER JOIN course ON course.Cno = sc.Cno
WHERE S.dept = '信息系'
UNION
SELECT S.Sname,S.sex,course.Cname,sc.grade
FROM student AS S inner JOIN sc ON S.Sno = sc.Sno
INNER JOIN course ON course.Cno = sc.Cno
WHERE S.dept = '計算機系'
-- 19.用子查詢實現如下查詢:
-- (1)查詢選修了C01號課程的學生的姓名和所在系
SELECT Sname,dept
FROM student
WHERE Sno in (SELECT Sno
FROM sc
WHERE Cno = 'C01')
-- (2)查詢數學系成績80分以上的學生的學號和姓名。
SELECT Sno,Sname
FROM student
WHERE dept = '數學系' AND Sno in (SELECT Sno FROM sc WHERE grade > 80)
-- (3)查詢計算機系學生所選的課程名
SELECT Cname
FROM course
WHERE Cno in (SELECT Cno
FROM sc
where Sno in (SELECT Sno
FROM student
WHERE dept = '計算機系'))
-- 20.將計算機系成績高于80分的學生的修課情況插入到另一張表中,分兩種情況實現:
-- (1)在插入的過程中建表
SELECT Sno,Sname
FROM(
SELECT Sno,Sname
FROM student
WHERE dept = '數學系' AND Sno in (SELECT Sno FROM sc WHERE grade > 80)) AS newtable
-- (2)先建一個新表,然后再插入數據
CREATE TABLE newtable2 AS
SELECT Sno,Sname
FROM student
WHERE dept = '數學系' AND Sno in (SELECT Sno FROM sc WHERE grade > 80)
-- 21.刪除修課成績小于50分的學生的修課記錄
DELETE FROM sc
WHERE grade < 50 or grade IS NULL
-- 22.將所有選修了C01課程的學生的成績加10分。
UPDATE sc
SET grade = grade + 10
WHERE Cno = 'C01'
老師留的作業嗎?為你好,自己做吧。
相關推薦:
電子合同糾紛解決(簽電子合同糾紛怎么解決)
現戶籍所在地怎么填寫(戶籍所在地怎么填寫)
超生新生兒如何上戶(超生如何給孩子上戶口)
行政處罰會留案底嗎(行政處罰決定書會留案底嗎)
公民如何保護生命健康權(法律如何保護我們的健康)