Connecting Tech Pros Worldwide Forums | Help | Site Map

Query to find Primary and Foreign keys

rsrinivasan's Avatar
Familiar Sight
 
Join Date: Mar 2007
Location: India
Posts: 221
#1: May 9 '07
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

chandu031's Avatar
Expert
 
Join Date: Mar 2007
Posts: 76
#2: May 9 '07

re: Query to find Primary and Foreign keys


Quote:

Originally Posted by rsrinivasan

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.  
Expert
 
Join Date: May 2007
Location: India
Posts: 101
#3: May 9 '07

re: Query to find Primary and Foreign keys


Quote:

Originally Posted by rsrinivasan

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.
frozenmist's Avatar
Expert
 
Join Date: May 2007
Location: Bangalore
Posts: 176
#4: May 9 '07

re: Query to find Primary and Foreign keys


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
chandu031's Avatar
Expert
 
Join Date: Mar 2007
Posts: 76
#5: May 9 '07

re: Query to find Primary and Foreign keys


Quote:

Originally Posted by chandu031

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.
rsrinivasan's Avatar
Familiar Sight
 
Join Date: Mar 2007
Location: India
Posts: 221
#6: May 9 '07

re: Query to find Primary and Foreign keys


Hi,
Thanks for all...
rsrinivasan's Avatar
Familiar Sight
 
Join Date: Mar 2007
Location: India
Posts: 221
#7: May 9 '07

re: Query to find Primary and Foreign keys


Hi,

Fine. It is working.
What is that table ALL_CONSTRAINTS and ALL_CONS_COLUMNS.

Whether it is System table or anyother?

Thanks,
Srinivasan r
Expert
 
Join Date: May 2007
Location: India
Posts: 101
#8: May 9 '07

re: Query to find Primary and Foreign keys


Quote:

Originally Posted by rsrinivasan

Hi,

Fine. It is working.
What is that table ALL_CONSTRAINTS and ALL_CONS_COLUMNS.

Whether it is System table or anyother?

Thanks,
Srinivasan r

These are system views present in SYS schema.
rsrinivasan's Avatar
Familiar Sight
 
Join Date: Mar 2007
Location: India
Posts: 221
#9: May 9 '07

re: Query to find Primary and Foreign keys


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
cpiyush's Avatar
Newbie
 
Join Date: Jan 2007
Posts: 31
#10: May 9 '07

re: Query to find Primary and Foreign keys


It was quite useful...

Thanks to everyone.

Cheer.
cPiyush.
Expert
 
Join Date: May 2007
Location: India
Posts: 101
#11: May 9 '07

re: Query to find Primary and Foreign keys


Quote:

Originally Posted by rsrinivasan

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

Views are snapshots of tables. Data is not physically stored in the views. Views reflect the present data in the tables upon which they are created. You can access data from the views in the same way as you access data from tables.

E.g:Say you have a Employee table with the following columns:
Empno Deptno Name Salary
--------- ----------- --------- - --------

You can create a view which contains a subset of these columns.

Expand|Select|Wrap|Line Numbers
  1. CREATE VIEW EMP_VIEW
  2. AS
  3. SELECT Empno, Name, Salary 
  4. FROM Employee
  5.  
Now every time you fire a query on the view, the actual query on which the view is built is executed first followed by the query you fired.
A single View can be built upon multiple tables (you can also use JOINs whie creating the views).
I hope this cleared some of your doubts.
rsrinivasan's Avatar
Familiar Sight
 
Join Date: Mar 2007
Location: India
Posts: 221
#12: May 9 '07

re: Query to find Primary and Foreign keys


Quote:

Originally Posted by pradeep kaltari

Views are snapshots of tables. Data is not physically stored in the views. Views reflect the present data in the tables upon which they are created. You can access data from the views in the same way as you access data from tables.

E.g:Say you have a Employee table with the following columns:
Empno Deptno Name Salary
--------- ----------- --------- - --------

You can create a view which contains a subset of these columns.

Expand|Select|Wrap|Line Numbers
  1. CREATE VIEW EMP_VIEW
  2. AS
  3. SELECT Empno, Name, Salary 
  4. FROM Employee
  5.  
Now every time you fire a query on the view, the actual query on which the view is built is executed first followed by the query you fired.
A single View can be built upon multiple tables (you can also use JOINs whie creating the views).
I hope this cleared some of your doubts.

hi,
Nice. From ur stat I feel that Views are not phisicaly stored in memory. And it is just a Virtual table. Right?

1. Whether it is possible insert or delete records from views?
2. If I insert record in views whether it affects the real table(physical table)?

Why we are using Views instead of Tables directly?

Thanks,
Srinivasan r
Expert
 
Join Date: May 2007
Location: India
Posts: 101
#13: May 9 '07

re: Query to find Primary and Foreign keys


