niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Databases » Oracle
Creating a relationship between 2 tables
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
Andyza
*nix forums beginner


Joined: 17 Feb 2005
Posts: 5

PostPosted: Thu Jul 20, 2006 1:11 pm    Post subject: Creating a relationship between 2 tables Reply with quote

I'm running Oracle 9i on Windows XP with TOAD as a "front-end" to
Oracle.

Sample database structure:

STUDENTS {
SYSTEMID (PK),
STUDENTID,
NAME,
SURNAME,
IDNUMBER
}

COURSESDONE {
COURSEID (PK),
COURSENAME,
RESULT,
STUDENTID
}

Currently the tables aren't related as no relationships between the two
tables have been defined. I'd like to add the SYSTEMID column to the
COURSESDONE table and add a foreign key relationship between the two
tables, i.e:

ALTER TABLE COURSESDONE ADD (
"SYSTEMID" VARCHAR2(50),
CONSTRAINT "COURSESDONE_SYSTEMID_FK" FOREIGN KEY("SYSTEMID")
REFERENCES STUDENTS("SYSTEMID")
)

That's fine... The tricky part is that I also want to copy the existing
data from the SYSTEMID column on the STUDENTS table to the new SYSTEMID
column on the COURSESDONE table.

Each student only appears on the STUDENTS table once but each student
can have many records on the COURSESDONE table (1:M).

How do I get the SYSTEMID data into the COURSESDONE table? I suppose
I'd have to loop through each of the STUDENTS records and check if the
student in each record has a matching COURSESDONE.STUDENTID record and
then update the COURSESDONE.SYSTEMID value if any matches are found. Is
this correct? If so, what's the syntax that I can run from TOAD or SQL
Navigator?
Back to top
Ed Prochak
*nix forums Guru Wannabe


Joined: 17 Mar 2005
Posts: 271

PostPosted: Thu Jul 20, 2006 2:23 pm    Post subject: Re: Creating a relationship between 2 tables Reply with quote

Andyza wrote:
Quote:
I'm running Oracle 9i on Windows XP with TOAD as a "front-end" to
Oracle.

Sample database structure:

STUDENTS {
SYSTEMID (PK),
STUDENTID,
NAME,
SURNAME,
IDNUMBER
}

COURSESDONE {
COURSEID (PK),
COURSENAME,
RESULT,
STUDENTID
}

Currently the tables aren't related as no relationships between the two
tables have been defined. I'd like to add the SYSTEMID column to the
COURSESDONE table and add a foreign key relationship between the two
tables, i.e:

ALTER TABLE COURSESDONE ADD (
"SYSTEMID" VARCHAR2(50),
CONSTRAINT "COURSESDONE_SYSTEMID_FK" FOREIGN KEY("SYSTEMID")
REFERENCES STUDENTS("SYSTEMID")
)

That's fine... The tricky part is that I also want to copy the existing
data from the SYSTEMID column on the STUDENTS table to the new SYSTEMID
column on the COURSESDONE table.

Each student only appears on the STUDENTS table once but each student
can have many records on the COURSESDONE table (1:M).

How do I get the SYSTEMID data into the COURSESDONE table? I suppose
I'd have to loop through each of the STUDENTS records and check if the
student in each record has a matching COURSESDONE.STUDENTID record and
then update the COURSESDONE.SYSTEMID value if any matches are found. Is
this correct? If so, what's the syntax that I can run from TOAD or SQL
Navigator?

You design is horrible. Why is SYSTEMID the PK for students and not
STUDENTID? You have gone pseudokey happy. This looks like a homework
assignment, so you should post sample code and we can try to help. But
we will not solve your homework for you. Look up the UPDATE statement.
(BIG HINT you will use a version that includes a subquery).

post your attempt and we'll help move you along. (but meanwhile
consider fixing your data model design!)

Ed

..
Back to top
Andyza
*nix forums beginner


Joined: 17 Feb 2005
Posts: 5

PostPosted: Fri Jul 21, 2006 10:58 am    Post subject: Re: Creating a relationship between 2 tables Reply with quote

Quote:
You design is horrible.

I know! That's how I inherited it! But gotta work with what we've got
for now...

Quote:
Why is SYSTEMID the PK for students and not STUDENTID?

Because STUDENTID isn't guaranteed remain constant. And in some cases
the STUDENTID might change over time. And some users might have more
than one STUDENTID. It's used only for display purposes because users
get a warm fuzzy feeling when they see it on reports...

Quote:
You have gone pseudokey happy.

Yeah! Is that a new drug?

Quote:
This looks like a homework assignment,

ROFLOreallyreallyL! Thank you. That made my day!

Quote:
Look up the UPDATE statement.
(BIG HINT you will use a version that includes a subquery).

That's what I've done since I posted the original message - a
"sub-subquery" in a subquery in an update statement. It's awful...
I'll post some code and "re-explain" myself.
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 2:10 pm | All times are GMT
navigation Forum index » Databases » Oracle
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Creating relational view for an ODBC result set? antilog@gmail.com Server 0 Fri Jul 21, 2006 5:56 am
No new posts Exim4 not creating and/or receiving incoming mail... jtelep@localonline.net Exim 1 Fri Jul 21, 2006 1:59 am
No new posts idea/ concept behind creating breadcrumbs crescent_au@yahoo.com PHP 3 Fri Jul 21, 2006 12:10 am
No new posts creating another database on the same box ? newhorizon Server 3 Thu Jul 20, 2006 9:18 am
No new posts Linking Tables from 2 databases Pratik Shukla PostgreSQL 2 Wed Jul 19, 2006 5:54 pm

Credit Cards UK | Online Advertising | Home Equity Loan | Bay Area Web Design | Charity
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2890s ][ Queries: 16 (0.1380s) ][ GZIP on - Debug on ]