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 > General > New Members & Introductions > how to make a multple insert

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-06-11, 13:20
simerw90 simerw90 is offline
Registered User
 
Join Date: Aug 2011
Posts: 6
how to make a multple insert

Hi every body.
I've a got a problem for what i couldn't get an anwser.I'll try to explain the problem, through an example:
supposing that i have two tables where each one references the other:

emp(num_emp,name,department,num_ser)
service(num_serv,name,num_emp)

how can i do to insert in the same time using a view, through a trigger or whatever, into the tables?
Reply With Quote
  #2 (permalink)  
Old 08-06-11, 16:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
one of the FKs will have to be nullable, and you will need to use two INSERT statements
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-06-11, 16:14
simerw90 simerw90 is offline
Registered User
 
Join Date: Aug 2011
Posts: 6
The problem is that i use an application where i need to insert the tuples through a view that include the two tables
Reply With Quote
  #4 (permalink)  
Old 08-06-11, 17:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
one of the FKs will have to be nullable, and you will need to use an INSERT and then an UPDATE statement
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-06-11, 18:17
simerw90 simerw90 is offline
Registered User
 
Join Date: Aug 2011
Posts: 6
Thank you for your answer r937
But could you show me how i can do that, to be executed in once, as trigger for example.
Reply With Quote
  #6 (permalink)  
Old 08-06-11, 18:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
can you dump the table layouts please
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 08-06-11, 18:34
simerw90 simerw90 is offline
Registered User
 
Join Date: Aug 2011
Posts: 6
-- TABLE NUMber 1
create table etudiant(num_etu int primary key ,nom varchar(20),prenom varchar(20),num_cours int )
-- Table Number 2
create table cours(num_cours int primary key,lib varchar(20),num_etu int)

alter table etudiant add constraint fk1 foreign key(num_cours) references cours(num_cours)
alter table cours add constraint fk2 foreign key(num_etu) references etudiant(num_etu)
--------Creation of the view
create view v1 as
select e.num_etu,nom,prenom,c.num_cours,lib from etudiant e join cours c on e.num_etu=c.num_etu

----Creation of the trigger

create trigger tr1
on v1 instead of insert
as
INSERT INTO etudiant SELECT num_etu,nom,prenom,num_cours FROM INSERTED;
INSERT INTO cours SELECT num_cours,lib,num_etu FROM INSERTED;

---Insertion through the view which will enable the trigger

insert into sd1 values(1,'smith','fred',1,'Info',2)
Reply With Quote
  #8 (permalink)  
Old 08-06-11, 22:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
you've completely lost me

why don't your primary keys say NOT NULL?

why are all other columns nullable? (e.g. it doesn't make much sense for the database to allow adding a student with null prenom and null nom)

why does the cours table have a link to a single student?

why can the student only have one course?

where do the values of the primary keys in both tables come from?

are you ~sure~ you know what you're doing with this design?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 08-07-11, 09:28
simerw90 simerw90 is offline
Registered User
 
Join Date: Aug 2011
Posts: 6
Hi r937
why don't your primary keys say NOT NULL?
I use sql server, and with it we can make NOT NULL using the graphics.

why are all other columns nullable? (e.g. it doesn't make much sense for the database to allow adding a student with null prenom and null nom)

The are not nullable, notice at the and i inserted into the view sd1 (i meant v1)

why does the cours table have a link to a single student?

In the where we have 1-1 cardinality, means that cours have 1 or 0 student, and student have 0 or 1 cours .

the information came from an application java.
Reply With Quote
  #10 (permalink)  
Old 08-07-11, 09:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
i don't think i can help you, sorry
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 08-07-11, 11:16
simerw90 simerw90 is offline
Registered User
 
Join Date: Aug 2011
Posts: 6
Ok.
Thanks any way
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