Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Is it possible to create a Foreign key on part of a field in MSSQL?

 

i have a field that is a delimited list of values contained in another table that i would like to establish a relation...

Posted
Foreign keys are defined at the field level - if a field contains a list of values then it would probably be better to break the field down into either multiple fields or a related table.
as plaus said, you need to do this.

 

A delimited set of values in one field fails to satisfy first normal form -

 

in this thread is a SQL split function

 

after executing the script to create the function you can do the following -

 

given a table like this

 

OriginalTable

Key (int), Description (varchar(255)), DELIMITEDFIELD (varchar(8000))

 

execute this sql script -

 

' you can execute this entire script in one DBCommand.ExecuteNonQuery call

declare @key int

declare @DELIMITEDFIELD int

create table DELIMITEDFIELDTABLE(

originalKey (ID) references OriginalTable(Key),

order int,

value varchar(255), primary key (originalKey, order)

 

declare curs for select key, DELIMITEDFIELD from OriginalTable

open curs

fetch next from curs into @key, @DELIMITEDFIELD

while @@fetch_status = 0

begin

insert into DELIMITEDFIELDTABLE (originalKey, order, value)

select @KEY, ID, VALUE from dbo.split(@DELIMITEDFIELD, ',')

fetch next from curs into @key, @DELIMITEDFIELD

end

close curs

deallocate curs

alter table OriginalTable drop column DELIMITEDFIELD

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted
my example assumes comma delimited

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...