Postgres - How to retrieve default value in table structure query - sql

I'm trying to retrieve some table's structures with the column default values using this query :
SELECT column_name, is_nullable, character_maximum_length, udt_name, default_value
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'USER'
ORDER BY ordinal_position
But I'm unable to find the correct value to select the default values. I tried "default_value" but it doesn't work...

As documented in the manual the default value is available in column_default
SELECT column_name, is_nullable, character_maximum_length, udt_name, column_default
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'USER'
ORDER BY ordinal_position

Related

SQL query return Error "Function "has_column_privilege(oid,smallint,text)" not supported

i have built Join query on redshift, but it doesn't work. Do you know why?
select
table_schema,
table_name,
sizeMB,
tbl_rows
from
(
select
table_schema ,
table_name
from information_schema.columns
where column_name = 'abcdef' -- enter table name here
and table_schema = 'prev'
and table_name not like '%crfg%'
group by table_schema, table_name
) as a(table_schema, table_name)
join
(
select "table" as tablename, size as sizeMB, tbl_rows
from svv_table_info
where "schema" = 'prev'
group by "table", size , tbl_rows
) as b(tablename, sizeMB,tbl_rows)
on a.table_name = b.tablename
and it got Error: "Function "has_column_privilege(oid,smallint,text)" not supported."

Data type sql database

I have a sql database and are trying to get the datatype of a column
Database: "dbo.Parts"
Table: "miniParts"
I tried the following using Microsoft SSMS
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = "miniPARTS" AND COLUMN_NAME = "Date"
I get the following errors:
Invalid column name "dbo.PARTS"
Invalid column name "Date"
any ideas??
Try this :
SELECT COLUMN_NAME,
DATA_TYPE,
IS_NULLABLE,
CHARACTER_MAXIMUM_LENGTH,
NUMERIC_PRECISION,
NUMERIC_SCALE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='your_table_name';

Select data for columns that are only in another table

I have two tables, table1 and table2, that have columns common to both.
The queries that obtain the column names of each table are given below,
Query to get columns from first table:
select column_name from information_schema.columns
where table_schema = 'schema1'
and table_name = 'table1';
Query to get columns from second table:
select column_name from information_schema.columns
where table_schema = 'schema2'
and table_name = 'table2';
I need to select data from table2, only the columns which are also in table1.
I do not have a postrgesql intace at the moment but Dynamic SQL is what you need.
The following query will get you the column names that appear in both table 1 and table 2.
select string_agg(column_name, ',') FROM (
select column_name from information_schema.columns
where table_schema = 'schema1'
and table_name = 'table1'
intersect
select column_name from information_schema.columns
where table_schema = 'schema2'
and table_name = 'table2'
)
And you need to build
EXECUTE 'select ' || select string_agg(column_name, ',') FROM (
select column_name from information_schema.columns
where table_schema = 'schema1'
and table_name = 'table1'
intersect
select column_name from information_schema.columns
where table_schema = 'schema2'
and table_name = 'table2'
) || ' from schema2.table2 '
Sory if there any syntax error as writing from mobile app, you can join both result set to get common data.
select column_name from information_schema.columns T2
JOIN (select column_name from information_schema.columns where table_schema = 'schema1' and table_name = 'table1') T1
ON T2.COLUMN_NAME = T1.COLUMN_NAME Where T2.table_schema = 'schema2' and T2.table_name = 'table2';

SQL: counting columns that can have null values

so I have a query that retrieves names of tables and count of columns in each of them.
SELECT Table_Schema, Table_Name, COUNT(*)
FROM Information_Schema.Columns
GROUP BY Table_Schema, Table_Name
HAVING Table_Schema = 'schema';
How can I add to this query count of columns that CAN store NULL values?
You can add a FILTER condition to the aggregate function to only count the rows where the condition is met:
SELECT table_Schema,
table_Name,
count(*) AS total_columns,
count(*) FILTER (WHERE is_nullable = 'YES') AS nullable_columns
FROM information_schema.columns
GROUP BY table_Schema, table_Name
HAVING table_schema = 'schema';
Use the is_nullable column:
SELECT Table_Schema, Table_Name, COUNT(*)
FROM Information_Schema.Columns
WHERE is_nullable = 'YES'
GROUP BY Table_Schema, Table_Name
HAVING Table_Schema = 'schema';
The documentation on information_schema.columns describes the table in full detail, including other columns that might be of interest.
EDIT:
If you want to add the information to the existing query, just use conditional aggregation:
SELECT Table_Schema, Table_Name, COUNT(*), SUM( (is_nullable = 'YES')::int ) as nullable_clumns
FROM Information_Schema.Columns
GROUP BY Table_Schema, Table_Name
HAVING Table_Schema = 'schema';

How to fetch Column Names from Table?

I am trying to fetch column names form Table in Oracle. But I am not getting Column Names.
I used Many query's, And Find may query's in Stack overflow But I didn't get answer.
I used below query's:
1. SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME='TABLE_NAME';
2. SELECT COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME='TABLE_NAME';
But Out Put is
no row selected
What is the problem here. Thank you very much
both of the queries are correct, just the thing which can cause this problem is that ,maybe you did n't write your table name with capital letters you must do something like this:
SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS
where TABLE_NAME = UPPER('TABLE_NAME');
OR
SELECT COLUMN_NAME FROM USER_TAB_COLUMNS
where TABLE_NAME = UPPER('TABLE_NAME');
Try this:
SELECT column_name
FROM all_tab_cols
WHERE upper(table_name) = 'TABLE_NAME'
AND owner = ' || +_db+ || '
AND column_name NOT IN ( 'password', 'version', 'id' )
or
SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS where TABLE_NAME = UPPER('TABLE_NAME');
I hope This query would help yu out
SELECT SCHEMA_NAME (schema_id) + '.' + t.name AS 'Table Name'
FROM sys.tables t
INNER JOIN sys.columns c
ON c.object_id = t.object_id
WHERE c.name like '%hmy%
ORDER BY 'Table Name'