Oracle principle: data type

1. Data types and usage of PL/SQL

PL/SQL can use variables and constants. There are two ways to assign variables: one is: = and the other is select .. into [variable], variable declaration syntax:

Variable name [constraint] Data type [NOT NULL] [:= | DEFAULT expression ]

Create table, initialize table

drop table salary_tbl;

create   table salary_tbl(
   employer_nm varchar(20),
   department varchar(20) not null,
   salary number not null,
   leader_nm varchar(20)
);
truncate table salary_tbl;
declare
 totalnum number := 130000;
begin
 for i in  1..totalnum
     loop
     insert into salary_tbl values('雇佣者'||i,'部门'||Mod(i,50),100+sqrt(i),'雇佣者'||Mod(i,20)); 
     if Mod(i,1000)=0 then 
       commit;
     end if;
   end loop;
end; 
/
commit;

When the process is wrong, you can show error to see the compilation error message

There are three built-in data types supported by PL/SQL: scalar type, LOB type, and attribute type;

Scalar types include: date and time, numbers, characters, and Boolean.

Date types include: DATE type, TIMESTEMP type

The number type includes the BINARY_INTEGER type ( representing a signed integer, and the range is -2 to the 31st power of 2 ). The subtypes in this type are: SIGNTYPE (signature , can only take -1, ,0,1 ), POSITIVEN ( non-empty positive integer ), POSITIVE ( positive integer ), NATURALLN ( non-empty natural number ), NATURAL ( natural number ); number types include the NUMBER type, and subtypes include DECIMAL ( exact decimals, which can be Store 38 numbers ), FLOAT ( single-precision floating-point type is approximated by rounding ), INTEGER ( can be abbreviated as INT, which is the same as NUMBER(38) ), REAL ( real number, double-precision floating-point type is approximated by rounding method ) ; The number types include the PLS_INTERGER type, ( signed integer, suitable for complex operations ). SIMPLE_INTEGER is a new data type of 11g (a data type that directly acts on the hardware, greatly improving performance. The stored value is -2^31 ~2^31-1 )

Character types include char type, varchar2, long ( character data up to 2GB ) type, raw ( up to 4KB binary data ), and long raw ( up to 2GB binary data ) type.

Boolean

LOB types include: BFILE, BLOB, CLOB, NCLOB types, used to store large texts, images, videos, and sounds can store 4GB

BLOB: stores binary objects; CLOB stores large character data; NCLOB, stores UNICODE character data; BFILE stores binary objects in operating system files, and the database only stores pointers to BFILE files. The LOB type cannot read the data completely through the select statement. Need to be achieved through PL/SQL

 

The attribute type includes: the field type of a column in %TYPE table, and the data type of a row in %ROWTYPE table.

Second, the method of operating the LOB type of the Oracle11g squadron, namely

