当前位置:网站首页>[Oracle] is there a "time" data type in oracle-- Research on Oracle data types

[Oracle] is there a "time" data type in oracle-- Research on Oracle data types

2022-06-21 17:44:00 DarkAthena

Preface

Oracle Are there in “time” Data type ? Most familiar oracle Most people will say no , But is this the case ?
The following picture will certainly surprise many people
img-ExtqT2DZ-1655564231189

Analysis methods

Front dish

oracle There are many things that will not be written in the document , You need to find out for yourself , But fortunately, oracle There is a lot of code that is visible plsql Language , So you can use these visible codes and logical analysis to understand oracle Of “ Customary practice ”.

Like functions , If you only look at official documents , contrast DBA_OBJECTS Inside sys Under the user function, You will find that many functions are missing from the official documentation . In addition, there are quite a few functions in official documents , stay DBA_OBJECTS There's no , However, the automatic code completion function of the tool can complete the functions , So it's natural to think , What mechanism is this ?

select * from dba_objects h where h.object_type='FUNCTION' AND OWNER='SYS';
select * from dba_objects h where h.object_name='NVL';

dba_objects There is no corresponding name in the , Maybe these names are in dba_source Are there in

select * from dba_source h where lower(h.text) like '%function%nvl(%' and owner='SYS';

image-1655555860031

So I am sys The user found standard This package, There are many familiar functions in it , And it also conforms to my previous understanding :

In the same schema Next , Only functions in the package can have duplicate names , Each function has a certain parameter type and number

If nvl This function supports multiple types in Oracle Is just a function , Nature does not conform to this logic , So nature analyzes a mechanism :

It can be used without package name , And you can pass in functions of the same name with different types of parameters , It's all defined in standard In this bag .

Dinner

And then back to type, First, create tables in the usual way , To enter a paragraph sql, You will find that the tool automatically prompts "time" This keyword and "time with tz" This type, Of course, both of them will be wrong .

img-a3wVraVz-1655564231191
image-1655557668186
But that means oracle I won't support it time What's the type ?

If you ask oracle Which types are supported in , It's OK to read the official documents first , But because of the previous experience , You must suspect that the document is incomplete , Will check dba_types, That's too much , Take a glance , Add one owner Empty condition , Namely oracle All in “ basic ” data type , Except for the fact that "time" Outside this type , It is also found that the names of some types are not exactly the same as those actually used , For example, the common "PLS_INTEGER" Name and... In the view CODE Are all "PL/SQL PLS INTEGER".

select * from dba_types where owner is null;

We know , about PLS_INTEGER type , Can only be used in stored procedures , Cannot be used as a table field , This is because oracle It is limited . You can guess "PLS" Built in type at the beginning , Can not be used to create tables
image-1655557905392
however , You'll find that , This report is wrong , Even with the top time The types of errors reported are different .

Think of the previous standard The mechanism of the package , The name you want to use is also defined in this package , Open it and see .

  type TIME is new DATE_BASE;
  type TIMESTAMP is new DATE_BASE;
  type "TIME WITH TIME ZONE" is new DATE_BASE;
  type "TIMESTAMP WITH TIME ZONE" is new DATE_BASE;
  type "INTERVAL YEAR TO MONTH" is new DATE_BASE;
  type "INTERVAL DAY TO SECOND" is new DATE_BASE;

  SUBTYPE TIME_UNCONSTRAINED IS TIME(9);
  SUBTYPE TIME_TZ_UNCONSTRAINED IS TIME(9) WITH TIME ZONE;
  SUBTYPE TIMESTAMP_UNCONSTRAINED IS TIMESTAMP(9);
  SUBTYPE TIMESTAMP_TZ_UNCONSTRAINED IS TIMESTAMP(9) WITH TIME ZONE;
  SUBTYPE YMINTERVAL_UNCONSTRAINED IS INTERVAL YEAR(9) TO MONTH;
  SUBTYPE DSINTERVAL_UNCONSTRAINED IS INTERVAL DAY(9) TO SECOND (9);

 Screenshot of enterprise wechat _16553061564524

In the title of this article time type , stay oracle It does , However, there are limitations in use , And this limitation and pls The type of performance is not the same , The error reported during table creation is not an invalid data type , Instead, there are fewer attributes or keywords , But according to package In the time The use of type , It is also impossible to create tables .

