best way to joing two tables

Hi all

I have two tables I want to join but I don't know how to do it? I tried to use the primary key and foreign key, but after the use of primary and foreign keys, I am not able to insert records help out me thanks in advance



Sarah

Published by: SarahSarahSarah on August 31, 2009 12:08

Here is the code example:

SQL> create table emp(
  2  empid int,
  3  name varchar(20),
  4  deptid int);

Table created.

SQL> create table dept(
  2  deptid int,
  3  name varchar(20));

Table created.

SQL> alter table dept add constraint dept_pk primary key(deptid)
  2  /

Table altered.

SQL> alter table emp  add constraint emp_pk primary key(empid)
  2  /

Table altered.

SQL> alter table emp add constraint emp_dept_fk
  2  foreign key (deptid) references dept(deptid)
  3  /

Table altered.

SQL> insert into emp(empid,name, deptid) values(1,'Krystian',null)
  2  /

1 row created.

Insert succeeded because deptid in the emp table can accept null values

SQL> insert into emp(empid,name, deptid) values(1,'Krystian',1)
  2  /
insert into emp(empid,name, deptid) values(2,'Krystian',1)
*
ERROR at line 1:
ORA-02291: integrity constraint (A.EMP_DEPT_FK) violated - parent key not found

Insert was not successful, because I have not inserted dept with deptid
1 in a table dept and I have defined foreign key relation ship
between emp and dept

SQL> insert into dept(deptid, name) values(1,'IT')
  2  /

1 row created.

SQL> insert into emp(empid,name, deptid) values(2,'Krystian',1)
  2  /

1 row created.

After inserting the Department with deptid = 1 in dept table insert works.

