createDATABASE students
createTABLE t_soures(
c_number char(10) notnullPRIMARYkey,
c_name char(30) notnull,
hours int ,
credit real
)
createTABLE t_score(
s_number char(10) foreignkeyreferences
)
USE [students];
GO
DROPTABLE [dbo].[t_soures];
GO
select s_number, t_socre.c_number, socer FROM t_socre, t_sourse where (dbo.t_socre.c_number = dbo.t_sourse.c_number)
select * FROM t_socre where socer <90select * from t_student where polity='团员'AND sex='男'
BETWEEN 是在一个范围 (range) 内抓出数据库中的值。BETWEEN 这个子句的语法如下:
这将选出栏位值包含在值一及值二之间的每一笔资料。
在 SQL 中,在这个用法下,事先已经知道至少一个需要的值,而将这些知道的值都放入 IN这个子句。 IN 指令的 语法为下:
在括弧内可以有一或多个值,而不同值之间由逗点分开。值可以是数目或是文字。若在括弧内只有一个值,那这个子句就等于
select * from t_student where polity in('团员','非')
select * from t_student where birthday BETWEEN '1996/12/1'AND'1997/2/2'
USE [students];
GO
DROPTABLE [dbo].[t_soures];
GO
/* 联合查询 *//* 内联
高效,低效
把用户信息、积分、等级都列出来*/select * FROM t_student, t_socre where dbo.t_student.s_number = dbo.t_socre.s_number
select * from t_student innerjoin dbo.t_socre ON dbo.t_socre.s_number = dbo.t_student.s_number
/* 左连
显示左表T1中的所有行,并把右表T2中符合条件加到左表T1中;右表T2中不符合条件,就不用加入结果表中,并且NULL表示。*/
select * from t_student leftouterjoin dbo.t_socre ON dbo.t_socre.s_number = dbo.t_student.s_number
/* 右表无004信息 所以用null代替 */
/* 右连
显示右表T2中的所有行,并把左表T1中符合条件加到右表T2中;左表T1中不符合条件,就不用加入结果表中,并且NULL表示。*/
select * from t_student rightouterjoin dbo.t_socre ON dbo.t_socre.s_number = dbo.t_student.s_number
/* 全连
显示左表T1、右表T2两边中的所有行,即把左联结果表+右联结果表组合在一起,然后过滤掉重复的 */
select * from t_student fullouterjoin dbo.t_socre ON dbo.t_socre.s_number = dbo.t_student.s_number
/*
update 员工表 set 部门编号=01 --如果01是字符型,则加上单引号
where 员工编号='0004'
update t_socre set s_number='003' where c_number ='01'
*/
select * from t_student where s_number = any (
select s_number FROM t_socre
Groupby s_number
having COUNT(c_number)>=2
)