Query to find Primary and Foreign keys

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsrinivasan
    New Member
    • Mar 2007
    • 221

    Query to find Primary and Foreign keys

    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
    Recognized Expert New Member
    • Mar 2007
    • 77

    #2
    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:
    Code:
    SELECT * FROM ALL_CONS_COLUMNS A JOIN ALL_CONSTRAINTS C  ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME WHERE C.TABLE_NAME = <[I]your table[/I]> AND C.CONSTRAINT_TYPE = 'P'
    And this is the query for foreign keys:

    Code:
    SELECT * FROM ALL_CONS_COLUMNS A JOIN ALL_CONSTRAINTS C  ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME WHERE C.TABLE_NAME = <[I]your table[/I]> AND C.CONSTRAINT_TYPE = 'R'

    Comment

    • pradeep kaltari
      Recognized Expert New Member
      • May 2007
      • 102

      #3
      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.

      Code:
      SELECT * 
      FROM SYS.ALL_CONSTRAINTS A , SYS.ALL_CONS_COLUMNS B
      WHERE S.OWNER=<owner_name> AND S.TABLE_NAME=<table_name> AND A.CONSTRAINT_NAME=B.CONSTRAINT_NAME
      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.

      Comment

      • frozenmist
        Recognized Expert New Member
        • May 2007
        • 179

        #4
        Hi ,

        To correct Chandu. you should also have the owner name.
        So code becomes
        Code:
        SELECT * FROM ALL_CONS_COLUMNS A JOIN ALL_CONSTRAINTS C  ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME WHERE C.TABLE_NAME = <[I]your table[/I]> AND C.CONSTRAINT_TYPE = 'R' and C.OWNER='[I]<OWNER NAME>[/I]'
        Cheers

        Comment

        • chandu031
          Recognized Expert New Member
          • Mar 2007
          • 77

          #5
          Originally posted by chandu031
          Hi,

          This is the query to get a list of primary keys:
          Code:
          SELECT * FROM ALL_CONS_COLUMNS A JOIN ALL_CONSTRAINTS C  ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME WHERE C.TABLE_NAME = <[I]your table[/I]> AND C.CONSTRAINT_TYPE = 'P'
          And this is the query for foreign keys:

          Code:
          SELECT * FROM ALL_CONS_COLUMNS A JOIN ALL_CONSTRAINTS C  ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME WHERE C.TABLE_NAME = <[I]your table[/I]> AND C.CONSTRAINT_TYPE = 'R'

          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.

          Comment

          • rsrinivasan
            New Member
            • Mar 2007
            • 221

            #6
            Hi,
            Thanks for all...

            Comment

            • rsrinivasan
              New Member
              • Mar 2007
              • 221

              #7
              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

              Comment

              • pradeep kaltari
                Recognized Expert New Member
                • May 2007
                • 102

                #8
                Originally posted by rsrinivasan
                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.

                Comment

                • rsrinivasan
                  New Member
                  • Mar 2007
                  • 221

                  #9
                  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

                  Comment

                  • cpiyush
                    New Member
                    • Jan 2007
                    • 31

                    #10
                    It was quite useful...

                    Thanks to everyone.

                    Cheer.
                    cPiyush.

                    Comment

                    • pradeep kaltari
                      Recognized Expert New Member
                      • May 2007
                      • 102

                      #11
                      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.

                      Code:
                      CREATE VIEW EMP_VIEW
                      AS
                      SELECT Empno, Name, Salary 
                      FROM Employee
                      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.

                      Comment

                      • rsrinivasan
                        New Member
                        • Mar 2007
                        • 221

                        #12
                        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.

                        Code:
                        CREATE VIEW EMP_VIEW
                        AS
                        SELECT Empno, Name, Salary 
                        FROM Employee
                        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

                        Comment

                        • pradeep kaltari
                          Recognized Expert New Member
                          • May 2007
                          • 102

                          #13
                          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.

                          Comment

                          • rsrinivasan
                            New Member
                            • Mar 2007
                            • 221

                            #14
                            Hi,
                            I understood the real use of Views. Thanks...

                            Comment

                            • rsrinivasan
                              New Member
                              • Mar 2007
                              • 221

                              #15
                              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

                              Comment

                              Working...