Primary Key and Foreign Key Constraints

Hi All,
I would like to know PRIMARY KEY and FOREIGN KEY constraints on existing oracle tables. Could any one suggest me how to find out.
Thanks,
RED

You can query DBA_CONSTRAINTS to get a list of all the constraints on table A and/or table B. The documentation I linked to gives a full list of the data you can see in DBA_CONSTRAINTS, but it includes things like the referenced table name and referenced constraint name for a foreign key constraint. If A is a parent of B or B is a parent of A, you could match up the parent's primary key constraint to the child's foreign key constraint.
More generally, though, if you don't know that one of the tables is a parent of the other, figuring out how to join the two tables is probably not something that can be done using just the Oracle data dictionary. You would probably need an understanding of the data model being used to figure out what intermediate table(s) needed to be joined in order to relate rows in A to rows in B.
Justin

Similar Messages

  • How to transfer database table contain null values, primary key, and foreign key to the another database in same server. using INSERT method.

    how to transfer database table contain null values, primary key, and foreign key to the another database in same server. using INSERT method.  thanks

    INSERT targetdb.dbo.tbl (col1, col2, col3, ...)
       SELECT col1, col2, col3, ...
       FROM   sourcedb.dbo.tbl
    Or what is your question really about? Since you talke about foreign keys etc, I suspect that you want to transfer the entire table definition, but you cannot do that with an INSERT statement.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Primary key and Foreign key on same column

    Hi, I am able to create below tables , primary and foreign keys as below.
    But Is this valid design? Can I define a column (ckey2 in table "child") as
    primary key and as well as foreign key?
    CREATE TABLE parent (
    col1 NUMBER,
    col2 NUMBER
    CREATE TABLE child (
    ckey1 NUMBER,
    ckey2 NUMBER,
    ckey3 NUMBER
    alter table parent add constraint parent_pk primary key( col1 );
    alter table child add constraint child_pk primary key( ckey2 );
    alter table child add constraint child_fk foreign key( ckey2 ) references parent( col1);
    Thanks.

    Can I define a column (ckey2 in table "child") as primary key and as well as foreign key?You mean you want to define a one-to-one relationship between parent and child tables.. why would you want to do that ? You might as well merge the 2 tables into one.

  • Mapping in OWB with primary key and foreign key relationship

    Hi all,
    I am new to this datawarehousing field. I have just started my career. I have to now create a mapping in owb where a table has a field which is a primary key of another table in the same staging area. If you guys could help me out with the a method it can be created that would be very helpful to me.
    I thought of 2 ideas,
    1. If I can use a look up, but then I am not sure if i can use a lookup for primary key, foreign key relationship. If I can use also, I do not know how to use that.
    2. What if I can directly take that the first table and link the primary key of that table to the second table which uses that primary key of the first table as one of its fields.
    I do not know how feasible these methods are. Please guys help me out.
    Thanks in advance.

    I have a similar case where table a and table b having relation but table a got inserted with data and table b is empty so there no values for foriegn key column in table b to realte with table a.
    Now i want to load table b foriegn key with primary key column values of table a.
    how can we do this in owb
    thanks
    kumar

  • Primary key and Foreign Key

    Hello,
             I have a question which was asked in an interview
    Can a Primary key in a table also act as a foreign key in the same table ?
    If yes than please provide a simple example
    Thanks
    www.techgulf.blogspot.com

    See this illustration
    create table t
    id int identity(1,1) not null primary key,
    val varchar(100)
    create table t2
    id int not null primary key,
    val varchar(100)
    -- make pk itself as fk to table t
    alter table t2 add constraint fk_t2 foreign key (id) references t(id)
    --some test values to t
    insert t (val)
    values ('test'),('test1'),('test2')
    -- populate t2 first two suceeds
    insert t2(id,val)
    values (1,'iuyi')
    insert t2(id,val)
    values (2,'ef24r')
    -- this will fail as we dont have record with id 4 in t
    insert t2(id,val)
    values (4,'weqw')
    --check the output
    select * from t
    select * from t2
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • Primary keys and foreign keys

    Hi,
    is there a script available for changing the value of a primary key which automaticly
    detect all foreigs keys refering to this primary key and change them as well?
    William Oorschot

    I don't have the script now. but you can make a procedure of ur own.
    using a join on user_constraints, and user_cons_columns you can find out all the columns in all the tables which are referencing the particular primary key. once you have the table and the referencing columns, you can change the value of those columns with the new value.
    you'll have to call this procedure from a before row level trigger.
    HTH
    Naveen

  • Lack of Primary and Foreign Keys and Efficiency

    I am reverse engineering a Maximo database. I am shock that it does not have Primary or foreign keys define for 590 out of 620 tables.
    My question is does the lack of Primary and Foreign key relationships adversely impact the efficiency of an Oracle database? I think it does IBM techs do not think it does.
    Anyone has the answer?

    does the lack of Primary and Foreign key relationships adversely impact the efficiency of an Oracle database? Not necessarily. There are many large OLTP databases that do not define PKs and FKs.
    It is true that the presence of constraint defintions is used by the optimizer. However,
    what matters is how the application is written and how the optimizer handles it.
    Remember that Oracle Databases have been in production since before constraint definitions were introduced.
    Hemant K Chitale
    Edited by: Hemant K Chitale on Jun 9, 2010 10:05 AM

  • Lost about 60-75% of my primary and foreign keys when Migrating from SQL Server 7

    Hi All,
    I did an almost successful migration between SQL Server 7.0 and Oracle9i.
    What happened:
    I seem to have lost my primary and foreign keys in the migration. The funny thing is when I viewed the constraints and indexes in Toad, they were there. However when I tried to enabled those constraints nothing happened.
    I really don't know how I can fixed this, but I don't understand why I didn't get a warning or an error message in the Migration Workbench (latest version).
    I did ignore the following errors, could that have anything to do with the keys being lost.
    Failed to create User:omwb_emulation; ORA-1920: user name "OMWB_EMULATION" conflicts with another user or role name
    Failed to create User:guest;ORA-01920: user name "GUEST" conflicts with another user or role name
    These users do not own any objects therefore I ignored them.
    Please advise
    Thanks

    Hi Joe,
    Not sure why you are able to see these objects and yet not able to enable them. Did you encounter any errors during the create Oracle Model phase?? Did you encounter any errors during the final Migrate phase?
    Also, it is really important to have a tablespace created before an attempt is made to create tables and indexes in the destination Oracle database.
    Regards
    John

  • Primary and Foreign Keys

    I've attempted to identify a primary and foreign key in these two tables, but I am getting a bunch of errors re duplicate keys and column names needing to be unique. Can someone explain to me what I am doing wrong? I have watched numerous tutorials. Perhaps
    the primary and foreign key I have identified don't meet the criteria?
    CREATE TABLE StockNames
    -- Added Primary key to [stock_symbol]
    [stock_symbol] VARCHAR(5) NOT NULL CONSTRAINT PK_stock_symbol PRIMARY KEY,
    [stock_name] VARCHAR(150) NOT NULL,
    [stock_exchange] VARCHAR(50) NOT NULL,
    GO
    stock_symbol stock_name stock_exchange
    AEA American External Accessories NYSE
    ASP All Sports Portal NYSE
    AIT Anthony Interest Technology NYSE
    ATV American Technology Vehicles NYSE
    ACM Anderson Charles and Mitchell NYSE
    ACH American Career Helpers NYSE
    AVA Alleviate America NYSE
    and the second table
    CREATE TABLE DailyPricesA (
    [stock_symbol] VARCHAR (5) NOT NULL CONSTRAINT FK_stocksymbol REFERENCES [StockNames]([stock_symbol]),
    [date] VARCHAR (15) NOT NULL,
    [stock_price_open] FLOAT (53) NOT NULL,
    [stock_price_high] FLOAT (53) NOT NULL,
    [stock_price_low] FLOAT (53) NOT NULL,
    [stock_price_close] FLOAT (53) NOT NULL,
    [stock_volume] INT NOT NULL,
    [stock_price_adj_close] FLOAT (53) NOT NULL,
    stock_exchange stock_symbol date stock_price_open stock_price_high stock_price_low stock_price_close stock_volume stock_price_adj_close
    NYSE AEA 8/02/2010 4.42 4.42 4.21 4.24 205500 4.24
    NYSE AEA 5/02/2010 4.42 4.54 4.22 4.41 194300 4.41
    NYSE AEA 4/02/2010 4.55 4.69 4.39 4.42 233800 4.42

    Hi Stan,
    I have removed "stock_exchange" - that removed that error. These are the other errors I am experiencing:Msg
    2714, Level 16, State 6, Line 8
    There is already an object named 'DailyPricesA' in the database.
    Msg 2714, Level 16, State 6, Line 36
    There is already an object named 'StockNames' in the database.
    Msg 2627, Level 14, State 1, Line 47
    Violation of PRIMARY KEY constraint 'PK__StockNam__117C59C768B2BB7F'. Cannot insert duplicate key in object 'dbo.StockNames'. The duplicate key value is (AA).
    The statement has been terminated.
    Msg 1913, Level 16, State 1, Line 54
    The operation failed because an index or statistics with name 'SNIndex' already exists on table 'DailyPricesA'.
    And the code:
    USE NYSEDatabaseProject
    -- Drop the table to clean up database.
    --DROP TABLE DailyPricesA
    --GO
    -- Create the table.
    CREATE TABLE DailyPricesA (
    [stock_symbol] VARCHAR (5) NOT NULL CONSTRAINT FK_stocksymbol REFERENCES [StockNames]([stock_symbol]),
    [date] VARCHAR (15) NOT NULL,
    [stock_price_open] FLOAT (53) NOT NULL,
    [stock_price_high] FLOAT (53) NOT NULL,
    [stock_price_low] FLOAT (53) NOT NULL,
    [stock_price_close] FLOAT (53) NOT NULL,
    [stock_volume] INT NOT NULL,
    [stock_price_adj_close] FLOAT (53) NOT NULL,
    -- Add a foreign key.
    -- [stock_symbol] VARCHAR (5) NOT NULL CONSTRAINT FK_stocksymbol REFERENCES [StockNames]([stock_symbol]),
    BULK INSERT DailyPricesA
    from 'C:\Users\fenwky\NYSE_daily_prices_A.csv'
    -- firstrow = 2 skips the header row, field terminator =',' deterines the separator, and rowterminator defines where the row ends
    with (firstrow = 2, fieldterminator = ',', rowterminator = '\n')
    GO
    -- Create an index named DPAIndex, for the columns stock_symbol//'DPAIndex' already exists on table 'DailyPricesA'
    --CREATE INDEX DPAIndex
    --ON DailyPricesA (stock_symbol);
    -- Drop the table to clean up database.
    -- DROP TABLE StockNames
    -- GO
    CREATE TABLE StockNames
    -- Added Primary key to [stock_symbol]
    [stock_symbol] VARCHAR(5) NOT NULL CONSTRAINT PK_stock_symbol PRIMARY KEY,
    [stock_name] VARCHAR(150) NOT NULL,
    [stock_exchange] VARCHAR(50) NOT NULL,
    GO
    -- Insert the CSV into the table named Stocknames
    BULK INSERT StockNames
    from 'C:\Users\fenwky\NYSE_stock_names.csv'
    -- firstrow = 2 skips the header row, field terminator =',' deterines the separator, and rowterminator defines where the row ends
    with (firstrow = 2, fieldterminator = ',', rowterminator = '\n')
    GO
    -- Create an index named SNIndex, for the columns stock_symbol and stock_exchange.
    CREATE INDEX SNIndex
    ON DailyPricesA (stock_symbol);
    -- Select all from DailyPricesA
    select * from StockNames

  • Difference between PRIMARY KEY and UNIQUE KEY with NOT NULL

    What is the difference between PRIMARY KEY and UNIQUE KEY with NOT NULL constraint?
    Message was edited by:
    Nilesh Hole

    Answer for the master!!!
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:8743855576462
    Thanks,
    Karthick

  • Diff b/w primary key and unique key?

    what is the diff b/w primary key and unique key?

    Hi,
    With respect to functionality both are same.
    But in ABAP we only have Primary key for the Database tables declared in the Data Dictionary.
    Unique is generally is the term used with declaring key's for internal tables.
    Both primary and Unique keys can identify one record of a table.
    Regards,
    Sesh

  • About Surrogate Key and Dimension Key on OWB 10.2

    Hi, everyone.
    I am using OWB 10.2 and I have a question about Surrogate key and Dimension Key.
    I indicated the foreign key as VARCHAR2 type in Fact Table and Dimension Key as VARCHAR2 type is operated as Primary key in Dimension Table. I made Single Level in Dimension Table.
    I know that Dimension Key stores the surrogate ID for dimension and is the primary key of the table. Also, Surrogate ID should be only NUMBER type.
    So, in this case, Surrogate ID is NUMBER type
    Dimension key should be NUMBER type to store the surrogate ID.
    But, Dimension key also should operate the primary to relate Foreign key as VARCHAR2 type.
    How I can solve this confusing condition?
    Please let me know that.
    JWS

    Hi JWS,
    From a SQL point of view it should not be a problem to join a NUMBER field to a VARCHAR2 field because during execution there will be an implicite cast for the NUMBER value to a VARCHAR2 value. See the example below.
       SELECT * FROM DUAL
       WHERE   1 = '1'From an OWB point of view it is not possible to have a Dimension with an NUMBER value Key that has a relation to a VARCHAR2 value Foreign key in a Fact table. This is caused due to the creation of a Fact table in OWB in which the Foreign keys in it are build from de Dimension tables that refer to them.
    You will loose the reference to the Dimension when changing the type of the Foreign Key.
    To resolve this issue I would advise you to use a Sequence that generates your Surrogate Key (NUMBER type) for the Dimension table and store it in the Primary Key Column (VARCHAR2 type).
    When validating the mapping you will get a warning, but when executing this should give no problems.
    Regards,
    Ilona

  • Arrow keys and enter key not functioning in Firefox 3.6 when viewing PDF

    I'm using Adobe Reader 9.3.0.
    I have a web application that displays PDFs (using Adobe Reader) in a browser.  The PDF is loaded onto the page inside an IFRAME.  In addition to the IFRAME containing a PDF, the page also includes several forms, text fields, etc. For the most part this is working fine.  However, I have a glitch when using this in Firefox 3.6. I'm finding that certain keyboard events are being lost and I strongly suspect that they are being swallowed by Adobe Reader.  When I place focus on an HTML text field and press enter key or arrow keys, I do not get the appropriate key events.  If I run the exact same test without the PDF IFRAME these key events work correctly, but as soon as I load the PDF into the IFRAME they do not.  I believe the primary key event that gets lost is the keydown event.  I do get the keyup event.
    Again, the events work correctly up until I load the PDF onto the page.
    I've verified that this error does not occur in Firefox 3.5 and 3.0.
    Here is a testcase to reproduce this:
    <html>
    <head>
        <title>JavaScript - Detecting keystrokes</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <script type="text/javascript">
            var pdfURL = 'http://samplepdf.com/sample.pdf';
            window.onload = function () {
                init();
            document.defaultAction = true;
            function init() {
                var x = document.getElementById('testForm').getElementsByTagName('input');
                for (var i=0;i<x.length;i++) {
                    x[i].onclick = setEvents;
                    if (x[i].checked)
                        x[i].onclick();
                writeroot = document.getElementById('writeroot');
                document.getElementById('emptyWriteroot').onclick = function () {
                    writeroot.innerHTML = '';
                    return false;
            function setEvents() {
                if (this.id == 'default') {
                    document.defaultAction = !this.checked;
                    return;
                var eventHandler = (this.checked) ? detectEvent : empty;
                document.getElementById('textfield')['on'+this.id] = eventHandler;
                //document['on'+this.id] = eventHandler;
            function detectEvent(e) {
                var evt = e || window.event;
                writeData('<b>'+evt.type+'</b>:  keyCode=' + evt.keyCode+' : charCode=' + evt.charCode);
                return document.defaultAction;
            function empty() {
                // nothing
            var writeroot;
            function writeData(msg) {
                writeroot.innerHTML += msg + '<br />';
        </script>
        <style type="text/css">
            body {
                font-size: 12px;
            #writeroot {
                height: 300px;
                overflow: auto;
                border: 1px solid #2EB2DC;
        </style>
    </head>
    <body>
    <form id="testForm">
    <input type="checkbox" id="keydown" /> <label for="keydown">keydown</label><br />
    <input type="checkbox" id="keypress" /> <label for="keypress">keypress</label><br />
    <input type="checkbox" id="keyup" /> <label for="keyup">keyup</label><br />
    <input type="checkbox" id="default" /> <label for="default">Suppress default action</label><br />
    <input type="text" id="textfield" /><br />
    <button id="emptyWriteroot">Remove messages</button>
    </form>
    <a href="#" onclick="document.getElementById('docviewer-page').src=pdfURL;return false">Now load IFrame</a>
    <p id="writeroot"></p>
    <div id="container2" style="border:1px solid black;width:400px;height:200px">
            <iframe
                id='docviewer-page'
                name='docviewer-page'
                title='document page'
                src='http://www.google.com'
                align='left'
                width='100%'
                height='100%'
                scrolling='auto'
                frameborder=0></iframe>
    </div>
    </body>
    </html>
    Paste the above into an HTML document.  Open the HTML document in Firefox 3.6.  Check the four checkboxes.  Move into the text field and press arrow keys and enter key to observe the results when it is working correctly.  Then click on the "Now Load PDF" link.  Press the arrow keys and enter key again and observe that the behavior has changed.
    Anybody have any idea why this is happening or how to work around it?

    I found a workaround!
    By using an object tag instead of an iframe, all the keys seems to work fine and the PDF still renders in the page the same way.
    Here's a page that works:
    <html>
    <head>
        <title>JavaScript - Detecting keystrokes</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <script type="text/javascript">
            var pdfURL = 'http://samplepdf.com/sample.pdf';
            window.onload = function () {
                init();
            document.defaultAction = true;
            function init() {
                var x = document.getElementById('testForm').getElementsByTagName('input');
                for (var i=0;i<x.length;i++) {
                    x[i].onclick = setEvents;
                    if (x[i].checked)
                        x[i].onclick();
                writeroot = document.getElementById('writeroot');
                document.getElementById('emptyWriteroot').onclick = function () {
                    writeroot.innerHTML = '';
                    return false;
            function setEvents() {
                if (this.id == 'default') {
                    document.defaultAction = !this.checked;
                    return;
                var eventHandler = (this.checked) ? detectEvent : empty;
                document.getElementById('textfield')['on'+this.id] = eventHandler;
                //document['on'+this.id] = eventHandler;
            function detectEvent(e) {
                var evt = e || window.event;
                writeData('<b>'+evt.type+'</b>:  keyCode=' + evt.keyCode+' : charCode=' + evt.charCode);
                return document.defaultAction;
            function empty() {
                // nothing
            var writeroot;
            function writeData(msg) {
                writeroot.innerHTML += msg + '<br />';
        </script>
        <style type="text/css">
            body {
                font-size: 12px;
            #writeroot {
                height: 300px;
                overflow: auto;
                border: 1px solid #2EB2DC;
        </style>
    </head>
    <body>
    <form id="testForm">
    <input type="checkbox" id="keydown" /> <label for="keydown">keydown</label><br />
    <input type="checkbox" id="keypress" /> <label for="keypress">keypress</label><br />
    <input type="checkbox" id="keyup" /> <label for="keyup">keyup</label><br />
    <input type="checkbox" id="default" /> <label for="default">Suppress default action</label><br />
    <input type="text" id="textfield" /><br />
    <button id="emptyWriteroot">Remove messages</button>
    </form>
    <a href="#" onclick="document.getElementById('docviewer-page').data=pdfURL;return false">Now load IFrame</a>
    <p id="writeroot"></p>
    <div id="container2" style="border:1px solid black;width:400px;height:200px">
            <object
                id='docviewer-page'
                name='docviewer-page'
                title='document page'
                data='http://www.google.com'
                align='left'
                width='100%'
                height='100%'
                ></object>
    </div>
    </body>
    </html>

  • Arrow Left key and arrow Up key and fn key not working properly !? Lenovo G580 PLEASE HELP x_x !

    Hi guys i have a Lenovo G580 im not sure which version but i can tell you its intel i.3  and its got usb3.0 on it and 500gb hdd. 2.20Ghz  4.00RAM.   
    been days that i try to solve my Fn Key problem and also the arrow left and arrow up keys < i was using Win8 and downgraded to Win7 last week and that problem has occured since ,it misbehaves and it only works for a short amount of time it randomly works those keys FN.arrow up/left keys randomly they work , i upgraded again to Win8.1 now for the first few boots evrything worked well fn key and arrow keys np.< and now again it stops working.. and btw. whenever i press hard against it it would work pressing down the fn and arrow left/up keys when i press really hard it works but then again it doesnt Soo end of the story its not functioning properly and i have Energy management and Display driver HD 3000 installed and i still got this problem .. is there any fix to this anyone please !? 

    Hey mate the keys are faulty nothing can fix it except for buying a new Replacement! , i fixed the problem simply by Replacing the KEYBOARD !  i went downtown and looked for a g580 keyboard and they didnt exactly have the same version g580 they had c or b580 i dont remmber what letter it was sorry ! but the thing is the whole keyboard layout of many Lenovo Laptops are the same just look one thats the same, cause mine wasnt for G580 but the thing is its still working perfectly ! only problem is the Screws didnt fit i had to cut out the sides of the screw hole thingies to make the keyboard fit in well in my G580 , Simply put all you need is a new keyboard replacement buy one they cost between 20-40$ worth it cause you wont have any faulty keys anymore evrything will work fine. no need to reinstall anything just replace it with new Keyboard. and Plus its easy to replace it by yourself. i replaced mine by myself easily. Problem fixed.

  • HP G62-348CA number keys and arrow keys not working

    Good day Experts! Can someone help me fix my laptop? I got G62-348CA unit. only number 5 and 6 in my number keys are working and also arrow keys down and right are not functioning. at first it was built with windows 7 home premium 64bit but then i have to reformat my laptop with windows 7 ultimate but i dont have available 64bit so i install 32bit of the OS. before i install the OS, number keys and arrow keys are working fine but after I reformat my laptop with ultimate 32bit, the said keys are not working. please guys help me. im having trouble with my unit and i dont know what to do. its pretty bad i dont have any idea. all the best with you guys and i'm hoping for your fast and kind response. thank you!

    Because the operating system does not affect the keyboard in that way . .. If the operating system has issues it would switch keys or assign wrong characters.
    I have replaced over 200 keyboards because of such issues

Maybe you are looking for

  • Error Message - Couldn't complete your request because there is not enough memory

    I'm using Adobe CS5 Extended on a PC running under Windows Vista Home Premium with 4GB RAM.  When I complete editing a RAW file in ACR and choose to Open Image into Photoshop, I often receive an error message "Could not complete your request because

  • Connecting new iPod Classic to a TV

    Hi! I am hoping that someone can help me. I am somewhat new to this whole mp3 thing. I did get my 9yr old daughter an iPod nano for Christmas and have been helping her set hers up, but that's pretty much the extent of my experience. So, please forgiv

  • SVG Viewer not working

    I installed Oracle 10g (10.1.0.2.0) and the HTML Server and HTML DB from the companion CD. I then started looking at some of the examples and even create a quick application myself based on a single table. Any time I wanted to look at something that

  • Safari Downloads - How do i not save by default?

    When I am using Safari as an application (webmail or other) and I select a file or file i want to view, it downloads the file into the doanloads directory before opening. After I view the file, and close it, a copy of that is permanmetly contained in

  • How to locate photos in finder through the Photos for Mac app?

    Hi, is there an option to locate where the photos are saved in finder through the Photos app. This feature was available in iPhoto but I am unable to locate it in Photos.