create table test_time_table(a time(9));

image-1655563725261

then , Try again in another way .
You can't create a table , Let's start with a function return try

create or replace function test_time_func return time is
begin
  return '21:19:00';
end;
/
select test_time_func from dual;

Found that it could be select !!
Need to know oracle Although there are boolean type , Can't be select Of , This further illustrates time Types and those pls The types are different .

This function is implicitly converted directly by me , There was no error , Then try it directly cast

select cast('12:34:56' as time) from dual


Yes !, For it to_char

select to_char(cast('12:34:56' as time),'hh24:mi:ss') from dual

image-1655558799147

No problem , It's all right time type , even to_char Formatting can be used !

So , Try create table as, Let's see how it works

no way , Then try create view as , because view You can also view field types

It looks like time(0)(7) ?
One more first create table as select * from View
image-1655559334559

So far , Basically, we can judge , stay dba_types in owner Empty type , Distinguish by what you can use , At least it can be divided into 3 class ,

  1. It can be used everywhere ( representative :varchar2/number/date)
  2. Only in plsql What can be used in ( representative :boolean/pls_integer)
  3. Only create table What can't be used in , But can be used as a column select、 Can be used as a view field type 、 Can be in plsql Use in ( representative :time)

Then continue to study this time The meaning of the last two numbers .
go back to standard package , See these two lines

  SUBTYPE TIME_UNCONSTRAINED IS TIME(9);
  SUBTYPE TIME_TZ_UNCONSTRAINED IS TIME(9) WITH TIME ZONE;

We try to build view To see what the fields created with these two field types look like
image-1655559902113
image-1655560015331
The range of the first number is 0-9.
But ,0 To 9 The number of seconds is complete , I think I have seen colleagues discuss timestamp(6) and timestamp(9), Naturally, it is associated with a similar meaning , That is, this number represents the number of decimal places after the second , But I forgot how to format it in seconds , Just check it out nls_database_parameters, This check , Another parameter that I didn't notice before
image-1655560400757

here time It's clear that ...
The following sql Verified my conjecture

select to_char(cast('12:34:56.987654321' as time(5)),'hh24:mi:ss XFF') from dual;

image-1655561318108

Next is the 2 A digital
image-1655561505539
It seems wrong , There was no 7, First dump have a look

find 7 了 , It turns out that No 2 A digital 7, It means time Number of bytes occupied by type data , And this length will not be due to time Vary with accuracy , Always fixed 7 position . As for those with time zones time, It's fixed 9 position
image-1655562262671

Dessert

time The content of the type is basically studied , And then back to standard package , Take a look at some of the first code

type DATE is DATE_BASE;

  type NUMBER is NUMBER_BASE;
  subtype FLOAT is NUMBER; -- NUMBER(126)
  subtype REAL is FLOAT; 

You can see that there is *_base In this way , Search the whole bag , You can get the following 6 Kind of

  • DATE_BASE
  • NUMBER_BASE
  • CHAR_BASE
  • BLOB_BASE
  • CLOB_BASE
  • BFILE_BASE

"base" It means basic , in other words ,oracle All the basic types it uses , Divided into this 6 Categories: .
then "type NUMBER is NUMBER_BASE" That is to say NUMBER It belongs to NUMBER_BASE One of ;
"subtype FLOAT is NUMBER;" namely FLOAT by NUMBER Subtypes of ( Here we need to pay attention to , Binary stored values of subtypes , Not necessarily the same as the original type , such as “subtype BINARY_FLOAT is NUMBER;”).

Keep looking at this bag , Would be right oracle Have a deeper understanding , Of course, this article will not continue , Leave a little space for readers to explore .

summary

Although the title of this article is "time" type , And the main line of the article is also around “time” Type expansion , But what is more, the author himself dares to “ authority ” The question of , As well as the record of the deep excavation of the unknown with logical thinking and experiments . I hope readers will learn more than “oracle Are there in time type ” This conclusion , I also hope that readers will work in the future , Have the exploration spirit and logical thinking ability to dig deep into problems .

原网站

版权声明
本文为[DarkAthena]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211547539888.html