create  table file_lobtest(
   filePath varchar2(50),
   clob_content clob ,
   blob_content blob ,
   bfile_content bfile
);
truncate table file_lobtest;
declare 
content_test clob := '我与父亲不相见已二年余了,我最不能忘记的是他的背影。那年冬天,祖母死了,父亲的差使也交卸了,正是祸不单行的日子,我从北京到徐州,打算跟着父亲奔丧回家。到徐州见着父亲,看见满院狼藉的东西,又想起祖母,不禁簌簌地流下眼泪。父亲说,“事已如此,不必难过,好在天无绝人之路!”
  回家变卖典质,父亲还了亏空;又借钱办了丧事。这些日子,家中光景很是惨淡,一半为了丧事,一半为了父亲赋闲。丧事完毕,父亲要到南京谋事,我也要回北京念书,我们便同行。
  到南京时,有朋友约去游逛,勾留了一日;第二日上午便须渡江到浦口,下午上车北去。父亲因为事忙,本已说定不送我,叫旅馆里一个熟识的茶房陪我同去。他再三嘱咐茶房,甚是仔细。但他终于不放心,怕茶房不妥帖;颇踌躇了一会。其实我那年已二十岁,北京已来往过两三次,是没有甚么要紧的了。他踌躇了一会,终于决定还是自己送我去。我两三回劝他不必去;他只说,“不要紧,他们去不好!”
  我们过了江,进了车站。我买票,他忙着照看行李。行李太多了,得向脚夫行些小费,才可过去。他便又忙着和他们讲价钱。我那时真是聪明过分,总觉他说话不大漂亮,非自己插嘴不可。但他终于讲定了价钱;就送我上车。他给我拣定了靠车门的一张椅子;我将他给我做的紫毛大衣铺好坐位。他嘱我路上小心,夜里警醒些,不要受凉。又嘱托茶房好好照应我。我心里暗笑他的迂;他们只认得钱,托他们直是白托!而且我这样大年纪的人,难道还不能料理自己么?唉,我现在想想,那时真是太聪明了!
  我说道,“爸爸,你走吧。”他望车外看了看,说,“我买几个橘子去。你就在此地,不要走动。”我看那边月台的栅栏外有几个卖东西的等着顾客。走到那边月台,须穿过铁道,须跳下去又爬上去。父亲是一个胖子,走过去自然要费事些。我本来要去的,他不肯,只好让他去。我看见他戴着黑布小帽,穿着黑布大马褂,深青布棉袍,蹒跚地走到铁道边,慢慢探身下去,尚不大难。可是他穿过铁道,要爬上那边月台,就不容易了。他用两手攀着上面,两脚再向上缩;他肥胖的身子向左微倾,显出努力的样子。这时我看见他的背影,我的泪很快地流下来了。我赶紧拭干了泪,怕他看见,也怕别人看见。我再向外看时,他已抱了朱红的橘子望回走了。过铁道时,他先将橘子散放在地上,自己慢慢爬下,再抱起橘子走。到这边时,我赶紧去搀他。他和我走到车上,将橘子一股脑儿放在我的皮大衣上。于是扑扑衣上的泥土,心里很轻松似的,过一会说,“我走了;到那边来信!”我望着他走出去。他走了几步,回过头看见我,说,“进去吧,里边没人。”等他的背影混入来来往往的人里,再找不着了,我便进来坐下,我的眼泪又来了。
  近几年来,父亲和我都是东奔西走,家中光景是一日不如一日。他少年出外谋生,独力支持,做了许多大事。那知老境却如此颓唐!他触目伤怀,自然情不能自已。情郁于中,自然要发之于外;家庭琐屑便往往触他之怒。他待我渐渐不同往日。但最近两年的不见,他终于忘却我的不好,只是惦记着我,惦记着我的儿子。我北来后,他写了一信给我,信中说道,“我身体平安,惟膀子疼痛利害,举箸提笔,诸多不便,大约大去之期不远矣。”我读到此处,在晶莹的泪光中,又看见那肥胖的,青布棉袍,黑布马褂的背影。唉!我不知何时再能与他相见!'; 
 
begin
    insert into file_lobtest values('F:\\sqllobtest',content_test,null,null); 
end; 
/
commit;

select * from file_lobtest view the results as follows

You can see, except for clicking "..." in PL/SQL develop. Select cannot see the complete content. Querying the complete CLOB needs to be implemented with procedures or functions.

In the SQL command window or SQLPLUS

select to_char(Clob_content) from file_lobtest; 或者

set serveroutput on;
declare 
 clob_var clob;
 amount INTEGER :=5000;
 offset INTEGER :=1;
 output_var varchar2(5000);
begin
 select clob_content into clob_var from file_lobtest where filepath='F:\c.txt'; 
 dbms_lob.read(clob_var,amount,offset,output_var);
 dbms_output.put_line(output_var );  
--不用缓存时直接 dbms_output.put_line(clob_var );  
end;
/

You can see all the values, if the buffer area is too small, increase it

 

Insert BFILE and BLOB need to use a stored procedure to insert, the example is as follows

create or replace procedure insertBlob(ifilepath in  varchar2,ifilename  in varchar2  )
authid current_user is
       img_file bfile;
       img_blob blob;
       lob_length number;
       sqlcommand  varchar2(500); 
       isexit number;
begin
   ----动态SQL,需要相应权限 create table  read,write on directory等 ---
     --设置虚拟路径
     sqlcommand :='create or replace directory PHOTOPATH as ''' ||ifilepath ||'''' ;
     execute immediate sqlcommand;
      --获取bfile文件
     img_file := bfilename('PHOTOPATH',ifilename);
       --打开文件
     dbms_lob.open(img_file);
     --获取文件长度
     lob_length := dbms_lob.getlength(img_file);
     --判断文件是否存在,先把blob设成empty_blob()
     select count(1) into isexit from file_lobtest t1 where t1.filepath = ifilepath;
     if isexit = 0 then 
        insert into file_lobtest values(ifilepath,null,empty_blob(),img_file);
     else 
        update file_lobtest t1 set bfile_content=img_file,blob_content=empty_blob() where t1.filepath = ifilepath;
     end if ;
       --然后赋值blob变量
     select blob_content into img_blob from file_lobtest t1 where t1.filepath = ifilepath;
       --加载blob
     dbms_lob.loadfromfile(img_blob,img_file,lob_length);
     --关闭文件
     dbms_lob.close(img_file);
     commit;
end;

After compiling, put a photo on the local file path

Call stored procedure

begin
insertBlob ('F:\\sqllobtest','aaa.jpg');
end;
/

Check it out

Guess you like

Origin blog.csdn.net/superSmart_Dong/article/details/106885070