Oracle DB query data_length(what's it return)
I am running some queries on an Oracle DB from my java program. Once a certain table is selected this query runs and returns some traits for the user.
A simplified version of my code as follows.
String query = "SELECT column_name, data_type, data_length FROM all_tab_columns WHERE table_name = 'blahblah'";
String ColumnName;
String DataType;
String DataLength;
System.out.println(ColumName);
System.out.println(DataType);
System.out.println(DataLength);
I am unsure as to what data_length
is returning.
Is it returning the length of the data in the field? Or is it returning the max value. After some searching I have come across conflicting opinions. And when comparing the data type I get with its associated length it seems that it could be the max length but who knows maybe the field is just m开发者_高级运维axed out?
For Example:
Data Type = Number
is returning Data Length = 22
Data Type = VARCHAR2
is returning Data Length = 2000
Bonus: (+5 points on final grade, and 1 free absence from class)
How could I change the query to get the data I dont have. (actual data length, or max data length).
It should be relatively easy to test. Note that there is no ALL_TABLE_COLUMNS
view-- there is an ALL_TAB_COLUMNS
and an ALL_TAB_COLS
view that provide this information. I'll create an empty table and run the query I assume your professor intended.
SQL> create table empty_table (
2 col1 number(10,2),
3 col2 varchar2(100),
4 col3 varchar2(100 byte),
5 col4 varchar2(100 char)
6 );
Table created.
SQL> select column_name, data_type, data_length
2 from all_tab_columns
3 where table_name = 'EMPTY_TABLE'
4 order by column_name;
COLUMN_NAME DATA_TYPE DATA_LENGTH
------------------------------ -------------- -----------
COL1 NUMBER 22
COL2 VARCHAR2 100
COL3 VARCHAR2 100
COL4 VARCHAR2 300
Now that you've seen the actual result, it should be clear what length is being returned in the DATA_LENGTH
column.
This should represent the length that column was created with.
i.e. numbers default to 22, but you may override that NUMBER(5) or something. varchar2 (4000) etc.
In order to find the actual max length of the values contained in the column, you need to query the table itself, not the data dictionary.
SELECT max( len( mycol )) from mytab;
there may be some statistics gathered for these things, but that may not be reliable for your application.
精彩评论