x

Maintaining constraints across different databases

What are the different approaches to maintain constraints accross different databases? what are the pro's and con;s of the process, If creating constraints is not possible, what is the best way of doing it? I was also wondering if there is a possibility that, we create a trigger on a datasbase table A residing in the database 1 To insert a new record in the table B in the database 2, When ever there is a new record in the table A?

Any advice is appreciated.

Thanks,
more ▼

asked Nov 30 '10 at 10:53 AM in Default

Katie 1 gravatar image

Katie 1
1.4k 129 163 202

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

Constraints between tables among different databases are only possible using the Triggers.

The ON UPDATE and ON DELETE rules you can simply implement by the AFTER UPDATE, DELETE triggers. The referential integrity you can enforce by the INSTEAD OF triggers.

There was a similar question in past about this.

You can create a simple AFTER INSERT Trigger, which will insert data to a table in other database.

CREATE TRIGGER dbo.trg_aTable_AI
   ON  dbo.aTable
   AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON;

    INSERT INTO [otherdb].[dbo].[OtherTable](Col1, Col2)
    SELECT
       Col1,
       Col2
    FROM INSERTED
END
GO
more ▼

answered Nov 30 '10 at 11:00 AM

Pavel Pawlowski gravatar image

Pavel Pawlowski
22.2k 8 11 21

Thank you !
Nov 30 '10 at 11:15 AM Katie 1
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1816
x977
x672
x107
x21

asked: Nov 30 '10 at 10:53 AM

Seen: 1641 times

Last Updated: Nov 30 '10 at 11:19 AM