文档内容
常考语句
select 年龄 from student_info where 年龄 < some (select 年龄 from student_info where
系名 = '计算机') and 系名 != '计算机'
select 姓名 from student_info
where not exists
(select * from student_info where 年龄 = '80')
select 姓名 from student_info
where 系名 = (select 系名 from student_info where 姓名 = '王一')
people(pno,pname,sex,job,wage,dptno)
pno- 职工号
pname-职工姓名
sex-性别
wage-工资
dptno-所在部门号
(1)查询出工资比其所在部门平均工资都高的所有职工信息
select p1.* from people p1 where p1.wage > (select avg(p2.wage) from people p2 where
p1.dptno = p2.dptno)
(2)查询工资大于赵明华工资的所有职工的信息
select * from people where wage > (select wage from people where pname='超明
华')
创建索引
例1:为表jbxx创建一个非聚集索引,索引字段为employee_name,索引名为i_employeename
create index i_employeename on jbxx(employee_name)
例2:新建一个表,名称为temp,为此表创建一个惟一聚集索引,索引字段为temp_number,
索引名为i_temp_number。
Create table t_temp
(temp_number int,
Created by cherish58,2010temp_name char(10),
temp_age int)
create unique clustered index i_temp_number on t_temp(temp_number)
例3:为表s创建一个复合索引,使用sex和birthday字段。
Create index i_s on s(sex,birthday)
关于左连接和右连接总结性的一句话:
左连接 where 只影向右表,右连接 where 只影响左表。
Left Join
select * from tbl1 Left Join tbl2 where tbl1.ID = tbl2.ID
左连接后的检索结果是显示tbl1的所有数据和tbl2中满足where 条件的数据。
简言之 Left Join影响到的是右边的表
Right Join
select * from tbl1 Right Join tbl2 where tbl1.ID = tbl2.ID
检索结果是tbl2的所有数据和tbl1中满足where 条件的数据。
简言之 Right Join影响到的是左边的表。
inner join
select * FROM tbl1 INNER JOIN tbl2 ON tbl1.ID = tbl2.ID
功能和 select * from tbl1,tbl2 where tbl1.id=tbl2.id相同。
Created by cherish58,2010