ORACLE实用SQL语句(一)
工作实用基本SQL
现在记录的都是我工作中用到的SQL,许多SQL也是我在工作中一点一点学来记录下来的,解决了我工作中的许多问题,记录下来以后方便查找。
这篇文章包含有触发器、序列、循环、表空间等。特别是表空间,工作中遇到数据库爆满了,查找问题才发现数据库文件最大32G,所以要增加更多的数据文件才行。
触发器
create or replace trigger test2
after update on TESTDATA_2
for each row
begin
update TESTDATA_1 set time=:new.time where :old.time = time;
end;
:new.属性名---表示获取插入/更新的哪一行记录的相应属性值;
:old.属性值---表示获取更新前的数据:old.time = time与他更新卡你的数据相等
创建序列
create sequence my_seq --创建序列名:my_seq
start with 1 --从1开始
increment by 1 --每次增长1
maxvalue 999999 --nomaxvalue(不设置最大值) ---最大值
minvalue 1 --最小值
cycle --nocycle 一直累加,不循环 ;cycle 表示循环
nocache --缓存
cache 10 --表示一次产生10个号,
循环
循环插入数据
declare i number;
--用for实现
begin
for i in 0 .. 500 loop
insert into "TEST_TEMP" VALUES ('1111', '2222', 2+i, '111');
end loop;
end;
查看表空间路径
查看表空间路径
select tablespace_name, file_id, file_name as "LJ" , round(bytes / (1024 * 1024), 0) total_space
from dba_data_files order by tablespace_name;
查看表空间使用情况和比例DX大小,YSY已使用,KXKJ空闲空间,SYB使用比
select
a.tablespace_name as "name" ,
a.bytes / 1024 / 1024 as "DX" ,
(a.bytes - b.bytes) / 1024 / 1024 as "YSY" ,
b.bytes / 1024 / 1024 "KXKJ" ,
round(((a.bytes - b.bytes) / a.bytes) * 100, 2) "SYB"
from
( select tablespace_name, sum (bytes) bytes from dba_data_files group by tablespace_name) a,
( select tablespace_name, sum (bytes) bytes, max (bytes) largest from dba_free_space group by tablespace_name) b
where
a.tablespace_name = b.tablespace_name order by ((a.bytes - b.bytes) / a.bytes) desc ;
修改表空间数据文件大小
alter database datafile '/mnt/data/share/system01.dbf' resize 50000M;
表空间最大32G,增加表空间数据文件
ALTER TABLESPACE SYSTEM ADD DATAFILE '/mnt/data/share/system51.dbf' size 1000M autoextend on next 1000M maxsize unlimited;
创建表空间
create tablespace test_date datafile 'E:\oracle\test_date01.dbf' size 100M autoextend on next 10M;