当前位置:文档之家› sql练习作业~

sql练习作业~

sql练习作业~
sql练习作业~

------------------------------------表结构--------------------------------------

--学生表tblstudent(编号stuid、姓名stuname、年龄stuage、性别stusex)

--课程表tblcourse(课程编号courseid、课程名称coursename、教师编号teaid)

--成绩表tblscore(学生编号stuid、课程编号courseid、成绩score)

--教师表tblteacher(教师编号teaid、姓名teaname)

---------------------------------------------------------------------------------

--1、查询“001”课程比“002”课程成绩高的所有学生的学号;

select t1.stuid

from(select stuid, score from tblscore where courseid ='001') t1,

(select stuid, score from tblscore where courseid ='002') t2

where t1.stuid = t2.stuid

and t1.score > t2.score;

--2、查询平均成绩大于60分的同学的学号和平均成绩;

select stuid,avg(nvl(score,0))

from tblscore

group by stuid

having avg(nvl(score,0))>60

order by stuid;

--3、查询所有同学的学号、姓名、选课数、总成绩;

select tblstudent.stuid,

tblstudent.stuname,

count(tblscore.courseid),

sum(nvl(score,0))

from tblstudent

left outer join tblscore

on tblstudent.stuid = tblscore.stuid

group by tblstudent.stuid, stuname;

--4、查询姓“李”的老师的个数;

select count(*) from tblteacher where teaname like '李%';

--5、查询没学过“叶平”老师课的同学的学号、姓名;

select s.stuid, s.stuname

from tblstudent s

where stuid not in

(select stuid

from tblscore

where courseid in

(select courseid

from tblcourse

where teaid =(select teaid from tblteacher where teaname ='叶平'))); --6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

select s.stuid, s.stuname

from tblstudent s,

(select stuid from tblscore where courseid ='001') t1,

(select stuid from tblscore where courseid ='002') t2

where t1.stuid = t2.stuid

and s.stuid = t1.stuid;

--7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

select stuid,stuname

from tblstudent

where stuid in

(select tblscore.stuid

from tblcourse

left join tblscore

on tblcourse.courseid = tblscore.courseid

where tblcourse.teaid =(select teaid from tblteacher where teaname ='叶平')

group by tblscore.stuid

having count(*)=(select count(*)

from tblcourse

where teaid =

(select teaid from tblteacher where teaname ='叶平'))); --8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;select t1.stuid, t1.stuname

from(select s.stuid, s.stuname, tblscore.score

from tblstudent s

left join tblscore

on s.stuid = tblscore.stuid

where courseid =002) t1

left join(select s.stuid, s.stuname, tblscore.score

from tblstudent s

left join tblscore

on s.stuid = tblscore.stuid

where courseid =001) t2

on t1.stuid = t2.stuid

where t1. score < t2. score;

--9、查询所有课程成绩小于60分的同学的学号、姓名;

select stuid, stuname

from tblstudent

where stuid not in(select tblstudent.stuid

from tblstudent, tblscore

where tblstudent.stuid = tblscore.stuid

and score >60);

--10、查询没有学全所有课的同学的学号、姓名;

select s.stuid, s.stuname

from tblstudent s

left join tblscore

on s.stuid = tblscore.stuid

group by s.stuid, s.stuname

having count(tblscore.courseid)<(select count(*)from tblcourse);

--11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;select distinct s.stuid, s.stuname

from tblstudent s

left join tblscore

on s.stuid = tblscore.stuid

where tblscore.courseid in

(select courseid from tblscore where stuid ='1001');

--12、查询至少学过学号为“1001”同学所有课程的其他同学学号和姓名;

select distinct s.stuid,s.stuname

from tblstudent s

left join tblscore

on s.stuid = tblscore.stuid

where tblscore.stuid <>'001'

and tblscore.courseid in

(select courseid from tblscore where stuid ='001');

--13、把“sc”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;update tblscore sct

set sct.score =

(select avg(score)

from tblscore sct2

where sct2.courseid = sct.courseid)

where sct.courseid =(select c.courseid

from tblcourse c, tblteacher t

where c.courseid = sct.courseid

and c.teaid = t.teaid

and t.teaname ='叶平');

--14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;select stuid

from tblscore

where stuid !='1002'

and courseid in(select courseid from tblscore where stuid ='1002')

group by stuid

having count(*)=(select count(*)from tblscore where stuid ='1002');

--15、删除学习“叶平”老师课的sc表记录;

delete from tblscore sct

where sct.courseid in(select c.courseid

from tblcourse c, tblscore sct2, tblteacher t

where c.courseid = sct2.courseid

and c.teaid = t.teaid

and t.teaname ='叶平');

--16、向sc表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、'002'号课的平均成绩;

insert into tblscore

(stuid, courseid, score)

select s.stuid,

'003',

(select avg(score)from tblscore sct where sct.courseid ='002')

from tblstudent s

where s.stuid not in

(select sct.stuid from tblscore sct where sct.courseid ='003');

--17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示:学生id,,数据库,企业管理,英语,有效课程数,有效平均分

select s.stuid,

sum((case

when sct.courseid ='数据库'then

sct.score

else

end))database,

sum((case

when sct.courseid ='企业管理'then

sct.score

else

end))enterprise,

sum((case

when sct.courseid ='英语'then

sct.score

else

end)) english,

(select avg(sct2.score)from tblscore sct2 where sct2.stuid = s.stuid) score from tblstudent s, tblscore sct

where s.stuid = sct.stuid

group by s.stuid

order by score desc;

--18、查询各科成绩最高和最低的分:以如下形式显示:课程id,最高分,最低分

select c.courseid,max(sct.score),min(sct.score)

from tblscore sct, tblcourse c

where c.courseid = sct.courseid

group by c.courseid;

--19、按各科平均成绩从低到高和及格率的百分数从高到低顺序(百分数后如何格式化为两位小数??)

select tblscore.courseid,

avg(tblscore.score),

(convert(varchar(10),

((select count(*)

from tblscore sc

Where sc.courseid = cs.courseid

and sc.score >=60)*10000/

(select count(*)

from tblscore sc

Where sc.courseid = cs.courseid))/100)+'%')

from tblscore cs as tmp

group by课程id,

平均分,

及格率order by平均分,

convert(float, substring(及格率,1,len(及格率)-1))desc;

--20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),oo¨ (003),数据库(004)

select sct.courseid,

avg(score),

(select count(*)

from tblscore sct2

where sct2.courseid = sct.courseid

and sct2.score >=60)/

(select count(*)

from tblscore sct2

where sct2.courseid = sct.courseid) pass

from tblscore sct

where sct.courseid in('001','002','003','004')

group by sct.courseid

order by pass desc;

--21、查询不同老师所教不同课程平均分从高到低显示

select t.teaname, c.coursename,avg(sct.score) avgscore

from tblteacher t, tblcourse c, tblscore sct

where t.teaid = c.teaid

and c.courseid = sct.courseid

group by t.teaname, c.coursename

order by avgscore desc;

--22、查询如下课程成绩第3 名到第6 名的学生成绩单:企业管理(001),马克思(002),uml (003),数据库(004)格式:[学生id],[学生姓名],企业管理,马克思,uml,数据库,平均成绩

select s.stuid,s.stuname,"企业管理","马克思",v.uml, "数据库",v.avgscore from

(select rownum id, s.stuid,s.stuname,v.mng "企业管理",v.mks "马克思",v.uml,v.db "数据库",v.avgscore from

tblstudent s,

(select s.stuid,sum((case when sct.courseid=001then sct.score else0end)) mng, sum((case when sct.courseid=002then sct.score else0end)) mks,

sum((case when sct.courseid=003then sct.score else0end)) uml,

sum((case when sct.courseid=004then sct.score else0end)) db,

(sum((case when sct.courseid=001then sct.score else0end))+

sum((case when sct.courseid=002then sct.score else0end))+

sum((case when sct.courseid=003then sct.score else0end))+

sum((case when sct.courseid=004then sct.scoreo else0end)))/4 avgscore

from

sct s,tblscore sct where s.stuid=sct.stuid group by s.stuid) v

where s.stuid=v.stuid

order by avgscore desc)

where id between3and6;

with s1 as(select t.stuid,t.courseid,t.score,t.rank from

(select s2.*,rank()over(partition by courseid order by score desc)rank

from tblscore s2 where courseid in('001','002','003','004'))t

where t.rank betWeen3and6),

s2 as(select stuid,sum(decode(courseid,'001',score)) cj1,sum(decode(courseid,'002',score)) cj2,

sum(decode(courseid,'003',score)) cj3,sum(decode(courseid,'004',score)) cj4 from tblscore group by stuid),

s3 as(select stuid,avg(nvl(score,0)) avgs from tblscore group by stuid)

select distinct s1.stuid,s4.stuname,s2.cj1,s2.cj2,s2.cj3,s2.cj4,s3.avgs from

s1,s2,s3,tblstudent s4 where s1.stuid=s2.stuid and s1.stuid=s3.stuid and s1.stuid=s4.stuid;

--23、统计列印各科成绩,各分数段人数:课程id,课程名称,[100-85],[85-70],[70-60],[ <60] select c.courseid 课程id,

c.coursename 课程名称,

v.a "[<60]",

v.b "[60-70]",

v.c "[70-85]",

v.d "[85-100]"

from tblcourse c,

(select sct.courseid,

sum(case

when sct.score <60then

1

else

end) a,

sum(case

when sct.score >=60and sct.score <70then

1

else

end) b,

sum(case

when sct.score >=70and sct.score <85then

1

else

end) c,

sum(case

when sct.score >=85and sct.score <100then

1

else

end) d

from tblscore sct

group by sct.courseid) v

where c.courseid = v.courseid;

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

select s.stuid,

s.stuname,

v.avgscore,

(select count(*)

from(select sct.stuid,avg(sct.score) avgscore

from tblscore sct

group by sct.stuid) v2

where v.avgscore < v2.avgscore)+1 seqno

from tblstudent s,

(select sct.stuid,avg(sct.score) avgscore

from tblscore sct

group by sct.stuid) v

where s.stuid = v.stuid

order by seqno;

--25、查询各科成绩前三名的记录:(不考虑成绩并列情况)

SELECT t1.stuid as student ID, t1.courseid as class ID, Score as score FROM SC t1

WHERE score IN