Quote:

Originally Posted by rsrinivasan

hi,
Nice. From ur stat I feel that Views are not phisicaly stored in memory. And it is just a Virtual table. Right?

1. Whether it is possible insert or delete records from views?
2. If I insert record in views whether it affects the real table(physical table)?

Why we are using Views instead of Tables directly?

Thanks,
Srinivasan r

Yes you can say views are virtual tables. You cannot insert/delete records from views. To understand the usage of views consider the following:

Suppose 2 ppl (Admin and clerk) access a table. The clerk is authorised only to view (and cant modify the contents of the table) some part of data from the table but the Admin has full access. Now the Admin can create a view containing only the data required for the clerk and allow the clerk to access it.

Also, suppose data is spread out in two tables and you want to see it in a single table. Instead of creating another table to store this data, you can create a view for the same and hence utilize memo in a better way.
rsrinivasan's Avatar
Familiar Sight
 
Join Date: Mar 2007
Location: India
Posts: 221
#14: May 9 '07

re: Query to find Primary and Foreign keys


Hi,
I understood the real use of Views. Thanks...
rsrinivasan's Avatar
Familiar Sight
 
Join Date: Mar 2007
Location: India
Posts: 221
#15: May 9 '07

re: Query to find Primary and Foreign keys


Hi,
I have another doubt.
How to create user for oracle and assign only some previlliges. For example only to insert recort into the table.

thanks,
Srinivasan r
Expert
 
Join Date: May 2007
Location: India
Posts: 101
#16: May 9 '07

re: Query to find Primary and Foreign keys


Quote:

Originally Posted by rsrinivasan

Hi,
I have another doubt.
How to create user for oracle and assign only some previlliges. For example only to insert recort into the table.

thanks,
Srinivasan r

Hi,
Follow the link: http://www.techonthenet.com/oracle/grant_revoke.php

Cheers,
Pradeep
rsrinivasan's Avatar
Familiar Sight
 
Join Date: Mar 2007
Location: India
Posts: 221
#17: May 9 '07

re: Query to find Primary and Foreign keys


Hi,
I create a function in oracle

Expand|Select|Wrap|Line Numbers
  1. create or replace function 
  2. divide(first integer, second integer) return integer is 
  3. begin 
  4.   if first>second 
  5.       return first 
  6.   else 
  7.       return second 
  8. end;
  9.  
But it tells warning as below..

Warning: Function created with compilation errors.

What is the error? Give correct syntex...

Thanks,
Srinivasan r
debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,509
#18: May 9 '07

re: Query to find Primary and Foreign keys


check the code
Expand|Select|Wrap|Line Numbers
  1. create or replace function divide(first integer, second integer) return integer is
  2. begin
  3. if first > second then return first;
  4. else return second;
  5. end if;
  6. end;
  7.  
cpiyush's Avatar
Newbie
 
Join Date: Jan 2007
Posts: 31
#19: May 9 '07

re: Query to find Primary and Foreign keys


Quote:

Originally Posted by rsrinivasan

Hi,
I create a function in oracle

Expand|Select|Wrap|Line Numbers
  1. create or replace function 
  2. divide(first integer, second integer) return integer is 
  3. begin 
  4.   if first>second 
  5.       return first 
  6.   else 
  7.       return second 
  8. end;
  9.  
But it tells warning as below..

Warning: Function created with compilation errors.

What is the error? Give correct syntex...

Thanks,
Srinivasan r

Execute this query if you are getting compilation errors:-
show errors;
This will list the line number/col number combination & the description of error which you have made in the function definition.

Check your mistakes using this, as I can see one mistake just by reviewing ur code that you have not specifieds the type(IN/OUT) of the arguments passed in the prototype...

Cheer!!!
cPiyush.
debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,509
#20: May 9 '07

re: Query to find Primary and Foreign keys


You had made some basic mistakes

1. Forgotten to add semicolons.
2. Forgotten to add End if.
debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,509
#21: May 9 '07

re: Query to find Primary and Foreign keys


It is not compulsary to specify type.

If nothing is specified default type Is IN .

Not specifying the parameter MODE is not an error.
cpiyush's Avatar
Newbie
 
Join Date: Jan 2007
Posts: 31
#22: May 10 '07

re: Query to find Primary and Foreign keys


Quote:

Originally Posted by debasisdas

It is not compulsary to specify type.

If nothing is specified default type Is IN .

Not specifying the parameter MODE is not an error.

Oh...Thanks Das...:-)
Newbie
 
Join Date: Sep 2007
Posts: 6
#23: Sep 21 '07

re: Query to find Primary and Foreign keys


Quote:

Originally Posted by pradeep kaltari

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.

Hi all,
the disscusion is very interesting... I would need to know the way to get the FK between two tables, could you help me?
Thank you in advance,
Pumuky
Reply