Briana Briana - 2 months ago 14
SQL Question

How to match values of primary key and foreign key?

I am having a problem matching the values of my primary column from a table called "Course" and foreign key in my other table called "Curriculum". My table in sql server looks like this




COURSE TABLE
CourseID | CourseName | CourseCode
----------------------------------
8 | IT | 62345
9 | BA | 64738
10 | IS | 62341








CURRICULUM TABLE
CourseCode | CurriculumID | CourseID
----------------------------------------
62345 | 87 | 4
64738 | 98 | 5
62341 | 78 | 6





these values are imported in a website I made using asp.net. I uploaded the csv file there and then it will be added in the database, after that I get these values and it seems that the primary and foreign key doesn't match. I need a query that will match these both columns. Please help. I need my Curriculum table to look like this:




CURRICULUM TABLE
CourseCode | CurriculumID | CourseID
----------------------------------------
62345 | 87 | 8
64738 | 98 | 9
62341 | 78 | 10




Answer

If you have course code in both the tables,you can use the below script.

UPDATE cu
SET cu.CourseID=c.CourseID
FROM Course c
 JOIN CURRICULUM  cu ON c.CourseCode=cu.CourseCode
Comments