您的位置:首页 > 数据库 > Oracle

oRacle 简单操作

2012-05-18 18:41 405 查看

oracle中设置自增id字段

create sequence GDID_ID increment by 1 start with 1 nomaxvalue nocycle nocache; create or replace trigger GDID before insert on tableTest for each row declare NEXTID Integer; BEGIN IF :NEW.GDID IS NULL OR :NEW.GDID=0 THEN SELECT GDID_ID.NEXTVAL INTO NEXTID FROM SYS.DUAL; :NEW.GDID:=NEXTID; End IF; END GDID;

-------------------------------------------------------------

--表

Create table E_MAILS

(

EMAILID NUMBER not null primary key,

EMAILNAME VARCHAR(50) not null,

HOST varchar(64) not null,

PSWD varchar(64) not null);

desc E_MAILS

--序列

CREATE sequence emaid_sequence

INCREMENT BY 1

start with 1

nomaxvalue

nocycle

nocache

--触发器

create or replace trigger TRI_EMAIDS

before insert on E_MAILS

for each row

declare

-- local variables here

begin

SELECT emaid_sequence.NEXTVAL

INTO :NEW.EMAILID

FROM DUAL;

end TRI_EMAIDS;

--查看触发器

select object_name from user_objects where object_type='TRIGGER';

--看索引 index

select object_name from user_objects where object_type='INDEX';

--查看表 table

select object_name from user_objects where object_type='TABLE';

--查看序列

select count(*) from dba_sequences where sequence_owner='submail';

select * from dba_sequences;

--查看当前序列号

select emaid_sequence.currval from dual;

--游标查询

set serveroutput on

declare

v_name varchar(20);

cursor emp_c1 is select emailname

from e_mails where emailname='xianshiming';

begin

open emp_c1;

fetch emp_c1 into v_name;

DBMS_OUTPUT.PUT_LINE(v_name);

close emp_c1;

end;

--用decode函数可以避免重复扫描相同的行或重复连接相同的表

--使用DECODE函数来减少处理时间

select count(decode(username,'han01','xx',null) ) num from users where username like '%an%'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: