现在的位置: 首页 > 综合 > 正文

ORACLE 常用语句

2012年12月05日 ⁄ 综合 ⁄ 共 13443字 ⁄ 字号 评论关闭

create user bond  identified by bond;
grant connect ,resource to bond; 

create tablespace 
create table clob_content(
       id integer primary key,
       clob_column clob not null
);
create table blob_content(
       id integer primary key,
       blob_colum blob not null
);
create table bfile_conent(
       id integer primary key,
       bfile_colum bfile not null
);
//EMPTY_clob()来初始化clob_content表中两个新行
insert into clob_content(id,clob_column)values(1,empty_clob());
insert into clob_content(id,clob_column)values(2,Empty_clob());
insert into blob_content(id,blob_colum)values(1,empty_blob());

select * from clob_content;
//向clob和blob中添加内容
update clob_content set clob_column='Creeps in this petty pace' where id =1;
update clob_content set clob_column='from day to day' where id=2;
//
update blob_content set blob_colum = '10011101010101111' where id=1;
//使用bfile 1.首先创建目录对象,它表示文件在文件系统的存储目录,要执行该语句必须有Create an DirectorY数据库权限
create or replace directory sample_files_dir as 'C:\sample_files';
//用文件指针填充BFILE列
insert into bfile_conent(id,bfile_colum)values(1,bfilename('sample_files_dir','textContent.txt'));
insert into bfile_conent(id,bfile_colum)values(2,bfileName('sample_files_dir','binaryContent.doc'));
select * from bfile_conent;
//
create table order_status2(
       id integer 
       constraint order_status2_pk primary key,
       status varchar2(10),
       last_modified date default sysdate
);
//创建临时表
create global temporary table order_status_temp(
       id integer ,
       status varchar2(10),
       last_modified date default sysdate
)
on commit preserve rows;
//向order_status_temp表添加数据
insert into order_status_temp(id,status)values(1,'new');
//断开连接
disconnect;
//连接
connect bond/bond
//查询将没有数据
select * from order_status_temp;

