首页 > 数据库 >Oracle day14

Oracle day14

时间:2024-07-02 09:00:42浏览次数:19  
标签:insert -- into day14 values Oracle bno select

/*create table f0810(
id number
,times varchar2(50));
insert into f0810 values(1,'2019-12-25 11:01');
insert into f0810 values(2,'2019-12-25 11:03');
insert into f0810 values(3,'2019-12-25 11:05');
insert into f0810 values(4,'2019-12-25 11:09');
insert into f0810 values(5,'2019-12-25 11:17');
insert into f0810 values(6,'2019-12-25 11:19');
insert into f0810 values(7,'2019-12-25 11:29');
insert into f0810 values(8,'2019-12-25 11:37');*/
select id,substr(to_char(times),-5,5) mi from f0810;

with tt as(
select sum(case when t.mi<'15' then 1 else 0 end) first,
sum(case when t.mi<'30' and t.mi > '15' then 1 else 0 end) second,
sum(case when t.mi>'30' then 1 else 0 end) third
from (select id,substr(to_char(times),-2) mi from f0810) t)
select '2019-12-25 11:15:00' "Times",first cnt from tt
union all
select '2019-12-25 11:15:00' ,second from tt
union all
select '2019-12-25 11:15:00' ,third from tt
 
-----------------------------QTing-XLJ-------------------------------
with a as(
(select sum(case when mi<=15 then 1 end) "2019-12-25 11:g15",
sum(case when mi>15 and mi<=30 then 1 end) "2019-12-25 11:30",
sum(case when mi>30 and mi<=45 then 1 end) "2019-12-25 11:45"
from
(select id,substr(times,15) mi from f0810)))
select '2019-12-25 11:15:00' as times,"2019-12-25 11:15" as cnt from a
union all
select '2019-12-25 11:30:00',"2019-12-25 11:30" from a
union all
select '2019-12-25 11:45:00',"2019-12-25 11:45" from a
-----------------------------QTing-LJQ-------------------------------
with ff as(
(select sum(case when s between 0 and 15 then 1 end) a,
sum(case when s between 16 and 30 then 1 end) b,
sum(case when s between 31 and 45 then 1 end) c
from
(select id,substr(times,instr(times,':')+1,2) s from f0810)))

select '2019-12-25 11:15:00' times,a cnt from ff
union all
select '2019-12-25 11:30:00',b from ff
union all
select '2019-12-25 11:45:00',c from ff

-----------------------------查看索引-----------------------
select * from user_indexes where table_name = 'EMP_BAK';

-----创建B树索引
create index index_emp_bak_empno on emp_bak(empno);

-----创建位图索引
create bitmap index index_emp_bak_job on emp_bak(job);

-----创建函数索引
create index emp_bak_lower_ename on emp_bak(lower(ename));

-----创建唯一索引
create unique index index_unique_empno on emp_bak(empno);

-----创建复合索引
create index index_empno_ename_job on emp_bak(empno,ename,job);

select * from user_indexes where table_name = 'DEPT_BAK';

--在scott.dept表的dname字段上创建一个具有唯一性的B树索引
create unique index index_dept_bak_dname on dept_bak(dname);

--在scott.emp表的ename和deptno字段上创建一个复合索引。
create index index_ename_deptno on emp_bak(ename,deptno);

--在scott.emp表的sal字段上创建一个名为index_sal的B树索引,
--按字段值的降序排列。
create index index_sal on emp_bak(sal desc);
drop index index_sal;

-----创建视图

 
--4.创建一个 只读 视图v_emp_salesman,内容是所有的SALESMAN信息:
create view v_emp_salesman
as select * from emp where job = 'SALESMAN'
with read only;
select * from v_emp_salesman

--5.创建一个视图v_mgr_salary,列出每个主管的名字,
--以及他的下属中的最高薪水和最低薪水:
create view v_mg_salary 
as select e2.ename,max(e1.sal) 最高薪水,min(e1.sal) 最低薪水
 from emp e1 
join emp e2 on e1.mgr = e2.empno group by e2.ename;
select * from v_mg_salary

--6.创建一个视图,通过该视图可以查看每个部门的名称,
--最低工资,最高工资,平均工资:
create view v_dname_salary as
select d.dname,min(e.sal) 最低工资,max(e.sal) 最高工资,
avg(e.sal) 平均工资 from emp e 
join dept d on d.deptno = e.deptno group by d.dname;

select * from v_dname_salary;
-------------------------------------------------------------------------
/*create table card( --借书卡
 cno number  --卡号
 ,name varchar2(30)  --姓名
 ,class varchar2(30));  --班级
insert into card(cno, name, class)values(01, 'Tony','C01');
insert into card(cno, name, class)values(02, 'Jack', 'A02');
insert into card(cno, name, class)values(03, 'Lisa', 'B03');
insert into card(cno, name, class)values(04, 'TOM', 'B01');
insert into card(cno, name, class)values(05, 'JERRY', 'A03');
insert into card(cno, name, class)values(06, 'YIFAN', 'C02');*/
--2---------------------------------------------------
drop table borrow;
/*create table borrow(  --借书记录。 
cno number  -- 借书卡号,
,bno number  -- 书号,
,rdate date  --借书日期
,getdate date);  --还书日期
insert into borrow
values(01, 111, to_date('2022-09-25','yyyy-mm-dd'),'');
insert into borrow
values(01, 131, to_date('2022-09-25','yyyy-mm-dd'),'');
insert into borrow
values(03, 131, to_date('2022-05-24','yyyy-mm-dd'),'');
insert into borrow
values(03, 161, to_date('2022-05-24','yyyy-mm-dd'),'');
insert into borrow
values(04, 151, to_date('2022-04-13','yyyy-mm-dd'),'');
insert into borrow
values(04, 161, to_date('2022-04-13','yyyy-mm-dd'),'');
insert into borrow
values(05, 171, to_date('2022-12-23','yyyy-mm-dd'),'');
insert into borrow
values(06, 181, to_date('2022-08-26','yyyy-mm-dd'),'');*/
/*create table bookss(   --图书
 bno number  --书号
 ,bname varchar2(30)  --书名
 ,author varchar2(30) --作者
 ,price varchar2(30) --单价
 ,quantity number);  --库存册数
insert into bookss(bno, bname, author, price, quantity)
values(111, '水浒', 'qwe', '35', 5);
insert into bookss(bno, bname, author, price, quantity)
values(121, '网络安全', 'asd', '40', 3);
insert into bookss(bno, bname, author, price, quantity)
values(131, '计算方法', 'zxc', '78', 4);
insert into bookss(bno, bname, author, price, quantity)
values(141, '计算方法习题集', 'qaz', '24', 8);
insert into bookss(bno, bname, author, price, quantity)
values(151, '数据库技术及应用', 'wsx', '28', 6);
insert into bookss(bno, bname, author, price, quantity)
values(161, '组合数学', 'wsx', '56', 7);
insert into bookss(bno, bname, author, price, quantity)
values(171, '高等数学', 'edc', '99', 3);
insert into bookss(bno, bname, author, price, quantity)
values(181, '石岩水库', 'rfv', '47', 2);*/
select * from card;
select * from borrow;
select * from bookss;
--备注:限定每人每种书只能借一本;库存册数随借书、还书而改变。
--1.假设建立BORROW表时没有定义主外键,
--要求单独定义其主键约束和外键约束:
alter table bookss add constraint bno_pri primary key(bno);
alter table card add constraint card_pri primary key(cno);
alter table borrow add constraint fk_bno foreign key(bno) references bookss;
alter table borrow add constraint fk_cno foreign key(cno) references card;

--2.找出借书超过5本的读者,输出借书卡号及所借图书册数:
select b.cno,count(*) from borrow b group by b.cno having count(*) > 5;

--3.查询借阅了"水浒"一书的读者,输出姓名及班级:
select name,class from card where cno in
(select cno from borrow where bno in
(select bno from bookss where bname = '水浒'));

--4.查询过期未还图书,输出借阅者(卡号)、书号及还书日期:
select cno,bno,getdate from borrow where getdate is null;

--5.查询书名包括"网络"关键词的图书,输出书号、书名、作者:
select bno,bname,author from bookss where bname like '%网络%';

--6.查询现有图书中价格最高的图书,输出书名及作者:
select bname,author from bookss 
where price = (select max(price) from bookss);

--7.查询当前借了"计算方法"但没有借"计算方法习题集"的读者,
--输出其借书卡号,并按卡号降序排序输出:
select cno from borrow
where bno in 
(select bno from bookss where bname = '计算方法') 
and
bno not in (select bno from bookss where bname = '计算方法习题集')
order by cno desc;

--8.将"C01"班同学所借图书的还期都延长一周:
update borrow set getdate=getdate+7 where cno in
(select cno from card where class = 'C01');

--9.从BOOKS表中删除当前无人借阅的图书记录:
delete from bookss where bno not in
(select bno from borrow);

--10.如果经常按书名查询图书信息,请建立合适的索引:
create index index_bookss_bname on bookss(bname);
select * from user_indexes where table_name = 'BOOKSS';

--11.建立一个视图,显示"C01"班学生的借书信息,只要求显示姓名和书名:
create view v_name_bname as
select c.name,bo.bname from card c,borrow b, bookss bo 
where c.cno = b.cno and b.bno = bo.bno and c.class = 'C01';
select * from v_name_bname;

