DB2 SQL Show Long and Short Table Names? - sql

I've started creating descriptive table names in DB2 and have found DB2 has a short name and a long name for each table.
EmployeePlan has a short name of Emplo00001
I would like to be able to display both names from a sql statement.
Here's my existing SQL -- is there a table I can join to in order to get the short name?
select * --table_name, system_column_name, column_text, Type_Name, column_Size, *
from sysibm.SQLColumns
where table_schem IN ('LAWMOD9T', 'LIBDDS')
and upper(table_name) IN ('EMPLOYEEPLAN')
ORDER BY system_column_name
And thank you, Darius X for answering my question so quickly. Here's my final query:
SELECT b.system_table_name as ShortName,
a.table_name, a.system_column_name, a.column_text,
a.type_name, a.column_size
FROM sysibm.SQLColumns a
INNER JOIN qsys2.systables b
ON a.table_name = b.table_name
AND a.table_schem = b.table_schema
WHERE UPPER(a.table_schem) IN ('LAWMOD9T', 'LIBDDS')
AND UPPER(a.table_name) IN ('EMPLOYEEPLAN')
ORDER BY a.table_schem, a.table_Name, a.ordinal_position

There may be more than one way, but if you run this query:
select *
from qsys2.systables
where table_schema IN ('LAWMOD9T', 'LIBDDS')
You'll see that SYSTEM_TABLE_NAME is one of the columns. So, you can join to qys2.systables using the schema and "long" table name.

You can add too the short name filter too
select
TABLE_NAME
from QSYS2.SYSTABLES
where table_schema = 'SchemaName' AND SYSTEM_TABLE_NAME = 'SystemName';

Related

Is there a function to return which phrase in array the row matched when using LIKE ANY (array['%term1%', '%term2%', etc.])?

I want all tables from my schema that match a list of other tables with similar names from a different schema. I am using the following query:
SELECT TABLE_NAME, COUNT(COLUMN_NAME) ColCount
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'schema_2' and TABLE_NAME LIKE ANY (array[
'%table1%',
'%table2%',
'%table3%',
'%table4%',
'%table5%'])
I want another column added to the output which specifies which name in the array the table from schema_2 matched, i.e.
TABLE_NAME COL_COUNT SCHEMA_1_TABLE
table1a 15 table1
Is there a way to do this?
You could try using a join. Kind of like:
WITH a AS (SELECT unnest(array['%table1%','%table2%','%table3%','%table4%','%table5%'])
AS table)
SELECT
isc.TABLE_NAME,
COUNT(isc.COLUMN_NAME),
REPLACE(a.table,'%','')
FROM
INFORMATION_SCHEMA.COLUMNS isc
LEFT JOIN a ON (isc.TABLE_NAME LIKE a.table)
WHERE
isc.TABLE_SCHEMA = 'schema_2'
AND a.table IS NOT NULL
GROUP BY
isc.TABLE_NAME,
a.table
You could also join INFORMATION_SCHEMA.COLUMNS with INFORMATION_SCHEMA.TABLES in order to just detect any duplication, not just the ones specified.

What is the sql command for Sybase IQ to get the List of columns from a table separated with a delimeter. We use this below command in oracle

Below is the command I am using in SQL developer and that result is Like Col1, Col2, Col3,..n
select listagg(COLUMN_NAME, ', ') within group (order by column_id asc)
from all_tab_columns
where table_name = 'NEIGHBORHOOD_ZIP_IQ';
Sybase (now SAP) IQ is built on top of (or uses as a front-end) the Sybase (also now SAP) SQLAnywhere database engine; net result is that, depending on what you're trying to do, you can use the SQL constructs (eg, functions) from both products.
NOTE: I don't have a IQ db in front of me so you may need to tweak the following ...
SQLAnywhere has a list() function that works similar to Oracle's listagg().
Here are a few examples of displaying the columns (as a comma-delimited list) from the SYSTABLE table:
# all of the following examples use the comma (',') as the delimiter
# output determined by order in which column names are pulled from table (probably column_id)
select list(c.column_name,',')
from SYSTABLE t
join SYSCOLUMN c
on t.table_id = c.table_id
where t.table_name = 'SYSTABLE'
go
table_id,file_id,count,first_page,last_page,primary_root,creator,first_ext_page,last_ext_page,table_page_count,ext_page_count,object_id,table_name,table_type,view_def,remarks,replicate,existing_obj,remote_location,remote_objtype,srvid,server_type,primary_hash_limit,page_map_start,source,encrypted,location_escape_char
# order the output by column_name
select list(c.column_name order by c.column_name,',')
from SYSTABLE t
join SYSCOLUMN c
on t.table_id = c.table_id
where t.table_name = 'SYSTABLE'
go
count,creator,encrypted,existing_obj,ext_page_count,file_id,first_ext_page,first_page,last_ext_page,last_page,location_escape_char,object_id,page_map_start,primary_hash_limit,primary_root,remarks,remote_location,remote_objtype,replicate,server_type,source,srvid,table_id,table_name,table_page_count,table_type,view_def
# order the output by column_id
select list(c.column_name order by c.column_id,',')
from SYSTABLE t
join SYSCOLUMN c
on t.table_id = c.table_id
where t.table_name = 'SYSTABLE'
go
table_id,file_id,count,first_page,last_page,primary_root,creator,first_ext_page,last_ext_page,table_page_count,ext_page_count,object_id,table_name,table_type,view_def,remarks,replicate,existing_obj,remote_location,remote_objtype,srvid,server_type,primary_hash_limit,page_map_start,source,encrypted,location_escape_char

