Solved

Enforcing Data Integrity Across Three Columns in SQL

Posted on 2014-02-06
3
137 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article I will describe the Copy Database Wizard 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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now