How to get all not null columns in a table - oracle

I have a requirement to find all not-null columns in a table. For example, my table is the below one
Lets say, Column1, Column2 and Column3 have not-null constraints and Column4, Column5 and Column6 are of nullable types. Is there any query in Oracle that list the column names that are of not-null types, ie I need to get the column names Column1, Column2 and Column3.
DESIRED OUTPUT
Column1
Column2
Column3
I know there should be a simple way to achieve this, but am new to Oracle. Any help would be highly appreciated.

You can query the all_tab_columns table:
select column_name
from all_tab_columns
where table_name = 'TABLE1'
and nullable = 'N';

I know there should be a simple way to achieve this, but am new to Oracle.
Well, online documentation is exactly what you need to look into.
Depending on the privilege, you need to look into [DBA|USER|ALL]_TAB_COLUMNS.
ALL_TAB_COLUMNS
Column Datatype Description
NULLABLE VARCHAR2(1) Indicates whether a column allows NULLs.
The value is N if there is a NOT NULL constraint
on the column or if the column is part of a PRIMARY KEY.
The constraint should be in an ENABLE VALIDATE state.
So, per the documentation, you need to use the filter:
NULLABLE = 'N'

Related

how to check null condition on plsql cursor column?

i am creating a cursor on a query getting all the columns from a table. in a loop i am using each column and printing it out. I wanted to know any easy way to check whether a column has any value or not. if a column in a row is null, put "NULL". I know I can use if condition to check each and every column in a row from the cursor in a for loop. but I have alot of columns, so is there an easy way of doing that?
Rather than handle this requirement within PL/SQL, you should consider doing it in the cursor itself. In general, the more you can do in your SQL the better (The optimizer will handle the performance). Using lots of PL/SQL logic especially unnecessary if/else conditions in a loop will benefit you if you are processing a lot of data.
In your cursor you can simply:
CURSOR c1 IS
SELECT NVL(column, 'NULL')
FROM dual;
There are plenty of in-built SQL functions that will handle this for you. Above, I used NVL, but you can use COALESCE, CASE, etc.
CASE example (note - if the column is not datatype VARCHAR then you will need to apply some datatype conversion to avoid any errors):
CURSOR c1 IS
SELECT (CASE
WHEN column IS NULL THEN
'NULL'
ELSE
column
END)
FROM dual;
COALESCE example:
CURSOR c1 IS
SELECT COALESCE(column, 'NULL')
FROM dual;
It's worth noting that NVL is a binary function, meaning only one column/value can be tested, however COALESCE can test multiple columns/values at the same time like below:
SELECT COALESCE(column1, column2, column3, 'NULL')
FROM YOUR_TABLE;
In the case that column1 value is NULL and column2 is NOT NULL then column2 value will be returned.
If column1 and column2 values are both NULL then column3 value will be returned.
If column1, column2, column3 are all NULL then the value 'NULL' is returned.

Find the primary key on a table in oracle

I am trying to write a stored procedure to get all the dependencies on a table. Here is the code which I tried and I dont really get any output from it.
Tere are two blocks of cod. The first one is to get the FK and PK for this table. The second block gets the FK references from other tables.
I have taken table name and schema names as input and all the other variables as out. i was trying to make this code dynamic.
You use out variable in where clause:
and rowner is not null
and rconstraintname is not null
and rconstraintname in (select constraint_name from all_constraints) ;
It should be r_constraint_name and r_owner
In the second query add constraint_name in subquery:
and table_name = tablename
and constraint_name = ac.r_constraint_name)
And hear #WilliamRobertson ;)

how to get column names from Hive table, if that column contains null values?

I tried with following queries, these queries giving only the count of null values, What i need was to do a null check and return the column name.
select count(*)-count(columnA), count(*)-count(columnB) from table;
select count(*) from table where columnA is null;
You can also do
SELECT
SUM(IF(columnA is NULL,1,0)) as canulls,
SUM(IF(columnB is NULL,1,0)) as cbnulls
FROM table;
This will also give you the number of null fields for each columns.

How to select multiple column names (not values) in a table when column value is specified-Oracle

I have a table which has around 30 columns.Out of 30 columns i need to retrieve around 25 column names where value is set to some specified value(say 1).
I am not able to find a way to do this.Will multiple if statement work as below
select if columnname1='1' then 'columnname1' else null
if columnname2='1' then 'columnname2' else null from table.
In case the value is not set to 1, i don't want to retrieve the column names.
The below query can give me the column names but i can't specify the value with below query
select DISTINCT COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME='table_name'.
Does this work for you?
SELECT COLUMN_NAME
from ALL_TAB_COLUMNS
where TABLE_NAME='TABLE_NAME'
AND INSTR(column_name,'1') >0

Oracle: Is there a way to get the column data types for a view?

For a table in oracle, I can query "all_tab_columns" and get table column information, like the data type, precision, whether or not the column is nullable.
In SQL Developer or TOAD, you can click on a view in the GUI and it will spit out a list of the columns that the view returns and the same set of data (data type, precision, nullable, etc).
So my question is, is there a way to query this column definition for a view, the way you can for a table? How do the GUI tools do it?
You can use user_tab_columns (or all_tab_columns and dba_tab_columns respectively) regardless if table_name refers to a view or a table.
View columns appear in all_tab_columns, so you can query them just as you can tables.
Just simply write this query:
SQL> desc TABLE/VIEW NAME;
For example if the table/view name is "department" Then just write:
SQL> desc department;
This will give the list of all fields, it's type and default Null info of the table or view.
you can use the ANSI catalog views, should work for most RDBMs
select *
from information_schema.columns c
join information_schema.tables t on c.table_name = t.table_name
where table_type = 'view'

Resources