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
 General SQL Server Forums
 New to SQL Server Programming
 Referential Integrity

Author  Topic 

Pinto
Aged Yak Warrior

590 Posts

Posted - 2010-04-06 : 06:32:57
I want to enforce referential integrity between two tables.Each table has an Id (the key) which is an autonumber. However, my tables are linked by a text field. This is unique in the 'one' table. The other table contains the 'many' records. How can I do this?

The database has been moved from Access to SQL, so I am stuck with the link being a text field.

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-06 : 06:37:51
add a constraint (foreign key) on the child table on that column.

There's no problem having your relationships as character columns (as long as they are still good candidates for a primary key).

You haven't mentioned which column / set of columns in the table(s) are your keys. An ID column isn't necessary the key to a table.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2010-04-06 : 06:43:45
add a constraint (foreign key) on the child table on that column

How do I do that ?
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-06 : 06:50:32
I'd advise you to read up on the:

CREATE TABLE
ALTER TABLE

commands in books online.

Here's a direct link to the CONSTRAINTS
http://msdn.microsoft.com/en-US/library/ms188066(v=SQL.90).aspx

I see you've made about 500 posts. But I'm guessing they are mostly Access related?

Which version of SQL SERVER are you using now. I've posted reference to 2005.

Feel free to post the DML of the tables. You'll get some comment and advice.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-06 : 07:01:23
quote:
Originally posted by Pinto

add a constraint (foreign key) on the child table on that column

How do I do that ?


specifically look for ALTER TABLE ADD CONSTRAINT statement

http://msdn.microsoft.com/en-us/library/ms177463.aspx

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

Go to Top of Page

Pinto
Aged Yak Warrior

590 Posts

Posted - 2010-04-06 : 07:42:14
Not Access on this site and not for about 8 years. I haven't used Access for about 8 years, but we have some old ones that need changing to sql and rewriting the apps in .net

Thanks for the links
Go to Top of Page
   

- Advertisement -