--12.查询当前同时借有"计算方法"和"组合数学"两本书的读者,
--输出其借书卡号,并按卡号升序排序输出:
select cno,count(*) from borrow where bno in
(select bno from bookss where bname in ('计算方法','组合数学'))
group by cno having count(*) = 2 order by cno ;

--13.假定在建BOOKS表时没有定义主键,
--写出为BOOKS表追加定义主键的语句:
alter table bookss add constraint bno_pri primary key(bno);

--14.将CARD 表中的CNAME最大列宽增加到10个字符:(假定原为6个字符)
alter table card modify cname char(10);

--15.为BOOKS表增加1列XNAME(系名),可变长,最大30个字符:
alter table bookss add xname varchar2(30);

标签:insert,--,into,day14,values,Oracle,bno,select
From: https://blog.csdn.net/m0_71787206/article/details/140049117

相关文章

  • navicat使用Oracle(创建库以及用户,详细教程!!!)
    1.首先准备好navicat和已经安装Oracle数据库2.打开navicat新建连接,3.用户名和密码是安装Oracle数据库时候创建的(登录高级用户)4.测试连接5.开始建库:orcale中库的概念可以理解为用户,也就是所谓的表空间,并且每建立一个表空间都需要建立一个用户绑定然后登陆。(一个用户只能......
  • Oracle数据库知识汇总
    一、数据库三大范式第一范式:每个列都不可再拆分;第二范式:在第一范式的基础上,非主键字段完全依赖于主键字段,而不是依赖部分主键字段;第三范式:在第二范式的基础上,非主键字段只依赖于主键字段,不依赖其他非主键字段。二、索引定义:建立在表一列或多列的辅助对象,目的是加快访问表的......
  • Oracle中的GROUP BY 子句
    数据准备--第一步:建表:--删除表(包括数据结构)droptableTMP_EMP;droptableTMP_DEPT;--删除表(不包括数据结构)truncatetableTMP_EMP;truncatetableTMP_DEPT;createtableTMP_DEPT(deptnonumber(10)primarykey,dnamevarchar2(30),loc......
  • Oracle PL / SQL 函数
    FUNCTION是返回值的PL/SQL块或方法,因此它可以在赋值的右侧使用。这里是一个例子:n_value:=to_number('123.45');由于FUNCTION返回一个值,因此也可以在SQL语句中使用它,如下例所示:selectto_number('1')fromdual;创建函数让我们创建一个无错的to_number()函数,而不......
  • Oracle PL / SQL 存储过程
    PL/SQL存储过程不返回值。他们执行他们的指示并返回。您不能在赋值语句(如函数)的右侧使用存储过程。创建存储过程以下代码是一个非常简单的存储过程示例。它基于SYS.DBMS_LOCK包的程序sleep(数量)。此存储过程将停止执行指定的秒数。CREATEORREPLACEPROCEDUREwait(ai......
  • 【Oracle】Oracle数据库查询某张表的全部字段与类型
    【Oracle】Oracle数据库查询某张表的全部字段与类型原文链接:https://blog.csdn.net/LI_AINY/article/details/86597377PS:TABLE_NAME对应的表名要全部大写查询表的所有字段名以及属性(所有用户)SELECT*FROMALL_TAB_COLUMNSWHERETABLE_NAME='T_UNIT_NAME'查询表的所有字......
  • oracle 19c 安装、卸载
    Oracle数据库19c下载安装安装登录oracle官网进入下载界面https://www.oracle.com/cn/database/technologies/oracle-database-software-downloads.html#db_free选择OracleDatabase19cforMicrosoftWindowsx64(64-bit)下载将下载下来的zip文件解压缩,点击setup.exe运行安......
  • python连接mysql、sqlserver、oracle、postgresql数据库进行封装
    python连接mysql、sqlserver、oracle、postgresql数据库进行封装python连接mysql、sqlserver、oracle、postgresql数据库进行封装详解一、引言二、python连接MySQL数据库进行封装三、python连接SQLServer数据库进行封装四、Python连接Oracle数据库进行封装五、Python连......
  • Oracle 11gR2 RAC 集群服务启动与关闭总结
      关闭过程(CRS集群关闭->关闭数据库)1.关闭数据库:用oracl用户执行srvctl命令语法:srvctlstopdatabase-ddbname[-oimmediate]作用:可以一次性关闭dbname的所有实例[oracle@rac1 ~]$ srvctl stop database -d racdb  -停止所有节点上的实例然后查看状态:[oracle@ra......
  • 对比Oracle和MySQL索引对于like的支持,MySQL一言难尽啊...
     对比Oracle和MySQL索引对于like的支持,MySQL一言难尽啊... MySQL:|Oracle:(root@localhost09:44:08)[zkm](673009)>select*fromtest;|09:52:11ZKM@test(1076......