|
|
|
|
|
|
| Author |
Message |
Andyza *nix forums beginner
Joined: 17 Feb 2005
Posts: 5
|
Posted: Thu Jul 20, 2006 1:11 pm Post subject:
Creating a relationship between 2 tables
|
|
|
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
|
Posted: Thu Jul 20, 2006 2:23 pm Post subject:
Re: Creating a relationship between 2 tables
|
|
|
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
|
Posted: Fri Jul 21, 2006 10:58 am Post subject:
Re: Creating a relationship between 2 tables
|
|
|
| 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 |
|
 |
|
|
The time now is Sun Nov 23, 2008 2:10 pm | All times are GMT
|
|
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
|
|