//
select * from user_tables;
//
select table_name,tablespace_name,temporary from user_tables where table_name in('order_status2','order_status_temp');
//修改表添加列
alter table order_status2 add modified_by integer;
alter table order_status2 add initially_created date default sysdate not null;
describe order_status2
//修改列
//1.修改列的长度
alter table order_status2 modify status varchar2(15);
//2,修改数字列的精度
alter table order_status2 modify id number(5);
//3,修改列的数据类型
alter table order_status2 modify status char(15);
//4.修改列的默认值
alter table order_status2 modify last_modified default sysdate-1;
//5.删除列
alter table order_status2 drop column initially_created;
//6.添加check约术
alter table order_status2 add constraint order_status2_status_ck check(status in ('placed','pending','shipped'));
//7,添加一条记录
insert into order_status2 order_status2(id,status,last_modified,modified_by)values(1,'pending','01-12月-2003',1);
//8,添加id必须大于0的约束
alter table order_status2 add constraint  order_status2_id_ck check(id>0);
//9,添加not null约束
alter table order_status2 modify status constraint order_status2_nn not null;
alter table order_status2 modify modified_by constraint order_status2_modified_by_nn not null;
//11.添加foreign key约束首先删除一列
alter table order_status2 drop column modified_by;
alter table order_status2 add constraint order_status2_modified_by_fk modified_by references employees(employeeid);
//12.添加unique约束
alter table order_status2 add constraint order_status2_status_uq unique (status);
//13.删除约束
alter table order_status2 drop constraint order_status2_status_uq;
//14.添加一个然后禁用约束
alter table order_status2 add constraint order_status2_status_uq unique (status) disable;
alter table order_status2 disable constraint order_status2_status_nn;
//15.启用约束
alter table order_status2 enable constraint order_status2_status_uq;
//16,延迟约束
alter table order_status2 drop constraint order_status2_status_uq;
alter table order_status2 add constraint order_status2_status_uq unique (status) deferrable initially deferred;
//查询有关约束信息通过查询user_constraints可以获得有关约束的信息
select * from user_constraints where table_name='ORDER_STATUS2';
//18.通过查询user_cons_columns可以获得有关列的约束信息
column column_name FORMAT a15 select constraint_name,column_name from user_cons_columns where table_name='ORDER_STATUS2';
//19.user_constraints和user_cons_columns进行连接
select ucc.column_name,ucc.constraint_name,uc.constraint_type,uc.status from user_constraints uc,user_cons_columns ucc where uc.table_name=ucc.table_name and uc.constraint_name=ucc.constraint_name and ucc.table_name='ORDER-STATUS2';
//20,重命名表
rename order_status2 to order_state;
rename order_state to order_status2;
//21.向表中添加注释
comment on table order_status2 is 'order_status2 stores the state of an order';
//22.获得标的注释
select * from user_tab_comments where table_name='ORDER_STATUS2';
//23.获得列的注释
select * from user_col_comments where table_name='ORDER_STATUS2';
//24.截断表
truncate table order_status2;
//25.删除表
drop table order_status2;
//序列
//序列是一种数据库项,它生成整个整数序列.
//创建序列语法
create sequence sequence_name [start with start_nun][increment by increment_num][{maxvalue maximun_num | nomaxvalue}]
[{minvalue minimum_num} | nominvalue] [ cycle | nocycle] [ cache cache_num | nocache][order | noorder];
create sequence test_seq ;
create sequence test_seq1 start with 10 increment by 5;
create sequence test_seq2 start with 10 increment by 5 minvalue 10 maxvalue 20 cycle cache 2 order;
//创建从10开始增量为1的序列
create sequence test_seq3 start with 10 increment by -1 minvalue 1 maxvalue 10 cycle cache 5;
//获取有关序列的信息
column sequence_name format a6
select * from user_sequences;
//使用序列
select test_seq.nextval from dual;
select test_seq.currval from dual;
//使用序列填充主键
create table order_status2(
       id integer constraint order_status2_pk primary key,
       status varchar2(10),
       last_modified date default sysdate 
);
create sequence order_status2_seq nocache; 
//修改序列Alter sequence语句来修改序列可以修改的序列的内容有如下限制
//1.不能修改序列的值
//2.序列的最小值不能大于当前值
//3.序列的最大值不能小于当前值
//将test_seq的增量修改为2
alter sequence test_seq increment by 2;
select test_seq.currval from dual;
select test_seq.nextval from dual;
//删除序列
drop sequence test_seq;
//创建索引
//创建索引的语法
create [unique] index index_name on table_name(column_name[,cloumm_name...])tablespace tab_space;
//由于性能方面的原因,通常将索引存储到不同的表空间中,
create index customers_last_name_idx on customers(last_name);
//创建唯一索引
create unique  index customers_phone_idx on customers(phone);
//创建一个复合索引
create index employees_fist_last_name_idx on employees(fist_name,last_name);
//创建基于函数的索引,基于函数索引,dba必须将初始化参数query_rewrite_enabled设置为true(默认为false)
create index customers_last_name_func_idx on customers(upper(last_name));
//获取有关索引的信息
select * from user_indexes;
select index_name,table_name,uniqueness,status from user_indexes where table_name in('CUSTOMERS','EMPLOYEES');
//获取列索引的信息
select index_name,table_name,column_name from user_ind_columns where table_name in('CUSTOMERS','EMPLOYEES');
//修改索引
alter index customers_phone_idx rename to customers_phone_number_idx;
//删除索引
drop index custoemrs_phone_number_idx;
//视图, 视图并不存储数据,他们只会访问基表中的行
//创建视图语法
create [or replace] view [{force | noforce}] view view_name [(alias_name[,alias_name...])] as subquery [with {check option | read only} constraint constraint_name];
//创建一个视图
create view cheap_product_view as select * from products where price<15;
create view employees_view as select employees_id,manager_id,first_name,last_name,title from employees;
//对视图进行select操作
select product_id,name,price,from cheap_products_view;
//对视图进行增加操作
insert into cheap_products_view(product_id,product_type_id,name,price) values(14,1,'Eastern Front',16.50);
insert into employees_view(employee_id,manager_id,fist_name,last_name,title)values(5,1,'jeff ','jones','CTO');
//创建具有check option约束的视图
create view cheap_products_view2 as select * from products where price<15 with check option constraint cheap_products_view2_price;
//插入会抱错,因为这个视图不能检索出这一行记录
insert into cheap_product_view2(product_id,product_type_id,name,price)values(15,1,'southern front',19.50);
//创建具有readyonly的视图
create view cheap_products_view3 as select * from products where price<15 with read only constraint cheap_products_view3_read_only;
//插入时会抱错,因为这个视图是只读的
insert into cheap_product_view3(product_id,product_type_id,name,price) values(16,1,'northen front',19.50);
//获取有关视图的信息
describe cheap_products_view3;
select view_name,text_length,text from user_views;
//获取有关视图约束的信息
select constraint_name,constraint_type,status,deferrable,deferred from user_constraints where table_name in('CHEAP_PRODUCTS_VIEW2','CHEAP_PRODUCTS_VIEW3');
//创建并使用复杂视图
create view products_and_types_views as select p.name product_name,pt.name product_type_name,p.price from products p full outer join product_types pt using (product_type_id);
//使用内连接创建试图
create view employee_salary_grades_view as select  e.fist_name,e.last_name,e.title,e.salary,sg.salary_grade_id from employees e inner join salary_grades sg on e.salary between sg.low_salary and sg.high_salary;
//使用group by ,having字句
create view product_average_view as select product_type_id,avg(price) average_price from products where price<15 group by product_type_id having avg(price)>13
//修改视图
create or replace view product_average_view as select product_type_id ,avg(price) average_price from products where price<12 group by product_type_id having avg(price)>11;
//删除视图
drop view cheap_products_view2;