(SELECT TOP 3 score FROM SC WHERE t1.Courseid = Courseid ORDER BY score DESC)

ORDER BY https://www.doczj.com/doc/c17910877.html,ourseid;

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

select courseid,count(stuid)from tblscore group by courseid;

--27、查询出只选修了一门课程的全部学生的学号和姓名

select tblscore.stuid, tblstudent.stuname,count(courseid)

from tblscore, tblstudent

where tblscore.stuid = tblstudent.stuid

group by tblscore.stuid, tblstudent.stuname

having count(courseid)=1;

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

select stusex,count(*)from tblstudent group by stusex;

--29、查询姓“张”的学生名单

select stuname from tblstudent where stuname like'张%';

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

select stuname,count(*)from tblstudent group by stuname having count(*)>1

--31、1981年出生的学生名单(注:student表中sage列的类型是datetime)

select stuname, stuage from tblstudent where to_char(sysdate,'yyyy')- stuage ='1981';

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

select courseid,avg(score)from tblscore

group by courseid

order by avg(score), courseid desc;

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

select stuname, tblscore.stuid,avg(score)

from tblstudent, tblscore

where tblstudent.stuid = tblscore.stuid

group by tblscore.stuid, stuname

having avg(score)>85;

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

select stuname, score

from tblstudent, tblscore, tblcourse

where tblscore.stuid = tblstudent.stuid

and tblscore.courseid = tblcourse.courseid

and tblcourse.coursename ='数据库'

and score <60;

--35、查询所有学生的选课情况;

select tblscore.stuid, tblscore.courseid, stuname, coursename

from tblscore, tblstudent, tblcourse

where tblscore.stuid(+)= tblstudent.stuid

and tblscore.courseid = tblcourse.courseid;

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

select distinct tblstudent.stuid,

tblstudent.stuname,

tblscore.courseid,

c.coursename,

tblscore.score

from tblstudent, tblscore, tblcourse c

where tblscore.score >=70

and tblscore.stuid = tblstudent.stuid

and c.courseid = tblscore.courseid;

--37、查询不及格的课程,并按课程号从大到小排列

select*from tblscore Where score<60order by courseid desc

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

select tblscore.stuid, tblstudent.stuname

from tblscore, tblstudent

where tblscore.stuid = tblstudent.stuid

and score >80

and courseid ='003';

--39、求选了课程的学生人数

select count(distinct stuid)from tblscore;

--40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩

select tblstudent.stuname, score

from tblstudent, tblscore, tblcourse, tblteacher

where tblstudent.stuid = tblscore.stuid

and tblscore.courseid = tblcourse.courseid

and tblcourse.teaid = tblteacher.teaid

and tblteacher.teaname ='叶平'

and tblscore.score =

(select max(score)from tblscore where courseid = tblcourse.courseid);

--41、查询各个课程及相应的选修人数(有些课程可能没有人选)

select count(distinct stuid)from tblscore group by courseid;

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

Select学号= StuId,课程号= CourseId,成绩= Score

From tblScore sc

Where Exists(Select*

From tblScore

Where Score = sc.Score

And StuId = sc.StuId

And CourseId <> sc.CourseId)

Order by学号,成绩

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

select tblcourseid,

第1名=(select top 1 stuid from tblscore where courseid=cs.courseid order by score desc),第2名=(select top 1 stuid from(select top 2 stuid,score from tblscore where

courseid=cs.courseid order by score desc)as tmp order by score)

from tblcourse cs;

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

select courseid,count(*) c from tblscore group by courseid having count(*)>10order by c desc,courseid;

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

select distinct stuid from tblscore group by stuid having count(courseid)>1;

--46、查询全部学生都选修的课程的课程号和课程名

select courseid, coursename

from tblcourse

where courseid in

(select courseid

from tblscore

group by courseid

having count(distinct stuid)=(select count(stuid)from tblstudent));

--47、查询没学过“叶平”老师讲授的任一门课程的学生姓名

select distinct stuname

from tblstudent st, tblscore

where tblscore.stuid = st.stuid

and courseid not in(select distinct courseid

from tblteacher t, tblcourse c

where t.teaid = c.teaid

and teaname ='叶平');

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

select stuid,avg(score)

from tblscore sc

where(select count(*)

from tblscore s1

where s1.stuid = sc.stuid

and score <60)>=2group by stuid;

--49、检索“004”课程分数小于60,按分数降序排列的同学学号(ok)

select stuid, score

from tblscore

where courseid ='004'

and score <60

order by score desc;

--50、删除“002”同学的“001”课程的成绩

delete from tblscore where stuid='1002'and courseid='001';

SQL常用命令使用方法:

SQL常用命令使用方法: (1) 数据记录筛选: sql="select * from 数据表where 字段名=字段值order by 字段名" sql="select * from 数据表where 字段名like ‘%字段值%‘order by 字段名" sql="select top 10 * from 数据表where 字段名order by 字段名" sql="select * from 数据表where 字段名in (‘值1‘,‘值2‘,‘值3‘)" sql="select * from 数据表where 字段名between 值1 and 值2" (2) 更新数据记录: sql="update 数据表set 字段名=字段值where 条件表达式" sql="update 数据表set 字段1=值1,字段2=值2 ……字段n=值n where 条件表达式" (3) 删除数据记录: sql="delete from 数据表where 条件表达式" sql="delete from 数据表" (将数据表所有记录删除) (4) 添加数据记录: sql="insert into 数据表(字段1,字段2,字段3 …)valuess (值1,值2,值3 …)" sql="insert into 目标数据表select * from 源数据表" (把源数据表的记录添加到目标数据表) (5) 数据记录统计函数: AVG(字段名) 得出一个表格栏平均值 COUNT(*|字段名) 对数据行数的统计或对某一栏有值的数据行数统计 MAX(字段名) 取得一个表格栏最大的值 MIN(字段名) 取得一个表格栏最小的值 SUM(字段名) 把数据栏的值相加 引用以上函数的方法: sql="select sum(字段名) as 别名from 数据表where 条件表达式" set rs=conn.excute(sql) 用rs("别名") 获取统的计值,其它函数运用同上。 (5) 数据表的建立和删除: CREATE TABLE 数据表名称(字段1 类型1(长度),字段2 类型2(长度) ……) 例:CREATE TABLE tab01(name varchar(50),datetime default now()) DROP TABLE 数据表名称(永久性删除一个数据表) 4. 记录集对象的方法: rs.movenext 将记录指针从当前的位置向下移一行 rs.moveprevious 将记录指针从当前的位置向上移一行 rs.movefirst 将记录指针移到数据表第一行 rs.movelast 将记录指针移到数据表最后一行 rs.absoluteposition=N 将记录指针移到数据表第N行 rs.absolutepage=N 将记录指针移到第N页的第一行 rs.pagesize=N 设置每页为N条记录 rs.pagecount 根据pagesize 的设置返回总页数 rs.recordcount 返回记录总数 rs.bof 返回记录指针是否超出数据表首端,true表示是,false为否 rs.eof 返回记录指针是否超出数据表末端,true表示是,false为否 rs.delete 删除当前记录,但记录指针不会向下移动 rs.addnew 添加记录到数据表末端 rs.update 更新数据表记录 判断所填数据是数字型 if not isNumeric(request("字段名称")) then response.write "不是数字" else response.write "数字" end if -------------------------------------------------------------------------------- simpleli 于2002-03-23 15:08:45 加贴在ASP论坛上

常用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常用命令汇总 SQL是结果化查询语言(Structured Query Language)的缩写,其功能包括数据查询、数据定义、数据操纵和数据控制四个部分。SQL简洁、功能齐全,已成为目前应用最广的关系数据库语言。 一、SQL的发展及标准化 1.SQL的发展 SQL是当前最为成功、应用最为广泛的关系数据库语言,其发展主要经历了以下几个阶段; (1)1974年,由Chamberlin和Boyce提出,当时称为SEQUEL(Structured English Query Language); (2)1976年,IBM公司对SEQUEL进行了修改,并将其用于本公司的SYSTEM R关系数据库系统中; (3)1981年,推出了商用关系数据库SQL/DS,并将其名字改为SQL,由于SQL功能强大,简洁易用,因此得到了广泛的应用; (4)今天,SQL广泛应用于各种大型数据库,如Sybase,INFORMIX,SQL Server,Oracle,DB2,INGRES等,也用于各种小型数据库,如FoxPro,Access等。 2.SQL标准化 随着关系数据库系统和日益广泛,SQL的标准化工作也在紧张地进行着,二十多年来已制订了多个SQL标准:SQL-86,SQL-89,SQLL2和SQL-99。 二、SQL的基本概念 1.基本表——一个关系对应一个表。基本表是独立存在的表,不是由其他的表导出的 表。 2.视图——是一个或几个基本表导出的表,是一个虚拟的表。它本身不独立存在于数 据中,数据库中只存放视图对应的数据,这些数据仍存放在导出视图的基本表中。 当基本表中的数据发生变化时,从视图中查询出来的数据也随之改变。 三、SQL的主要特点 SQL有如下几个特点。 (1)SQL是类似于英语的自然语言,简洁易用。 (2)SQL是一种非过程语言。 (3)SQL是一种面向集合的语言。 (4)SQL既是自含式语言,又是嵌入式语言;可独立使用,也可以嵌入到宿主语言中。 (5)SQL是数据库查询(Query)、数据定义(Definition)、数据操纵(Manipulation)和数据控制(Control)四种功能。 创建数据表 语法格式:CREA TE TABLE<表名>(<列定义>[{,<列定义>|<表约束>}]) 例:create table s(xingm c(8),xueh c(8),chengj c(3)) 结果:

面试数据库常用操作语句

数据库复习资料准备 1、创建/删除数据库:Create/ Drop database name 2、创建新表:Create table name(id int not null primary key, name char(20)) // 带主键 Create table name(id int not null, name char(20), primary key (id, name)) // 带复合主键Create table name(id int not null default 0, name char(20)) // 带默认值 3、删除表:Drop table name 4、表中添加一列:Alter table name add column size int 5、添加/删除主键:Alter table name add/drop primary key(size) 6、创建索引:Create [unique] index idxname on tabname(col) 7、删除索引:Drop index idxname 8、选择:Select *from table where 范围 9、删除重复记录Delete from name where id not in (select max(id) from name group by col1) 10、插入:Insert into table(field1, field2) values (value1, value) 11、删除:Delete from table where 范围 12、更新:Update table set field=value where 范围 13、查找:Select *from table where field like “” 14、排序:Select *from table order by field [desc] 15、总数:Select count as totalcount from table 16、求和:Select sum(field) as sumvalue from table 17、平均:Select avg(field) as avgvalue from table 18、最大:Select max(field) as maxvalue from table 19、最小:Select min(field) as minvalue from table 20、复制表:Select * into b from a where 范围Select top 0 * into b from a where 范围 21、拷贝表:Insert into b(a, b, c) select d,e,f from b; 22、子查询: select ename from emp where deptno=(select deptno from dept where loc='NEW');// 单查询select ename from emp where deptno in (select deptno from dept where dname like 'A%');// 多行子查询 select deptno,ename ,sal from emp where (deptno,sal) IN (select deptno,MAX(sal) from emp group by deptno);// 多列子查询 23、外连接查询:Select a.a, a.b, a.c, b.c, b.d, b.f from a left out join b on a.a = b.c 24、between用法:Select a,b,c, from table where a not between 数值1 and 数值2 25、in用法:select * from table1 where a [not] in (‘值1’,‘值2’,‘值4’,‘值6’) 26、两张关联表,删除主表中在副表中没有的信息:delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1) 27、日程提前五分钟提醒:select * from 日程where datediff(‘minute‘,f开始时间,getdate())>5 28、前10条记录:select top 10 * form table1 where 范围 29、包括所有在TableA 中但不在TableB和TableC 中的行:select a from tableA except (select a from tableB) except (select a from tableC) 30、随机取出10条数据:select top 10 * from tablename order by newid() 31、列出数据库里所有的表名:select name from sysobjects where type=‘U’ 32、列出表里的所有的字段:select name from syscolumns where id=object_id(‘TableName’)

SQL Server2000常用语句

SQL Server 2000常用命令,语法使用方法 (1) 数据记录筛选: sql="select * from 数据表 where 字段名=字段值 order by 字段名 [desc]" sql="select * from 数据表 where 字段名 like '%字段值%' order by 字段名 [desc]" sql="select top 10 * from 数据表 where 字段名 order by 字段名 [desc]" sql="select * from 数据表 where 字段名 in ('值1','值2','值3')" sql="select * from 数据表 where 字段名 between 值1 and 值2" (2) 更新数据记录: sql="update 数据表 set 字段名=字段值 where 条件表达式" sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式" (3) 删除数据记录: sql="delete from 数据表 where 条件表达式" sql="delete from 数据表" (将数据表所有记录删除) (4) 添加数据记录: sql="insert into 数据表 (字段1,字段2,字段3 …) values (值1,值2,值3 …)" sql="insert into 目标数据表 select * from 源数据表" (把源数据表的记录添加到目标数据表) (5) 数据记录统计函数: AVG(字段名) 得出一个表格栏平均值 COUNT(*|字段名) 对数据行数的统计或对某一栏有值的数据行数统计 MAX(字段名) 取得一个表格栏最大的值 MIN(字段名) 取得一个表格栏最小的值 SUM(字段名) 把数据栏的值相加 引用以上函数的方法:

SQL常用增删改查语句

SQLSQL常用增删改查语句 作者:hiker 一.Insert 插入语句 1.Insert into 表名(列名)values (对应列名值) //插入一行. 2.Insert into 新表名(列名) Select (列名) 旧表名 3.Select 旧表名.字段… Into 新表名from 旧表名 4.Select identity ( 数据类型,标识种子,标识增长量) as 列名 Into新表名 From 旧表名 5.Insert 表名(列名) Select (对应列名值) union Select (对应列名值) union Select (对应列名值) 二.Update 更新语句 1.Update 表名set 列名=’更新值’ where 更新条件 三.delete 删除语句 1.delete from 表名where 删除条件 2.truncate table 表名//删除表中所有行 四.select 基本查询语句 1.select 列名from 表名where 查询条件 order by 排序的列名asc或desc升/降 2.select 列名as 别名from 表名where 查询条件 3.select 列名from 表名where 列名is null //查询空值 4.select 列名, ‘常量值’ as 别名from 表名//查询时定义输出一列常量值 5.select top 5 列名from 表名//查询前5行 6.select top 5 percent 列名from 表名//查询前百分之5的数据行 五.select 函数查询语句 1.select LEN(Class_Name)from Class //查询class_Name字符串长度 2.select upper(Class_Name)from Class //查询class_Name并转换为大写 3.ltrim和rtrim //清除字符串左右空格 4.select REPLACE(card_No,'0','9')from CardRecord//修改列中字符串中的字符 列名字符串中0修改为9 5.select STUFF(Card_No,2,3,'8888')from CardRecord 列名字符串中第2个开始删除3个字符,再从第二个开始插入8888字符串 6.select GETDATE()//显示系统日期

常用sql操作总结_尚硅谷_宋红康

