Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Primary/Foreign Key constraints applied in views

Author  Topic 

Inquisitor
Starting Member

4 Posts

Posted - 2009-06-16 : 11:26:36
Hello all:

I am a self tough individual on SQL and I want to learn how to manage the constraints applied to 3 tables that have Primary and Foreign key relationships applied. I have created a View that joins these tables by there Keys. I am in the beginning of the development of the tables and I want to know how or if SQL will manage Key values between tables automatically when a new record is added to the view.

Table 1 has a (auto number) primary key. Table 2 has an (auto number) primary key and a foreign key to table 1. Table 3 has an (auto number) primary key and a foreign to table 2.

What I want to know is when I open a view that is a join of the three tables by the Keys for edit. I want to know how to manage a constraint or something to take the next auto number of table 1 and apply it to the foreign key of table 2 new record row and take the next auto number of table 2 and apply it to the foreign key of table 3 new record row . I am hoping that it can be done by a constraint or by a trigger. This is where my knowledge ends and I am asking for advice.

Thank you very much.

nr
SQLTeam MVY

12543 Posts

Posted - 2009-06-16 : 11:35:35
You should be able to do it via an instead of insert trigger.
Sounds like you are designing something that is going to be difficult to maintain though.

Would be much easier to use a stored procedure for the retrieval and update. Would also mean you could restructure the database without changing the client.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Inquisitor
Starting Member

4 Posts

Posted - 2009-06-16 : 12:48:07
Thank you for your imput. I will look into creating a stored procedure and what it can do for me.

Thank you again!
Go to Top of Page
   

- Advertisement -