日期:2014-05-16  浏览次数:20417 次

oracle 学习笔记

一、常用SQL语句:

--创建表空间
CREATE TABLESPACE SSCP2 NOLOGGING DATAFILE 'E:\Oracle\oradata\sscp201.DBF' SIZE 10M AUTOEXTEND ON NEXT 500K MAXSIZE 1800M DEFAULT STORAGE ( INITIAL 16K NEXT 104K MINEXTENTS 1 MAXEXTENTS 5050000 PCTINCREASE 0 );

--创建用户
drop user SSCP2 cascade;
create user SSCP2 identified by VALUES???? '2CC87B525FA7F50F'
default tablespace SSCP2
temporary tablespace temp;
grant dba,connect,resource to SSCP2;

---导出数据
exp userid=sscp2/sscp2 file=D:\sscp2.dmp log=D:\sscp2.log owner=SSCP2


---导入数据
imp userid=system/manager file=D:\sscp2.dmp log=d:\sscp2imp.log fromuser=SSCP2 touser=SSCP2

-- 创建表
create table AA01
(
???? AAB034 VARCHAR2(8) default '00' not null,
???? AAE140 VARCHAR2(3) default '00' not null,
???? AAA001 VARCHAR2(10) not null,
???? AAA002 VARCHAR2(50),
???? AAA003 VARCHAR2(10) not null,
???? AAA004 VARCHAR2(50),
???? AAA005 NUMBER(12,4) not null,
???? AAE030 DATE not null,
???? AAE031 DATE,
???? AAE013 VARCHAR2(200)
)
tablespace SSCP2
???? pctfree 10
???? pctused 40
???? initrans 1
???? maxtrans 255
???? storage
???? (
?????? initial 40K
?????? minextents 1
?????? maxextents unlimited
???? );


-- 增加注释
comment on table AA01
???? is '综合参数表';
-- Add comments to the columns
comment on column AA01.AAB034
???? is '经办机构名称';

oracle中将一个数据库内容复制到另一个数据库的步骤:
设原来的数据库为A,须同步的数据库为B
一。新建数据库B
二,在B中创建A中的所有表空间
三,在B中创建A中的所有用户
四,从A中导出数据(用上面的SQL语句)
五,将数据导入进B中(用上面的SQL语句)

删除表空间时必须先删除上面的用户
drop drop user SSCP2 cascade;
drop tablespace sscp2

查询字段注释
select * from all_col_comments(user_col_comments--当前用户)
查询表的注释
select * from all_tab_comments(user_tab_comments)

修改表名:

alter table table_name rename table table_name to new_table_name;

修改列名:

alter table table_name rename column column_name to new_column_name;

获取数据库名和实例名:

select d.NAME,i.INSTANCE_NAME from v$database d,v$instance i

增加新列:

alter table table_name add column_name type

修改列:

alter table table_name modify column_name newtype

得到当前连接的用户名和操作系统的时间格式

select???
?? SYS_CONTEXT('USERENV','CURRENT_USER')??? current_user,???
?? SYS_CONTEXT('USERENV','NLS_DATE_FORMAT')??? nls_date_format???
?? from??? dual

建立触发器及游标的使用:

create or replace trigger event
?? before insert on salary??
??? --referencing old as old_value new
?? for each row

declare
?? name varchar2(8);
??
?? cursor cs is select SYS_CONTEXT('USERENV','CURRENT_USER')??? current_user from??? dual;
?? rec cs%rowtype;
begin
?? open cs;
?? name:= :NEW.name;
?? fetch cs into rec;
?? name:= rec.current_user;
?? insert into log values ('b',name,sysdate);
?? dbms_output.put_line(name);
?? close cs;
end event;