With greetings
Krystian Zieja
[Project Envision | http://www.projectenvision.com]

Tags: Database

Similar Questions

  • What is the best way to refresh the table after autosubmit (10.1.3.4)

    What is the best way to refresh the table after autosubmit?

    I have a page that contains a table where if one of the fields is changed it autosubmitted where the view object changes some attributes, based on the field having been changed. I need these modified attributes that appear in the table. But without doing anything, the only way to see these values is to cause the iterator updated table.

    I've been refreshing the table is having a method in a grain of beacking called "getSystemSettingIter.getCurrentRow ();". This seems to be a bit of a hack for me and I was wondering if there is a better way to get the table to update.

    Thanks in advance!

    Have you tried setting between the two partial page refresh?
    http://www.Oracle.com/pls/as111120/lookup?ID=ADFUI385

    http://download.Oracle.com/docs/CD/E15523_01/Web.1111/b31974/web_form.htm#CACEIEEI

  • AS3 best way to clear a table

    What is the best way to clear a table? (performance and resources)

    Array.Length = 0;

    array = [];

    array.splice(0);


    Array.Length = 0;

  • What is the best way to run two concurrent programs using the output of either executing the other?

    I wanted to just kind of get a notice of some people here. I have two main screws the first is a data acquisition system that displays 99 different sensors and which feeds on these data in a table. The second is a real-time processing system that is designed to take the painting and do many calculations. The two output data from screw at the front that I want to see, so I don't want to use just as a Subvi.

    Is there a simple way to run two screws, using the output of the data acquisition system to power the processor so that you can easily see the signs before two screws? I had thought to simply copy the code from the processor in real-time in the DAQ VI, but I fear that the VI resulting will be very large and unsightly.

    On a side note, I'm more thinking about this? I apologize if this is a stupid question, it has been a long week... aaaand it's Wednesday.

    Look at the Architecture of producer/consumer.  It will be a good starting point, even if you find that you need to change.  When performing data acquisition and processing of tasks in parallel loops, they can both run simultaneously.  Both can feed data to the main draw of the VI.

    I'd probably use three loops with the third being the user interface only.  All that must be displayed to the user is sent to the GUI of the acquisition of data or processing loop loops and commands or entered by users are returned in these loops, probably through queues.  The wall of the VI of data acquisition and processing VI are never displayed to the user.  Especially the treatment, because it runs on a system in real-time, should avoid synchronization issues that can present a graphical interface.

    Lynn

  • best way to connect two PC 2748 s?

    I just upgraded our office in two PC 2748 s (while still also using one of our PC 2324 devices) and I would like to know the best way to connect these three switches.

    At the present time, I have a cord Ethernet connecting each of the 2748 s via the 49 port and then another cord ethernet connecting the 2324 to the 50 of my first 2748 port (which has our main file server and the outgoing Internet connection on it, among other things).

    Should I also connect the 2324 to SECOND 2748 or is asking for disaster? Also, is there an advantage to invest in GBIC modules for the two 2748 s so that I can connect this way or this advantage of flow occurs only when connecting a 2748 to another device (i.e. There is no speed advantage when only connecting two 2748 s together)?

    Thanks in advance for your clarification...


  • The best way to manage two GUI

    I have two classes using java swings. The first Panel is where all the calculations will be made and the result will appear in a JList (in the 1st Panel). After that, the next button will show a 2nd Panel where the JList of the 1st group results are transferred. I need a back button in the 2nd Panel to return to the Panel 1. After you click the back button, I need the previous results on the 1st Panel to always be there for extra change (add / change / delete data). If I changed something and click the next button again, I want the 2nd Panel show a set results to date from the 1st Panel.

    What is the best way to do it? I spent two days trying to apply this using the presentation of the card, but it does not work as I want. All the examples I found use only one category. Even if I break down for separate classes, I face problem where when I click the back button, all the data in the 1st Panel disappeared. I'm out of my mind right now. It would be great if anyone can share some ideas in this. Any suggestions are welcome.

    Yes. I already tried several times to break it down into categories before posting here again. I am new to Java, that's why I found quite difficult and decided to post my problem here after he tried for two days. In any case, I thought about it already. I share the codes so that anyone who has problems like me can be useful if not much.

    Presentation of the main card:

    import java.awt.CardLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class CardCard {
      private static final String FIRST_PANEL = "firstPanel";
      public static JPanel mainPanel;
      private JFrame frame;
      public static CardPanel_1 cp1 = null;
      public static final CardLayout cardLayout = new CardLayout();
    
      public CardCard (){
      frame = new JFrame("Test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationByPlatform(true);
      mainPanel = new JPanel(cardLayout);
      cp1 = new CardPanel_1();
      mainPanel.add(cp1, FIRST_PANEL);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setVisible(true);
      }
    
      public static void main(String... args) {
      SwingUtilities.invokeLater(new Runnable()
      {
      public void run()
      {
      new CardCard();
      }
      });
      }
    
    }
    

    First card:

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Observable;
    import java.util.Random;
    
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class CardPanel_1 extends JPanel {
      public static JTextField textField, textField3;
      private static final String SECOND_PANEL = "secondPanel";
      private CardPanel_2 observer;
      private JButton btnNext, btnRandom;
      private String show;
      public CardPanel_2 cp2 = null;
      private List numList;
      private String numResult;
    
      /**
      * Create the panel.
      */
      public CardPanel_1() {
      setLayout(new FlowLayout());
    
      textField = new JTextField();
      textField.setBounds(64, 5, 134, 28);
      add(textField);
      textField.setColumns(10);
    
      textField3 = new JTextField();
      textField3.setBounds(64, 5, 134, 28);
      add(textField3);
      textField3.setColumns(10);
    
      btnRandom = new JButton("Randomise");
      btnRandom.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      String str = textField.getText();
      int num = Integer.parseInt(str);
      numList = new ArrayList();
      for (int i =0;i
    

    2nd map:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Observable;
    import java.util.Observer;
    
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class CardPanel_2 extends JPanel implements Observer {
     private static JTextField textField2;
      private JButton btnBack;
     private static final String FIRST_PANEL = "firstPanel";
    
     /**
      * Create the panel.
      */
      public CardPanel_2() {
      System.out.println("PANEL 2");
      textField2 = new JTextField();
      add(textField2);
      textField2.setColumns(10);
      btnBack = new JButton("Back");
      btnBack.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
      CardCard.cardLayout.show(CardCard.mainPanel, FIRST_PANEL);
      System.out.println("BACK");
      }
      });
      add(btnBack);
      }
    
      public void update(Observable o, Object arg) {
      System.out.println("Updated: " + CardCard.cp1.textField3.getText());
      textField2.setText(CardCard.cp1.textField3.getText());
      }
    }
    
  • Best way to resolve this table?

    Lets say you're dealing with these two tables:

    CREATE TABLE VEHICLES
    (
    NUMBER OF VEHICLE_ID
    VEHICLE_NAME VARCHAR2 (100 BYTE),
    NUMBER OF MILES
    );


    CREATE TABLE VEHICLE_PARTS
    (
    NUMBER OF PART_ID,
    VEHICLE_ID NUMBER OF NON-NULL,
    PART_TYPE NUMBER OF NON-NULL,
    PART_DESCRIPTION VARCHAR2 (1000 BYTE) NOT NULL,
    START_SERVICE_DATE DATE NOT NULL,
    DATE OF END_SERVICE_DATE,
    PART_TYPE_NAME VARCHAR2 (100 BYTE)
    );

    And some data for example as follows:

    Insert into VEHICLES (VEHICLE_ID, VEHICLE_NAME, MILES) Values (1, "Honda Civic", 75500);
    Insert into VEHICLES (VEHICLE_ID, VEHICLE_NAME, MILES) Values (2, 'Ford Taurus', 156000);

    Insert into VEHICLE_PARTS
    (PART_ID, VEHICLE_ID, PART_TYPE, PART_DESCRIPTION, START_SERVICE_DATE, END_SERVICE_DATE, PART_TYPE_NAME)
    Values
    (1, 1, 1, "1.4 VTEC",)
    TO_DATE('07/07/2009_00:00:00',_'MM/DD/YYYY_HH24:MI:SS'), TO_DATE (3 MAY 2010 00:00:00 "," MM/DD/YYYY HH24:MI:SS'), "ENGINE");
    Insert into VEHICLE_PARTS
    (PART_ID, VEHICLE_ID, PART_TYPE, PART_DESCRIPTION, START_SERVICE_DATE, PART_TYPE_NAME)
    Values
    (2, 1, 1, "1.6 VTEC",)
    TO_DATE('05/03/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'ENGINE');
    Insert into VEHICLE_PARTS
    (PART_ID, VEHICLE_ID, PART_TYPE, PART_DESCRIPTION, START_SERVICE_DATE, END_SERVICE_DATE, PART_TYPE_NAME)
    Values
    (3, 1, 2, 'Happy new year all seasons',)
    TO_DATE('07/07/2009_00:00:00',_'MM/DD/YYYY_HH24:MI:SS'), TO_DATE (10 AUGUST 2010 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'), 'TYRES');
    Insert into VEHICLE_PARTS
    (PART_ID, VEHICLE_ID, PART_TYPE, PART_DESCRIPTION, START_SERVICE_DATE, PART_TYPE_NAME)
    Values
    (4, 1, 2, 'Bridgestone Blizzaks',)
    TO_DATE('08/10/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), "TIRES");
    Insert into VEHICLE_PARTS
    (PART_ID, VEHICLE_ID, PART_TYPE, PART_DESCRIPTION, START_SERVICE_DATE, PART_TYPE_NAME)
    Values
    (5, 2, 1, "3.5 L Duratec",)
    TO_DATE('06/01/2008 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'ENGINE');
    Insert into VEHICLE_PARTS
    (PART_ID, VEHICLE_ID, PART_TYPE, PART_DESCRIPTION, START_SERVICE_DATE, END_SERVICE_DATE, PART_TYPE_NAME)
    Values
    (6, 2, 2, 'Happy new year all seasons',)
    TO_DATE('06/01/2008_00:00:00',_'MM/DD/YYYY_HH24:MI:SS'), TO_DATE (15 MARCH 2009 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'), 'TYRES');
    Insert into VEHICLE_PARTS
    (PART_ID, VEHICLE_ID, PART_TYPE, PART_DESCRIPTION, START_SERVICE_DATE, END_SERVICE_DATE, PART_TYPE_NAME)
    Values
    (7, 2, 2, 'Michelin All-Seaon',)
    TO_DATE('03/15/2009_00:00:00',_'MM/DD/YYYY_HH24:MI:SS'), TO_DATE (12 JANUARY 2011 00:00:00 ',' DD/MM/YYYY HH24:MI:SS'), 'TYRES');
    Insert into VEHICLE_PARTS
    (PART_ID, VEHICLE_ID, PART_TYPE, PART_DESCRIPTION, START_SERVICE_DATE, PART_TYPE_NAME)
    Values
    (8, 2, 2, "Nokian")
    TO_DATE('01/12/2011 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), "TIRES");


    And you need produce a view that shows the flattened attached data where each vehicle has one row with columns representing their most recent part (which has a service as Start Date of null terminator).

    Like this:

    Car: Engine: tires:
    Honda Civic 1.6 VTEC Bridgestone Blizzaks
    Ford Taurus 3.5 L Duratec Nokian


    There he has a fast and efficient to do that?

    My current approach, which is the brute force method is to have a separate outer join for each column, I need to shoot with the condition of max (START_SERVICE_DATE) to get the current part for each type (engine, tires, etc...).

    but its so slow and painful code.

    I thought pivot but I don't think that Pivot help here since there is no aggregation going, right?

    Anything with the partition could over aid? IM unfamiliar with this syntax

    Hello

    Trant says:
    Lets say you're dealing with these two tables:

    CREATE TABLE VEHICLES
    (
    NUMBER OF VEHICLE_ID
    VEHICLE_NAME VARCHAR2 (100 BYTE),
    NUMBER OF MILES
    ); ...

    Thanks for posting the CREATE TABLE and INSERT statements; It's very useful!

    I thought pivot but I don't think that Pivot help here since there is no aggregation going, right?

    Pivot is always aggregation; you take any number of rows in the table and only one line display. It is the aggregation.

    Anything with the partition could over aid? IM unfamiliar with this syntax

    Yes: ROW_NUMBER analytic function can help to identify the most recent line for each vehicle_id and part_type_name in vehicle_parts.

    Here's a way to do it:

    WITH     got_r_num     AS
    (
         SELECT     vehicle_id
         ,     part_description
         ,     part_type_name
         ,     ROW_NUMBER () OVER ( PARTITION BY  vehicle_id
                             ,             part_type_name
                             ORDER BY        start_service_date
                           )     AS r_num
         FROM     vehicle_parts
         WHERE     end_service_date     IS NULL
    )
    SELECT       v.vehicle_name
    ,       MIN (CASE WHEN r.part_type_name = 'ENGINE' THEN r.part_description END)     AS engine
    ,       MIN (CASE WHEN r.part_type_name = 'TIRES'  THEN r.part_description END)     AS tires
    FROM            vehicles     v
    LEFT OUTER JOIN     got_r_num     r  ON     v.vehicle_id     = r.vehicle_id
    WHERE       r.r_num     = 1
    GROUP BY  v.vehicle_name
    ORDER BY  v.vehicle_name
    ;
    

    You must add another column in the main query for each part. It's just one line of code, not nearly as bad but to join another subquery.

    Whenever you have a problem, don't forget to tell what version of Oracle you are using.
    The above query will work in Oracle 9 (and), but if you have Oracle 11, you'll want to use SELECT... PIVOT.

  • What is the best way to get a Table name in Oracle Applications: 12.1.1 (web)

    Hello friends...


    I need your help my friends...


    We are currently working on Oracle Applications: 12.1.1

    I would like to know the best way to get the name of the Table to form based on a Web...


    Concerning

    Yas.

    Hello

    Please see this thread and documents referenced in it.

    RECORD HISTORY (or) COLUMNS in R12?
    RECORD HISTORY (or) COLUMNS in R12?

    Kind regards
    Hussein

  • best way to create a table based on another table

    Hello
    I am trying to create a table based on another table with all the data in it. It contains important data.

    create table < tablename > select * from table1.

    Is the best way to do it, or is there another way. Please advice.

    Thank you

    Insert / * + append * / in as select * from ;

    It should be->

    insert /*+ append */ into 
    select * from ;
    

    Kind regards.

    LOULOU.

  • Best way to update a table with separate values

    Hi, I would really appreciate some advise:

    I need to regularly perform a task where I update 1 table with all the new data that has been entered in another table. I cannot perform a complete insert because this will create data duplicated each time it works, so the only way I can think of is the use of cursors in the script below:


    CREATE OR REPLACE PROCEDURE update_new_mem IS
    tmpVar NUMBER;

    CURSOR c_mem IS
    SELECT nom_membre, member_id
    OF gym.members;
    CREC c_mem % ROWTYPE;


    BEGIN
    OPEN c_mem.
    LOOP
    SEEK c_mem INTO crec;
    EXIT WHEN c_mem % NOTFOUND;
    BEGIN
    UPDATE gym.lifts
    Name = crec.member_name
    WHERE member_id = crec.member_id;
    EXCEPTION
    WHEN NO_DATA_FOUND THEN NULL;
    END;
    IF SQL % NOTFOUND THEN
    BEGIN
    INSERT INTO gym.lifts
    (name, member_id)
    VALUES (crec.member_name, crec.member_id);
    END;
    END IF;
    END LOOP;
    CLOSE C_mem;

    END update_new_mem;



    This method works, but y at - it a (faster) easier way to update another table with new data only?

    Thank you very much

    >
    This method works, but y at - it a (faster) easier way to update another table with new data only?
    >
    Almost anything would be better than this treatment of slow-by-slow loop.

    You don't need a procedure, you should just use MERGE for this. See the examples in the section of the MERGER of the doc of the SQL language
    http://docs.Oracle.com/CD/B28359_01/server.111/b28286/statements_9016.htm

    MERGE INTO bonuses D
       USING (SELECT employee_id, salary, department_id FROM employees
       WHERE department_id = 80) S
       ON (D.employee_id = S.employee_id)
       WHEN MATCHED THEN UPDATE SET D.bonus = D.bonus + S.salary*.01
         DELETE WHERE (S.salary > 8000)
       WHEN NOT MATCHED THEN INSERT (D.employee_id, D.bonus)
         VALUES (S.employee_id, S.salary*.01)
         WHERE (S.salary <= 8000);
    
  • Best way to add two mic clips to a video clip

    I have two microphones tie run in two different channels (not the camera).  I want to add these two audio clips to video.  I took the two clips and clip video and synchronized together... that works although I... now no control over each audio clip is what it is.  Is there a better way to do it, or where I can have control over each audio clip in the timeline?

    Have you used the function of the extent of the audio components in the timeline?

  • BB10 best way to create two lists view

    Hello!

    I am doing an application with two lists, something like a file browser. A single list have records and the other have files.

    When I type on a folder on the first list, a second is updated with the files.

    The playbook, I show the two lists, a narrow with folders on the left and the one with the files on the right, but this type of display is not very usable on smartphone.

    What do you think would be the best solution in this kind of scenario?

    How would you present two lists discovered?

    Page 1A records (list). Select a folder and it slides open page with files (list) 2. An action on the bottom bar would have a back button to return to the folders. Additional action buttons allow the actions to perform on the selected file.

  • Best way to index a table for display

    I often do something like this for indicators on a Panel, as you can see the block diagram is messy and it takes a lot of space.

    There must be a better way.

    You can also use auto-indexation with a structure in case of having a cleaner schema.

  • What is the best way to have an indicator that scrolls the content of a table that is updated during the execution of a program?

    I have a program that runs continuously. During this time the errors are produced and I store them in a table. What is the best way to display the table in a window that the user can scroll upwards or downwards, while the program is running?

    Thank you.

    Hi chuck,

    If allowing you to feed two tables to the table you will see also all of these channels in the table

  • Best way to update the individual rows of a Table?

    I took a quick glance at a few examples, but did not get a clarification on this.  I am looking to have something close to a listbox control or the table to where I can update just one column of values to line a 1 time per second pace.  I'm looking to display our acquisition of data values in a table or a listbox control.  The single list box seemed to work well for that, but I couldn't use the row headers to list the names of channel beside the channel values.  I thought to link the values of cursor in two areas of list to do this, but did not find any info on it for the single list box.

    I have a few questions:

    (1) I have a 1 d table to where I want to use this data to constantly update the first column (with a multitude of lines) of a table.  I'm looking for the best route to not take too much time for treatment by doing this.

    What is the best way to update the individual rows of a table?   Invoke the node "Value of the cell value"... or is there another method?

    (2) why is that, after each iteration else, row values are deleted?

    Also, for adding additional channels originally arrray... it is better to use the 'Array' subset then the function "Construct the table" or function "Subset of the table" and "insert table"?

    See the attached example.

    Thank you.

    Jeff· Þ· Bohrer says:

    (2) why is that, after each iteration else, row values are deleted?

    Classic race condition.  dump the loop and node-p and just wire the 2D table on the terminal Board. !

    I don't see the race condition.  What I see is the table once the last element has been written for it all run the oil.  I saw looked it with point culminating performance on.

    But I agree entirely with writing to the Terminal.  It is a 1 d array, so you will need to use an array of generation and convert a 2D array in order so that he could write correctly.

Maybe you are looking for