CREATE TABLESPACE "SAMPLE"
2      LOGGING
3      DATAFILE 'D:\ORACLE\ORADATA\ORA92\SAMPLE.ora' SIZE 5M,
4      'D:\ORACLE\ORADATA\ORA92\dd.ora' SIZE 5M
5      EXTENT MANAGEMENT LOCAL
6      UNIFORM SEGMENT SPACE MANAGEMENT
7*     AUTO

//创建表空间
 create tablespace "BOND"
 nologging 
   datafile 'C:\oracle\oradata\test\bond.ora' size 5m,
   'C:\oracle\oradata\test\bond.ora' size 5M
   extent management local
   uniform segment space management auto
//删除表空间
drop tablespace lmtbsb;
//创建表空间
CREATE TABLESPACE lmtbsb DATAFILE 'C:\oracle\oradata\test\lmtbsb.dbf' SIZE 50M 
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
-----------------------------------------------------------------------------------------------------------------------
1、创建本地管理TableSpace 
 
Oracle创建的TableSpace默认均是本地管理的,明确的表述应该是: 
在CREATE TABLESPACE 语句中 EXTENT MANAGEMENT 子句中指定 LOCAL 
 
然后可以用 AUTOALLOCATE 选项(默认)来使Oracle自动管理盘区 
    CREATE TABLESPACE lmtbsb DATAFILE '/u02/oracle/data/lmtbsb01.dbf' SIZE 50M 
    EXTENT MANAGEMENT LOCAL AUTOALLOCATE; 
 
    也可以使用一个指定大小(UNIFORM SIZE)的同意盘区来管理该TableSpace 
    CREATE TABLESPACE lmtbsb DATAFILE '/u02/oracle/data/lmtbsb01.dbf' SIZE 50M 
    EXTENT MANAGEMENT LOCAL UNIFORM SIZE 128K; 
 
    如果未指定盘区大小,则默认为1M,说明数据文件至少要大于1MB 
 
    注:如果TableSpace被期望用于包含需要不同盘区大小和拥有很多盘区的大小变动的对象
    ,那选择AUTOALLOCATE是最好的选择。AUTOALLOCATE是一种管理TableSpace的简便方法,只是有可能会对一些空间造成浪费。
    如果需要准确控制未用空间,并能够精确预计为一个或多个对象要非配的空间和盘区大小,那就可以使用UNIFORM。 
 ------------------------------------------------------------------------------------------------------------------------------   
    2、在本地管理TableSpace中指定段空间管理 
 
    可使用 SEGMENT SPACE MANAGEMENT子句来设置端控件的管理模式,有以下几种模式: 
 
    MANUAL(默认):使用段中管理空闲空间的空闲列表。 
    AUTO:使用位图来管理段中的空闲空间。又称为自动段空间管理。 
 
    AUTO立功了一种更加简单、有效的管理方法,完全消除了为表空间中创建的段指定和调整PCTUSED、FREELISTS、FREELISTS GROUPS属性的任何必要。 
 
    CREATE TABLESPACE lmtbsb DATAFILE 'C:\oracle\oradata\test\lmtbsb.dbf' SIZE 50M 
    EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO; 
 
    注:LOB类型不能指定自动段空间管理。
 ---------------------------------------------------------------------------------------------------------------------------
 3、修改本地管理TableSpace 
 
    * 不能讲本地管理TableSpace改为本地管理的temp TableSpace 
    * 不能改变TableSpace的段空间管理办法 
    * 本地管理TableSpace没有必要合并空闲盘区 
 
    * 可以添加数据文件 
    * 可以改变TableSpace状态(ONLINE/OFFLINE) 
    * 修改TableSpace为只读or只写 
    * rename数据文件 
    * 启用/禁用该TableSpace数据文件的自动盘区大小 
    
 -----------------------------------------------------------------------------------------------------------------------------
 4、创建字典管理的TableSpace 
 
    CREATE TABLESPACE tbsb 
    DATAFILE 'C:\oracle\oradata\test\lmtbsb.dbf' SIZE 50M 
    EXTENT MANAGEMENT DICTIONARY 
    DEFAULT STORAGE ( 
    INITIAL 50K 
    NEXT 50K 
    MINEXTENTS 2 
    MAXEXTENTS 50 
    PCTINCREASE 0); 
 --------------------------------------------------------------------------------------------------------------------------------
