I have a table X that has a column with an auto-incrementing identifier as its primary key. I have other tables A, B, C and D that supplement the information in table X. Each of them should contain a column that refers to the identifier from table X. I did this in my code (Java), and I have a way to return the identifier of each record in table X and use it when pasting into other tables. Everything works well.
Now I am advised to assign these identifier columns in tables A, B, C and D as FOREIGN KEYS, because "this is the right thing." I did it. Now removing rows from table X takes an incredible amount of time to complete . Pasting into other tables takes longer.
Please do not get me wrong, I know why foreign keys are related to the definition of table relationships in the database. But it starts to seem only ceremonial, not actual, especially as my transactions are getting slower.
Questions:
1. Is it worth it to lose some productivity in order to maintain official relations, even if it is not so necessary?
2. Is there a way to speed up transactions and save FOREIGN KEY specifications.
Thanks.
ANSWERS
Here's how the tables were created.
CREATE Tables SQL:
CREATE TABLE [dbo].[MainTableX]( [col1] [smalldatetime] , [col2] [varchar] (20) , [ID] [int] IDENTITY(1,1) NOT NULL, CONSTRAINT [PK_MainTableX] PRIMARY KEY CLUSTERED ( [ID] ASC ) ) GO CREATE TABLE [dbo].[RelatedTableA]( [differentID] [varchar] (50), [detail1] [varchar] (40), [detail2] [varchar] (40), CONSTRAINT [PK_RelatedTableA] PRIMARY KEY CLUSTERED ( [differentID] ASC ) GO -- Tables B, CD are pretty much similar to table A
Add SQL Foreign Keys:
ALTER TABLE RelatedTableA ADD ID INT CONSTRAINT fk_refTableX_A FOREIGN KEY (ID) REFERENCES MainTableX(ID) GO
Decision
I made the Foreign Key columns an index. Now my queries are quick again.
Create nonclustered index IX_RelatedTableA on RelatedTableA (ID) GO