I am new to SQL Server. I have created a DB in Access and defined my relationships there. After this, I have used SQL Server Import and Export Data tool to convert the DB and use in SQL Server. My table structure is as follows:
Student Course Course-Module Module
------------- ------------- ------------- -------------
TagID (PK) CourseID (PK) CourseID (FK) ModuleID (PK)
StudentName CourseName ModuleID (FK) ModuleName
CourseID
Module-Session Session Atendance
-------------- -------------- --------------
ModuleID (FK) SessionID (PK) TagID (FK)
SessionID (FK) SessionName SessionID (FK)
SessionDate ScanningTime
SessionTimeStart
SessionTimeEnd
I have developed a query that will find a current SessionID
for the given TagID
insert the result into Attendance
table along with the time of insertion.
My query is (VS C# syntax)
string query = @"INSERT INTO Attendance " +
"SELECT s.TagID, se.SessionID, " +
" @todaysDate " +
" AS ScanningTime " +
" FROM (((Student s " +
" LEFT JOIN [CourseID-ModuleID] cm ON s.CourseID = cm.CourseID) " +
" LEFT JOIN [ModuleID-SessionID] ms ON cm.ModuleID = ms.ModuleID) " +
" LEFT JOIN [Session] se ON ms.SessionID = se.SessionID) " +
" WHERE s.TagID = @tagNo " +
" AND se.SessionDate = cast(getdate() as date) " +
" AND se.SessionTimeStart <= @Plus30Min " +
" AND se.SessionTimeEnd >= @Plus30Min ";
Query runs, and produce expected results and inserts them to Attendance
table.
My problem is that I would like to make fields TagID
and SessionID
of Attendance
table to be unique, i.e I want one student to register for particular session only ONCE, not multiple times as it is possible now.
EDIT:
An example of my problem. Student scans a tag and the result is placed in Attendance table:
Attendance
--------------------------------------
TagID SessionID ScanningTime
4820427 Test1 2013-08-13 15:10:02.590
After a few second, student tries to scan again, and the result is added as new record
Attendance
--------------------------------------
TagID SessionID ScanningTime
4820427 Test1 2013-08-13 15:10:02.590
4820427 Test1 2013-08-13 15:10:09.103
This is want I want to avoid. I just want to allow the student to scan ONCE (So it should allow only for one record with combination of 4820427 and Test1). If he will try to do it again, the error message should pop up.