I was asked in a job interview to design the database for a dictionary that keeps tracks of synonyms. The synonym part was a later question in the interview. I had a table like
Words
=============
id | ...
=============
1 | .
2 | .
. | .
. | .
and so to relate words to words I made a table
Synonyms
======================
wordA_id | wordB_id
======================
. | .
. | .
. | .
For example, if words with id
equal to 1
, 69
and 72
are synonyms then
1 | 69
69 | 72
might be an entry in Synonyms
. But so might
1 | 69
1 | 72
I realized. And the pairing order doesn't matter, so
69 | 1
1 | 72
words just as well. And we probably don't want duplicate records of any order, so something like
69 | 1
1 | 72
69 | 1
of
69 | 1
1 | 72
1 | 69
I'm wondering the best way to configure the database to ensure all this. What I suggested (in the interview) is inserting synynoms through a sproc that checks all this, like
CREATE PROCEDURE InsertSynonym
@idA INT,
@idB INT
AS
-- make @idA the smaller if it isn't already
IF @idA > @idB
BEGIN
@DECLARE @temp INT
SET @temp = @idA
@idA = @idB
@idB = @temp
END
-- insert if not already there
IF NOT EXISTS (SELECT * FROM Synonyms WHERE wordA_id=@idA)
BEGIN
INSERT INTO Synonyms (wordA_id,wordB_id) VALUES (@idA,@idB)
END
- Are there any bugs in my T-SQL syntax?
- Assuming that the only way synonyms are inserted is through this procedure, does the logic cover all the cases and does it keep the size of the
Synonyms
table from exploding? - Any better way to design this synonyms pattern to begin with?
- By the way, is it possible to do an XOR swap in T-SQL?