每周练习题3
1.ucase是转换成大写函数
2.理解datediff函数
理解datediff(dd,RDDATE,getdate())==0含义,即返回以日为单位(dd),和当前日期(getdate)相差为0日的RDDATE。 这里用这个函数的意义在于,RDDATE包括日期和时间,这里因为有时间,和getdate不能直接比较,所以用datediff转换为范围3.关系代数有五个基础运算符
关系代数有五个基础运算符,这五个基础运算符能派生出其他组合运算符。它们分别是:
选择(σ, selection)、投影(π, projection)、叉乘(x, cross-product)、
差(-, set-difference)和并(υ, union)
它们和SQL语句的对应关系为:
选择(σ, selection)相当于SQL语句中的where,表示选出满足一定条件的行。
如:σ rating>8 (S2)相当于 select * from S2 where rating>8;
投影(π, projection)相当于SQL语句中的select。。。distinct, 表示选择哪些列。注意:投影是会去重的!
如:π sname,rating (σ rating>8 (S2))相当于 select sname, rating from S2 where rating>8;
叉乘(x, cross-product)相当于SQL语句中的from,表示穷举所有集合两边元素的组合量
如: AxB 相当于 select * from A, B; 注意:叉乘时两个集合不能有重名列
差(-, set-difference)R-S返回所有在R中而不在S中的元组
并(υ, union)RυS返回包含在R中或在S中的所有元组
4.REVOKE 回收权限
1.GRANT 赋于权限常用的系统权限集合有以下三个:
CONNECT(基本的连接), RESOURCE(程序开发), DBA(数据库管理)
常用的数据对象权限有以下五个:
ALL ON 数据对象名, SELECT ON 数据对象名, UPDATE ON 数据对象名,
DELETE ON 数据对象名, INSERT ON 数据对象名, ALTER ON 数据对象名
GRANT CONNECT, RESOURCE TO 用户名;
GRANT SELECT ON 表名 TO 用户名;
GRANT SELECT, INSERT, DELETE ON表名 TO 用户名1, 用户名2;
2.REVOKE 回收权限
REVOKE CONNECT, RESOURCE FROM 用户名;
REVOKE SELECT ON 表名 FROM 用户名;
REVOKE SELECT, INSERT, DELETE ON表名 FROM 用户名1, 用户名2;
5.Mysql中表student_table(id,name,birth,sex),查询张姓、李姓的学生总人数,错误的是()?
select sum(case when name like '张%' then 1 else 0 end) as zhang_first_name ,
sum(case when name like '李%' then 1 else 0 end) as li_first_name
from
student_table;
select count(case when name like '张%' then 2 else null end) as zhang_first_name ,
count(case when name like '李%' then 2 else null end) as li_first_name
from
student_table;
select count(case when name like '张%' then 1 else 0 end) as zhang_first_name ,
count(case when name like '李%' then 2 else 0 end) as li_first_name
from
student_table;
select sum(case when name like '张%' then 1 else null end) as zhang_first_name ,正确答案: C 你的答案: D (错误) 参考分析: A、D选项都是使用SUM函数,满足条件就计1,不满足就计0,所以正确 B、C选项使用COUNT函数,是计的返回的条数,不管返回的是0、1还是2,都会计1,而B选项中不满足条件返回的是NULL不会计数,所以B对,C错
sum(case when name like '李%' then 1 else null end) as li_first_name
from
student_table;
6.Mysql中表student_table(id,name,birth,sex),插入如下记录:
('1001' , '' , '2000-01-01' , '男'); ('1002' , null , '2000-12-21' , '男'); ('1003' , NULL , '2000-05-20' , '男'); ('1004' , '张三' , '2000-08-06' , '男'); ('1005' , '李四' , '2001-12-01' , '女'); ('1006' , '张三' , '2001-12-02' , '女'); 执行 select t1.name ,t2.name from (select * from student_table where sex = '女') t1 left join (select * from student_table where sex = '男') t2 on t1.name = t2.name where t2.name is null union select t1.name ,t2.name from (select * from student_table where sex = '女') t1 right join (select * from student_table where sex = '男') t2 on t1.name = t2.name where t1.name is null;
该题可能要问的是“查询结果有几条行数据”
如果问的是查询结果有几条数据的话,结果又有不同:
- 如果使用Oracle是2条,因为Oracle数据库中空字符串和null是相等的,而且用union连接时会进行唯一性过滤——该题2条数据。
- 如果是mySql是3条,因为MySql数据库中空字符和null不相等,所以在进行union连接时的唯一性过滤后,会比Oracle多一条——该题3条数据。
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
('1001' , '' , '2000-01-01' , '男');('1002' , null , '2000-12-21' , '男');
('1003' , NULL , '2000-05-20' , '男');
('1004' , '张三' , '2000-08-06' , '男');
('1005' , '李四' , '2001-12-01' , '女');
('1006' , '张三' , '2001-12-02' , '女'); 执行
select t1.name ,t2.name from
(select * from student_table where sex = '女') t1
left join
(select * from student_table where sex = '男') t2
select t1.name ,t2.name from
(select * from student_table where sex = '女') t1
right join
(select * from student_table where sex = '男') t2