四数据库写SQL题

更新时间:2024-06-30 02:40:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

四 数据库写 SQL 题(30)

1.按要求写 SQL 语句:根据集团成员培训业务,建立以下三张表:

S (S#,SN,SD,SA) S#,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄 C (C#,CN ) C#,CN 分别代表课程编号、课程名称

SC ( S#,C#,G ) S#,C#,G 分别代表学号、所选修的课程编号、学习成绩 要求如下:

1)使用标准 SQL 语句查询成员名单中所属单位叫“技术一部”的人员总数及 平均年龄;

2)使用标准的 SQL 语句更新学号为?S#1?的姓名为“Mike” ; 3)使用嵌套语句查询选修课程编号为?C2?的学员姓名和所属单位; 4)使用嵌套语句查询不选修课程编号为?C5?的学员姓名和所属单位; 5)查询选修课程超过 5 门的学员学号和所属单位; 解答:

1) select count(SN),avg(SA) from S where SD='技术一部'; 2) update S set SN='Mike' where S#='S#1';

3) select SN,SD from S where S#=(select S# from SC where C#='C2'); 4) select SN,SD from S where S# not in(select S# from SC where C#='C5'); ?5) select S#,SD from S where S#=

(select S# from SC group by S# having count(S#)>=5); 2.请根据以下四张表(其中 course_t 表的 teacher_id 字段是 teacher_t 表的 id 字段的外键引用) ,

拼写出相应的 sql 语句(oracle 语法)(15 分) 。 学生表:students_t

id name sex 001 赵学生 Male 002 钱学生 Male 003 孙学生 Male 004 李学生 Female 005 周学生 Female ? ? ? 教师表:teacher_t

id name sex 001 吴老师 Male 002 郑老师 Male 003 王老师 Male 004 刘老师 Female 005 张老师 Female 课程表:course_t

id name credit teacher_id 001 语文 3 001 002 数学 3 002 003 英语 4 003

004 物理 3 004 005 化学 2 005 006 政治 1 001 007 生物 1 005 008 计算机 2 005 选课表:student_course_t

id student_id course_id 001 001 001 002 001 002 003 001 003 004 002 001 005 002 007 ? ? ?

1)统计每个学生选修的学分,并按学分降序排序

2)统计每个学生选修的所有课程和对应的任课老师;并按学生 Id 和课程 Id 排序 3)统计所有学生、所有课程和所有任课老师的对应关系;并按学生 Id 和课程 Id 排序 解答:

1)select sc.student_id,count(c.credit)

from students_t s, course_t c, student_course_t sc

where s.id=sc.student_id and c.id=sc.course_id group by sc.student_id order by count(c.credit);

2) select s.name as s_name,c.name as c_name ,t.name as t_name from students_t s, course_t c, student_course_t sc,teacher_t t

where s.id=sc.student_id and c.id=sc.course_id and t.id=c.teacher_id order by s.id,c.id; 3)与 2)相同

3.假设有以下的两个表: Cus_A

ID* Name Address ? ? ? Cus_B

ID* Name Address ? ? ? *主键

表 Cus_A 和表 Cus_B 的结构完全相同,表 Cus_A 和表 Cus_B 中既存在 ID 相同的记录,也存在 ID 不

同的记录。 现要求将 ID 只存在于表表 Cus_A 中而不存在于表 Cus_B 中的记录全部插入到 Cus_B 表中,

并用表 Cus_A 中的记录更新 Cus_B 中相同的 ID 的记录,请写出完成这一功能的存储过程。 解答:

create or replace procedure test is

cust_record cus_a%rowtype ;

cursor cust_cursor is select id,name,address from cus_a;

Begin

Open cust_cursor; LOOP

Fetch cust_cursor into cust_record;

EXIT WHEN cust_cursor %NOTFOUND; --先删除在插入

delete from cus_b where id=cust_record.id;

insert into cus_b values(cust_record.id, cust_record.name, cust_record.address); END LOOP; end;

4、已有“成绩”如下表所示:

学号 课程号 分数 S1 C1 80 S1 C2 75 S2 C1 null S2 C2 55 S3 C3 90 1) 执行 SQL 语句:

Select Count(学号)From 成绩 Where 分数〉60 后的结果是什么?

2)请写出 SQL 语句来进行查询“成绩”表中学号为 S1、课程号为 C2 的学号和分数 解答:

1)统计分数超过 60 的学生总数。

2)select 学号,分数 from 成绩 where 学号=?S1? and 课程号=?C2?;

