文档库 最新最全的文档下载
当前位置:文档库 › 学生表_课程表_成绩表_教师表_50个常用sql语句

学生表_课程表_成绩表_教师表_50个常用sql语句

学生表_课程表_成绩表_教师表_50个常用sql语句
学生表_课程表_成绩表_教师表_50个常用sql语句

原文地址:sql语句多表查询(学生表/课程表/教师表/成绩表 )作者:海豚湾孬蛋

问题及描述:

--1.学生表

Student(S#,Sname,Sage,Ssex) --S# 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别

--2.课程表

Course(C#,Cname,T#) --C# --课程编号,Cname 课程名称,T# 教师编号

--3.教师表

Teacher(T#,Tname) --T# 教师编号,Tname 教师姓名

--4.成绩表

SC(S#,C#,score) --S# 学生编号,C# 课程编号,score 分数

*/

--创建测试数据

create table Student(S# varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10))

insert into Student values('01' , N'赵雷' , '1990-01-01' , N'男')

insert into Student values('02' , N'钱电' , '1990-12-21' , N'男')

insert into Student values('03' , N'孙风' , '1990-05-20' , N'男')

insert into Student values('04' , N'李云' , '1990-08-06' , N'男')

insert into Student values('05' , N'周梅' , '1991-12-01' , N'女')

insert into Student values('06' , N'吴兰' , '1992-03-01' , N'女')

insert into Student values('07' , N'郑竹' , '1989-07-01' , N'女')

insert into Student values('08' , N'王菊' , '1990-01-20' , N'女')

create table Course(C# varchar(10),Cname nvarchar(10),T# varchar(10))

insert into Course values('01' , N'语文' , '02')

insert into Course values('02' , N'数学' , '01')

insert into Course values('03' , N'英语' , '03')

create table Teacher(T# varchar(10),Tname nvarchar(10))

insert into Teacher values('01' , N'张三')

insert into Teacher values('02' , N'李四')

insert into Teacher values('03' , N'王五')

create table SC(S# varchar(10),C# varchar(10),score decimal(18,1))

insert into SC values('01' , '01' , 80)

insert into SC values('01' , '02' , 90)

insert into SC values('01' , '03' , 99)

insert into SC values('02' , '01' , 70)

insert into SC values('02' , '02' , 60)

insert into SC values('02' , '03' , 80)

insert into SC values('03' , '01' , 80)

insert into SC values('03' , '02' , 80)

insert into SC values('03' , '03' , 80)

insert into SC values('04' , '01' , 50)

insert into SC values('04' , '02' , 30)

insert into SC values('04' , '03' , 20)

insert into SC values('05' , '01' , 76)

insert into SC values('05' , '02' , 87)

insert into SC values('06' , '01' , 31)

insert into SC values('06' , '03' , 34)

insert into SC values('07' , '02' , 89)

insert into SC values('07' , '03' , 98)

go

--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

--1.1、查询同时存在"01"课程和"02"课程的情况

select a.* , b.score [课程'01'的分数],c.score [课程'02'的分数] from Student a , SC b , SC c

where a.S# = b.S# and a.S# = c.S# and b.C# = '01' and c.C# = '02' and b.score > c.score --1.2、查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)(以下存在相同内容时不再解释)

select a.* , b.score [课程"01"的分数],c.score [课程"02"的分数] from Student a left join SC b on a.S# = b.S# and b.C# = '01'

left join SC c on a.S# = c.S# and c.C# = '02'

where b.score > isnull(c.score,0)

--2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

--2.1、查询同时存在"01"课程和"02"课程的情况

select a.* , b.score [课程'01'的分数],c.score [课程'02'的分数] from Student a , SC b , SC c

where a.S# = b.S# and a.S# = c.S# and b.C# = '01' and c.C# = '02' and b.score < c.score --2.2、查询同时存在"01"课程和"02"课程的情况和不存在"01"课程但存在"02"课程的情况select a.* , b.score [课程"01"的分数],c.score [课程"02"的分数] from Student a left join SC b on a.S# = b.S# and b.C# = '01'

left join SC c on a.S# = c.S# and c.C# = '02'

where isnull(b.score,0) < c.score

--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

select a.S# , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score

from Student a , sc b

where a.S# = b.S#

group by a.S# , a.Sname

having cast(avg(b.score) as decimal(18,2)) >= 60

order by a.S#

--4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

--4.1、查询在sc表存在成绩的学生信息的SQL语句。

select a.S# , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score

from Student a , sc b

where a.S# = b.S#

group by a.S# , a.Sname

having cast(avg(b.score) as decimal(18,2)) < 60

order by a.S#

--4.2、查询在sc表中不存在成绩的学生信息的SQL语句。

select a.S# , a.Sname , isnull(cast(avg(b.score) as decimal(18,2)),0) avg_score from Student a left join sc b

on a.S# = b.S#

group by a.S# , a.Sname

having isnull(cast(avg(b.score) as decimal(18,2)),0) < 60

order by a.S#

--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

--5.1、查询所有有成绩的SQL。

select a.S# [学生编号], a.Sname [学生姓名], count(b.C#) 选课总数, sum(score) [所有课程的总成绩]

from Student a , SC b

where a.S# = b.S#

group by a.S#,a.Sname

order by a.S#

--5.2、查询所有(包括有成绩和无成绩)的SQL。

select a.S# [学生编号], a.Sname [学生姓名], count(b.C#) 选课总数, sum(score) [所有课程的总成绩]

from Student a left join SC b

on a.S# = b.S#

group by a.S#,a.Sname

order by a.S#

--6、查询"李"姓老师的数量

--方法1

select count(Tname) ["李"姓老师的数量] from Teacher where Tname like N'李%'

--方法2

select count(Tname) ["李"姓老师的数量] from Teacher where left(Tname,1) = N'李'

--7、查询学过"张三"老师授课的同学的信息

select distinct Student.* from Student , SC , Course , Teacher

where Student.S# = SC.S# and SC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'张三'

order by Student.S#

--8、查询没学过"张三"老师授课的同学的信息

select m.* from Student m where S# not in (select distinct SC.S# from SC , Course , Teacher where SC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'张三') order by m.S#

--9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

--方法1

select Student.* from Student , SC where Student.S# = SC.S# and SC.C# = '01' and exists (Select 1 from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# = '02') order by Student.S#

--方法2

select Student.* from Student , SC where Student.S# = SC.S# and SC.C# = '02' and exists (Select 1 from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# = '01') order by Student.S#

--方法3

select m.* from Student m where S# in

(

select S# from

(

select distinct S# from SC where C# = '01'

union all

select distinct S# from SC where C# = '02'

) t group by S# having count(1) = 2

)

order by m.S#

--10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

--方法1

select Student.* from Student , SC where Student.S# = SC.S# and SC.C# = '01' and not exists (Select 1 from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# = '02') order by Student.S#

--方法2

select Student.* from Student , SC where Student.S# = SC.S# and SC.C# = '01' and Student.S# not in (Select SC_2.S# from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# = '02') order by Student.S#

--11、查询没有学全所有课程的同学的信息

--11.1、

select Student.*

from Student , SC

where Student.S# = SC.S#

group by Student.S# , Student.Sname , Student.Sage , Student.Ssex having count(C#) < (select count(C#) from Course)

--11.2

select Student.*

from Student left join SC

on Student.S# = SC.S#

group by Student.S# , Student.Sname , Student.Sage , Student.Ssex having count(C#) < (select count(C#) from Course)

--12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

select distinct Student.* from Student , SC where Student.S# = SC.S# and SC.C# in (select C# from SC where S# = '01') and Student.S# <> '01'

--13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

select Student.* from Student where S# in

(select distinct SC.S# from SC where S# <> '01' and SC.C# in (select distinct C# from SC where S# = '01')

group by SC.S# having count(1) = (select count(1) from SC where S#='01'))

--14、查询没学过"张三"老师讲授的任一门课程的学生姓名

select student.* from student where student.S# not in

(select distinct sc.S# from sc , course , teacher where sc.C# = course.C# and course.T# = teacher.T# and teacher.tname = N'张三')

order by student.S#

--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select student.S# , student.sname , cast(avg(score) as decimal(18,2)) avg_score from student , sc

where student.S# = SC.S# and student.S# in (select S# from SC where score < 60 group by S# having count(1) >= 2)

group by student.S# , student.sname

--16、检索"01"课程分数小于60,按分数降序排列的学生信息

select student.* , sc.C# , sc.score from student , sc

where student.S# = SC.S# and sc.score < 60 and sc.C# = '01'

order by sc.score desc

--17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

--17.1 SQL 2000 静态

select a.S# 学生编号 , a.Sname 学生姓名 ,

max(case https://www.wendangku.net/doc/cb1136672.html,ame when N'语文' then b.score else null end) [语文],

max(case https://www.wendangku.net/doc/cb1136672.html,ame when N'数学' then b.score else null end) [数学],

max(case https://www.wendangku.net/doc/cb1136672.html,ame when N'英语' then b.score else null end) [英语],

cast(avg(b.score) as decimal(18,2)) 平均分

from Student a

left join SC b on a.S# = b.S#

left join Course c on b.C# = c.C#

group by a.S# , a.Sname

order by 平均分 desc

--17.2 SQL 2000 动态

declare @sql nvarchar(4000)

set @sql = 'select a.S# ' + N'学生编号' + ' , a.Sname ' + N'学生姓名'

select @sql = @sql + ',max(case https://www.wendangku.net/doc/cb1136672.html,ame when N'''+Cname+''' then b.score else null end) ['+Cname+']'

from (select distinct Cname from Course) as t

set @sql = @sql + ' , cast(avg(b.score) as decimal(18,2)) ' + N'平均分' + ' from Student a left join SC b on a.S# = b.S# left join Course c on b.C# = c.C#

group by a.S# , a.Sname order by ' + N'平均分' + ' desc'

exec(@sql)

--24、查询学生平均成绩及其名次

--24.1 查询学生的平均成绩并进行排名,sql 2000用子查询完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。

select t1.* , px = (select count(1) from

(

select m.S# [学生编号] ,

m.Sname [学生姓名] ,

isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩]

from Student m left join SC n on m.S# = n.S#

group by m.S# , m.Sname

) t2 where 平均成绩 > t1.平均成绩) + 1 from

(

select m.S# [学生编号] ,

m.Sname [学生姓名] ,

isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩]

from Student m left join SC n on m.S# = n.S#

group by m.S# , m.Sname

) t1

order by px

select t1.* , px = (select count(distinct 平均成绩) from

(

select m.S# [学生编号] ,

m.Sname [学生姓名] ,

isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩]

from Student m left join SC n on m.S# = n.S#

group by m.S# , m.Sname

) t2 where 平均成绩 >= t1.平均成绩) from

(

select m.S# [学生编号] ,

m.Sname [学生姓名] ,

isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩]

from Student m left join SC n on m.S# = n.S#

group by m.S# , m.Sname

) t1

order by px

--24.2 查询学生的平均成绩并进行排名,sql 2005用rank,DENSE_RANK完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。

select t.* , px = rank() over(order by [平均成绩] desc) from

(

select m.S# [学生编号] ,

m.Sname [学生姓名] ,

isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩]

from Student m left join SC n on m.S# = n.S#

group by m.S# , m.Sname

) t

order by px

select t.* , px = DENSE_RANK() over(order by [平均成绩] desc) from

(

select m.S# [学生编号] ,

m.Sname [学生姓名] ,

isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩]

from Student m left join SC n on m.S# = n.S#

group by m.S# , m.Sname

) t

order by px

--25、查询各科成绩前三名的记录

--25.1 分数重复时保留名次空缺

select m.* , n.C# , n.score from Student m, SC n where m.S# = n.S# and n.score in (select top 3 score from sc where C# = n.C# order by score desc) order by n.C# , n.score desc

--25.2 分数重复时不保留名次空缺,合并名次

--sql 2000用子查询实现

select * from (select t.* , px = (select count(distinct score) from SC where C# = t.C# and score >= t.score) from sc t) m where px between 1 and 3 order by m.c# , m.px

--sql 2005用DENSE_RANK实现

select * from (select t.* , px = DENSE_RANK() over(partition by c# order by score desc) from sc t) m where px between 1 and 3 order by m.C# , m.px

--26、查询每门课程被选修的学生数

select c# , count(S#)[学生数] from sc group by C#

--27、查询出只有两门课程的全部学生的学号和姓名

select Student.S# , Student.Sname

from Student , SC

where Student.S# = SC.S#

group by Student.S# , Student.Sname

having count(SC.C#) = 2

order by Student.S#

--28、查询男生、女生人数

select count(Ssex) as 男生人数 from Student where Ssex = N'男'

select count(Ssex) as 女生人数 from Student where Ssex = N'女'

select sum(case when Ssex = N'男' then 1 else 0 end) [男生人数],sum(case when Ssex = N'女' then 1 else 0 end) [女生人数] from student

select case when Ssex = N'男' then N'男生人数' else N'女生人数' end [男女情况] , count(1) [人数] from student group by case when Ssex = N'男' then N'男生人数' else N'女生人数' end

--29、查询名字中含有"风"字的学生信息

select * from student where sname like N'%风%'

select * from student where charindex(N'风' , sname) > 0

--30、查询同名同性学生名单,并统计同名人数

select Sname [学生姓名], count(*) [人数] from Student group by Sname having count(*) > 1

--31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime) select * from Student where year(sage) = 1990

select * from Student where datediff(yy,sage,'1990-01-01') = 0

select * from Student where datepart(yy,sage) = 1990

select * from Student where convert(varchar(4),sage,120) = '1990'

--32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

select m.C# , https://www.wendangku.net/doc/cb1136672.html,ame , cast(avg(n.score) as decimal(18,2)) avg_score

from Course m, SC n

where m.C# = n.C#

group by m.C# , https://www.wendangku.net/doc/cb1136672.html,ame

order by avg_score desc, m.C# asc

--33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

select a.S# , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score

from Student a , sc b

where a.S# = b.S#

group by a.S# , a.Sname

having cast(avg(b.score) as decimal(18,2)) >= 85

order by a.S#

--34、查询课程名称为"数学",且分数低于60的学生姓名和分数

select sname , score

from Student , SC , Course

where SC.S# = Student.S# and SC.C# = Course.C# and https://www.wendangku.net/doc/cb1136672.html,ame = N'数学' and score < 60

--35、查询所有学生的课程及分数情况;

select Student.* , https://www.wendangku.net/doc/cb1136672.html,ame , SC.C# , SC.score

from Student, SC , Course

where Student.S# = SC.S# and SC.C# = Course.C#

order by Student.S# , SC.C#

--36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;

select Student.* , https://www.wendangku.net/doc/cb1136672.html,ame , SC.C# , SC.score

from Student, SC , Course

where Student.S# = SC.S# and SC.C# = Course.C# and SC.score >= 70

order by Student.S# , SC.C#

--37、查询不及格的课程

select Student.* , https://www.wendangku.net/doc/cb1136672.html,ame , SC.C# , SC.score

from Student, SC , Course

where Student.S# = SC.S# and SC.C# = Course.C# and SC.score < 60

order by Student.S# , SC.C#

--38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;

select Student.* , https://www.wendangku.net/doc/cb1136672.html,ame , SC.C# , SC.score

from Student, SC , Course

where Student.S# = SC.S# and SC.C# = Course.C# and SC.C# = '01' and SC.score >= 80 order by Student.S# , SC.C#

--39、求每门课程的学生人数

select Course.C# , https://www.wendangku.net/doc/cb1136672.html,ame , count(*) [学生人数]

from Course , SC

where Course.C# = SC.C#

group by Course.C# , https://www.wendangku.net/doc/cb1136672.html,ame

order by Course.C# , https://www.wendangku.net/doc/cb1136672.html,ame

--40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩

--40.1 当最高分只有一个时

select top 1 Student.* , https://www.wendangku.net/doc/cb1136672.html,ame , SC.C# , SC.score

from Student, SC , Course , Teacher

where Student.S# = SC.S# and SC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'张三'

order by SC.score desc

--40.2 当最高分出现多个时

select Student.* , https://www.wendangku.net/doc/cb1136672.html,ame , SC.C# , SC.score

from Student, SC , Course , Teacher

where Student.S# = SC.S# and SC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'张三' and

SC.score = (select max(SC.score) from SC , Course , Teacher where SC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'张三')

--41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

--方法1

select m.* from SC m ,(select C# , score from SC group by C# , score having count(1) > 1) n

where m.C#= n.C# and m.score = n.score order by m.C# , m.score , m.S#

--方法2

select m.* from SC m where exists (select 1 from (select C# , score from SC group by C# , score having count(1) > 1) n

where m.C#= n.C# and m.score = n.score) order by m.C# , m.score , m.S#

--42、查询每门功成绩最好的前两名

select t.* from sc t where score in (select top 2 score from sc where C# = T.C# order by score desc) order by t.C# , t.score desc

--43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select Course.C# , https://www.wendangku.net/doc/cb1136672.html,ame , count(*) [学生人数]

from Course , SC

where Course.C# = SC.C#

group by Course.C# , https://www.wendangku.net/doc/cb1136672.html,ame

having count(*) >= 5

order by [学生人数] desc , Course.C#

--44、检索至少选修两门课程的学生学号

select student.S# , student.Sname

from student , SC

where student.S# = SC.S#

group by student.S# , student.Sname

having count(1) >= 2

order by student.S#

--45、查询选修了全部课程的学生信息

--方法1 根据数量来完成

select student.* from student where S# in

(select S# from sc group by S# having count(1) = (select count(1) from course)) --方法2 使用双重否定来完成

select t.* from student t where t.S# not in

(

select distinct m.S# from

(

select S# , C# from student , course

) m where not exists (select 1 from sc n where n.S# = m.S# and n.C# = m.C#) )

--方法3 使用双重否定来完成

select t.* from student t where not exists(select 1 from

(

select distinct m.S# from

(

select S# , C# from student , course

) m where not exists (select 1 from sc n where n.S# = m.S# and n.C# = m.C#) ) k where k.S# = t.S#

)

--46、查询各学生的年龄

--46.1 只按照年份来算

select * , datediff(yy , sage , getdate()) [年龄] from student

--46.2 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

select * , case when right(convert(varchar(10),getdate(),120),5) <

right(convert(varchar(10),sage,120),5) then datediff(yy , sage , getdate()) - 1 else datediff(yy , sage , getdate()) end [年龄] from student

--47、查询本周过生日的学生

select * from student where datediff(week,datename(yy,getdate()) +

right(convert(varchar(10),sage,120),6),getdate()) = 0

--48、查询下周过生日的学生

select * from student where datediff(week,datename(yy,getdate()) +

right(convert(varchar(10),sage,120),6),getdate()) = -1

--49、查询本月过生日的学生

select * from student where datediff(mm,datename(yy,getdate()) +

right(convert(varchar(10),sage,120),6),getdate()) = 0

--50、查询下月过生日的学生

select * from student where datediff(mm,datename(yy,getdate()) +

right(convert(varchar(10),sage,120),6),getdate()) = -1

drop table Student,Course,Teacher,SC

常用SQL语句大全

常用SQL语句大全 一、基础 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 DROP database dbname 3、说明:备份sql server --- 创建备份数据的device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2…from tab_old definition only 5、说明:删除新表 DROP table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明:添加主键:Alter table tabname add primary key(col) 说明:删除主键:Alter table tabname DROP primary key(col) 8、说明:创建索引:create [unique] index idxname on tabname(col….) 删除索引:DROP index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明:创建视图:create view viewname as select statement 删除视图:DROP view viewname 10、说明:几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like ’%value1%’---like的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count as totalcount from table1 求和:select sum(field1) as sumvalue from table1 平均:select avg(field1) as avgvalue from table1 最大:select max(field1) as maxvalue from table1 最小:select min(field1) as minvalue from table1 11、说明:几个高级查询运算词

精典的SQL语句

精典的SQL语句ffice ffice" /> 1. 行列转换--普通 假设有张学生成绩表(CJ)如下 Name Subject Result 张三语文80 张三数学90 张三物理85 李四语文85 李四数学92 李四物理82 想变成 姓名语文数学物理 张三80 90 85 李四85 92 82 declare @sql var char(4000) set @sql = 'select Name' select @sql = @sql + ',sum(case Subject when '''+Subject+''' then Res ult end) ['+Subject+']' from (select distinct Subject from CJ) as a select @sql = @sql+' from test group by name' exec(@sql) 2. 行列转换--合并 有表A, id pid 1 1 1 2 1 3 2 1 2 2 3 1 如何化成表B BR>id pid 1 1,2,3 2 1,2 3 1 创建一个合并的函数 create function fmerg(@id int) returns var char(8000) as begin declare @str var char(8000) set @str=''

select @str=@str+','+cast(pid as var char) from 表A where id=@id se t @str=right(@str,len(@str)-1) return(@str) End go --调用自定义函数得到结果 select distinct id,dbo.fmerg(id) from 表A 3. 如何取得一个数据表的所有列名 方法如下:先从SYSTEMOBJECT系统表中取得数据表的SYSTEMID,然后再SYSCOL UMN表中取得该数据表的所有列名。 SQL语句如下: declare @objid int,@objname char(40) set @objname = 'tablename' select @objid = id from sysobjects where id = object_id(@objname) select 'Column_name' = name from syscolumns where id = @objid order b y colid 是不是太简单了?呵呵不过经常用阿. 4. 通过SQL语句来更改用户的密码 修改别人的,需要sysadmin role EXEC sp_password NULL, 'newpassword', 'User' 如果帐号为SA执行EXEC sp_password NULL, 'newpassword', sa 5. 怎么判断出一个表的哪些字段不允许为空? select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where IS_NULLABLE= 'NO' and TABLE_NAME=tablename 6. 如何在数据库里找到含有相同字段的表? a. 查已知列名的情况 SELECT https://www.wendangku.net/doc/cb1136672.html, as TableName,https://www.wendangku.net/doc/cb1136672.html, as columnname From syscolumns a INNER JOIN sysobjects b ON a.id=b.id AND b.type='U' AND https://www.wendangku.net/doc/cb1136672.html,='你的字段名字' b. 未知列名查所有在不同表出现过的列名 Select https://www.wendangku.net/doc/cb1136672.html, As tablename,https://www.wendangku.net/doc/cb1136672.html, As columnname From syscolumns s1, sysobjects o Where s1.id = o.id And o.type = 'U' And Exists ( Select 1 From syscolumns s2 Where https://www.wendangku.net/doc/cb1136672.html, = https://www.wendangku.net/doc/cb1136672.html, And s1.id <> s2.id ) 7. 查询第xxx行数据 假设id是主键: select *

ORACLE常用SQL语句大全

ORACLE常用SQL语句大全 一、基础 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:/mssql7backup/MyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not nul l],..) 根据已有的表创建新表: A:select * into table_new from table_old (使用旧表创建新表) B:create table tab_new as select col1,col2… from tab_old definition only<仅适用于Oracle> 5、说明:删除表 drop table tablename

6、说明:增加一个列,删除一个列 A:alter table tabname add column col type B:alter table tabname drop column colname 注:DB2DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、添加主键: Alter table tabname add primary key(col) 删除主键: Alter table tabname drop primary key(col) 8、创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、创建视图:create view viewname as select statement 删除视图:drop view viewname 10、几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count as totalcount from table1 求和:select sum(field1) as sumvalue from table1 平均:select avg(field1) as avgvalue from table1 最大:select max(field1) as maxvalue from table1 最小:select min(field1) as minvalue from table1 11、几个高级查询运算词 A:UNION 运算符 UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。 B:EXCEPT 运算符 EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。 C:INTERSECT 运算符 INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。 注:使用运算词的几个查询结果行必须是一致的。 12、使用外连接

SQL查询语句大全集锦(超经典)

SQL查询语句大全集锦 MYSQL查询语句大全集锦 一、简单查询 简单的Transact-SQL查询只包括选择列表、FROM子句和WHERE子句。它们分别说明所查询列、查询的 表或视图、以及搜索条件等。 例如,下面的语句查询testtable表中姓名为“张三”的nickname字段和email字段。 复制内容到剪贴板 代码:SELECT `nickname`,`email`FROM `testtable`WHERE `name`='张三' (一) 选择列表 选择列表(select_list)指出所查询列,它可以是一组列名列表、星号、表达式、变量(包括局部变量和全局变量)等构成。 1、选择所有列 例如,下面语句显示testtable表中所有列的数据: 复制内容到剪贴板 代码:SELECT * FROM testtable 2、选择部分列并指定它们的显示次序 查询结果集合中数据的排列顺序与选择列表中所指定的列名排列顺序相同。 例如: 复制内容到剪贴板 代码:SELECT nickname,email FROM testtable 3、更改列标题 在选择列表中,可重新指定列标题。定义格式为: 列标题=列名 列名列标题 如果指定的列标题不是标准的标识符格式时,应使用引号定界符,例如,下列语句使用汉字显示列 标题:

复制内容到剪贴板 代码:SELECT 昵称=nickname,电子邮件=email FROM testtable 4、删除重复行 SELECT语句中使用ALL或DISTINCT选项来显示表中符合条件的所有行或删除其中重复的数据行,默认 为ALL。使用DISTINCT选项时,对于所有重复的数据行在SELECT返回的结果集合中只保留一行。 5、限制返回的行数 使用TOP n [PERCENT]选项限制返回的数据行数,TOP n说明返回n行,而TOP n PERCENT时,说明n是 表示一百分数,指定返回的行数等于总行数的百分之几。 例如: 复制内容到剪贴板 代码:SELECT TOP 2 * FROM `testtable` 复制内容到剪贴板 代码:SELECT TOP 20 PERCENT * FROM `testtable` (二) FROM子句 FROM子句指定SELECT语句查询及与查询相关的表或视图。在FROM子句中最多可指定256个表或视图, 它们之间用逗号分隔。 在FROM子句同时指定多个表或视图时,如果选择列表中存在同名列,这时应使用对象名限定这些列 所属的表或视图。例如在usertable和citytable表中同时存在cityid列,在查询两个表中的cityid时应使用下面语句格式加以限定: 复制内容到剪贴板 代码:SELECT `username`,citytable.cityid FROM `usertable`,`citytable` WHERE usertable.cityid=citytable.cityid在FROM子句中可用以下两种格式为表或视图指定别名: 复制内容到剪贴板 代码:表名 as 别名 表名别名例如上面语句可用表的别名格式表示为: 复制内容到剪贴板

SQL常用语句+举例

SQL 常用语句+举例 相关表: 1. distinct: 剔除重复记录 例:select distinct stroe_name from Store_information 结果: 2. And / or: 并且/或 例:在表中选出所有sales 高于$1000或是sales 在$275及$500之间的记录 Select store_name ,sales from Store_information Where sales>1000 Or (sales>275 and sales <500) 3. 例:在表中查找store_name 包含 Los Angeles 或San Diego 的记录 Select * from Store_information where store_name in (‘Los Angeles ’,’San Diego ’) 结果: 4. Between : 可以运用一个范围抓出表中的值

与in 的区别:in 依照一个或数个不连续的值的限制抓出表中的值 例:查找表中介于Jan-06-1999 及Jan-10-1999 中的记录 Select * from Store_information where date between ‘Jan-06-1999’ and ‘Jan-10-1999’ 结果: 5. Like : 让我们依据一个套式来找出我们要的记录 套式通常包含: ’A_Z ’: 所有以A 开头,中间包含一个字符,以Z 结尾的字串 ’ABC%’: 所有以ABC 起头的字串 ’%XYZ ’: 所有以XYZ 结尾的字串 ’%AN%’: 所有包含AN 的字串 例:Select * from Store_information where store_name like ‘%An%’ 结果: 6. Order by: 排序,通常与ASC (从小到大,升序)、DESC (从大到小,降序)结合使用 当排序字段不止一个时,先依据字段1排序,当字段1有几个值相同时,再依据字段2排序 例:表中sales 由大到小列出Store_information 的所有记录 Select Store_name, sales,date from Store_information order by sales desc 结果: 7. 函数:AVG (平均值)、COUNT (计数)、MAX (最大值)、MIN (最小值)、SUM(求和) 语句:select 函数名(字段名) from 表名 例:求出sales 的总和 Select sum(sales) from Store_information 结果 8. COUNT (计数) 例:找出Store_information 表中 有几个store_name 值不是空的记录

经典SQL语句大全

一、基础 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1. dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2… from tab_old definition only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明:添加主键:Alter table tabname add primary key(col) 说明:删除主键: Alter table tabname drop primary key(col) 8、说明:创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明:创建视图:create view viewname as select statement 删除视图:drop view viewname 10、说明:几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count as totalcount from table1

数据库基本SQL语句大全

数据库基本SQL语句大全 数据库基本----SQL语句大全 一、基础 1、说明:创建数据库 Create DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2…from tab_old definition only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明:添加主键:Alter table tabname add primary key(col) 说明:删除主键:Alter table tabname drop primary key(col) 8、说明:创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明:创建视图:create view viewname as select statement 删除视图:drop view viewname 10、说明:几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围

50个常用sql语句实例(学生表 课程表 成绩表 教师表)

Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 create table Student(S# varchar(20),Sname varchar(10),Sage int,Ssex varchar(2)) 前面加一列序号: if exists(select table_name from information_schema.tables where table_name='Temp_Table') drop table Temp_Table go select 排名=identity(int,1,1),* INTO Temp_Table from Student go select * from Temp_Table go drop database [ ] --删除空的没有名字的数据库 问题: 1、查询“”课程比“”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b where a.score>b.score and a.s#=b.s#; 2、查询平均成绩大于分的同学的学号和平均成绩; select S#,avg(score) from sc group by S# having avg(score) >60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname)) from Teacher where Tname like '李%'; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.S#,Student.Sname from Student

数据库经典SQL语句大全

数据库经典SQL语句大全 篇一:经典SQL语句大全 下列语句部分是Mssql语句,不可以在access中使用。 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK) 首先,简要介绍基础语句: 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的 device USE master EXEC sp_addumpdevice 'disk','testBack', 'c:mssql7backupMyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2? from tab_old definition only 5、说明: 删除新表: tabname 6、说明: 增加一个列:Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明: 添加主键:Alter table tabname add primary key(col) 说明: 删除主键:Alter table tabname drop primary key(col) 8、说明: 创建索引:create [unique] index idxname on tabname(col?.) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。

大数据库基本SQL语句大全

数据库基本_SQL语句大全 学会数据库是很实用D~~记录一些常用的sql语句...有入门有提高有见都没见过的...好全...收藏下... 其实一般用的就是查询,插入,删除等语句而已....但学学存储过程是好事...以后数据方面的东西就不用在程序里搞喽..而且程序与数据库只要一个来回通讯就可以搞定所有数据的操作.... 一、基础 1、说明:创建数据库 Create DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的device USE master EXEC sp_addumpdevice ‘disk‘, ‘testBack‘, ‘c:\mssql7backup\MyNwind_1.dat‘ --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2…from tab_old definition only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明:添加主键:Alter table tabname add primary key(col)说明:删除主键:Alter table tabname drop primary key(col) 8、说明:创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明:创建视图:create view viewname as select statement 删除视图:drop view viewname 10、说明:几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围

50个经典sql语句总结

一个项目涉及到的50个Sql语句(整理版) --1.学生表 Student(S,Sname,Sage,Ssex) --S 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别--2.课程表 Course(C,Cname,T) --C --课程编号,Cname 课程名称,T 教师编号 --3.教师表 Teacher(T,Tname) --T 教师编号,Tname 教师姓名 --4.成绩表 SC(S,C,score) --S 学生编号,C 课程编号,score 分数 */ --创建测试数据 create table Student(S varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10)) insert into Student values('01' , N'赵雷' , '1990-01-01' , N'男') insert into Student values('02' , N'钱电' , '1990-12-21' , N'男') insert into Student values('03' , N'孙风' , '1990-05-20' , N'男') insert into Student values('04' , N'李云' , '1990-08-06' , N'男') insert into Student values('05' , N'周梅' , '1991-12-01' , N'女') insert into Student values('06' , N'吴兰' , '1992-03-01' , N'女') insert into Student values('07' , N'郑竹' , '1989-07-01' , N'女') insert into Student values('08' , N'王菊' , '1990-01-20' , N'女') create table Course(C varchar(10),Cname nvarchar(10),T varchar(10)) insert into Course values('01' , N'语文' , '02') insert into Course values('02' , N'数学' , '01') insert into Course values('03' , N'英语' , '03') create table Teacher(T varchar(10),Tname nvarchar(10)) insert into Teacher values('01' , N'张三') insert into Teacher values('02' , N'李四') insert into Teacher values('03' , N'王五') create table SC(S varchar(10),C varchar(10),score decimal(18,1)) insert into SC values('01' , '01' , 80) insert into SC values('01' , '02' , 90) insert into SC values('01' , '03' , 99) insert into SC values('02' , '01' , 70) insert into SC values('02' , '02' , 60) insert into SC values('02' , '03' , 80) insert into SC values('03' , '01' , 80) insert into SC values('03' , '02' , 80) insert into SC values('03' , '03' , 80) insert into SC values('04' , '01' , 50) insert into SC values('04' , '02' , 30) insert into SC values('04' , '03' , 20) insert into SC values('05' , '01' , 76) insert into SC values('05' , '02' , 87)

50个常用的SQL语句练习

基本信息Student(`S#`,Sname,Sage,Ssex) 学生表 Course(`C#`,Cname,`T#`) 课程表 SC(`S#`,`C#`,score) 成绩表 Teacher(`T#`,Tname) 教师表 问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.`S#` from (select `S#`,score from SC where `C#`='001') a,(select `S#`,score from SC where `C#`='002') b where a.score>b.score and a.`S#`=b.`S#`; ↑一张表中存在多对多情况的 2、查询平均成绩大于60分的同学的学号和平均成绩; 答案一:select `S#`,avg(score) from sc group by `S#` having avg(score) >60; ↑一对多,对组进行筛选 答案二:SELECT s ,scr FROM (SELECT sc.`S#` s,AVG(sc.`score`) scr FROM sc GROUP BY sc.`S#`) rs WHERE rs.scr>60 ORDER BY rs.scr DESC ↑嵌套查询可能影响效率 3、查询所有同学的学号、姓名、选课数、总成绩; 答案一:select Student.`S#`,Student.Sname,count(`C#`),sum(score) from Student left Outer join SC on Student.`S#`=SC.`S#` group by Student.`S#`,Sname ↑如果学生没有选课,仍然能查出,显示总分null(边界情况) 答案二:SELECT student.`S#`,student.`Sname`,COUNT(sc.`score`) 选课数,SUM(sc.`score`) 总分FROM Student,sc WHERE student.`S#`=sc.`S#` GROUP BY sc.`S#` ↑如果学生没有选课,sc表中没有他的学号,就查不出该学生,有缺陷! 4、查询姓“李”的老师的个数; select count(distinct(Tname)) from Teacher where Tname like '李%'; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.`S#`,Student.Sname from Student where `S#` not in (select distinct(SC.`S#`) from SC,Course,Teacher where SC.`C#`=Course.`C#` and Teacher.`T#`=Course.`T#` and Teacher.Tname='叶平'); ↑反面思考Step1:先找学过叶平老师课的学生学号,三表联合查询 Step2:在用not in 选出没学过的 Step3:distinct以防叶平老师教多节课;否则若某同学的几节课都由叶平教,学号就会出现重复 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; select Student.`S#`,Student.Sname from Student,SC where Student.`S#`=SC.`S#` and SC.`C#`='001'and exists( Select * from SC as SC_2 where SC_2.`S#`=SC.`S#` and SC_2.`C#`='002' ); ↑注意目标字段`S#`关联 exists subquery 可以用in subquery代替,如下 select Student.`S#`,Student.Sname from Student,Sc where Student.`S#`=SC.`S#` and SC.`C#`='001'and sc.`s#` in ( select sc_2.`s#` from sc as sc_2 where sc_2.`c#`='002' ); ↑不同之处,in subquery此处就不需要关联了

强化-SQL语句强化训练(史上最全最经典,呕血推荐) sql语句练习

2010/7 1. 有4个关系模式如下:出版社(出版社编号,出版社名称);图书(图书编号,书名,出版社编号,定价);作者(作者编号,姓名);著书(图书编号,作者编号,作者排序) 注:作者排序=1表示第一作者,依此类推。用SQL语句,完成第36~39题。 (1).检索所有定价超过20元的书名。 答案:SELECT书名(1分) FROM图书(1分) WHERE定价>20(2分) (2).统计每个出版社图书的平均定价。 答案:SELECT出版社编号,A VG(定价)(2分) FROM图书(1分) GROUP BY出版社编号(1分) (3).将科学出版社的所有图书定价下调5%。 答案:UPDATE图书SET定价=定价*0.95(1分) WHERE出版社编号IN(1分) (SELECT出版社编号FROM出版社(1分) WHERE出版社名称="科学")(1分) 【说明】WHERE出版社名称LIKE"科学"也正确。 (4).列出所有图书的书名、第一作者姓名和出版社名称。 答案:SELECT书名,姓名,出版社名称(1分) FROM出版社A,图书B,作者C,著书D(1分) WHEREA.出版社编号=B.出版社编号ANDB.图书编号=D.图书编号(1分) ANDC.作者编号=D.作者编号AND作者排序=1。(1分) S(SNO,SNAME,AGE,SEX,SDEPT) SC(SNO,CNO,GRADE) C(CNO,CNAME,CDEPT,TNAME) 1.试用SQL的查询语句表达下列查询: ①检索LIU老师所授课程的课程号和课程名。 ②检索年龄大于23岁的男学生的学号和姓名。 ③检索至少选修LIU老师所授课程中一门课程的女学生姓名。 ④检索W ANG同学不学的课程的课程号。 ⑤检索至少选修两门课程的学生学号。 ⑥检索全部学生都选修的课程的课程号与课程名。 ⑦检索选修课程包含LIU老师所授课程的学生学号。 2.试用SQL查询语句表达下列对教学数据库中三个基本表S、SC、C的查询: ①统计有学生选修的课程门数。 ②求选修C4课程的学生的平均年龄。 ③求LIU老师所授课程的每门课程的学生平均成绩。 ④统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列。 ⑤检索学号比WANG同学大,而年龄比他小的学生姓名。 ⑥检索姓名以WANG打头的所有学生的姓名和年龄。 ⑦在SC中检索成绩为空值的学生学号和课程号。 ⑧求年龄大于女同学平均年龄的男学生姓名和年龄。 ⑨求年龄大于所有女同学年龄的男学生姓名和年龄。 3.试用SQL更新语句表达对教学数据库中三个基本表S、SC、C的各个更新操作: ①往基本表S中插入一个学生元组('S9','WU',18)。

相关文档
相关文档 最新文档