Solved

Enforcing Data Integrity Across Three Columns in SQL

Posted on 2014-02-06
3
144 Views
Last Modified: 2014-02-20
I have a bit of a question regarding data integrity on a table I'm creating. For reference, here is the code to create the table:

CREATE TABLE tbl_Course_ExamModules
	(
		CourseExamModuleID INT IDENTITY(1,1) NOT NULL,
		CourseCode VARCHAR(3) NOT NULL,
		ExamModule VARCHAR(10) NOT NULL,
		CashInCode BIT NULL,
		AcademicYear INT NOT NULL,
		StudentLevel INT NOT NULL,
		CourseModuleCount INT NOT NULL
	)
	
	
ALTER TABLE tbl_Course_ExamModules
	ADD CONSTRAINT pk_Course_ExamModules_CourseExamModuleID 
		PRIMARY KEY CLUSTERED (CourseExamModuleID)

CREATE UNIQUE NONCLUSTERED INDEX idx_CourseExamModules_Code_Module_Year
	ON tbl_Course_ExamModules (CourseCode, ExamModule, AcademicYear)

Open in new window


Which is all fine and fairly straight forward. Each module can only be associated with each course once for each academic year.
Then I start to struggle. For each course there is a module which is called the "Cash-In Code" Each course can only have one cash-in code for each Student Level. There can be multiple modules which are not cash-in codes for each course within an academic year and student level. So, I can't place a unique index on the CashInCode, CourseCode, AcademicYear, StudentLevel columns the CashInCode column can have the value 0 more than once.
I hope that makes sense!
I looked at adding a WITH CHECK Constraint, but I don't think they can be applied the way I want (or can they?). Is there a solution to this without using a trigger?
0
Comment
Question by:Karl_mark
  • 2
3 Comments
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 39838533
Hi Karl_Mark,
It is possible to add a constraint which references a function - e.g.
CONSTRAINT chkCheckAge CHECK(dbo.fnGetAge(DateOfBirth) >= 18)

so the use of a function in a CHECK constraint may open up some possibilities for your scenario
0
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 500 total points
ID: 39838547
Hi Karl_Mark,
As a follow on from my previous post, something like the below may be an option:

CREATE FUNCTION CheckThatCourseHasOnlyOneCashInCode(@Course_code varchar(3), @StudentLevel int)
RETURNS bit
AS
BEGIN
    DECLARE @count int
    SELECT @count = COUNT(*) FROM dbo.tbl_Course_ExamModules WHERE CourseCode = @course_code AND StudentLevel = @StudentLevel AND CashInCode = 1
    RETURN @count <= 1
END;


ALTER TABLE dbo.tbl_Course_ExamModules CONSTRAINT OneCashInCodePerCourse CHECK (dbo.CheckThatCourseHasOnlyOneCashInCode(CourseCode, StudentLevel));

Open in new window

0
 

Author Closing Comment

by:Karl_mark
ID: 39872962
Thanks Barry. Very close to the solution I came up with. I hadn't thought of using a function to do the job!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question