Don't do this.
Such a table violates database normalization, because your real groups (projectName + fileName + fileLine) can get out of sync with the group ID. Instead create a group table:
CREATE TABLE my_group
(
groupid INTEGER PRIMARY KEY,
projectName TEXT,
fileName TEXT,
fileLine TEXT,
/* other columns ? */
UNIQUE(projectName, fileName, fileLine)
);
Then fill this table from the original one:
INSERT INTO my_group (projectName, fileName, fileLine)
SELECT DISTINCT projectName, fileName, fileLine
FROM my_data;
If there are more columns you need to copy, then there are two options:
Option #1: Continue using DISTINCT
. That will fail, if these column values are not unique for a group, because of the unique constraint, and would thus point you to data inconsistencies in your original table.
INSERT INTO my_group (projectName, fileName, fileLine, other_column)
SELECT DISTINCT projectName, fileName, fileLine, other_column
FROM my_data;
Option #2: Tell the DBMS which values to pick (usually the minimum, maximum or sum found for a group):
INSERT INTO my_group (projectName, fileName, fileLine, other_column)
SELECT projectName, fileName, fileLine, MIN(other_column)
FROM my_data
GROUP BY projectName, fileName, fileLine;
Now that you have done that, your tables are already related and you have assigned a group ID for each group. If you want to stay with these tables related by their business key (projectName + fileName + fileLine), create the foreign key on them:
ALTER TABLE my_data
ADD CONSTRAINT fk_group
FOREIGN KEY (projectName, fileName, fileLine)
REFERENCES my_group (projectName, fileName, fileLine);
and you are done.
If you don't want to do that, but relate the tables by group ID instead, add that to your table:
ALTER TABLE my_data ADD COLUMN groupid INTEGER;
Then fill it:
UPDATE my_data
SET groupid =
(
SELECT groupid
FROM my_group
WHERE my_group.projectName = my_data.projectName
AND my_group.fileName = my_data.fileName
AND my_group.fileLine = my_data.fileLine
);
Then delete the redundant columns:
ALTER TABLE my_data DROP COLUMN projectName INTEGER;
ALTER TABLE my_data DROP COLUMN fileName INTEGER;
ALTER TABLE my_data DROP COLUMN fileLine INTEGER;
And then add the foreign key:
ALTER TABLE my_data
ADD CONSTRAINT fk_group
FOREIGN KEY (groupid)
REFERENCES my_group (groupid);