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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 HELP WITH CONSTRAINTS...

Author  Topic 

blocker
Yak Posting Veteran

89 Posts

Posted - 2010-04-04 : 20:54:14
I have two tables, tbl_custid and tbl_trans. Ive created a primary key in tbl_custid in the cusid field which has a foreign key in tbl_trans. It works when i delete a data in tbl_custid who's cusid is the same as in tbl_trans. But when ive changed the value of the balance field in tbl_custid, the balance field value in tbl_trans was not changed/affected. Whats wrong with my relationships.?Heres the T-SQL of the two tables.


/****** tbl_custid creation ******/

USE [Referencing_Test]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tbl_custid](
[cusid] [varchar](50) NOT NULL,
[balance] [varchar](50) NULL,
CONSTRAINT [PK_tbl_custid_1] PRIMARY KEY CLUSTERED
(
[cusid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

/****** tbl_trans creation ******/

USE [Referencing_Test]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tbl_trans](
[transid] [varchar](50) NOT NULL,
[balance] [varchar](max) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[tbl_trans] WITH CHECK ADD CONSTRAINT [FK_tbl_trans_tbl_custid] FOREIGN KEY([transid])
REFERENCES [dbo].[tbl_custid] ([cusid])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[tbl_trans] CHECK CONSTRAINT [FK_tbl_trans_tbl_custid]
GO

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-04 : 23:17:51
The balance field is not part of the foreign key, so it will not get updated except through a manual UPDATE statement. Only the id field will get auto-updated/deleted from the parent.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

blocker
Yak Posting Veteran

89 Posts

Posted - 2010-04-04 : 23:31:32
So that means i will manually update the value of the balance field.? Is their any way to update the balance field via CONSTAINTS or foreign key by just referencing its ID from the main table(tbl_custid).?

Thank you guys gor giving time..
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-04 : 23:34:43
If the balance field is linked in both tables, then you should just modify the constraint so that it includes both columns. Make it a composite foreign key.

But to me it seems silly to have two tables that store the same data. Why do you need both?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

blocker
Yak Posting Veteran

89 Posts

Posted - 2010-04-04 : 23:56:20
The user only changes the value of the balance on tbl_custid. Once updated, it will automatically change the balance value of the tbl_trans table for viewing purposes.
Go to Top of Page

blocker
Yak Posting Veteran

89 Posts

Posted - 2010-04-05 : 01:40:38
Anyway,Thank you for this composite foreign key. Ill take a read for this to solve the problem.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-05 : 05:04:52
i think what you need to do is to implement a trigger to enforce this change upon updation on cust table. Alternatively you could implement this as a part of your original update procedure also

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

blocker
Yak Posting Veteran

89 Posts

Posted - 2010-04-05 : 23:50:24
Once again, thank you guys. I made it correct using triggers. I just want to ask about the efficiency of triggers in terms of data transmission and reliability compare to constraints.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-06 : 00:28:29
triggers certainly have some impact on performance. If you can, you may add the logic along with update procedure itself.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

blocker
Yak Posting Veteran

89 Posts

Posted - 2010-04-06 : 21:58:43
thank you very much. will post the trigger result as new topic.God bless us all
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-06 : 22:11:03
Discussion continues here: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=142575

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -