Check contraints

I have a primary key (client id) of five characters which contains:

      character # 1: A-Z      character # 2: 0-9      character # 3: 1-9      character # 4: blank or a-z      character # 5: blank or a-z

There’s no problem with the first three characters. Char 4 can be blank or a-z. Char 5 can be blank or a-z but cannot contain a-z if character 4 is blank. Do you have any suggestions?

A check constraint can be easily coded to your specifications using the LIKE keyword. Note that for comparison purposes, trailing blanks in a character field are not used. So “ABC” is equal to “ABC “

Here’s the code and the test data I used. The third insert causes an error.

use scratchgoif exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = 'dbo' and TABLE_NAME =		'checktest')-- INFORMATION_SCHEMA is a ver. 7 feature-- use sysobjects in 6drop table dbo.checktestgocreate table dbo.checktest(  sec_code char(5),  constraint chk_valid_code check  (sec_code like '[A-Z][0-9][1-9]' 		   or sec_code like '[A-Z][0-9][1-9][a-z][a-z]') )go insert into checktest values('A01')insert into checktest values('A01az')insert into checktest values('A01 z')
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS