If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > is it efficient to insert several table for executing 1 process?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-14-07, 11:12
tenma-tenma tenma-tenma is offline
Registered User
 
Join Date: Feb 2007
Posts: 26
is it efficient to insert several table for executing 1 process?

hi..

sorry, if i ask a simple question but it quite confusing me anyway..
so i have 3 table..
MR(MRId->pk, regDate)
patient(patientId->pk, fname, lname)
havepatient (MRId ->pk, patientId->pk)

my question is, if i want to add new MR, in which 1 MR can has several patients, it means that i have to execute3 different insert commands for both these 3 tables?

please reply me as soon as possible =)
thx for your attention
Reply With Quote
  #2 (permalink)  
Old 05-14-07, 11:36
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Firstly, do not apologise for asking a "simple question"
Secondly, this is not a simple question!

Why can't you create a MR without any patients "attached" to it?
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 05-14-07, 12:01
tenma-tenma tenma-tenma is offline
Registered User
 
Join Date: Feb 2007
Posts: 26
Hi, thank's for the fast reply.. =)

"Why can't you create a MR without any patients "attached" to it?"
MR is like an identification for each familiy, and each familiy consist of several patient..
does my erd is already correct? and if, for example, i want to add a new MR with new patients of course, so i have to do :

insert into havepatient(MRId, patientId) values (NULL, NULL);
insert into MR(MRId, regDate) values (1, 2007/04/05);
insert into patient(patientId, fname, lname) values(1, lala, baba);

is it correct? and is it efficient to perform 3 inserts just for adding 1 new MR?

thanks for your attention
Reply With Quote
  #4 (permalink)  
Old 05-14-07, 17:24
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Personally I'd advise you create the patients and MRs BEFORE you join them in your havepatient table
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 05-14-07, 20:26
tenma-tenma tenma-tenma is offline
Registered User
 
Join Date: Feb 2007
Posts: 26
Hi...

yeah.. i should entered those 2 first..
thank's for your replies..
Reply With Quote
  #6 (permalink)  
Old 05-15-07, 01:12
tenma-tenma tenma-tenma is offline
Registered User
 
Join Date: Feb 2007
Posts: 26
Hi..
sorry for asking the same problem againn..
so, i write something like this:

Code:
$queryMR= "INSERT INTO MR (MRId, time) values (NULL, current_timestamp())";
  $resultMR= safe_query($queryMR);

$queryPatient= "INSERT INTO patient ( patientId , fname ,lname)";
  $queryPatient.= "VALUES (NULL , '$firstname', '$lastname')";
  $resultPatient= safe_query($queryPatient);

$queryHave_Relation= "INSERT INTO havePatient(patientId, MRId) SELECT p.patientId, m.MRId";
  $queryHave_Relation.= "FROM patient p, MR m WHERE m.MRId='' and (p.fname= '$firstname' and p.lname= '$lastname')";
  $resultHave_Relation= safe_query($queryHave_Relation);
my question is what should i put in the MRId= '' (the red one), i want to take the result of MRId from $resultMR, but if i put the MRId straightaway (hardcode) then it is useless.. i have thought about taking the latest MRId from the MR table, but the problem is there are more than 1 user that perform insertion in the same time.

can anybody help me out from this problem? =)
thank's for ur attention..
Reply With Quote
  #7 (permalink)  
Old 05-15-07, 03:47
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
One thing I noticed straight away is that you're insering a NULL value as your MRId!! You should NOT have null values for an identity field like this - and as your patientId! If these are autonumbers you can simply ignore them in an insert as they will be populated automatically.

Secondl; according to your 3rd query you are searching for patients' first names and last names as a means of joining the two... Bad move...
What if you have 10 people called John Smith?
__________________
George
Twitter | Blog
Reply With Quote
  #8 (permalink)  
Old 05-15-07, 04:43
tenma-tenma tenma-tenma is offline
Registered User
 
Join Date: Feb 2007
Posts: 26
should i use the combination of fname, lname, and DOB instead? i think it would be better if i could take the patientId from $queryPatient straightaway as the input for pID in $queryHave_Relation..

and what do you think would be the best for MRId?
thankss a lot..
Reply With Quote
  #9 (permalink)  
Old 05-15-07, 07:53
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Code:
INSERT
INTO patient(fname, lname) values('John', 'Smith', '01/01/1980 00:00:00.000');

INSERT
INTO patient(fname, lname) values('John', 'Smith', '01/01/1980 00:00:00.000');

INSERT
INTO patient(fname, lname) values('John', 'Smith', '01/01/1980 00:00:00.000');
There are now 3 John Smiths who happen to be born on the same day...
The whole point in an identity (key) field is to uniquely identify a record.
__________________
George
Twitter | Blog
Reply With Quote
  #10 (permalink)  
Old 05-15-07, 14:33
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
Also, after inserting a record, (for SQL Server) you may get the value of your most recently inserted value by using the @@identity function

Code:
INSERT INTO MR (time) values (current_timestamp())

SELECT TOP 1 @@identity as LastRecordID from MR
This will return the last identity value in your connection. Even if another users inserts a record after you've inserted the record, but before you select @@identity, the value you receive is the value associated with your insert.
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
  #11 (permalink)  
Old 05-16-07, 05:06
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Wow, I never knew you could do that...
Just a quickie on ths above loquin...
I ran the following in query Analyzer
Code:
SELECT TOP 1 @@identity AS LastRecordID FROM employee
Which gave me this error
Code:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '1'.
I'm running this on SQL Server 2k - I've never used a TOP statement in SS before and it appears that it's not liking them. Any ideas?

EDIT: Just ran this on a SS 2K5 database... the TOP worked, but it returns NULL every try so far..
__________________
George
Twitter | Blog
Reply With Quote
  #12 (permalink)  
Old 05-16-07, 05:10
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Back to the original problem:
Tenna, what database are you using and what is the key field in each table (autonumber?) etc.
__________________
George
Twitter | Blog
Reply With Quote
  #13 (permalink)  
Old 05-16-07, 05:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
who said the PKs had to be identity/autonumber columns?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #14 (permalink)  
Old 05-16-07, 05:52
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
I just assumed that "patientId" was an identity PK, same goes for MRId
also why I asked the question too
*shrugs*
__________________
George
Twitter | Blog
Reply With Quote
  #15 (permalink)  
Old 05-22-07, 05:09
tenma-tenma tenma-tenma is offline
Registered User
 
Join Date: Feb 2007
Posts: 26
Hi..
thx for the replies..
i just read the forum..
thxx..
regards,

Lena
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On