Oracle常用常考集合
登陆远程服务器
sqlplus scott/tiger@192.168.2.1[:port]/sid [as sysdba]
简单查询
select table_name from user_tables;
select * from tab;
select * from dept;
创建表
create table t_user(id int primary key,username varchar2(20),password varchar2(20));
创建序列(用户主键增长策略)
create sequence seq_user increment by 1 start with 1 nomaxvalue nocache;
查看序列
select * from user_sequences;
建表空间 临时表空间 (默认表空间是user)
create tablespace test_ts datafile 'E:\oracledata\fcs.dbf' size 10M;
create temporary tablespace test_temp_ts tempfile 'E:\oracledata\fcs_temp.dbf' nsize 5M;
创建用户
create user fcs identified by 123 default tablespace test_ts temporary tablespace test_temp_ts;
给用户授权
grant create session,resource to fcs;
删除用户必须级联删除
drop user_name cascade;
每页显示50条记录
set line 100 pagesize 50;
对scott用户下的表恢复(查找脚本 复制语句 执行)
E:\oracle\product\10.2.0\db_1\RDBMS\ADMIN
内连接、外连接,全连接
内 select * from student s ,class c where s.classid = c.id;
左 select * from student s left outer join class c on s.classid = c.id;
全 select * from student s full outer join class c on s.classid = c.id;
函数
字符函数 (to_char) ------- select tochar(sysdate,'yyyy-MM-dd') from dual;
数值函数 ----- 跟Java大同小异
获取当前登陆用户拥有的表
select table_name from user_tables;
select * from tab/tabs;
复制一张表
create table emp2 as select * from emp;
复制表结构而不要数据
create table emp3 as select empno,ename from emp where 1=0;
取一张表中的信息插入另一张表
insert into emp3 select empno,ename from emp where job='MANAGER';N
标识列(自增长的列)
MYSQL-----create table demo(id int primary key auto_increment);
SQLServer----create table demo(id int primary key identity(1,1));
Oracle----1.创建序列 2.创建触发器
1.create sequence seq_emp increment by 1 start with 1 nomxavalue(mxavalue 99999)[nocache];
2.create or replace trigger tri_emp
before insert on emp
for each row
begin
select seq_emp.nextval into :new.empno from dual;
end;
/
执行脚本---@E:\fcs\oracle\tri_emp.sal;
插入时间字段和空字段
空字段 null或者不写
insert into emp(empno,job,hiredate) values(111,NULL,to_date('2011-1-2 09:10:11','yyy-MM-dd hh24:mi:ss'));
注意提交事物
更新
update emp3 set(empno,ename)=(select empno,ename from emp where empno =1121) where empno = 11;
删除(DML) or 还原 and 事物
delete from emp3;/truncate table emp3;(不进回收站 直接删除)
还原
rollback;
savepoint t;
xxxxxxxxxxx
rollback to t;(只能回退xxx的操作)
set autocommit on/off;
表的锁定
*隐式锁 update(一边未提交另一边无法再更新) delete(insert不会) 快速提交
锁定表 lock table manager2 in exclusive mode;
释放锁 commit/rollback;
表的操作
创建表 create table tablename..
查看表 decs tablename;
*修改表的结构 alter table test add age int [default 123]; alter table test drop column age;
修改字段大小 alter table test modify addr varcahr2(50);
表重命名 rename test to t_test;
完整性 数据完整性 参照完整性
约束:
(非空约束) not null
(默认约束) default
检查约束 check
唯一约束 unique
主键约束 primary key
外检约束 foreign key
补充:char和varchar的区别 可变/不可变长 时间(不需计算空间) 空间
alter table table_name modify stuaddress default '地址不详';
alter table table_name modify stuaddress not null;
alter table table_name add constraint uq_stuID unique(stuID);
alter table table_name add constraint ck_stuAge check(stuAge between 10 and 30);
alter table table_name add constraint pk_stuNo primary key(stuNo);
alter table stumark add contraints fk_stuno foregin key(stuNo) reference stuInfo(stuNo)[on delete cascade][on delete set null];
drop table stuInfo; 无法删除 因为有其他表参照它
-----》drop table stuInfo cascade constraints;
###################################### oracle特性----pl/sql #####################################
进行DML操作 用来编写过程 函数 包及数据库触发器 过程和函数也称为子程序
一个块包括三个部分 每个部分由一个关键字标识
一个简单的字符串输出
set serveroutput on
begin
dbms_output.put_line('hello');
end;
/
变量
*普通变量 *列变量 全局变量 *记录变量
1.普通变量
set serveroutput on
declare
greeting varchar2(20):='fcs';
begin
greeting:='google';
dbms_output.put_line('greeting:'||greeting);
end;
/
2.列变量
set serveroutput on
declare
greeting varchar2(20):='fcs';
myname class.name%type;
begin
greeting:='google';
dbms_output.put_line('greeting:'||greeting);
select name into myname from class where id=1;
dbms_output.put_line('name:'||myname);
end;
/
3.行变量 记录变量
set serveroutput on
declare
greeting varchar2(20):='fcs';
myname class.name%type;
myrow class%rowtype;
begin
greeting:='google';
dbms_output.put_line('greeting:'||greeting);
select * into myrow from class where id=1;
dbms_output.put_line('name:'||myrow.name||' classid:'||myrow.id);
end;
/
流程控制
1).选择判断
declare
score number(3):=56;
begin
if score>=90 and score<=100 then
dbms_output.put_line('您的成绩为:优');
else
dbms_output.put_line('您的成绩为:差');
end if;
end;
/
declare
score number(3):=85;
begin
if score>=90 and score<=100 then
dbms_output.put_line('您的成绩为:优');
elsif score>=80 and score<90 then
dbms_output.put_line('您的成绩为:良');
else
dbms_output.put_line('您的成绩为:差');
end if;
end;
/
2).多路分支(case-when-then-else-end)
1-基本结构
declare
score number(3):=65;
degree number(1):=0;
begin
if
score>=90 and score<=100 then
degree:=1;
elsif score>=80 and score<90 then
degree:=2;
else
degree:=3;
end if;
case degree
when 1 then dbms_output.put_line('您的成绩为:优');
when 2 then dbms_output.put_line('您的成绩为:良');
else dbms_output.put_line('您的成绩为:差');
end case;
end;
/
2-表达式结构
declare
score number(3):=65;
degree number(1):=0;
level char(2);
begin
if
score>=90 and score<=100 then
degree:=1;
elsif score>=80 and score<90 then
degree:=2;
else
degree:=3;
end if;
level:=case degree
when 1 then '优'
when 2 then '良'
else '差'
end;
dbms_output.put_line('您的成绩为:'||level);
end;
/
3-搜索结构(case后面没有变量 when中可以写表达式)
declare
score number(3):=77;
begin
case when score>=90 and score<=100 then
dbms_output.put_line('您的成绩为:优');
when score>=80 and score<90 then
dbms_output.put_line('您的成绩为:良');
else
dbms_output.put_line('您的成绩为:差');
end case;
end;
/
3).循环
set serveroutput on
declare
i number(5):=1;
v_sum number(5):=0;
begin
loop
v_sum:=v_sum+i;
i:=i+1;
exit when i>=10;
end loop;
dbms_output.put_line('sum:'||v_sum);
end;
---while---
declare
i number(5):=1;
v_sum number(5):=0;
begin
while i<10
loop
i:=i+1;
v_sum:=v_sum+i;
end loop;
dbms_output.put_line('sum:'||v_sum);
end;
/
----for-----
declare
v_sum number(5):=0;
begin
for i in 1..10
loop
v_sum:=v_sum+i;
end loop;
dbms_output.put_line('sum:'||v_sum);
end;
/
游标(用于临时存储):显式/隐式
select into...
1.隐式游标
begin
update class set name='wpj1403' where id=2;
if SQL%ISOPEN then
dbms_output.put_line('ok');
end if;
end;
/
2.显示游标
声明游标--->打开游标--->提取游标--->关闭游标
---标准写法---
declare
cursor cur_f is select * from class where id=1;
v_record class%rowtype;
begin
open cur_f;
fetch cur_f into v_record;
dbms_output.put_line('id='||v_record.id||',name:'||v_record.name);
close cur_f;
end;
/
---简单写法---
declare
cursor cur_f is select * from class;
begin
for v_record in cur_f
loop
dbms_output.put_line('id='||v_record.id||',name:'||v_record.name);
end loop;
end;
/
begin
for v_record in (select * from class)
loop
dbms_output.put_line('id='||v_record.id||',name:'||v_record.name);
end loop;
end;
/
存储过程
存储过程和函数都是以命名的数据库对象形式存储在数据库当中。
只有被授权的用户或者创建者本身才能执行存储过程或调用函数。
一个重复使用的功能 可以设计成为存储过程:一个经常调用的计算,可以设计为存储函数;
存储过程的返回值必须通过参数带回
create[or replace] procedure
创建一个显示雇员总人数的存储过程
create or replace procedure my_pro
is
v_num number;
begin
select count(*) into v_num from scott.emp;
dbms_output.put_line(v_num);
end;
/
调用
1).execute my_pro;
2).begin
scott.my_pro;
end;
/
删除
drop procedure my_pro;
编写显示雇员信息的存储过程emp_list 并引用emp_count存储过程
create or replace procedure emp_count
is
v_num number;
begin
select count(*) into v_num from scott.emp;
dbms_output.put_line(v_num);
end;
/
create or replace procedure emp_list
is
cursor emp_cur is select * from emp;
begin
for emp_rec in emp_cur
loop
dbms_output.put_line(emp_rec.ename||' '||emp_rec.sal);
end loop;
end;
/
参数传递
in 输入参数变量 用于传递参数给存储过程
out 出 从存储过程获取数据
inout 定义一个输入.输出参数变量
create or replace procedure change_salary(pno in number,psal in number)
as
begin
update emp set sal=sal+psal where empno=pno;
if SQL%FOUND then
dbms_output.put_line('salary update success!');
commit;
end if;
exception
when others then
dbms_output.put_line('salary update failure!');
rollback;
end;
/
execute change_salary(7788,500);
********************** Oracle 常考 ********************
如何输出前10条记录
select * from emp where rownum<=10;
输出第10到20条的数据
select * from (select e.*,rownum r from emp e) where r>=10 and r<=20;
注:这条语句即是输出第10到第20条纪录,这里之所以用rownum rn,是把rownum转成实例,因为rownum本身只能用 <=的比较方式,只有转成实列,这样就可做 >=的比较了。
按工资和工作月份的乘积排序(按计算结果排序)
select ename, sal*months_between(sysdate,hiredate) "total" from emp order by "total";
统计各部门的最高工资,排除最高工资小于3000的部门。(having 后不可用别名)
select deptno,max(sal) from emp group by deptno having max(sal)>3000;
select dname ,max(sal) from emp e,dept d where e.deptno=d.deptno group by dname having max(sal)>3000;
Oracle常用常考集合的更多相关文章
- oracle常用自定义函数集合
1.Oracle 判断值是否为数字的函数CREATE OR REPLACE FUNCTION ISNUMBER(MyStr VARCHAR2) RETURN NUMBERIS STR VARCHAR ...
- 为什么你学不会递归?告别递归,谈谈我的一些经验 关于集合中一些常考的知识点总结 .net辗转java系列(一)视野 彻底理解cookie,session,token
为什么你学不会递归?告别递归,谈谈我的一些经验 可能很多人在大一的时候,就已经接触了递归了,不过,我敢保证很多人初学者刚开始接触递归的时候,是一脸懵逼的,我当初也是,给我的感觉就是,递归太神奇了! ...
- Oracle常用语句集合
oracle常用经典SQL查询 常用SQL查询: .查看表空间的名称及大小 )),) ts_size from dba_tablespaces t, dba_data_files d where t. ...
- 面试常考的常用数据结构与算法(zz)
数据结构与算法,这个部分的内容其实是十分的庞大,要想都覆盖到不太容易.在校学习阶段我们可能需要对每种结构,每种算法都学习,但是找工作笔试或者面试的时候,要在很短的时间内考察一个人这方面的能力,把每种结 ...
- 近5年常考Java面试题及答案整理(三)
上一篇:近5年常考Java面试题及答案整理(二) 68.Java中如何实现序列化,有什么意义? 答:序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化.可以对流化后的对象进行读写 ...
- C++常考面试题汇总
c++面试题 一 用简洁的语言描述 c++ 在 c 语言的基础上开发的一种面向对象编程的语言: 应用广泛: 支持多种编程范式,面向对象编程,泛型编程,和过程化编程:广泛应用于系统开发,引擎开发:支持类 ...
- oracle 常用sql语句
oracle 常用sql语句 1.查看表空间的名称及大小 select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_sizefrom d ...
- C/C++常考面试题(一)
这算是一个系列吧,记录一下在准备秋招期间,所准备的C++面试题,望秋招顺利.所有的面试题均来源于各大论坛,网络. C/C++常考面试题(一) 常用的C++数据结构有哪些? vector,序列式容器,相 ...
- 近5年常考Java面试题及答案整理(二)
上一篇:近5年常考Java面试题及答案整理(一) 31.String s = new String("xyz");创建了几个字符串对象? 答:两个对象,一个是静态区的"x ...
随机推荐
- vijos 1471 线性DP+贪心
描述 Orz教主的成员为教主建了一个游乐场,在教主的规划下,游乐场有一排n个弹性无敌的跳跃装置,它们都朝着一个方向,对着一个巨大的湖,当人踩上去装置可以带你去这个方向无限远的地方,享受飞行的乐趣.但是 ...
- 「模板」网络最大流 FF && EK && Dinic && SAP && ISAP
话不多说上代码. Ford-Fulkerson(FF) #include <algorithm> #include <climits> #include <cstdio& ...
- ASP.NET 简单鼠标右键效果contextmenutrip
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- VS推荐插件
以下插件均可在NuGet下载 Smooth Scroll 平滑滚动 Format document on Save 保存时自动格式化代码 Supercharger VS增强插件[破解教程] HideM ...
- 对于所有对象都通用方法的解读(Effective Java 第三章)
这篇博文主要介绍覆盖Object中的方法要注意的事项以及Comparable.compareTo()方法. 一.谨慎覆盖equals()方法 其实平时很少要用到覆盖equals方法的情况,没有什么特殊 ...
- 纠结于arch+xfce还是xubuntu
现在用的是ubuntu gnome版 http://www.tuicool.com/articles/6r22eyU 现在纠结于arch+xfce还是xubuntu,因为不想在gnome下面搞什么美化 ...
- Python标准库笔记(2) — re模块
re模块提供了一系列功能强大的正则表达式(regular expression)工具,它们允许你快速检查给定字符串是否与给定的模式匹配(match函数), 或者包含这个模式(search函数).正则表 ...
- 网络设备之net_device结构与操作
net_device结构是一个很大的结构,其中包含了硬件信息,接口信息,其他辅助信息,以及设备操作函数等: 目前仍在读代码中,后续字段注释会逐渐补充: /** * struct net_device ...
- linux===Ubuntu修改设备名称
step1 sudo vim /etc/hostname 修改你需要的,保存退出 step2 sudo vim /etc/hosts 修改你需要的,保存退出 step3 reboot
- 项目评审ppt的纲要
1.prd不能模糊,产品的问题全部明确 2.收益在哪里 3.设计体现业务4.怎样保证数据的前后协作5.异常如何处理6.技术解决的痛点7.对外部依赖8.性能指标预期(响应时间)9.