Skip to main content
krishnav9816
Known Participant
Jul 15, 2022 5:29 PM UTC
Solved

how to set Foreign Key in database?

how to set Foreign Key in database? mariadb

    harshitb6843
    Brainy
    Jul 18, 2022 5:40 AM UTC

    1 

    22 replies

    harshitb6843
    Brainy
    Jul 15, 2022 5:51 PM UTC
    krishnav9816
    Known Participant
    Jul 16, 2022 1:44 AM UTC

     table1 

     table 2

    based on the above two tables tabel1 & table 2 ,I want claim id is foreign key in the table 2 ? what is the syntax ?

    harshitb6843
    Brainy
    Jul 17, 2022 6:08 AM UTC

    Did you check the link I provided in the above comment?

    Participating Frequently
    Jul 17, 2022 11:21 AM UTC

    ALTER TABLE table2
    ADD FOREIGN KEY (Claim ID) REFERENCES table1(id);

    krishnav9816
    Known Participant
    Jul 18, 2022 5:09 AM UTC

    Error

    SQL query: Copy

    ALTER TABLE LMS_Aprrovals
    ADD FOREIGN KEY (Claim ID) REFERENCES lmscustomerdetails(id);
    

    MySQL said: 

    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ID) REFERENCES lmscustomerdetails(id)' at line 2

    Participating Frequently
    Jul 18, 2022 5:34 AM UTC

    Try removing the space between Claim and ID in your column name by renaming the column name of 2nd table and try again with same syntax to add foreign key.