5、临时表空间 
 
    临时表空间主要用于提高多个排序操作的并发能力、减小开销,或避免Oracle空间管理操作在一起进行。临时表空间创建之后,可以被多个用户共享。 
    一个给定实例和表空间的所有排序操作共享一个单一的排序段。即排序段为一个给定表空间的每个执行排序操作的实例而存在。 
    排序段由使用临时表空间用于排序的第一个语句创建,并在关闭时释放,一个盘区不能被多个事务共享。 
 
    可以使用V$SORT_SEGMENT视图来查看临时表空间排序段的空间分配和回收情况 
    用V$SORT_USAGE视图查看这些段的当前排序用户 
 
    注:临时表空间中不能创建明确对象。
    ---------------------------------------------------------------------------------------------------------------------------------------
     
6、创建本地管理的临时表空间 
 
    CREATE TEMPORARY TABLESPACE lmtemp TEMPFILE 'C:\oracle\oradata\test\lmtbsb.dbf' SIZE 20M REUSE 
    EXTENT MANAGEMENT LOCAL UNIFORM SIZE 16M; 
 
    注1:临时数据文件信息查询与数据文件不同,需要查询V$TEMPFILE和DBA_TEMP_FILES,但结构与V$DATAFILE和DBA_DATA_FILES类似。 
    注2:临时表空间在初次使用时才分配空间,可以更快创建和修改大小,但要注意磁盘大小。 
    
------------------------------------------------------------------------------------------------------------------------------
7、本地管理临时表空间的修改 
 
    * 添加临时文件 
    ALTER TABLESPACE lmtemp ADD TEMPFILE 'C:\oracle\oradata\test\lmtbsb02.dbf' SIZE 20M REUSE; 
 
    * 改变临时文件状态 
    ALTER DATABASE TEMPFILE 'C:\oracle\oradata\test\lmtbsb02.dbf' ONLINE|OFFLINE; 
 
    * 更改临时文件大小 
    ALTER DATABASE TEMPFILE 'C:\oracle\oradata\test\lmtbsb02.dbf' RESIZE 100M; 
 
    * 取消临时文件并删除相应操作系统文件 
    ALTER DATABASE TEMPFILE  'C:\oracle\oradata\test\lmtbsb02.dbf' DROP 
    INCLUDING DATAFILES; 
---------------------------------------------------------------------------------------------------------------------------------------
drop tablespace lmtemp;
 8、创建字典管理的临时表空间 
 
 CREATE TABLESPACE sort 
    DATAFILE 'C:\oracle\oradata\test\lmtbsb01.dbf' SIZE 50M 
    DEFAULT STORAGE ( 
    INITIAL 2M 
    NEXT 2M 
    MINEXTENTS 1 
    PCTINCREASE 0) 
    EXTENT MANAGEMENT DICTIONARY 
    TEMPORARY; 
 
    注1:不能以这种方式创建EXTENT MANAGEMENT LOCAL的临时表空间。 
    注2:可以使用ALTER TABLESPACE tbsa TEMPORARY;语句将一个现有的字典管理永久表空间改为临时表空间。

----------------------------------------------------------------------------------------------------------------------------------
//分页
select * from (select my_table.*, rownum as my_rownum from ( select yhbh, yhmc from yysf_tb_yonghxx order by yhbh) my_table where rownum <20 ) where my_rownum>=10
1.根据ROWID来分
select * from t_xiaoxi where rowid in(select rid from (select rownum rn,rid from(select rowid rid,cid from 
t_xiaoxi  order by cid desc) where rownum<10000) where rn>9980) order by cid desc;
//其中t_xiaoxi为表名称,cid为表的关键字段,取按CID降序排序后的第9981-9999条记录,t_xiaoxi表有70000多条记录

///创建序列
create sequence seq_OPERATORID
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
cache 20;

//创建行级触发器
create or replace trigger SYSOPERATOR_bir
before insert on SYSOPERATOR
REFERENCING OLD AS old_value NEW AS
    new_value
    FOR EACH ROW
when (new_value.OPERATORID is null)
BEGIN
 select seq_OPERATORID.nextval into :new_value.OPERATORID from dual;
END;

//查询序列
select seq_a.nextval from dual;

//删除序列
drop sequence seq_a 

抱歉!评论已关闭.