Check for the existence of a column in v$database in Oracle

I am familiar how to check for a column name in a table or view by using the following query
select count(*)
from user_tab_columns
where table_name = [name of view]
and column_name = [name of column]
But, this doesn't work for a column that I am trying to check for in v$database.
Is there a workaround for this?
v$database is actually a public synonym for the v_$database view owned by sys (checked in Oracle 12).
So to get column information:
You can't use user_tab_columns, because that only considers tables/views owned by the current user. But the view you're interested in is owned by sys.
You can't use the synonym name v$database. You have to use the actual view name: v_$database.
So you can use either dba_tab_columns or all_tab_columns like so:
select count(*)
from dba_tab_columns
where table_name = 'V_$DATABASE'
and column_name = [name of column]
EDIT:
Another way that is less dependant on the actual view name in sys, which I guess could change between Oracle database versions, would be to join with dba_synonyms:
select count(*)
from dba_synonyms s
join dba_tab_columns c
on c.owner = s.table_owner
and c.table_name = s.table_name
and c.column_name = [column name]
where s.owner = 'PUBLIC'
and s.synonym_name = 'V$DATABASE'
Try this
select *
from dba_tab_columns
where table_name = 'V_%DATABASE%'

Find all tables without a certain column name

So, I've seen lots of questions about finding all tables with a specific column name. However, I'm trying to find all tables WITHOUT a specific column. (In this case, EndDate). Is there a more elegant solution than just finding all the tables with that column, and comparing it to the list of all tables?
SELECT
table_name
FROM
INFORMATION_SCHEMA.TABLES T
WHERE
T.TABLE_CATALOG = 'MyDB' AND
T.TABLE_TYPE = 'BASE TABLE' AND
NOT EXISTS (
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE
C.TABLE_CATALOG = T.TABLE_CATALOG AND
C.TABLE_SCHEMA = T.TABLE_SCHEMA AND
C.TABLE_NAME = T.TABLE_NAME AND
C.COLUMN_NAME = 'EndDate')
Try the following, It's standard SQL (and will work for almost every platform)
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
EXCEPT
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'EndDate'
Just as you suggested, you can't really get anything that's simpler than this.
This should do it.
SELECT * FROM INFORMATION_SCHEMA.TABLES t
WHERE NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS c
WHERE c.TABLE_NAME = t.TABLE_NAME AND c.TABLE_SCHEMA=t.TABLE_SCHEMA
AND c.COLUMN_NAME='EndDate')
I have many more schemas and tables than I really ought to, so I need an optimized version that runs in a second.
Why not get all tables, then get all tables with the field, UNION ALL these results, and just show the table names where HAVING COUNT(table_name) = 1? That's just what I did.
Notice the two vars to change below: YourSchemaHere and YourFieldHere.
SELECT TABLE_NAME, COUNT(TABLE_NAME) FROM (
(SELECT DISTINCT c1.TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS c1
WHERE c1.Table_Schema = "YourSchemaHere")
UNION ALL
(SELECT DISTINCT c2.TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS c2
WHERE c2.Table_Schema = "YourSchemaHere" AND
c2.Column_Name = "YourFieldHere")
) x GROUP BY TABLE_NAME
HAVING COUNT(TABLE_NAME) = 1;
Online Demo at SQL Fiddle: http://sqlfiddle.com/#!9/791dde/1/0
Explanation of my approach:
Get all tables with : SELECT DISTINCT c1.TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS c1 WHERE c1.Table_Schema = "YourSchemaHere"
Get all tables with column : SELECT DISTINCT c2.TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS c2 WHERE c2.Table_Schema = "YourSchemaHere" AND c2.Column_Name = "YourFieldHere"
UNION ALL these two results.
Items in resulting list will be duplicated if they have the column, but only appear once if they do not.
Select only those without the field:HAVING COUNT(*) = 1

mysql get table column names in alphabetical order

Is it possible to query a MySQL database to get the column names of a table in alphabetical order? I know that
SHOW COLUMNS `table_name`;
or
DESCRIBE `table_name`;
will give me a list of the columns in a table (along with other info), but is it possible to alter the query in order to get the columns sorted alphabetically. Adding ORDER BY 'Field' didn't work, it gave a syntax error.
The ANSI INFORMATION_SCHEMA tables (in this case, INFORMATION_SCHEMA.COLUMNS) provide more flexibility in MySQL:
SELECT c.column_name
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE c.table_name = 'tbl_name'
-- AND c.table_schema = 'db_name'
ORDER BY c.column_name
Every field was listed twice until I used group by column name
select c.COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS c
where c.TABLE_NAME = `'tbl_name'`
group by c.column_name
order by c.column_name
If you want more details, below query is really handy:
SELECT COLUMN_NAME as 'Field',
COLUMN_TYPE as 'Type',
IS_NULLABLE as 'Null',
COLUMN_KEY as 'Key',
COLUMN_DEFAULT as 'Default',
EXTRA as 'Extra'
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'my table' and
TABLE_SCHEMA = 'my database'
add ordering
order by Type; --