Wednesday, March 7, 2012

Inserting Values Into Primary Key and Foreign Key Tables

I have two tables that I would like to insert values into at the same time
and would like help with the SQL statement to do so. One table (Member_Info)
has a PK and the other (Image_Info) a FK. The relationship the two tables
share is through the (E_Mail) column. Example structure:
Member_Info table columns:
First_Name
Last_Name
Birthday
E_Mail (PK)
Image_Info table columns:
E_Mail (FK)
Use
Name
Please Help! Thanks!Use INSERT TRIGGER in Member_Info table
"Willie Davis via webservertalk.com" wrote:

> I have two tables that I would like to insert values into at the same time
> and would like help with the SQL statement to do so. One table (Member_Inf
o)
> has a PK and the other (Image_Info) a FK. The relationship the two tables
> share is through the (E_Mail) column. Example structure:
> Member_Info table columns:
> First_Name
> Last_Name
> Birthday
> E_Mail (PK)
> Image_Info table columns:
> E_Mail (FK)
> Use
> Name
>
> Please Help! Thanks!
>|||Due to not determining what table will be filled by your code and what table
has to be filled "automagicaly", the following just depends on my guesswork.
CREATE TRIGGER TrgIns Member_Info
FOR INSERT
AS
BEGIN
INSERT INTO (EMail, Name)
SELECT EMail, COALESCE(FirstName,'') + ' ' + COALESCE(LastName,'') FROM
Inserted
END
I dont know wheter you Name column will store the First and the Lastname or
just one of it, but you should consider an Update Trigger to update this
information if its changed in the Primary Key Table. (Guess you sure wont
need it, if this just stores the name of the Member, even this is already
stored in the PK Table)
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"Willie Davis via webservertalk.com" <forum@.nospam.webservertalk.com> schrieb im
Newsbeitrag news:4183b2e7baef434899249fa71b414bf6@.SQ
webservertalk.com...
>I have two tables that I would like to insert values into at the same time
> and would like help with the SQL statement to do so. One table
> (Member_Info)
> has a PK and the other (Image_Info) a FK. The relationship the two tables
> share is through the (E_Mail) column. Example structure:
> Member_Info table columns:
> First_Name
> Last_Name
> Birthday
> E_Mail (PK)
> Image_Info table columns:
> E_Mail (FK)
> Use
> Name
>
> Please Help! Thanks!|||Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Sample data is also a good idea, along with clear
specifications.
same time M<<
SQL works with one table at a time. Put you code into a stored
procedure, do two inserts and depend on DRI actions to maintain
integrity.

No comments:

Post a Comment