Transform SQL Query to Oracle Query - sql

I've got this SQL Query that I need to write for Oracle, please could you help ?
SELECT key_column_usage.column_name
FROM information_schema.key_column_usage
WHERE table_schema = SCHEMA()
AND constraint_name = 'PRIMARY'
AND table_name = 'posts'

I don't know what parts of your query represent, but - let me try. It looks like you'd like to query constraints table. If so, then one option is to query user_cons_columns:
from user_cons_columns c
where c.owner = user
and c.constraint_name = 'PRIMARY'
and c.table_name = 'POSTS'
where
user represents currently logged user
'PRIMARY' is name of that constraint
'POSTS' is table name; in Oracle, by default, table names are stored in UPPERCASE
Alternatively, join user_constraints and user_cons_columns:
from user_constraints r join user_cons_columns c on c.constraint_name = r.constraint_name
where r.owner = 'SCOTT'
and r.constraint_type = 'P'
and r.table_name = 'POSTS'
because constraint_type = 'P' represents primary keys (if that's what you actually want), and it is stored in user_constraints.
Just for example, this time I used 'SCOTT' as an owner name.
If you want and/or have sufficient privileges, instead of user_, you can query all_ or dba_ views to fetch information you need. So, that would be e.g. all_cons_columns etc.

Related

Get column name, data type, size and comments of Oracle table

How can I get column name, data type, size and comments of table?
I tried
SELECT all_tab.column_name, all_tab.data_type, all_tab.data_length, col_com.COMMENTS
FROM all_tab_columns all_tab
JOIN user_col_comments col_com ON all_tab.TABLE_NAME = col_com.TABLE_NAME
WHERE all_tab.TABLE_NAME='MY_TABLE'
But it didn't work.
You need to add column name connection:
SELECT all_tab.column_name,
all_tab.data_type,
all_tab.data_length,
(SELECT COMMENTS
FROM user_col_comments t
where t.TABLE_NAME = all_tab.TABLE_NAME
and t.COLUMN_NAME = all_tab.column_name)
FROM all_tab_columns all_tab
WHERE all_tab.TABLE_NAME = 'MY_TABLE'
The USER_ views show information about objects, tables in this case, that are owned by the schema user you are connecting as. The ALL_ views show information abut objects that the connected schema user has permissions to see.
If you are only interested in objects that are created by the schema owner then by all means use the USER_ views.
However, you probably want;
SELECT all_tab.owner,
all_tab.table_name,
all_tab.column_name,
all_tab.data_type,
all_tab.data_length,
col_com.comments
FROM all_tab_columns all_tab
JOIN all_col_comments col_com
ON all_tab.table_name = col_com.table_name
AND all_tab.owner = col_com.owner
WHERE all_tab.table_name = 'MY_TABLE'
This certainly works for me but you might want to consider also retrieving the DATA_PRECISION and DATA_SCALE

Primary keys for an Oracle table

I am trying to retrieve the primary keys for a table name EMPLOYEE as follows
SELECT cols.column_name
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name ='EMPLOYEE'
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position
The table EMPLOYEE exists for another user which also has a primary key called ID
So the result of the above query gives (ID, ID) instead of (ID) from the current user. How do I get only the primary keys of the tables belonging to the user's schema
Switch to USER_* views instead of ALL_*.
Eliminate conditions on the OWNER columns in your query.
ALL_* views return everything the querying schema has access to including the objects the schema owns, in contrast to
USER_* views returning only the objects the schema is owner of
DBA_* views return all objects in database
Therefore, you can query USER_* views or you can query DBA_* views specifying the condition on the OWNER column.
In your case your query could look like this.
select
cons.table_name
, cons.constraint_name
, cols.column_name
, cols.position
from user_constraints cons, user_cons_columns cols
where cons.constraint_name = cols.constraint_name
and constraint_type = 'P'
order by cons.table_name;
For more details about data dictionary and views' and their scope, consult the Data Dictionary and Dynamic Performance Views section in Concepts.

SQL Script To Generate a Database Dictionary **With Linked Fields**

I would like to generate a Data Dictionary for a SQL Server 2008 database that has one row for each field, and the following columns:
table_name
field_name
data_type
link_table (for when the field in question is a foreign key)
link_field (for when the field in question is a foreign key)
I can get the first 3 columns with something like the SQL script below...but I don't know how to get the last two columns of foreign key information. INFORMATION_SCHEMA.TABLE_CONSTRAINTS gets close, but doesn't have the data I'm looking for. Can someone help with this point?
SELECT TABLE_NAME,COLUMN_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
Secondarily if anyone has any suggestions on additional fields which would be helpful please post.
The following query will duplicate a column if it is involved in more than one relationship.
Select C.TABLE_SCHEMA, C.TABLE_NAME, C.COLUMN_NAME, C.DATA_TYPE
, PKCol.TABLE_SCHEMA, PKCol.TABLE_NAME, PKCol.COLUMN_NAME
From INFORMATION_SCHEMA.COLUMNS As C
Left Join (INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As FKCol
Join INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS As FK
On FK.CONSTRAINT_NAME = FKCol.CONSTRAINT_NAME
Join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As PKCol
On PKCol.CONSTRAINT_NAME = FK.UNIQUE_CONSTRAINT_NAME)
On FKCol.TABLE_SCHEMA = C.TABLE_SCHEMA
And FKCol.TABLE_NAME = C.TABLE_NAME
ANd FKCol.COLUMN_NAME = C.COLUMN_NAME

Dynamically determining table name given field name in SQL server

Strange situation: I am trying to remove some hard coding from my code. There is a situation where I have a field, lets say "CityID", and using this information, I want to find out which table contains a primary key called CityID.
Logically, you'd say that it's probably a table called "City" but it's not... that table is called "Cities". There are some other inconsistencies in database naming hence I can never be sure if removing the string "ID" and finding out the plural will be sufficient.
Note: Once I figure out that CityID refers to a table called Cities, I will perform a join to replace CityID with city name on the fly. I will appreciate if someonw can also tell me how to find out the first varchar field in a table given its name.
SELECT name FROM sysobjects
WHERE id IN ( SELECT id FROM syscolumns WHERE name = 'THE_COLUMN_NAME' )
To get column information from the specified table:
SELECT column_name, data_type, character_maximum_length
FROM information_schema.columns
WHERE table_name = 'myTable'
select table_name from information_schema.columns where column_name='CityID'
You can use the INFORMATION_SCHEMA tables to read metadata about the database.
SELECT
TABLE_NAME
FROM
[db].[INFORMATION_SCHEMA].[COLUMNS]
WHERE
COLUMN_NAME='CityID';
For a primer in what's in the INFORMAITON_SCHEMA, see INFORMATION_SCHEMA, a map to your database
The information you seek is all available in the information schema views. Note that you will find many sources telling you how to directly query the underlying system tables that these are views onto - and I must admit that I do the same when it's just to find something out quickly - but the recommended way for applications is to go through these views.
For example, to find your CityID column:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'CityID'
To find the first varchar field in a table:
SELECT TOP 1 * FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_NAME = 'TableName'
AND DATA_TYPE = 'varchar' -- This is off the top of my head!
ORDER BY ORDINAL_POSITION
As I understand from your question, you want to find tables which contain CITYID column in primary key
You can use SQL Server system views like sysindexes and sysindexkeys as shown in SQL tutorial to query database table primary keys including composite primary keys which are formed
SELECT
TBL.name as TableName
FROM sysobjects as PK
INNER JOIN sys.objects as TBL
on TBL.object_id = PK.parent_obj
INNER JOIN sysindexes as IND
on IND.name = PK.name AND
IND.id = TBL.object_id
INNER JOIN SysIndexKeys as KEYS
on KEYS.id = IND.id AND
KEYS.indid = IND.indid
INNER JOIN syscolumns as COL
on COL.id = KEYS.id AND
COL.colid = KEYS.colid
WHERE
PK.xtype = 'PK' AND
COL.name = 'CityID'

Finding all Nullable Columns in SQL 2000 Database

How to find out column with NULL values allowed in the insert in whole database ?
I don't have sql at hand, but the query goes something like this
SELECT * FROM information_schema.columns WHERE is_nullable = 'YES'
In general, search for this stardard view, for all the metadata info about your schema and structure of the database; there are many others (information_schema.tables, information_schema.constraints, etc)
Those who only want to see columns from base tables (not views) should join with INFORMATION_SCHEMA.TABLES. I also like to exclude the system table sysdiagrams.
Query
SELECT
c.TABLE_NAME,
COLUMN_NAME,
DATA_TYPE
FROM
INFORMATION_SCHEMA.COLUMNS AS c
JOIN INFORMATION_SCHEMA.TABLES AS t ON t.TABLE_NAME = c.TABLE_NAME
WHERE
is_nullable = 'YES' AND
TABLE_TYPE = 'BASE TABLE' AND
c.TABLE_NAME != 'sysdiagrams'
ORDER BY
c.TABLE_NAME,
COLUMN_NAME
If you have duplicate table names across schemas or table catalogs, you should involve those fields in the join as well, as shown in the answers here:
Differentiating tables and views in INFORMATION_SCHEMA.COLUMNS.