Solved

Enforcing Data Integrity Across Three Columns in SQL

Posted on 2014-02-06
3
146 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

752 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