473,587 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query to find Primary and Foreign keys

rsrinivasan
221 New Member
Hi,

1. I have some table. But i do not know how to find primary key column, forign key column in that table. Is any procedure or function is available in oracle to find this?.

Thanks,
Srinivasan r
May 9 '07 #1
26 137836
chandu031
78 Recognized Expert New Member
Hi,

1. I have some table. But i do not know how to find primary key column, forign key column in that table. Is any procedure or function is available in oracle to find this?.

Thanks,
Srinivasan r
Hi,

This is the query to get a list of primary keys:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM ALL_CONS_COLUMNS A JOIN ALL_CONSTRAINTS C  ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME WHERE C.TABLE_NAME = <your table> AND C.CONSTRAINT_TYPE = 'P'
  2.  
And this is the query for foreign keys:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM ALL_CONS_COLUMNS A JOIN ALL_CONSTRAINTS C  ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME WHERE C.TABLE_NAME = <your table> AND C.CONSTRAINT_TYPE = 'R'
  2.  
May 9 '07 #2
pradeep kaltari
102 Recognized Expert New Member
Hi,

1. I have some table. But i do not know how to find primary key column, forign key column in that table. Is any procedure or function is available in oracle to find this?.

Thanks,
Srinivasan r
Hi,
The following query will give all the constraints specified on a table belonging to <owner_name> schema.

Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM SYS.ALL_CONSTRAINTS A , SYS.ALL_CONS_COLUMNS B
  3. WHERE S.OWNER=<owner_name> AND S.TABLE_NAME=<table_name> AND A.CONSTRAINT_NAME=B.CONSTRAINT_NAME
  4.  
You can further filter this based upon the constraint type you are interested in. For primary key constraint, include CONSTRAINT_TYPE ='P' in the WHERE clause. For foreign key constraint, include CONSTRAINT_TYPE ='R' in the WHERE clause.
May 9 '07 #3
frozenmist
179 Recognized Expert New Member
Hi ,

To correct Chandu. you should also have the owner name.
So code becomes
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM ALL_CONS_COLUMNS A JOIN ALL_CONSTRAINTS C  ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME WHERE C.TABLE_NAME = <your table> AND C.CONSTRAINT_TYPE = 'R' and C.OWNER='<OWNER NAME>'
  2.  
Cheers
May 9 '07 #4
chandu031
78 Recognized Expert New Member
Hi,

This is the query to get a list of primary keys:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM ALL_CONS_COLUMNS A JOIN ALL_CONSTRAINTS C  ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME WHERE C.TABLE_NAME = <your table> AND C.CONSTRAINT_TYPE = 'P'
  2.  
And this is the query for foreign keys:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM ALL_CONS_COLUMNS A JOIN ALL_CONSTRAINTS C  ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME WHERE C.TABLE_NAME = <your table> AND C.CONSTRAINT_TYPE = 'R'
  2.  

Yes..You will have to add one more filter on the OWNER as the same table names can be used across users. Thks for pointing it out Frozen.

If you want only the column names you can use
SELECT C.COLUMN_NAME, C.POSITION instead of SELECT *

Position is helpful when you have composite keys.
May 9 '07 #5
rsrinivasan
221 New Member
Hi,
Thanks for all...
May 9 '07 #6
rsrinivasan
221 New Member
Hi,

Fine. It is working.
What is that table ALL_CONSTRAINTS and ALL_CONS_COLUMN S.

Whether it is System table or anyother?

Thanks,
Srinivasan r
May 9 '07 #7
pradeep kaltari
102 Recognized Expert New Member
Hi,

Fine. It is working.
What is that table ALL_CONSTRAINTS and ALL_CONS_COLUMN S.

Whether it is System table or anyother?

Thanks,
Srinivasan r
These are system views present in SYS schema.
May 9 '07 #8
rsrinivasan
221 New Member
Hi,
Just now i see it..

What is the difference between Views and Tables. Anything advantage by using Views? When should we use Views?

Thanks..
Srinivasan r
May 9 '07 #9
cpiyush
31 New Member
It was quite useful...

Thanks to everyone.

Cheer.
cPiyush.
May 9 '07 #10

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

Similar topics

31
3353
by: Robert Brown | last post by:
Let's say I have a type hierarchy: (just an example) the general entity customer: CREATE TABLE customer(customer_id int, customer_name varchar(250), customer_type int) three specific customer subtypes: 1 - business, 2 - home, 3 - university
26
14104
by: pb648174 | last post by:
I have a table called BidItem which has another table called BidAddendum related to it by foreign key. I have another table called BidFolder which is related to both BidItem and BidAddendum, based on a column called RefId and one called Type, i.e. type 1 is a relationship to BidItem and type 2 is a relationship to BidAddendum. Is there any...
10
17785
by: D. Dante Lorenso | last post by:
I'd like to run a clean up command on my tables to eliminate rows that I'm no longer using in the database. I want to do something like this: DELETE FROM tablename WHERE IS_REFERENCED_BY_FOREIGN_KEY IS FALSE; Does anyone know how something like this could be done in PostgreSQL? I know I can search all the tables that
5
3327
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example I'm adding a customer. The Customer fields are mostly foreign keys that refer to primary keys in other tables, left join instead of junction tables...
1
2071
by: Thomas T. Thai | last post by:
I'm looking for a better way to make use of foreign keys. Here is a sample setup: -- TESTING Foreign Keys create table mod ( mod_id int not null primary key, name varchar(32) not null default '' );
9
3898
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for eg: area.txt, school.txt, students.txt.... and so on (ok?!?) now, 1. area code used in the school.txt must be defined in the area.txt (Primary key...
6
14289
by: ravichoudhari | last post by:
i came accross requirement of multiple foreign keys in a table referencing the same primary key another table. i created the table relations using the relations editor in access. even though i could have multiple foreign keys to a table access did allow the referential integrity. my database structure is table1 - students table table 2...
4
2959
by: Wolfgang Keller | last post by:
Hello, so far it seems to me as if the only ORM module for Python which supports composite primary/foreign keys was SQLAlchemy. Which looks a little bit "overbloated" for my needs: I "just" need to be able to define a "logical model" (à la UML) in Python and have the ORM connect to a database (running on PostgreSQL in my case) which uses a...
2
3577
JnrJnr
by: JnrJnr | last post by:
I have two SQL database tables. One contains various products(with unique primary keys) and the other contains information related to the products aswel as the product's foreign keys. What I want to do is first get all the foreign keys(using SQL queries/select statements) and "store" them so that I can then, according to those foreign keys, get...
0
7915
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.