rmatthew Posted January 30, 2006 Posted January 30, 2006 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... Quote
Administrators PlausiblyDamp Posted January 30, 2006 Administrators Posted January 30, 2006 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Joe Mamma Posted February 2, 2006 Posted February 2, 2006 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 Quote 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.
Joe Mamma Posted February 2, 2006 Posted February 2, 2006 my example assumes comma delimited Quote 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.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.