views:

72

answers:

2

Ok I have a question and it is probably very easy but I can not find the solution.

I have 3 tables plus one main tbl.

tbl_1 - tbl_1Name_id 
tbl_2- tbl_2Name_id 
tbl_3 - tbl_3Name_id

I want to connect the Name_id fields to the main tbl fields below.

main_tbl 
___________ 
tbl_1Name_id 
tbl_2Name_id 
tbl_3Name_id

Main tbl has a Unique Key for these fields and in the other table, fields they are normal fields NOT NULL.

What I would like to do is that any time when the record is entered in tbl_1, tbl_2 or tbl_3, the value from the main table shows in that field, or other way. Also I have the relationship Many to one, one being the main tbl of course.

I have a feeling this should be very simple but can not get it to work.

+1  A: 

Take a look at SQL Server triggers. This will allow you to perform an action when a record is inserted into any one of those tables.

If you provide some more information like:

  • An example of an insert
  • The resulting change you would like to see as a result of that insert

I can try and give you some more details.

UPDATE

Based on your new comments I suspect that you are working with a denormalized database schema. Below is how I would suggest you structure your tables in the Employee-Medical visit scenario you discussed:

Employee
--------
EmployeeId
fName
lName

EmployeeMedicalVisit
--------------------
VisitId
EmployeeId
Date
Cost

Some important things:

  1. Note that I am not entering the employees name into the EmployeeMedicalVisit table, just the EmployeeId. This helps to maintain data integrity and complies with First Normal Form
  2. You should read up on 1st, 2nd and 3rd normal forms. Database normalization is a very imporant subject and it will make your life easier if you can grasp them.

With the above structure, when an employee visited a medical office you would insert a record into EmployeeMedicalVisit. To select all medical visits for an employee you would use the query below:

SELECT e.fName, e.lName
FROM Employee e
INNER JOIN EmployeeMedicalVisit as emv
ON e.EployeeId = emv.EmployeeId

Hope this helps!

Abe Miessler
Thanks Abe, I have a main table which has lot of details and will be used daily, So what I am planning to is when the main table is updated that it creates a id for other three tables so that the records are connected when the tbl1, tbl2 or tbl 3 have all the details from main tbl...
Tony
ok just to add, main tbl has more fields... like fistname, last name , address, passport details, etc.... plus the fields tbl_1Name_id tbl_2Name_id tbl_3Name_id.In the same time when this is inserted I want to update the tbl1, tbl2 and tbl3 with the same value from _id fields from main. And main tbl fields have identity and increment.
Tony
I really need a use case added to your original question. Soemthing like example insert SQL and the table changes it causes.
Abe Miessler
ok, Main table is used to add details of Employees and the other three are ,let us say, in case of visiting the doctor (medical), which does not happened often. So all the employees are in the table and when visit happens I want to link the person to the medical table. Maybe my approach was wrong from the beginning but I am open to suggestions.
Tony
When they have visit the doctor I want fields Last, first and address from the main tbl and all fields from Medical tbl.
Tony
My problem is that I can not add record into the Medcail tbl. I create view with main tbl and medical and can edit main tbl but can not update the medical tbl. It could be because of the unique key or something but I tried all options.
Tony
@user455350, what you might need to do may be beyond the scope of what I can answer in a SO question but I will give it a try. Please look at my updated answer.
Abe Miessler
Hi Abe thanks a lot, but in Employee_id is auto incremented value and the MedicalVisit Emplo_id is not. How do I get the Employee_id from Empl to the MedicalVisit Employee_id without entering it manually. to have them matching.
Tony
Thanks a lot everybody, I managed to change it a bit and it works...
Tony
A: 

Here is a sample trigger that may show you waht you need to have:

Create trigger mytabletrigger ON mytable
For INSERT 
AS

INSERT MYOTHERTABLE (MytableId, insertdate)
select mytableid, getdate() from inserted

In a trigger you have two psuedotables available, inserted and deleted. The inserted table constains the data that is being inserted into the table you have the trigger on including any autogenerated id. That is how you get the data to the other table assuming you don't need other data at the same time. YOu can get other data from system stored procuders or joins to other tables but not from a form in the application.

If you do need other data that isn't available in a trigger (such as other values from a form, then you need to write a sttored procedure to insert to one table and return the id value through an output clause or using scope_identity() and then use that data to build the insert for the next table.

HLGEM
Thanks a lot, sorry for late reply, I will try it out, If you could know this one also how can I copy exactly the field value (first table) when is entered, into the another field, second table, it will be unique so I can link them together
Tony