Oracle主键自动增长
Oracle主键自动增长
这几天搞Oracle,想让表的主键实现自动增长,查网络实现如下:
create table simon_example
(
id number(4) not null primary key,
name varchar2(25)
)
-- 建立序列:
-- Create sequence
create sequence SIMON_SEQUENCE
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
cache 20;
-- 建立触发器
create trigger "simon_trigger" before
insert on simon_example for each row when(new.id is null)
begin
select simon_sequence.nextval into:new.id from dual;
end;
相关文档:
select 'create sequence '||sequence_name||
' minvalue '||min_value||
' maxvalue '||max_value||
' start with '||last_number||
&n ......
IN和EXISTS区别
in 是把外表和内表作hash join,而exists是对外表作loop,每次loop再对内表进行查询。
一直以来认为exists比in效率高的说法是不准确的。
如果查询的两个表大小相当,那么用in和exists差别不大。
如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:
例如:表A(小表),表B ......
select trim(leading | trailing | both ' ' from ' abc d ') from dual;
去掉字符串 ' abc d ' 的前面/后面/前后的空格
类似函数:ltrim, ......
1. round(Num,n) : 四舍五入数字Num,保留n位小数,不写N默认不要小数,四舍五入到整数个位
select ROUND(21.237,2) from dual;
结果: 21.24
2. trunc(Num,n) : 截取数字Num,保留n位小数,不写N默认是0,即不要小数
select TRUNC(21.237,2) from dual;
结果:21.2 ......