473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to check primary key in a view

2
I want to list all the attributes that have constraint as primary key in a view
Mar 22 '10 #1
4 5162
rski
700 Expert 512MB
You mean, you have a view and you want to know which columns in that view are primary keys?
Mar 22 '10 #2
rk458p
2
yes, I have a view and want to know which columns in that view are primary keys?
Mar 22 '10 #3
rski
700 Expert 512MB
I'm afraid orcale do not write that information but:
you can try to parse text column from user_views and check columns you get from here
or do something similar to that
Expand|Select|Wrap|Line Numbers
  1. select distinct t1.table_name, t1.column_name,t2.referenced_name,t3.column_name,t4.constraint_name,t5.constraint_type from 
  2.     user_tab_columns t1
  3.     ,user_dependencies t2
  4.     ,user_tab_columns t3 
  5.     ,user_cons_columns t4
  6.     ,user_constraints t5
  7. where 
  8.     t1.table_name='<VIEW_NAME>'
  9.     and t1.table_name=t2.name
  10.    and t2.referenced_name=t3.table_name
  11.    and t1.column_name=t3.column_name
  12.    and t4.table_name=t3.table_name
  13.    and t4.column_name=t3.column_name
  14.    and t4.constraint_name=t5.constraint_name
  15.    and t5.constraint_type='P'
  16.  
but this query has some limits.
Mar 22 '10 #4
Are you sure, you are looking for the primary key of a view ?

The reason I'm asking is, that constraints on views have a different implementation than constraints on i.e. tables:

Constraints on views are used in data warehousing to ease the work of the optimizer and are NOT ENFORCED and NOT VALIDATED by Oracle (to be exact: they are only enforced and validated if they are a subset of a constraint on the table and the table constraint is enabled, which is pretty trivial).

If that is what you want, just can query users_constraints:

SQL> create table test (a number constraint pk_test primary key, b varchar2(10));
Table created.

SQL> create view v_test as select * from test;
View created.

SQL> alter view v_test add constraint pk_v_test primary key (a) disable novalidate;
View altered.

SQL> select constraint_name, constraint_type
2 from user_constraints where table_name = 'V_TEST';

CONSTRAINT_NAME C
------------------------------ -
PK_V_TEST P
If you are looking for the primary keys of the UNDERLYING tables, you might want to look into this (if you are working with views on tables in other schemas, you have to use the all_* views instead of the users_* views in the query below):

SQL> create table test_detail
2 ( id number constraint pk_test_detail primary key
3 ,det_col varchar2(10)
4 ,test_id number constraint fk_test references test(a)
5 );
Table created.

SQL> create or replace view v_test as
2 select a,b,id,detail from test t, test_detail d where t.a = d.id;
View created.

SQL> set col "TAB.COL" for a50
SQL> select cc.table_name || '.' || cc.column_name "TAB.COL"
2 from user_constraints c
3 ,user_cons_columns cc
4 ,user_dependencies d
5 where d.name = 'V_TEST'
6 and c.table_name = d.referenced_name
7 and c.constraint_type = 'P'
8 and cc.owner = c.owner
9 and cc.constraint_name = c.constraint_name
10 ;

TAB.COL
--------------------------
TEST.A
TEST_DETAIL.ID
Mar 23 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Neil | last post by:
I've been discussing here a SQL 7 view which scrolls slowly when linked to an Access 2000 MDB. After trying various things, I've distilled it down to the following: when the linked view has a...
4
by: serge | last post by:
I ran into a table that is used a lot. Well less than 100,000 records. Maybe not a lot of records but i believe this table is used often. The table has 26 fields, 9 indexes but no Primary Key at...
3
by: Rich | last post by:
I have a form with 2 check boxes. One of the check boxes is used to specify that the user is a "primary contact." When I check the primary contact box I want a second box for "standard contact"...
7
by: Philip | last post by:
Hey all, (Access 2000) I've been having a horror story with this design problem. My Database is Structured like This: AUTHORS, BOOKS, PAGES. Those are the Tables and each Item in each table...
4
by: Chris | last post by:
I have a created a SQL Database with a table called Tbl_Customer which includes lots of Rows of customer information. The primary Key is CustID which is an Identity (Auto Number) I want to be...
8
by: Paul Hunter | last post by:
I am new to databases and thus to Access. I have a situation where I am trying to figure out how to key some tables I am working with. Consider that I have a database of my own records which are...
115
by: LurfysMa | last post by:
Most of the reference books recommend autonum primary keys, but the Access help says that any unique keys will work. What are the tradeoffs? I have several tables that have unique fields. Can...
5
by: Alan Little | last post by:
I have affiliates submitting batches of anywhere from 10 to several hundred orders. Each order in the batch must include an order ID, originated by the affiliate, which must be unique across all...
16
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
5
by: starke1120 | last post by:
Im creating a check in – check out database for RF guns. I have a table that contains models. ID (primary key) Model A table that contains Gun Details ID (primary key) Model_id...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.