5.SAL 是 Product 表中的索引列,请优化如下 SQL 语句,并简述原因。原语句: SELECT*

FROM Product

WHERE SAL * 12 〉25000; 解答:

Select * from product where sal>(25000/12);

理由:WHERE 子句中,如果索引列是函数的一部分.优化器将不使用索引而使用全表扫描.

6.有一张表,字段有用户名、口令及备注,请用 SQL 选择出用户名和口令完全相同的记录(应包括用

户名和数量的出现次数)

T_USER(USER_NAME,PASSWORD) 显示

USER_NAME COUNT(*) QWE 4 WER 5

解答:select user_name,count(*) from t_user group by user_name,password;

7.有一张表,T_MONEY,字段有 ID,FEE,请用 SQL 语言选择出 FEE 值为前三条记录。 T_MONEY(ID,FEE) 显示

ID FEE 2 100 1 90 2 80

? Select Id,fee from (Select id,fee from t_money order by fee desc) where rownum<=3; 8、table_name temp Id name 1 a 2 b 3 a 4 a 结果为

Id name 1 a 2 b 写出 sql 语句。

? 解答:select rownum as id , name from(select distinct name from temp); 9、已知原表(t_salary) year salary 2000 1000 2001 2000 2002 3000 2003 4000

先要实现显示结果(salary 为以前的工资和) year salary 2000 1000 2001 3000 2002 6000

写出 sql 语句。 ? 解答:

select t.year, sum(t.salary) over (order by t.year) as sum_salary from salary_t t; 10.有两个表 A 和 B,均有 key 和 value 两个字段,如果 B 的 key 在 A 中也有,就把 B 的 value 换为 A 中对应的 value

这道题的 SQL 语句怎么写? 解答:

merge into A a using B b

on (a.key=b.key) when matched then update set

a.value=b.value

11.创建一张数据表,并插入如下数据。 购物人 商品名称 数量

A 甲 2 B 乙 4 C 丙 1 A 乙 2 B 丙 5 1)写出创建表和插入内容的 sql 语句 2)写出 sql 语句使其产生如下结果

购物人 商品甲 商品乙 商品丙 A 2 2 Null B Null 4 5 C Null Null 1 解答:

create table tb_order(

customer varchar2(20),

product_name varchar2(20), quantity number(2) )

Insert into tb_order(customer,product_name,quantity)values(?A?,?甲?,2); Insert into tb_order(customer,product_name,quantity)values(?B?,?乙?,4); Insert into tb_order(customer,product_name,quantity)values(?C?,?丙?,1); Insert into tb_order(customer,product_name,quantity)values(?A?,?甲?,2); Insert into tb_order(customer,product_name,quantity)values(?B?,?乙?,5); ? 2)select customer \购物人\

sum(decode(product_name,'甲',quantity,0)) \商品甲\sum(decode(product_name,'乙',quantity,0)) \商品乙\sum(decode(product_name,'丙',quantity,0)) \商品丙\from tb_order

group by customer;

12.有如下两张表:部门表和职员表,每个职员都属于一个部门,表结构如下: Dept 表

Deptno Deptname ? ? Emp 表

Empno Empname Deptno ? ? ?

请使用 SQL 语句查询每个部门有多少职员,要求查询结果包含两例(部门名称,人数)? ? 解答:select d.deptname,count(*) from dept d,emp e where d.deptno=e.deptno group by d.deptno,d.deptname;

13.业务场景:存在下面的表及记录 GOODS(进货表)

GOODSID(主键) GOODSNAME MEMO 1 青霉素 2 西瓜霜 3 创可贴

4 西洋参 SU(进货表)

GOODSID(主键) SUQTY 1 60 2 70 SA(销售表)

GOODSID(主键) SAQTY 3 80 4 90

要求一:进货记录,给出 SQL 达到以下结果

GOODSID(主键) GOODSNAME SUQTY 1 青霉素 60 2 西瓜霜 70 3 创可贴 0 4 西洋参 0 要求二:进销对比,给出 SQL 达到以下结果

GOODSID(主键) GOODSNAME SUQTY SAQTY 1 青霉素 60 0 2 西瓜霜 70 70 3 创可贴 0 80 要求三:将 GOODS.MEMO 更新为[进货数量 SU.SUQTY] 解答:

1)select g.goodsid,g.goodsname,s.quqty from goods g inner join su s on g.goodsid=s.goodsid; 2 ) select g.goodsid,g.goodsname,s.quqty,a.saqty from goods g, su s,sa a on

g.goodsid=s.goodsid and g.goodsid=a.goodsid;

3)update goods set demo=(select s.suqty from su s where s.goodsId=goods.goodsId) 14.表结构:

1) 表名:apply

字段(字段名/类型/长度):

applyno varchar 8;//申请单号(关键字) applydate bigint 8;//申请日期 state varchar 2;//申请状态 2) 表名:applydetail

字段(字段名/类型/长度):

applyno varchar 8;//申请单号(关键字) name varchar 30;//申请人姓名

idcard varchar 18;//申请人身份证号 state varchar 2;//申请状态

其中,两个表的关联字段为申请单号。 题目:

1)查询身份证号码为 440401430103082 的申请日期

2)查询同一个身份证号码有两条以上记录的身份证号码及记录个数 3)删除 applydetail 表中所有姓李的记录

解答:

1) Select applydate from apply a join applydetail d on a.applyno=d.applyno and Idcard='440401430103082';

2) select idcard,count(*) from applydetail group by idcard having count(*)>2; 3) delete from applydetail where name='李%';

15. 在 system 方案中建立表 table1,表中包含如下字段 字段名称 数据类型 要求 name Varchar2 非空 id Number 非空 age Number sex Varchar2 salary Number 解答:

Create table system.tablel1 ( Id number not null,

Name varchar(8) not null, Age number, Sex varchar(2), Salary number );

16 、 某 公 司 的 机 构 结 构 为 树 型 结 构 , 对 应 的 表 结 构 为 TableCompany(ComCode— 机 构 代 码 ,

UpperComCode—上级机构代码),如何查询出总公司的所有下级机构?(java 或者 SQL 均可)。你觉得

这种思维和设计是否合理?有什么好建议的?

答:select t1.* from TableCompany t1, TableCompany t2 Where t1.ComCode = t2.UpperComCode

这种设计比较容易让人理解,但是表中的数据联系过于紧密,数据量很大,会给后期维护造成不

便,如果根据第三范式要求,将每一子公司独立成一张表,对于关系的维护和数据的管理都会变得比 较方便。

17、一个简单的论坛系统,以数据库存储如下数据:

用户名,发帖标题,发帖内容,回复标题,回复内容。 每天论坛访问量 200 万左右,更新帖子 10 万左右。

请给出数据库表结构设计,并结合范式简要说明设计思路。 答:用户表:存储用户信息;

用户所发的帖子表:存储用户所发的帖子; 回复表:存储对帖子所做的回复。 设计: User:

Create table tb_user( id number(10) primary key,

Uname varchar2(20) not null unique

);

Comments:

Create table tb_comments( id number(10),

comments_id number(20) not null unique, title varchar2(20) not null,

comments varchar2(255) not null, foreign key(id) references tb_user(id) );

Replay:

Create table tb_replay( id number(10),

comments varchar2(255) not null,

foreign key(id) references tb_comments(comments_id) );

思路:因为此应用所要存储的数据量比较大,所以为了避免数据的冗余,表的设计依托于第三范式。

18、有一个数据表 userinfo,包含 userid,username 字段,其中 userid 是唯一的,username 可能

重复,请写一句 sql 查询语句,把重复的记录全部取出来。 userid username 1 老王 2 老王 3 老李 4 老李 5 小张 要求返回记录集 userid username 1 老王 2 老王 3 老李 4 老李 答:

select * from userinfo where username in (select username from userinfo group by username having count(username)>1); 19、建表 Department 部门

字段名 中文名称 类型 长度 备注

depid 部门号 变长字符 10 主键

depname 部门名称 变长字符

depcj 部门平均成绩 浮点型保留 2 位小数 表 Employee 人员表

字段名 中文名称 类型 长度

备注

empid 员工号 变长字符 10 主键

name 姓名 变长字符 10 depid 部门号 变长字符 10 Cj 成绩 浮点型保留 2 位小数 xorder 名次 整型 实现表中的记录备下面相关题目使用 Department 表中嵌入记录 部门号 部门名称 A001 人力资源部 A002 财务部 Employee 表中嵌入记录

员工号 姓名 部门号 成绩 001 张三 A001 90 002 李四 A001 90 003 王五 A001 80 004 张飞 A002 70 005 刘备 A002 60 006 关羽 A002 50 1)写出建表以及嵌入记录语句

2)显示 A001 部门员工的姓名、成绩

3)显示所有员工的员工号、姓名、部门名称、成绩 4)将关羽的成绩修改成 52 分

5)按要求写视图 VdepEmpMax 求各部门的最高分,显示部门号、最高分成绩

