How to find Primary keys in tables
Question: How do I determine if a table has a primary key and if it has one, how do I determine what columns are in the primary key?
Answer: You can retrieve primary key information with the following SQL statement: (See below for reference to all_constraints table)
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner FROM all_constraints cons, all_cons_columns cols WHERE cons.constraint_type = 'P' AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner ORDER BY cols.table_name, cols.position;
If you knew the table name that you were looking for, you could modify the SQL as follows:
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner FROM all_constraints cons, all_cons_columns cols WHERE cols.table_name = 'TABLE_NAME' AND cons.constraint_type = 'P' AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner ORDER BY cols.table_name, cols.position;
Make sure to type the table_name in uppercase, as Oracle stores all table names in uppercase.
Let's quickly explain the output from this query.
- table_name
- The name of the table (stored in uppercase).
- column_name
- The name of the column that is a part of the primary key. (also stored in uppercase)
- position
- The position in the primary key. A primary key can contain more than one column, so understanding the order of the columns in the primary key is very important.
- status
- Indicates whether the primary key is currently enabled or disabled.
- owner
- Indicates the schema that owns the table.
http://www.techonthenet.com/oracle/questions/find_pkeys.php
http://docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_1037.htm#i1576022
ALL_CONSTRAINTS
ALL_CONSTRAINTS
describes constraint definitions on tables accessible to the current user.DBA_CONSTRAINTS
describes all constraint definitions in the database.USER_CONSTRAINTS
describes constraint definitions on tables in the current user's schema.
Column | Datatype | NULL | Description |
---|---|---|---|
OWNER | VARCHAR2(30) | NOT NULL | Owner of the constraint definition |
CONSTRAINT_NAME | VARCHAR2(30) | NOT NULL | Name of the constraint definition |
CONSTRAINT_TYPE | VARCHAR2(1) | Type of constraint definition:
| |
TABLE_NAME | VARCHAR2(30) | NOT NULL | Name associated with the table (or view) with constraint definition |
SEARCH_CONDITION | LONG | Text of search condition for a check constraint | |
R_OWNER | VARCHAR2(30) | Owner of table referred to in a referential constraint | |
R_CONSTRAINT_NAME | VARCHAR2(30) | Name of the unique constraint definition for referenced table | |
DELETE_RULE | VARCHAR2(9) | Delete rule for a referential constraint (CASCADE or NO ACTION ) | |
STATUS | VARCHAR2(8) | Enforcement status of constraint (ENABLED or DISABLED ) | |
DEFERRABLE | VARCHAR2(14) | Whether the constraint is deferrable | |
DEFERRED | VARCHAR2(9) | Whether the constraint was initially deferred | |
VALIDATED | VARCHAR2(13) | Whether all data obeys the constraint (VALIDATED or NOT VALIDATED ) | |
GENERATED | VARCHAR2(14) | Whether the name of the constraint is user or system generated | |
BAD | VARCHAR2(3) | A YES value indicates that this constraint specifies a century in an ambiguous manner. To avoid errors resulting from this ambiguity, rewrite the constraint using the TO_DATE function with a four-digit year.
See Also: the
TO_DATE function in Oracle Database SQL Reference and Oracle Database Application Developer's Guide - Fundamentals | |
RELY | VARCHAR2(4) | Whether an enabled constraint is enforced or unenforced. | |
LAST_CHANGE | DATE | When the constraint was last enabled or disabled | |
INDEX_OWNER | VARCHAR2(30) | Name of the user owning the index | |
INDEX_NAME | VARCHAR2(30) | Name of the index (only shown for unique and primary-key constraints) | |
INVALID | VARCHAR2(7) | Whether the constraint is invalid | |
VIEW_RELATED | VARCHAR2(14) | Whether the constraint depends on a view |
Can one reference non primary key as a foreign key
http://stackoverflow.com/questions/18435065/foreign-key-to-non-primary-key
Foreign Key to Non Primary key
I have a table which holds data, and one of those rows needs to exist in another table. So, I want a foreign key to maintain referential integrity.
However, as you can see, the table I foreign key to, the column isn't the PK. Is there a way to create this foreign key, or maybe a better way to maintain this referential integrity?
| ||
25
|
If you really want to create a foreign key to a non-primary key, it MUST be a column that has a unique constraint on it.
From Books Online:
So in your case if you make
AnotherID unique, it will be allowed. If you can't apply a unique constraint you're out of luck, but this really does make sense if you think about it.
Although, as has been mentioned, if you have a perfectly good primary key as a candidate key, why not use that?
| ||
1
|
As others have pointed out, ideally, the foreign key would be created as a reference to a primary key (usually an IDENTITY column). However, we don't live in an ideal world, and sometimes even a "small" change to a schema can have significant ripple effects to the application logic.
Consider the case of a Customer table with a SSN column (and a dumb primary key), and a Claim table that also contains a SSN column (populated by business logic from the Customer data, but no FK exists). The design is flawed, but has been in use for several years, and three different applications have been built on the schema. It should be obvious that ripping out Claim.SSN and putting in a real PK-FK relationship would be ideal, but would also be a significant overhaul. On the other hand, putting a UNIQUE constraint on Customer.SSN, and adding a FK on Claim.SSN, could provide referential integrity, with little or no impact on the applications.
Don't get me wrong, I'm all for normalization, but sometimes pragmatism wins over idealism. If a mediocre design can be helped with a band-aid, surgery might be avoided.
| ||
0
|
Necromancing.
I assume when somebody lands here, he needs a foreign key to column in a table that contains non-unique keys. The problem is, that if you have that problem, the database-schema is denormalized.
You're for example keeping rooms in a table, with a room-uid primary key, a DateFrom and a DateTo field, and another uid, here RM_ApertureID to keep track of the same room, and a soft-delete field, like RM_Status, where 99 means 'deleted', and <> 99 means 'active'.
So when you create the first room, you insert RM_UID and RM_ApertureID as the same value as RM_UID. Then, when you terminate the room to a date, and re-establish it with a new date range, RM_UID is newid(), and the RM_ApertureID from the previous entry becomes the new RM_ApertureID.
So, if that's the case, RM_ApertureID is a non-unique field, and so you can't set a foreign-key in another table.
And there is no way to set a foreign key to a non-unique column/index, e.g. in T_ZO_REM_AP_Raum_Reinigung (WHERE RM_UID is actually RM_ApertureID).
But to prohibit invalid values, you need to set a foreign key, otherwise, data-garbage is the result sooner rather than later...
Now what you can do in this case (short of rewritting the entire application) is inserting a CHECK-constraint, with a scalar function checking the presence of the key:
|
-----------------------------------------------------
t is written in every book that foreign keys are actually primary key in some other table but can we have a foreign key which is not primary key in any other table
| |||||||||||||||||
|
24
|
Yes - you can have a foreign key that references a unique index in another table.
| ||||||||
|
table1.ID
? – zerkms Aug 26 '13 at 0:28