Solved

relational table

Posted on 2009-04-13
7
517 Views
Last Modified: 2013-12-07
hello...
can anyone please help me with that problem,,
i have created two table on Oracle 10g, one called CAR and other called  CUSTOMER,
as the CAR, i have CAR_REG as PK, and as the CUSTOMER i have CUST_NUM as PK. now i want to create a relationship  between them a relational table called HIRE. in HIRE table i want to have CAR_REG as FK, and CUST_NUM as FK, and DATE_HIRED  

i was ok creating the first two main tables for CAR and CUSTOMER, but i am having problem with the commands for the relational table (HIRE) i have used the command to create that, but i had error,
i will be more than thankful with any help,,, thank you
ALTER TABLE CAR

ADD (FOREIGN KEY (CAR_REG) REFERENCES facuty (CUST_NUM));

Open in new window

0
Comment
Question by:ferman-k
  • 4
  • 3
7 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24133524
Your code snippet has a table named "facuty", what is that? You don't mention it in your question.

Also, it is more common to create the foreign key during the "CREATE TABLE" statement. But since you are trying to add it afterwards, you don't add it to the parent table  (CAR or CUSTOMER), you have to add it to the child table (HIRE). Also, the columns must exist in the child table (car_reg and cust_num)


ALTER TABLE hire

  ADD CONSTRAINT fk_car_reg

     FOREIGN KEY(car_reg)

     REFERENCES car(car_reg)

;
 

ALTER TABLE hire

  ADD CONSTRAINT fk_cust_num

     FOREIGN KEY(cust_num)

     REFERENCES customer(cust_num)

;

Open in new window

0
 

Author Comment

by:ferman-k
ID: 24172062
I tried your commands exactly the same but it was not recognisable by my system... don't you think it its because that i am using "iSQL* pluse"...?
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24172106
Please post the error messages.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:ferman-k
ID: 24172648
here is the message i get when i try the commands;



 FOREIGN KEY(car_reg)
                 *

ERROR at line 3:
ORA-00904: "CAR_REG": invalid identifier

     FOREIGN KEY(cust_num)
                 *

ERROR at line 3:
ORA-00904: "CUST_NUM": invalid identifier


0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24172736
It looks like you don't have columns CAR_REG and CUST_NUM in the appropriate tables. In order to create the foreign key, the column has to exist.

0
 

Author Comment

by:ferman-k
ID: 24172883
sorry,
yes i have, they both  are pk in their original table
0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24172932
Yes, but that is not the foreign key. You need to create identical type columns in the HIRE table before running the commands above.

Can you describe your tables and paste them here?

You did not say what datatypes car_reg and cust_num are. So change them appropriately below or paste them in and I will verify.

Also, it probably makes sense to index these columns, but depends on how you will join them. I would create individual indexes based on the fact that it is a linking table.

create table hire

(

  car_reg varchar2(20) not null, -- change varchar2(20) to actual data type

  cust_num integer not null, -- change integer to actual data type

  date_hired date

);
 

create index ix_hire_car_reg on hire(car_reg);

create index ix_hire_cust_num on hire(cust_num);
 

ALTER TABLE hire

  ADD CONSTRAINT fk_car_reg

     FOREIGN KEY(car_reg)

     REFERENCES car(car_reg)

;

 

ALTER TABLE hire

  ADD CONSTRAINT fk_cust_num

     FOREIGN KEY(cust_num)

     REFERENCES customer(cust_num)

;

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle Database Upgrade 13 62
alter database link to change the password 2 52
Action link in Union Reports Not Working in OBIEE 11g 1 56
Schema creation in Oracle12c 6 39
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now