I’m trying to reference two primary keys from two different tables, but the problem is that they don’t match due to its character length.
For an example , person.personid = '1234' and department.personid = '12345'.
Is there an SQL statement in which I can use to match the first 4 or 5 characters of the primary keys or is there any other methods that I can use.
UPDATE : My sincere apologies. I am dealing with two tables. "dyndomrun.ddid" is with a Primary key and "domainregion.domainid" is without any primary key nor foreign key. “dyndomrun” table DDL is set to "character varying" and has 8 characters, whereas “domainregion” table DDL is also set to “character varying” but has 10 characters.
Problem : There are some fields in domainregion table that needs to be joined together with the primary key in dyndomrun table. I can’t seem to do this with a simple SQL statement such as below
SELECT domainregion.domainid, dyndomrun.ddid
FROM domainregion, dyndomrun
WHERE domainregion.domainid = dyndomrun.ddid
ORDER BY domainregion.domainid, dyndomrun.ddid;
I have tried JOINS, INNER JOINS, LIKE, none of them seems to work. The database that I am dealing with is purely SQL based stored using PostgreSQL.
Please advise.
Your question doesn’t make sense. We don’t map the primary key of one table to the primary key of another table.
Now we do map the foreign key of a dependent table to the primary key of the referenced table. But it that case the value of the column in both tables has to match.
12345in DEPARTMENT.PERSONID must refer to a record12345in PERSON.PERSONID, because1234in .PERSON.PERSONID is literally a different person.Perhaps you’re working with a whacky data model, in which the dependent table has some smart key only part of which refers to the parent key. If that’s the case, my condolences. But you need to give some specific details, and some indication of flavour of DBMS (because solutions will vary across different products).
But your biggest problem is this: once you try to apply relational integrity on anything other than equality between columns you run the risk of matching
1234in .PERSON.PERSONID with1234in DEPARTMENT.PERSONID,12345in DEPARTMENT.PERSONID and123456in DEPARTMENT.PERSONID. That’s probably not what you want.