aspose file tools*
The moose likes JDBC and the fly likes insert or update??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "insert or update???" Watch "insert or update???" New topic
Author

insert or update???

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
I guess this is a rocky question:

I have a table with 3 elements:

1. name
2. age
3. GPA

there's a gui and the user update the infomraiton on a weekly basis.
the table above has no primery key. and it can contain:

Dan 23 3.4
Dan 22 3.0
Gil 21 3.3

etc

So, the question is this:
say the user has entered the information for the first time - I'll use INSERT but what if he only UPDATE the info???

how can i tell if this is his first time or not?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61769
    
  67

Why is there no primary key? Without a primary key your table is hopelessly hobbled.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
becuase the name is not unique and there could be many instances of the same name.

Dan 23 3.4
Dan 22 3.0
Gil 21 3.3


if Dan is uniqe i will not be able to set the second line.
Bob Rocks
Greenhorn

Joined: Oct 21, 2004
Posts: 23
you still need some sort of an ID for the row ex

id name age gpa
1 dan 23 3.0
2 dan 20 2.8
3 mike 21 3.3

then if you want to update you do it like this

update table set name=dan, age=23, gpa=3.1 where id = 2;

if you did it like this

update table set name=dan, age=23, gpa=3.1 where name = dan;
it would change all data where the name is dan, this would create bad data.

you can set up the id field using a sequence (in oracle) or an auto-increment in mysql


no offence at all, but it seems you are pretty new to databases and sql, might i suggest a GREAT site to get you started.

www.sqlcourse.com and after you get through that www.sqlcourse2.com

these helped me when I was just starting out, they let you build a database on there server as you are going thorough the course. Best part is that you never have to leave the page!

-Bob

[ November 15, 2004: Message edited by: Bob Rocks ]
[ November 15, 2004: Message edited by: Bob Rocks ]
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
thanks bob - great site
Bob Rocks
Greenhorn

Joined: Oct 21, 2004
Posts: 23
no problem, glad I could help! I remember when I was just getting started and that site was a huge help! If you need any more help, feel free to let me know, I would be more than happy to lend a hand.

-Bob
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Thanks Bob,

I solved the issue according to your recommendation .
 
 
subject: insert or update???