IT Community - Software Programming, Web Development and Technical Support

Foreign key column

This is a discussion on Foreign key column within the Database Support forums, part of the Web Development category; Hi, I have one doubt. Is the foreign key column is really physical data?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 09-28-2007, 01:07 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Question Foreign key column

Hi,

I have one doubt. Is the foreign key column is really physical data?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-10-2007, 03:16 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Cool Re: Foreign key column

The data in both tables is physical, the link between them is an object is SysObjects that makes sure that all the data in both table is valid (as long as the FK is always enabled... or revalidated when turned off).



The proof I could offer is this in my short amount of time : SELECT id, Name FROM dbo.SysObjects WHERE XType = 'F' ORDER BY Name



You can also check out the information_schema views for more options.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-10-2007, 03:27 AM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Wink Re: Foreign key column

I don't think so... foreign keys don't automatically make indexes like Primary Keys do. They make excellent candidates for indexes, though.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-11-2007, 12:02 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Smile Re: Foreign key column

The foreign key constraints do NOT automatically create indexed. And they are more than excellent candidates to apply for indexes.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-13-2007, 12:53 AM
jeyaprakash.c jeyaprakash.c is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 228
jeyaprakash.c is on a distinguished road
Smile Re: Foreign key column

For a column that is not physical, which is a computed column, a foreign key constraint cannot be created. Here is a test case:
Create Table Foo
(FooPkvarchar(255) not null
, primary key (FooPk)
)
go
create table Fum
(FumC1varchar(255) not null
,FumC2 varchar(255) not null
,FumC3 as ( FumC1 + '/' + FumC2)
)
alter table Fum
add constraint Foo_F_Fum foreign key (FumC3) references Foo

For SQL Server 2000, the error is 1769
Foreign key 'Foo_F_Fum' references invalid column 'FumC3' in referencing table 'Fum'.

For SQL Server 2005, the error is 1764
Computed Column 'FumC3' in table 'Fum' is invalid for use in 'FOREIGN KEY CONSTRAINT' because it is not persisted.

Creating an index on the computed column still results in the error on the attempted creation of a foreign key constraint.
__________________
thanx n regards
jeyaprakash.c
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 10-13-2007, 12:55 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Cool Re: Foreign key column

That's exactly why it's so fun to have 450+ K members.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 10-15-2007, 11:27 PM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Default Re: Foreign key column

Just additional info... Foreign Keys must reference either a PK or a column with a UNIQUE index... that not-with-standing, you still can't reference a computed column as a Foreign Key...

CREATE TABLE Foo
(
FooPk VARCHAR(255) NOT NULL,
FumFum VARCHAR(512),
PRIMARY KEY (FooPk)
)
GO
CREATE TABLE Fum
(
FumC1 VARCHAR(255) NOT NULL,
FumC2 VARCHAR(255) NOT NULL,
FumC3 AS CAST(( FumC1 + '/' + FumC2) AS VARCHAR(512))
)
GO
CREATE UNIQUE INDEX IX_Fum_FumC3 ON Fum (FumC3)
GO
ALTER TABLE dbo.Foo
ADD CONSTRAINT FK_Foo_Fum
FOREIGN KEY (FumFum) REFERENCES dbo.Fum (FumC3)
GO

Server: Msg 3701, Level 11, State 5, Line 1
Cannot drop the table 'Foo', because it does not exist in the system catalog.
Server: Msg 1784, Level 16, State 1, Line 1
Cannot create the foreign key 'FK_Foo_Fum' because the referenced column 'Fum.FumC3' is a computed column.
Server: Msg 1750, Level 16, State 1, Line 1
Could not create constraint. See previous errors.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Foreign key with references of multiple tables with same primary key field vadivelanshanmugam Database Support 0 03-03-2008 10:53 PM
How can count a column from two tables in SQL Server? mobilegeek Database Support 1 07-24-2007 06:48 AM
How to hide a column in datagrid dynamically in ASP .NET? Archer C# Programming 1 07-24-2007 12:30 AM
applying color to specified column in datagrid nssukumar Flash Actionscript Programming 4 03-22-2007 10:31 AM
Adding a new Column in middle of the Table Murali Database Support 3 03-05-2007 11:59 PM


All times are GMT -7. The time now is 05:00 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0