SQL语句的多表查询方式 例如:按照department_id查询employees(员工表)和departments(部门表) 的信息。 方式一(通用型):SELECT ... FROM ... WHERE SELECT https://www.doczj.com/doc/c17910877.html,st_name,e.department_id,d.department_name FROM employees e,departments d where e.department_id = d.department_id 方式二:SELECT ... FROM ... NATURAL JOIN ... 有局限性:会自动连接两个表中相同的列(可能有多个:department_id和manager_id) SELECT last_name,department_id,department_name FROM employees NATURAL JOIN departments 方式三:SELECT ... JOIN ... USING ... 有局限性:好于方式二,但若多表的连接列列名不同,此法不合适 SELECT last_name,department_id,department_name FROM employees JOIN departments USING(department_id) 方式四:SELECT ... FROM ... JOIN ... ON ... 常用方式,较方式一,更易实现外联接(左、右、满) SELECT last_name,e.department_id,department_name FROM employees e JOIN departments d ON e.department_id = d.department_id --内连接 1) --等值连接 --不等值连接 2) --非自连接 --自连接 --外连接 --左外连接、右外连接、满外连接

数据库编程入门常用SQL语法

1. ASP与Access数据库连接: <%@ language=VBscript%> <% dim conn,mdbfile mdbfile=server.mappath("数据库名称.mdb") set conn=server.createobject("adodb.connection") conn.open "driver={microsoft access driver (*.mdb)};uid=admin;pwd=数据库密码;dbq="&mdbfile %> 2. ASP与SQL数据库连接: <%@ language=VBscript%> <% dim conn set conn=server.createobject("ADODB.connection") con.open "PROVIDER=SQLOLEDB;DATA SOURCE=SQL服务器名称或IP地址;UID=sa;PWD=数据库密码;DATABASE=数据库名称 %> 建立记录集对象: set rs=server.createobject("adodb.recordset") rs.open SQL语句,conn,3,2 3. SQL常用命令使用方法: (1) 数据记录筛选: sql="select * from 数据表 where 字段名=字段值 order by 字段名 [desc]" sql="select * from 数据表 where 字段名 like \'%字段值%\' order by 字段名 [desc]" sql="select top 10 * from 数据表 where 字段名 order by 字段名 [desc]" sql="select * from 数据表 where 字段名 in (\'值1\',\'值2\',\'值3\')" sql="select * from 数据表 where 字段名 between 值1 and 值2" (2) 更新数据记录: sql="update 数据表 set 字段名=字段值 where 条件表达式" sql="update 数据表 set 字段1=值1,字段2=值2 字段n=值n where 条件表达式" (3) 删除数据记录: sql="delete from 数据表 where 条件表达式" sql="delete from 数据表" (将数据表所有记录删除) (4) 添加数据记录: sql="insert into 数据表 (字段1,字段2,字段3 ) values (值1,值2,值3 )" sql="insert into 目标数据表 select * from 源数据表" (把源数据表的记录添加到目标数据表) (5) 数据记录统计函数: AVG(字段名) 得出一个表格栏平均值 COUNT(*|字段名) 对数据行数的统计或对某一栏有值的数据行数统计 MAX(字段名) 取得一个表格栏最大的值

常用经典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:\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]

sql常用语句与解释

SQL常用语句与解释 (1)数据记录筛选: sql="select * from 数据表where 字段名=字段值order by 字段名[desc]" //从数据表中读取“字段名=字段值”的所有字段并根据字段名排序【倒序】sql="select * from 数据表where 字段名like '%字段值%' order by 字段名[desc]" //从数据表中读取“字段名中包含字段值”的所有字段并根据字段名排序【倒序】sql="select top10 * from 数据表where 字段名=字段值order by 字段名[desc]" //从数据表中读取前10条满足“字段名=字段值”的所有字段并根据字段名排序【倒序】 sql="select * from 数据表where 字段名in('值1','值2','值3')" //从数据表中读取“字段名满足三个值中的一个”的所有字段 (2)更新数据记录: sql="update 数据表set 字段名=字段值where 条件表达式" //更新数据表中满足条件表达式的记录的字段(更新一条字段)sql="update 数据表set 字段1=值1,字段2=值2……字段n=值n where 条件表达式" //更新数据表中满足条件表达式的记录的多个字段(更新多条字段) (3)删除数据记录: sql="delete from 数据表where 条件表达式" //删除数据表中满足条件表达式的所有记录 sql="delete from 数据表" //将数据表所有记录删除 (4)添加数据记录: sql="insert into 数据表(字段1,字段2,字段3…) values(值1,值2,值3…)" //将值1,值2,值3...插入数据表的字段1,字段2,字段3... sql="insert into 目标数据表select * from 源数据表" //把源数据表的记录添加到目标数据表 (5)数据记录统计函数: AVG(字段名)//得出一个表格某个字段的平均值 COUNT(*|字段名)//对数据行数的统计或对某一字段有值的数据行数统计 MAX(字段名)//取得一个表格中某字段的最大值 MIN(字段名)//取得一个表格中某字段的最小值 SUM(字段名)//取得一个表格中某字段的总和 引用以上函数的方法: sql="select sum(字段名) as 别名from 数据表where 条件表达式" //别名是为了引用的需要

常用SQL语言语法格式

软件技术课程设计实验指导书 第四篇附录 目录 附录B 常用SQL语言语法格式 一、SQL模式的创建和撤消 CREATE SCHEMA <模式名> AUTHORIZATION <用户名> 例如, CREATE SCHEMA ST_COURSE AUTHORIZATION 李斌 DROP SCHEMA <模式名> [CASCADE|RESTRICT] DROP SCHEMA <模式名> [CASCADE|RESTRICT] 撤消方式有两种:CASCADE(连锁式)方式,执行DROP语句时,把SQL模式及其下属的基本表、视图、索引等所有元素全部撤消;RESTRICT(约束式)方式,执行DROP语句时,只有当SQL模式中没有任何下属元素时,才能撤消SQL模式,否则拒绝执行DROP语句。 例如,要撤消SQL模式ST_COURSE及其下属所有的元素,可用下列语句实现: DROP SCHEMA ST_COURSE CASCADE 二、基本表的创建、修改和撤消 (1)基本表的创建 句法:CREATE TABLE SQL模式名.基本表名 (列名类型, … … 完整性约束, ……) 主键子句(PRIMARY KEY)、检查子句(CHECK)和外键子句(FOREIGN KEY)。 例如:CREATE TABLE S (SNO CHAR(4) NOT NULL, SNAME CHAR(20) NOT NULL, STATUS CHAR(10), ADDR CHAR(20), PRIMARY KEY(SNO)); (2)基本表结构的修改 基本表建立后,可根据需要对基本表结构进行修改,即增加新的属性或删除原有的属性。 ①增加新的属性 句法:ALTER TABLE 基本表名ADD 新属性名新属性类型 例7.2 在基本表S中增加一个电话号码(TELE)属性,可用下列语句: ALTER TABLE S ADD TELE CHAR(12); 应注意,新增加的属性不能定义为“NOT NULL”。基本表在增加一个属性后,原有元组在新增加的属性列上的值都被定义为空值(NULL)。

常用SQL脚本

常用SQL脚本 例28.1 查看数据库所在机器操作系统参数(xp_msver) 查看数据库所在机器操作系统参数的存储过程如下 EXEC master..xp_msver 例28.2 查看当前数据库服务器名(xp_getnetname、HOST_NAME())EXEC master..xp_getnetname 或者: SELECT HOST_NAME() 例28.3 查看服务器上所有Windows本地组(xp_enumgroups) 查看服务器上所有Windows本地组的存储过程如下 EXEC master..xp_enumgroups 例28.4 查看服务器上安装的所有代码页(xp_enumcodepages) 查看服务器上安装的所有代码页的存储过程如下 EXEC master..xp_enumcodepages 嘿嘿,是不是报没开启错误啊~~没错误略过 先执行下面语句 USE master; GO EXEC sp_configure 'show advanced option', '1'; RECONFIGURE EXEC sp_configure 'Web Assistant Procedures',1 RECONFIGURE EXEC sp_configure 例28.5 查看指定目录的所有下一级子目录(xp_subdirs) 查看C:\WINDOWS\Drivers目录所有下一级子目录的存储过程如下EXEC [master].[dbo].[xp_subdirs] 'C:\WINDOWS\Drivers'

查看磁盘信息的存储过程如下 EXEC [master].[dbo].[xp_availablemedia] 或者采用下列语句 EXEC master..xp_fixeddrives 请注意:其中,low free为以字节为单位的空闲空间,media type为驱动器类型,软驱为1,硬盘为2,CD-ROM为8。 例28.8 查看硬盘文件信息(xp_dirtree) 查看C盘文件信息的存储过程如下 EXEC master..xp_dirtree 'c:' 查看C盘深度为1的文件信息的存储过程如下 EXEC master..xp_dirtree 'c:',1 查看C盘深度为1、是否为的信息的存储过程如下 EXEC master..xp_dirtree 'c:',1,1 查看“C:\WINDOWS\Web”目录中的文件信息的存储过程如下 EXEC master..xp_dirtree 'C:\WINDOWS\Web' 例28.9 查看服务器提供的OLEDB程序(xp_enum_oledb_providers) 查看服务器提供的OLEDB程序的存储过程如下 EXEC master..xp_enum_oledb_providers 例28.10 查看数据库服务名(@@SERVERNAME) print 'SQL Server数据库服务名: ' + convert(varchar(30),@@SERVERNAME) 查看结果如下: SQL Server数据库服务名: SZX\SQLS2008

用友维护人员常用SQL语句(上)

用友维护人员常用SQL语句(上) --查询用友版本号 use ufsystem go select * from UA_Version go -------------------------------------------------------------------查看系统用户信息表 use ufsystem select cUser_Id as 操作员编码, cUser_Name as 操作员名称, nState as 是否停用 , iAdmin as 是否帐套主管理, cDept as 所属部门, cBelongGrp as 所在组, nState as 是否停用 from UA_User --查看具有帐套主管身份的操作员 select cUser_Id as 操作员编码, cUser_Name as 操作员名称 from UA_User where iAdmin=1; --查看被停用的操作员 select cUser_Id as 操作员编码, cUser_Name as 操作员名称 from UA_User where nState=1; --帐套主子表相关信息 use ufsystem --帐套主表 select cAcc_Id as 账套号, cAcc_Name as 账套名称, cAcc_Path as 账套路径, iYear as 启用会计期年, iMonth as 启用会计期月, cAcc_Master as 账套主管, cCurCode as 本币代码,