6)按要求写存储过程 SP_Calc 求各部门的平均成绩,并更新到 Department 表 depcj 字段中 7)按要

求写存储过程 SP_Order 求员工的名次,并更新到 Employee 表 xorder 字段中 8)按要求写视图 VdepEmp2,求各部门的前 2 名,显示部门号、员工号、成绩 排序规则如下:

员工 部门 分数 名次 张三 A001 90 1 李四 A001 90 1 张飞 A002 70 1 刘备 A002 60 2 答: 1)

create table Department(depid varchar2(20) primary key, depname varchar2(20), depcj number(10,2));

create table Employee(empid varchar2(20) primary key, name varchar2(20), depid varchar2(20), cj number(10,2),

xorder number(10));

insert into Department(depid,depname) values('A001','人力资源部'); insert into Department(depid,depname) values('A002','财务部');

insert into Employee(empid, name, depid, cj) values('001','张三','A001',90); insert into Employee(empid, name, depid, cj) values('002','李四','A001',90); insert into Employee(empid, name, depid, cj) values('003','王五','A001',80); insert into Employee(empid, name, depid, cj) values('004','张飞','A002',70); insert into Employee(empid, name, depid, cj) values('005','刘备','A002',60); insert into Employee(empid, name, depid, cj) values('006','关羽','A002',50); 2)select name,cj from employee where depid='A001';

3)select e.empid,e.name,d.depname,e.cj from employee e, department d where e.depid=d.depid; 4)update employee set cj=52 where name=' 关羽'; 5)

create view VdepEmpMax as (select deptid,max(cj) from employee e group by deptid) 6)

create or replace procedure SP_Calc as begin

update department d set depcj=(

select nvl(avg(cj),0) from employee e where e.depid(+)=d.depid); end; 7)

create or replace procedure SP_Order as begin

update employee w set xorder =(select b.rn from

(select empid,rank() over (partition by depid order by cj desc ) rn from employee) b where w.empid=b.empid); end; 8)

create or replace view VdepEmp2 as select depid,name,cj,rn from

(select e.*,rank() over (partition by depid

order by cj desc) rn from employee e) where rn<3; 20、数据库基础:

1)使用 SQL 语句创建学生表 students

字段: 学号:s_id 姓名:s_name 年龄:age 班级:class 辅导员:assistant (请设计各字段类型 与长度)

2)查询学生表中年龄大于 20 的所有学生的学号与姓名 3)删除 0201 班的所有同学

4)查询 0302 班姓李的学生的个数

5)将班编号以?02?开头的所有班级的辅导员修改为?李四? 答:1)create table students(s_id number(10) primary key, s_name varchar(30) not null, age number(3) not null, class varchar(20) not null, assistant varchar(30));

2)select s_id,s_name from students where age>20; 3)delete from students where class=?0201?; 4)select count(s_name) from students

where s_name like ?李%? and class='0302';

5)update students set assistant='李四' where class like '02%'; 21、表名:高考信息表 students_info 准考证号 科目 成绩 no subject score

2006001 语文 119 2006001 数学 108 2006002 物理 142 2006001 化学 136 2006001 物理 127 2006002 数学 149 2006002 英语 110 2006002 语文 105 2006001 英语 98 2006002 化学 129

写出高考总分在 600 以上的学生准考证号的 SQL 答: select no

from students_info group by no

having sum(score)>600;

22、有一个表 LEANR,表里有三个字段分别是学号(student_id), 课程(kc),成绩(grade) 。 1).查询每一门课程的前两名

2).查询以 Grade 降序排列的第 31 至 40 条记录(不需要区分课程)

3).查询表中存在课程重复 4 次以上的记录,显示课程和重复的次数,并且按照重复次数的降序排列 答:

1).select student_id,kc,grade

from (select student_id,kc,grade,

row_number() over(partition by kc order by grade desc)rn from LEANR) where rn<=2;

2)select student_id,grade from (

select lea.*,rownum rm

from ( select *

from LEANR

order by grade desc ) lea

where rownum < 41 )

where rm between 31 and 40; 3). select kc,count(kc) from LEANR group by kc

having count(kc)>=2 order by count(kc) desc; 23、a 部门表 b 员工表

a 表字段( id --部门编号 departmentName-部门名称 ) b 表字段( id--部门编号 employee- 员工名称 )

问题:如何一条 sql 语句查询出每个部门共有多少人 答:

建表语句: create table a(

id number primary key,

departmentName varchar(20) );

create table b( id number,

employee varchar(20) );

insert into a values(1,'部门 1'); insert into a values(2,'部门 2'); insert into a values(3,'部门 3'); insert into b values(1,'emp1'); insert into b values(1,'emp2'); insert into b values(1,'emp3'); insert into b values(2,'emp4'); insert into b values(2,'emp5'); insert into b values(3,'emp6');

select departmentName,count(employee) from a,b where a.id=b.id group by departmentName;

24、为管理岗位业务培训信息,建立 3 个表:

S (SID,SN,SD,SA) SID,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄

C (CID,CN ) CID,CN 分别代表课程编号、课程名称

SC ( SID,CID,G ) SID,CID,G 分别代表学号、所选修的课程编号、学习成绩

1. 使用标准 SQL 嵌套语句查询选修课程名称为?税收基础?的学员学号和姓名

2. 使用标准 SQL 嵌套语句查询选修课程编号为?02?的学员姓名和所属单位

3. 使用标准 SQL 嵌套语句查询不选修课程编号为?03?的学员姓名和所属单位

4. 使用标准 SQL 嵌套语句查询选修全部课程的学员姓名和所属单位

5. 查询选修课程超过 5 门的学员学号和所属单位 答:

建表 sql 语句: create table s( sid int(10) primary key, sn varchar(20) not null, sd varchar(20) not null, sa int(3) not null );

create table c( cid int(10) primary key, cn varchar(20) not null

);

create table sc(

sid int(10) references s(sid), cid int(10) references c(cid), g int(10),

primary key(sid,cid) );

insert into s values(1,\insert into s values(2,\

insert into s values(3,\insert into s values(4,\insert into c values(01,\税收基础\insert into c values(02,\insert into c values(03,\insert into sc values(1,01,70); insert into sc values(1,02,75); insert into sc values(1,03,80); insert into sc values(2,01,80); insert into sc values(2,03,69); insert into sc values(3,02,73); 1)

select s.sid,s.sn

from s,c,sc where s.sid=sc.sid and c.cid=sc.cid and c.name='税收基础'; 2) select a.sn,a.sd from s a, c b

where b.cid in(select c.cid from sc c where a.sid=c.sid and b.cid=c.cid) and b.cid=02; 3) select a.sn,a.sd from s a, c b

where b.cid not in(select c.cid from sc c where a.sid=c.sid and b.cid=c.cid) and b.cid=03; 4) select sn,sd from s where sid in

(select sid from sc group by sid having count(cid)=(select count(cid) from c));

5) select sn,sd from s

where sid in(select sid from sc group by sid having count(distinct cid)>5);

25、请根据以下要求来完成题目:

会议室预定模块:某公司有多个会议室,以房间号区分。如果某部门需要预定会议室,则会提交预定

请求(包含预定开始使用时间、预定结束使用,所预定会议室房间号) 。 设计一个表,保存会议室预定信息。

要求采用 SQL 语句及 JAVA 代码段判断 在 2003-3-10 下午 3:00~4:00 3 号会议室是否空闲。

请写出有关 SQL 语句以及相关 JAVA 的代码段。 答:

1)Sql 语句:

create table meeting( id number primary key , room_id varchar(10), isUsed char, begin timestamp, end timestamp );

insert into meeting values(1,'201',1,to_date('2003-03-10 15:00:00','yyyy-mm-dd hh24:mi:ss')

,to_date('2003-03-10 16:00:00','yyyy-mm-dd hh24:mi:ss'));

insert into meeting values(2,'201',1,to_date('2003-03-10 17:00:00','yyyy-mm-dd hh24:mi:ss')

,to_date('2003-03-10 22:00:00','yyyy-mm-dd hh24:mi:ss')); 2)

