sqlsever練習題大全所有練習程式碼文字版程式
2。
修改
student
表(增加屬性列“
major
”)
alter
table
student
add
major
varchar
(
20
)
3。
修改
student
表(更改屬性列“
score
”資料型別)
alter
table
student
alter
column
score
int
4。
修改
student
表(刪除屬性列“
class
”)
alter
table
student
drop
column
class
5。
修改
student
表(更改屬性列“
subjects
”名)
sp_rename “student。subjects”,“cno”
6。
修改
subsjects
表(增加屬性列“
cname
”約束條件)
alter
table
subsjects
add
unique
(
cname
)
7。
刪除
subsjects
資料表
drop
table
subjects
8。查詢各個課程號和相應的選課人數。
select
cno
,
COUNT
(
sno
)
選課人數
from
CS
group
by
cno
9。查詢選修三門或三門以上課程的學生學號。
select
sno
from
CS
group
by
sno
having
COUNT
(*)>=
3
10。
查詢平均成績大於等於
90分的學生學號和平均成績。
select
sno
,
AVG
(
score
)
from
CS
group
by
sno
having
AVG
(
score
)>=
90
11。查詢選修2號課程且成績大於等於90分的學生姓名和學號。
select
CS
。
sno
,
sname
from
CS
,
S
where
CS
。
sno
=
S
。
sno
AND
CS
。
cno
=
‘2’
AND
CS
。
score
>=
90
12。查詢每個學生的學號、姓名、選修的課程名及成績。
對應
sql語句
select
S
。
sno
,
sname
,
cname
,
CS
。
score
from
CS
,
S
,
C
where
S
。
sno
=
CS
。
sno
AND
CS
。
cno
=
C
。
cno
13。查詢表中和趙偉在一個系學習的學生。
select
*
from
s
where
sdept
in
(
select
sdept
from
S
where
sname
=
‘趙偉’
)
14。查詢表中超過自己選修課程平均成績的課程號。
對應
sql語句
select
sno
,
cno
from
CS x
where
score
>=
(
select
AVG
(
score
)
from
CS y
where
x
。
sno
=
y
。
sno
)
15。查詢非計算機科學系中比計算機科學系所有學生年齡都小的學生姓名及年齡。
對應
sql語句
select
sname
,
sage
from
s
where
sage
( select sage from s where sdept = ‘計算機學院’ ) and sdept <> ; 16。查詢非計算機科學系中比計算機科學系任意一個學生年齡小的學生姓名和年齡。 對應 sql語句 select sname , sage from s where sage ( select sage from s where sdept = ) and sdept <> ; 17。查詢數學系或年齡大於等於19歲的學生。 對應 sql語句 select * from s where sdept = ‘數學學院’ union select * from s where sage >= 19 ; 18。查詢數學系的學生與年齡不小於19歲學生的交集。 對應 sql語句 select * from s where sdept = intersect select * from s where sage >= 19 ; 19。查詢數學系的學生與年齡不小於19歲學生的差集。 對應 sql語句 select * from s where sdept = except select * from s where sage >= 19 ; 20。 在 S表中插入如下學生資訊:‘數學學院’,201811,‘姍姍’,‘女’,18。 對應sql語句 INSERT INTO S ( sdept , sno , sname , ssex , sage ) VALUES ( ‘ 數學學院 ’ , 201811 , ‘ 姍姍 ’ , ‘ 女 ’ , 18 ); INSERT INTO S VALUES ( ‘ 數學學院 ’ , 201811 , ‘ 姍姍 ’ , ‘ 女 ’ , 18 ); 21。 在S表中插入如下學生資訊:‘數學學院’,201812,‘曼曼’。 對應sql語句 INSERT INTO S ( sdept , sno , sname ) VALUES ( ‘ 數學學院 ’ , 201812 , ‘ 曼曼 ’ ); INSERT INTO S ( sdept , sno , sname ) VALUES ( ‘ 數學學院 ’ , 201812 , ‘ 曼曼 ’ ,null,null); 22。 建立新表s_ascore包含兩列sno,ascore,記錄學生學號和平均成績。 對應 sql語句 CREATE TABLE s_ascore ( sno varchar ( 50 ), ascore INT ); INSERT INTO s_ascore ( sno , ascore ) select sno , AVG ( score ) from CS group by sno 23。 將學號為 201811的學生年齡改為28。 對應 sql語句 UPDATE s SET sage = 28 WHERE sno = ‘201811’ ; 24。計算機系學生年齡全部減1。 對應 sql語句 UPDATE s SET sage = sage - 1 WHERE sno in ( select sno from S where sdept = ‘ 計算機學院 ’ ) 25。刪除201811的學生資訊。 對應 sql語句 DELETE FROM s WHERE sno = 201811 ; 26。刪除計算機學院學生資訊。 對應 sql語句 delete from S WHERE sno in ( select sno from S where sdept = ‘ 計算機學院 ’ ) 27。 建立 s1 檢視,包含所有男生的姓名、學號、年齡、院系,並要求進行修改刪除操作時仍保證該檢視只有男生。 對應 SQL 語句 create view s1 as select sdept , sno , sname , sage from s where ssex = ‘ 男 ’ with check option 28。 建立 s2 檢視,包含所有男生且選修了 1 號課程的姓名、學號、成績。 對應 SQL 語句 create view s2 ( sno , sname , score ) as select s 。 sno , sname , score from s , cs where ssex = ‘ 男 ’ and s 。 sno = CS 。 sno and CS 。 cno = ‘1’ 29。 建立選修了一號課程的男生且分數在 90 分以上的人。 對應 SQL 語句 create view s3 as select * from s2 where score >= 90 30。建立一個包含學生姓名和出生年月的檢視。 對應 SQL 語句 create view s4 ( sname , b_d ) as select sname , YEAR ( GETDATE ())- sage from s 31。 將每個學生的學號、姓名及平均成績做一個檢視。 對應 SQL 語句 create view s5 ( sno , avgscore ) as select sno , avg ( score ) from cs group by sno 32。 刪除檢視 s4 。 對應 SQL 語句 drop view s4 33。 查詢 s1 中所有化學學院的學生。 對應 SQL 語句 select * from s1 where sdept=‘ 化學學院 ’ 34。s1 中學號為 201806 的學生姓名改為珊珊。 對應 SQL 語句 update s1 set sname = ‘ 珊珊 ’ where sno = 201806 35。 s1 中增加學生資訊。院系:化學學院,學號: 201817 ,姓名:阿豐,年齡: 27 。 對應 SQL 語句 create view s6 as select sdept , sno , sname , sage , ssex from s where ssex = ‘男’ with check option insert into s6 values ( ‘化學學院’ , ‘201817’ , ‘阿豐’ , ‘17’ , ‘男’ ) 36。 刪除記錄 對應 SQL 語句 Delete from s1 where sno = ‘201803’ 37。將student表中的sno屬性定義為主碼。 SQL語句 create table Student ( sno char ( 20 ) primary key , sname char ( 20 ) , ssex char ( 10 ) , sdeptNo char ( 20 ), ); 或者先定義再增加條件:(此刻必須先加上 sno不為空) create table Student1 ( Sno char ( 11 ) not null , Sname char ( 20 ) , Ssex char ( 1 ) , SdeptNo char ( 4 ), ); alter table student1 add primary key ( sno ) 38。在定義Course表時,Cno為主鍵,Cname屬性不允許空值。 SQL語句 create table Course ( Cno char ( 5 ) primary key , Cname varchar ( 20 ) not null, Preno char ( 5 ), credit int , ); 39。定義SC中的參照完整性。 SQL語句 create table SC ( SNo char ( 20 ), CNo char ( 5 ), Grade int foreign key ( Sno ) references Student ( Sno ) on delete cascade on update cascade , foreign key ( Cno ) references Course ( Cno ) on delete cascade on update cascade , ); 操作: DELETE FROM sc WHERE sno = 201811 ; 40。建立SD表,要求部門名稱Sdept列取值唯一。 SQL語句 create table SD ( SdeptNo char ( 4 ) primary key , Sdept varchar ( 20 ) unique , Mname char ( 10 ), ); 41。 用 CHECK短語指定性別只能為男或女。 SQL語句 create table Student1 ( Sno char ( 20 ) primary key , Sname char ( 20 ) not null, Ssex char ( 20 ) CHECK ( Ssex = ‘男’ or Ssex = ‘女’ ), SdeptNo char ( 20 ), ); 42。建立student2表,要求當學生的性別是男時,不能姓李。 SQL語句 create table student2 ( sno char ( 20 ) primary key , sname char ( 20 )not null, ssex char ( 20 ), sage smallint , sdept char ( 20 ), check ( ssex = ‘女’ OR sname not like ‘李%’ ) ); 自己練習規則與約束。 ( 1)規則 SQL語句 Create rule rssex as @Ssex in ( ‘男’ , ‘女’ ) Exec sp_bindrule ‘rssex’ , ‘Student。ssex’ 執行規則後不會對前面已經輸入的資料再次判斷檢驗,而是對之後的操作開始檢驗。 ( 2)約束 SQL語句 alter table student add CHECK ( Ssex = ‘男’ or Ssex = ‘女’ ) 因為前面有已經錄入的錯誤資料,所以拒絕執行。 程式碼速查 建立資料庫表: student、course create table student(sno nchar(10),sname nchar(10), ssex nchar(1),score int,sdept nchar(10),primary key(sno,sname)); create table course (cno nchar(10),cname nchar(10),ccredit nchar(10),primary key(cno)); 查詢學生表的總人數select COUNT (*) 總人數 from student 查詢學生表的總人數select COUNT (*) 總人數 from student 查詢計算機科學系全體學生的名單select * from student where sdept =‘CS’ 查詢所有學生的出生年份select * ,year(getdate())-sage 出生年份 from student 查詢學號為 “2018503”的數學分析成績 select grade from score where sno=‘2018503’ and cno=‘2’ 查詢所有王姓學生資訊select * from student where sname like ‘ 王 %’ 查詢那些學生的學科成績還沒有錄入select sno , cno from score where grade is null 查詢學分為 2,3學分的課程名select cname from course where ccredit in(2,3) 查詢計算機系學生及其課程得分的情況 select student。*,score。*from student,score where sdept=‘CS’ AND student。sno=score。sno 查詢王敏各個課程的成績select student。*,score。*,cname from student,score,course where sname=‘ 王敏 ’ and student。sno=score。sno and course。cno=score。cno 查詢選修 3號課程且成績在80分以上的所有學生的學號和姓名:select student。sno,sname from student,score where student。sno=score。sno and score。cno=‘2’ and score。grade>80 查詢選修了課程名為 ‘數學分析’的學生學號和姓名:select sno,sname from student where sno in (select sno from score where cno in (select cno from course where cname=‘ 數學分析 ’ )) 查詢非數學系中比數學系任意一個學生年齡小的學生資訊:select student。* from student where sage 查詢沒有選修 2號課程的學生姓名select sname,sno,sdept from student where not exists (select* from score where sno=student。sno and cno =‘2’) 查詢選修了全部課程的學生資訊select sname from student where not exists (select* from course where not exists (select* from score where cno =course。cno and sno=student。sno)) 查詢數學系的學生與年齡不大於 20歲的學生的差集select* from student where sdept = ‘MA’ except select* from student where sage<=20 查詢既選修了課程 1又選修了課程2的學生select score。sno,sname from student,score where cno = ‘1’ and score。sno=student。sno intersect select score。sno,sname from student,score where cno = ‘2’ and score。sno=student。sno 將一個新學生元組(學號: 2018518,姓名:李姍,性別:女,所在系:MA年齡:20歲) 插入到 Student表中insert into student(sno,sname,ssex,sdept,sage) values(‘2018518’,‘ 李珊 ’ ,‘ 女 ’ ,‘MA’,20); 將學生耿耿的資訊插入到 student表中 insert into student values(‘20181519’,‘ 耿耿 ’ ,‘ 女 ’ ,‘IS’,18); 插入一條選課記錄( ‘2018509’,‘1’)insert into score(sno,cno) values(‘2018507’,‘1’); 對每個系,求學生的平均年齡,並把結果存入資料庫 create table Dept_age (dept NCHAR(10) avg_age smallint); INSERT INTO Dept_age(dept,avg_age)select sdept,AVG(sage)from student group by sdept; 將學生王曼的年齡改為 18歲 UPDATE student set sage=18 where sname=‘ 王曼 ’ 將所有計算機系的學生年齡增加一歲 UPDATE student set sage=sage+1 where sdept=‘CS’ 刪除學號為 2018501的學生資訊DELETE FROM student where sno=‘2018501’ 刪除數學系所有學生的選課記錄DELETE FROM score where sno in (select sno from student where sdept=‘MA’); 建立計算機系學生的檢視CREATE VIEW CS_Stuent AS SELECT sno,sname,sage from student 建立數學系選修了 2號課程的學生的檢視 insert into student values(‘20181519’,‘ 耿耿 ’ ,‘ 女 ’ ,‘IS’,18); 建立數學系選修 1號課程且成績在90分以上的學生學生檢視 CREATE VIEW MA_S2 AS SELECT sno,sname,grade from MA_S1 where grade >=90 定義一個反映學生出生年份的檢視 CREATE VIEW BIRTH(sno,sname,sbirth) AS SELECT sno,sname,2021-sage from student 學生的學號及平均成績定義為一個檢視UPDATE student set sage=18 where sname=‘ 王曼 ’ 將 student表中所有男生記錄定義為一個檢視 CREATE VIEW B_STU(B_no,name,sex,age,dept) AS SELECT* from student where ssex=‘ 男 ’ 刪除檢視B_STU、BIRTH drop view BIRTH; drop view B_STU; 查詢avg_student 檢視中平均成績在 90分以下的學生學號和平均成績select*from avg_student where savg<=90 向計算系學生檢視CS_Stuent 中插入一個新的學生記錄,其中學號為 “2018520”,姓名“周扒皮”,年齡為20歲insert into CS_Student values(‘2018520’,‘ 周扒皮 ’ ,20); Insert into student(sno,sname,sage,sdept) values(‘2018520’,‘ 周扒皮 ’ ,20,‘CS’); 將 student表中的sno屬性定義為碼 create table student (sno char(9)primary key, sname char(20)not null,ssex char(2), sage smallint,sdept char(20)); 定義 SC中的參照完整性create table sc (sno char(9)not null, cno nchar(5) not null, grade smallint, primary key(sno,cno), foreign key (sno) references student(sno), foreign key (cno) references course(cno),); 在定義 sc表時,說明sno、cno、grade屬性不允許空值 create table sc (sno char(9)not null, cno nchar(4) not null, grade smallint not null ,primary key(sno,cno),); 建立部門表 dept,要求部門名稱dname列取值唯一,部門編號deptno列為主碼 create table dept (deptno numeric(2), dname char(9) unique not null, location char(10), primary key(deptno),); 用 CHECK短語指定列表值應該滿足的條件create table student (sno char(9)primary key, sname char(8)not null, ssex char(2) check(ssex in(‘ 男 ’ ,‘ 女 ’ )), sage smallint, sdept char(20) ); 將 student表中所有男生記錄定義為一個檢視 CREATE VIEW B_STU(B_no,name,sex,age,dept) AS SELECT* from student where ssex=‘ 男 ’ 當學生的性別是男時,其名字不能以 MS。打頭 create table student (sno char(9)primary key, sname char(8)not null, ssex char(2), sage smallint, sdept char(20), check (ssex=‘ 女 ’ OR sname not like ‘MS。%’) ); 建立教師表 teacher,要求每個教師的應發工資不低於3000元,應發工資是工資列sal與扣除列deduct之和 create table teacher (eno numeric(4)primary key, ename char(10), job char(8), sal numeric(7,2), deduct numeric(7,2), deptno numeric(2), constraint teacherkey foreign key(deptno) references dept(deptno), constraint c1 check(sal+deduct>=3000));
相關文章
- 2021-06-19「賽高神評」永不團滅的車 安全係數肯定很高
- 2021-06-14英語短語select into的用法是什麼?
- 2021-05-09Java面試題Statement和PrepareStatement的區別,你知道嗎?
- 2021-04-24「賽高神評」房管 把那個讓金爺道歉的給我封了
- 2021-04-12「賽高神評」 有的人打入四強 真的可以算成功