cCurName as 本币名称, cUnitName as 单位名称, cUnitAbbre as 单位简称, cUnitAddr as 单位地址, cUnitZap as 邮政编码, cUnitTel as 联系电话, cUnitFax as 传真, cUnitEMail as 电子邮件, cUnitTaxNo as 税号, cUnitLP as 法人, cEntType as 企业类型, cTradeKind as 行业类型, cIsCompanyVer as 是否集团版, cDomain as 域名, cDescription as 备注, cOrgCode as 机构编码, iSysID as 账套内部标识 from ua_account --帐套子表 select cAcc_Id as 账套号, iYear as 账套年度, cSub_Id as 模块标识, bIsDelete as 是否删除, bClosing as 是否关闭, iModiPeri as 会计期间, dSubSysUsed as 启用会计日期, cUser_Id as 操作员, dSubOriDate as 启用自然日期 from ua_account_sub --当客户的数据在其它机器上做的升级然后拷回到原机器 /*拷回的数据,通过‘系统管理’在原机器上引入后,并不会在ufsystem数据库中的ua_account_sub这个帐套子表中回写上一年度的bClosing字段来关闭上一年度 */ --比如002帐套结转后年度为2010,则用于关闭上一(2009)年度的sql如下: select * from ua_account_sub where cAcc_Id='002' and iYear=2008

SQL+server+实用教程第三版试题有及答案

一、选择题 1、下面的描述错误的是(B) A、每个数据文件中有且只有一个数据文件。 B、日志文件可以存在任何文件组中。 C、主数据文件默认为Primary文件组。 D、文件组是为了更好的实现数据库文件组织。 2、SQL server数据库文件有三类,其中主数据文件的后缀是(C) A、ndf B、ldf C、mdf D、idf 3、下面标志符不合法的是(C) A、#mybase B、-mybase C、$mybase D、mybase 4、下面对索引的相关描述正确的是(C) A、经常被查询的列不适合建索引。 B、列值唯一的列适合建索引。 C、有很多重复值的列适合建索引。 D、是外键成主键的列不适合建索引。 5、下列SQL语句中,修改表结构的是(A)

A、ALTER B、CREATE C、UPDATE D、INSERT 6、用于数据库恢复的重要文件是(C) A、数据库文件 B、索引文件 C、日志文件 D、数据文件 7、扩展存储过程是(B)才有的存储过程 A、modec数据库 B、master数据库 C、msdb数据库 D、tempelb数据库 8、用于进行安全管理的Transalt-SQL语言是(C) A、DDL语言 B、DML语言 C、DCL语言 D、DQL语言 9、SQL Server 2000中事务的类型有(A) A、系统,用户定义的事务 B、分布式事务

C、数据库事务 D、其它事务 10、下列说法正确的是(D) A、SQL中局部变量可以不声明就使用 B、SQL中全局变量必须先声明在使用 C、SQL中所有变量必须先声明在使用 D、SQL中只有局部变量先声明再使用,全局变量是由系统提供的,用户不能自己分立 11、下面字符串能与通配符表达式[abc]%a进行匹配的是(D) A、bedef B、a_bed C、abc-e D、a%a 12、下列说法正确的是(B) A、视图是观察数据的一种方法,只能基本表建立。 B、视图是虚表,观察到的数据是实际基本表中的数据。 C、索引查找法一定比表扫描法查询速度快。 D、索引的创建只和数据库的存储有关系。 13、下面仅存在于服务器端的组件是(A) A、服务管理器 B、企业管理器 C、查询分析器

[SQL Server]管理常用SQL语句

[SQL Server]管理常用SQL语句 1. 查看数据库的版本 select @@version 2. 查看数据库所在机器操作系统参数 exec master..xp_msver 3. 查看数据库启动的参数 sp_configure 4. 查看数据库启动时间 select convert(varchar(30),login_time,120) from master..sysprocesses where spid=1 查看数据库服务器名和实例名 print 'Server Name...............:' + convert(varchar(30),@@SERVERNAME) print 'Instance..................:' + convert(varchar(30),@@SERVICENAME) 5. 查看所有数据库名称及大小 sp_helpdb 重命名数据库用的SQL sp_renamedb 'old_dbname', 'new_dbname' 6. 查看所有数据库用户登录信息 sp_helplogins

查看所有数据库用户所属的角色信息 sp_helpsrvrolemember 修复迁移服务器时孤立用户时,可以用的fix_orphan_user脚本或者LoneUser过程更改某个数据对象的用户属主 sp_changeobjectowner [@objectname =] 'object', [@newowner =] 'owner' 注意:更改对象名的任一部分都可能破坏脚本和存储过程。 把一台服务器上的数据库用户登录信息备份出来可以用add_login_to_aserver脚本查看某数据库下,对象级用户权限 sp_helprotect 7. 查看链接服务器 sp_helplinkedsrvlogin 查看远端数据库用户登录信息 sp_helpremotelogin 8.查看某数据库下某个数据对象的大小 sp_spaceused @objname 还可以用sp_toptables过程看最大的N(默认为50)个表 查看某数据库下某个数据对象的索引信息 sp_helpindex @objname 还可以用SP_NChelpindex过程查看更详细的索引情况

相关主题
文本预览
相关文档 最新文档