数值型 number:最高精度38位 日期时刻型 date:准确到ss timestamp:秒值准确到小数点后6位 函数 sysdate,systimestamp返回体系当前日期,时刻和时区。 变动时刻的表现 alter session set nls_date_language=’american’; alter session set nls_date_format=’yyyy-mm-dd’; Oracle中的伪列 像一个表列,但没有存储在表中 伪列可以查询,但不能插入、更新和修改它们的值 常用的伪列:rowid和rownum rowid:表中行的存储地点,可独一标示数据库中的某一行,可以行使该列快速定位表中的行。 rownum:查询返回功效齐集的行的序号,可以行使它来限定查询返回的行数。 3.数据界说说话 用于操纵表的呼吁 create table alter table truncate table drop table 修改表的呼吁 alter table stu_table rename to stu_tbl;--修改表名 alter table stu_tbl rename column stu_sex to sex;--修改列名 alter table stu_tbl add (stu_age number);--添加新列 alter table stu_tbl drop(sex);--删除列 alter table stu_tbl modify(stu_sex varchar2(2));--变动列的数据范例 alter table stu_tbl add constraint pk_stu_tbl primary key(id);--添加束缚 4.数据哄骗说话 select,insert 操作现有的表建设表 create table stu_tbl_log as select id,stu_name,stu_age from stu_tbl;-- 选择无一再的行 select distinct stu_name from stu_tbl;-- 插入来自其他表中的记录 insert into stu_tbl_log select id,stu_age from stu_tbl; 5.数据节制说话 grant,revoke 6.事宜节制说话 commit,rollback 7.SQL操纵符 算术操纵符:L+-*/ 较量操纵符:L=,!=,<>,>,<,>=,<=,between-and,in,like,is null等 逻辑操纵符:Land,or,not 荟萃操纵符:Lunion,union all,intersect,minus 毗连操纵符:L|| 示例中stu_tbl_log中的数据如下: ID STU_NAME STU_AGE ---------- -------------------- ---------- 1000 李华 20 1001 accp 20 1003 nimda 3 stu_tbl中的数据如下: ID STU_NAME ST STU_AGE ---------- -------------------- -- ---------- 1000 李华 男 20 1001 accp 男 20 1002 admin 男 30 示例: select (3+2)/2 from dual;--算术操纵符,功效:2.5 select * from stu_tbl where stu_age>=20;--较量操纵符 select * from stu_tbl where stu_name like ‘%a%‘;--较量操纵符:like select * from stu_tbl where stu_name like ‘a___‘;--较量操纵符:like select * from stu_tbl where stu_age in(20,30);--较量操纵符:in select * from stu_tbl where stu_age between 20 and 30;--较量操纵符:between select stu_name from stu_tbl union all select stu_name from stu_tbl_log;--荟萃操纵符:union all,测试功效详细如下: STU_NAME ----------- 李华 accp admin 李华 accp nimda
已选择6行。 select stu_name from stu_tbl union select stu_name from stu_tbl_log;--荟萃操纵符:union,测试功效详细如下: STU_NAME --------- accp admin nimda 李华 select stu_name from stu_tbl intersect select stu_name from stu_tbl_log;--荟萃操纵符:intersect,测试结详细如下: STU_NAME ---------- accp 李华 select stu_name from stu_tbl minus select stu_name from stu_tbl_log;--荟萃操纵符:minus,测试功效如下: STU_NAME ---------- Admin 从中可以看出: minus是获取第一张表独占的数据 intersect是获取两张表中都有的数据 union是整合两张表的数据,都有的只表现一次 union all是纯粹的两张表数据整合 select id,stu_name||‘ ‘||stu_sex as name_sex,stu_age from stu_tbl;--毗连操纵符||,测试功效详细如下: ID NAME_SEX STU_AGE ---------- ----------------------- ---------- 1000 李华 男 20 1001 accp 男 20 1002 admin 男 30
8.SQL函数 单行函数:从表中查询的每一行只返回一个值,可呈此刻select子句,where子句中 日期函数 数字函数 字符函数 转换函数:ToChar(),ToDate(),ToNumber() 其他函数: Nvl(exp1,exp2):表达式一为null时,返回表达式二 Nvl2(exp1,exp2,exp3):表达式一为null时返回表达式三,不然返回表达式二 Nullif(exp1,exp2):两表达式相称时,返回null,不然返回表达式一 分组函数:基于一组行来返回 Avg,Min,Max,Sum,Count Group by,having 说明函数 Row_number,rank,dense_rank 示例: select u.user_name,sum(oi.order_num*oi.order_price) as total,row_number() over (order by sum(oi.order_num*oi.order_price) desc) as sort from order_item_tbl oi,user_tbl u,order_tbl o where oi.order_id = o.id and o.user_id = u.id group by u.user_name;
三.锁和数据库工具 1.锁:数据库用来节制共享资源并发会见的机制。 锁的范例:行级锁,表级锁 行级锁:对正在被修改的行举办锁定。行级锁也被称之为排他锁。 在行使下列语句时,Oracle会自动应用行级锁: insert,select…… for update select……for update应承用户一次锁定多笔记录举办更新。 行使commit or rollback开释锁。 表级锁: lock table user_tbl in mode mode; 表级锁范例: 行共享 row share 行排他 row exclusive 共享 share 共享行排他 share row exclusive 排他 exclusive 死锁:两个或两个以上的事宜彼此守候对方开释资源,从而形成死锁 2.数据库工具 oracle数据库工具又称模式工具 数据库工具是逻辑布局的荟萃,最根基的数据库工具是表 数据库工具: 表,序列,视图,索引
序列 用于天生独一,持续序号的工具。 建设语法: create sequence user_id_seq start with 1000 increment by 1 maxvalue 2000 minvalue 1000 nocycle cache 1000;--指定内存中预先分派的序号 会见序列: select user_id_seq.currval from dual; select user_id-seq.nextval from dual; 变动删除序列: alter sequence user_id_seq maxvalue 10000;--不能修改其start with 值 drop sequence user_id_seq; 在Hibernate中会见序列:
user_id_seq
视图 以颠末定制的方法表现来自一个或多个表的数据 建设视图: create or replace view user_tbl_view (vid,vname,vage) as select id,user_name,age from user_tbl [with check option]|[with read only]; 建设带有错误的视图: create force view user_tbl_force_view as select * from user_table;--此时user_table可以不存在 建设外联接视图: create view user_stu_view as select u.id,u.user_name,u.password,s.ddress from user_tbl u,stu_tbl s where u.s_id(+)=s.id;--哪一方带有(+),哪一方就是次要的 删除视图: drop user_stu_view;
索引 用于进步SQL语句执行的机能 索引范例: 独一索引,位图索引,组合索引,基于函数的索引,反向键索引 建设尺度索引: create index user_id_index on user_tbl(id) tablespace schooltbs; 重建索引: alter index user_id_index rebuild; 删除索引: drop index user_id_index; 建设独一索引: create unique index user_id_index on user_tbl(id); 建设组合索引: create index name_pass_index on user_tbl(user_name,password); 建设反向键索引: create index user_id_index on user_tbl(id) reverse;
四.行使PL/SQL 可用于建设存储进程,触发器,措施包,给SQL语句的执行添加措施逻辑。 支持SQL,在PL/SQL中可以行使: 数据哄骗呼吁 事宜节制呼吁 游标节制 SQL函数和SQL运算符 支持面向工具编程(OOP) 可移植性 更佳的机能,PL/SQL颠末编译执行
分为三个部门:声明部门,可执行部门和非常处理赏罚部门 [declare declarations] begin executable statements [exception handlers] end; 打开输出 set serverout on;
--按照输入编号获取某学员的后果--if declare score user_tbl.score%type; begin select score into score from user_tbl where id=‘&id‘; if score>90 then dbms_output.put_line(‘优越‘); elsif score>80 then dbms_output.put_line(‘精采‘); elsif score>60 then dbms_output.put_line(‘合格‘); else dbms_output.put_line(‘差‘); end if; end;
--按照学员姓名获取某学员的后果--if declare score user_tbl.score%type; begin select score into score from user_tbl where user_name=‘&name‘; if score>90 then dbms_output.put_line(‘优越‘); elsif score>80 then dbms_output.put_line(‘精采‘); elsif score>60 then dbms_output.put_line(‘合格‘); else dbms_output.put_line(‘差‘); end if; end;
--case的行使 declare grade user_tbl.grade%type; begin select grade into grade from user_tbl where id=‘&id‘; case grade when ‘A‘ then dbms_output.put_line(‘优秀‘); when ‘B‘ then dbms_output.put_line(‘优越‘); when ‘C‘ then dbms_output.put_line(‘精采‘); else dbms_output.put_line(‘一样平常‘); end case; end;
--根基轮回 declare i number(4):=1; begin loop dbms_output.put_line(‘loop size:‘||i); i:=i+1; exit when i>10; end loop; end;
--while轮回 declare i number(4):=1; begin while i<=10 loop dbms_output.put_line(‘while loop size=‘||i); i:=i+1; end loop; end;
--for轮回 declare i number(4):=1; begin for i in 1..10 loop dbms_output.put_line(‘for loop Size:‘||i); end loop; end;
declare i number(2):=1; j number(2):=1; begin for i in reverse 1..9 loop for j in 1..i loop dbms_output.put(j||‘x‘||i||‘=‘||j*i||‘ ‘); end loop; dbms_output.put_line(‘‘); end loop; end;
--动态SQL declare userId number(2); sql_str varchar2(100); userName user_tbl.user_name%type; begin execute immediate ‘create table testExe(id number,test_name varchar2(20))‘; userId:=‘&userId‘; sql_str:=‘select user_name from user_tbl where id=:id‘; execute immediate sql_str into userName using userId; dbms_output.put_line(userName); end; (or declare id_param number:=‘&id_param‘; sql_str varchar2(100); name_param stu_tbl.stu_name%type; begin sql_str:=‘select stu_name from stu_tbl where id=:p‘; execute immediate sql_str into name_param using id_param; dbms_output.put_line(name_param); end; / )
--非常处理赏罚 declare grade number(4); begin grade:=‘&grade‘; case grade when 1 then dbms_output.put_line(‘好的‘); --else dbms_output.put_line(‘欠好‘); end case; exception when case_not_found then dbms_output.put_line(‘输入范例不匹配!‘); end; (编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|