package com.tarena; import java.sql.*; public class Test {

public static void main(String[] args) {

String driverName = \ String url = \ String username = \ String pwd = \ Connection con = null; Statement stmt = null; ResultSet rs = null; try {

Class.forName(driverName);

con = DriverManager.getConnection(url, username, pwd); stmt = con.createStatement();

String sql = \ \

\ ((begin between to_date('2003-03-10 15:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('2003-03-10 16:00:00','yyyy-mm-dd hh24:mi:ss')) \ \between to_date('2003-03-10 15:00:00','yyyy-mm-dd hh24:mi:ss')

and to_date('2003-03-10 16:00:00','yyyy-mm-dd hh24:mi:ss')))\ \ if (stmt.execute(sql)) {

rs = stmt.getResultSet();

}

StringBuffer sb = new StringBuffer(); while (rs.next()) {

sb.append(\ }

System.out.print(sb.toString()); } catch (Exception e) { e.printStackTrace(); } finally { try {

con.close();

} catch (Exception e1) { e1.printStackTrace(); } } } }

26、下面是两个数据库表,分别记录员工姓名和工资 T_EMPLOYEE

ID NAME 2 张三 3 李四 5 王五 ··· ·· ··· ·· T_SALARY

ID SALARY 2 3400 3 4300 5 2500 ··· ·· ··· ··

1.查询表 T_EMPLOYEE 中 id = 3 的员工记录 2.查询表 T_EMPLOYEE 中所有员工记录

3.联合查询表 T_EMPLOYEE 和 T_SALARY 中所有员工的姓名和工资记录,并按照薪水从高到低排列 答:

1).select * from t_employee where id = 3; 2).select * from t_employee; 3).select e.name,s.salary

from t_employee e,t_salary s where e.id=s.id

order by s.salary;

27、有三张表,学生表 S,课程表 C,学生课程表 SC,学生可以选修多门课程,一门课程可能被多个

学生选修,通过 SC 表关联。 1)写出建表以及插入语句;

2)写出 SQL 语句,查询选修了所有选修课程的学生;

3)写出 SQL 语句,查询选修了至少 2 门以上的课程的学生。 答: 1)

create table student (id number(10) primary key,name varchar2(20)); create table course (id number(10) primary key,name varchar2(20));

create table sc(sid number(10) references student(id),cid number(10) references course(id),grade number(4,2)); insert into student values(1,'feifei'); insert into student values(2,'jingjing'); insert into student values(3,'nannan'); insert into student values(4,'yuanyuan'); insert into student values(5,'jiejie'); insert into course values(1,'corejava'); insert into course values(2,'c++'); insert into course values(3,'jdbc'); insert into course values(4,'hibernate'); insert into sc values(1,1,98); insert into sc values(2,1,97); insert into sc values(3,1,94); insert into sc values(4,1,92); insert into sc values(5,1,93); insert into sc values(1,2,94); insert into sc values(2,2,92); insert into sc values(3,2,95); insert into sc values(5,2,97); insert into sc values(1,3,92); insert into sc values(2,3,92); insert into sc values(4,3,91); insert into sc values(1,4,99); insert into sc values(3,4,89);

2)select sid,count(*) from sc group by sid having count(*)=(select count(*) from course); 3)select sid,count(*) from sc group by sid having count(*)>=2; 28、SQL 题 --操作员表

select pkid,name,sys_corp_id '单位主键' from base_operator --角色表

select pkid,sys_corp_id '单位主键',name from base_role --角色与操作员的对应关系表

select pkid,base_role_id '角色主键',base_operator_id '操作员主键' from base_role_operator --单位表

select pkid,name from sys_corps --问题:

--1.显示出'开发'公司所拥有的操作员

--2.显示出'开发'公司每个角色所对应的操作员信息 --3.显示出'开发'公司每个角色所对应的操作员的个数 答:1).Select name from base_operator;

2).Select op.pkid,op.name,op.sys_corp_id

From base_operator op,base_role_operator ro ,base_role br Where ro.base_operator_id =op.pkid And br.pkid=ro.base_role_id; 3).Select max(br.name) ,count(*)

From base_role_operator ro,base_role br Where ro.base_role_id=br.pkid Group by br.base_role_id;

29、说明在一个系统中权限管理中应该有哪些表、表间关系、各表哪些功能? 答:

角色和权限表是 m:n 的关系 操作表和权限表是 1:m 的关系 模块表和操作表是 1:m 的关系 表的大体设计如下: -- 角色表

create table roles (

id number primary key, name varchar2(20) );

-- 系统模块表

create table modules (

id number primary key, name varchar2(50), url varchar2(50) );

-- 模块操作表

create table operations ( id number primary key, name varchar2(20), mid number,

constraint foreign key (mid) references modules(id) );

-- 权限表

create table rights (

id number primary key, name varchar2(20),

url varchar2(50),

operationid int references operations(id) );

-- 角色权限设置表 create table rolerights (

id number primary key, roleid number references roles(id), rightid number references rights(id) );

30、说出下面语句的作用: Select rownum,last_name,salary

From (select last_name,salary from s_emp order by salary desc) Where rownum<=10;

答:选出 s_emp 表中工资前 10 名员工的姓名和工资

本文来源:https://www.bwwdw.com/article/3m33.html

Top