Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Enforcing Data Integrity Across Three Columns in SQL

Posted on 2014-02-06
3
Medium Priority
?
152 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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

783 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