Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I am a beginner to C# and SQL server.

I have created these tables in SQL server.

1. Subject
-SubjectID (PK)
-SubjectName
-Mark

2. Class
-ClassNo (PK)
-HomeRoomTeacher(FK)
-StudentID(FK)

3. Subject_Class
-SubjectID (PK)
-ClassNo (PK)

4. Teacher
-TeacherID(PK)
-TeacherName
-ClassNo (only the HomeRoomTeacher has the assigned Class)

5. Student
-StudentID(PK)
-StudentName
-ClassNo(FK)

I want to input the following data to the corresponding tables.
Subjects: English, Maths, Physics, Chemistry, Geography, History & Science
Class : 5A,5B,5C,6A,6B,6C,7A,7B,7C,8A,8B,8C,....
Student : John, Mary, Kaito,........
Teacher : Mark, Jane, Rosy,......


Then I tried to input data using C# form. But the exception dialogbox says conflicts with the foreign key constraint.
How can I solve this problem?

What I have tried:

I have been told to try to input data into a table with no foreign keys first or simply don't use foreign keys in real projects. But I don't get it since it is the complete opposite of maintaining data integrity. Moreover, all the tables are linked to each other in the above database. Is there a table that I should input data first?
Posted
Updated 9-Jun-18 0:36am

1 solution

Think about it: consider an physical invoice.
Customer name
Customer address
Date
Invoice numner
Items:
   12     Widgets           $10.00     $120.00
    5     Thingumies         $1.50       $7.50

Total:                                 $127.50
When you create that, you enter the customer info, allocate an invoice number, set the date - and then start adding items to the invoice - it's the only sensible way to do it!

The electronic version would be three tables:
Customers:
ID
Name
Address
Invoices:
ID
CustomerID
Number
Date
InvoiceItems:
ID
InvoiceID
Count
Description
UnitCost
And the CustomerID and InvoiceID would be Foreign keys.
You can't create an Invoice if you don't have a Customer set up (or you don;t know where to send it!) and you can't create an InvoiceItem unless you have an Invoice to allocate it to!

So you create Customers first, so you can use the CustomerId foreign key in the Invoice, and the Invoice before the InvoiceItem so you can use the InvoiceID foreign key.

Your system is the same: Your Teachers and Subjects will be created before the Classes can be assigned, then the Students can be created and added to the system using the existing info.

Foreign keys are very useful - they help preserve data integrity by making sure you can't delete a Teacher if they are still supposed to have Classes...

But ... why does your Class reference a Student, and your Student reference a Class? Wouldn't a Student have more than one class?
 
Share this answer
 
Comments
Naga Sindhura 13-Jun-18 8:07am    
Agreed to your Explanation -
Here he/she needs to define/alter the tables structure in the following way:
1. Student table
-StudentID(PK)
-StudentName

2. Class table
-ClassID (PK)
-ClassName/Room Name

3. StudentClass - for Normalization
-StudentID(FK)
-ClassID (FK)

4. Subject - Table
-SubjectID(PK)
-StudentName

5. ClassSubject - Table
-ClassID (FK)
-StudentID(FK)

6. Teacher - Table
-TeacherID(PK)
-TeacherName

7. TeacherSubject - for normalization/mapping
-TeacherID(FK)
-SubjectID(FK)

First insert the records into following Core table:
1. Student
2. Class
3. Subject
4. Teacher
And then insert into the following mapping table:
1. StudentClass
2. ClassSubject
3. TeacherSubject

i.e. core tables first and then add the data into the reference tables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900