The Use of SQL Check Constraint

时间:2015-05-12 23:21:16

标签: sql sql-server sql-server-2008 tsql sql-server-2012

I am currently using SQL Server 2012 and am needing some help with adding a SQL Check Constraint to a column, I have an exsisting table and on a column I want the user to be able to only enter one of three things, these would be as follows :-

IEU

None IEU

The third thing is if they type in none of the 2 options above the only other things they could type in is

_ _ _ / _ _ _ _

the underscores can be either a letter or a number but must include the forward slash after the first 3 characters so the entry would have to be in this format.

In the ideal world this would be done at the application layer but I have no ability to edit this as its a specific piece of licenced software.

The only way I can think about doing this is with using the SQL Check Constraint option but I am not sure how to do this to cover all 3 scenarios.

Can anyone help. Thanks P

2 个答案:

答案 0 :(得分:2)

试试这个:

CHECK ((ColumnName = 'IEU') OR (ColumnName = 'None IEU') OR
(ColumnName LIKE '[a-z0-9][a-z0-9][a-z0-9]/[a-z0-9][a-z0-9][a-z0-9][a-z0-9]'))
);

这样可以确保只能使用字母或数字。 ' _'允许任何非保留字符。

答案 1 :(得分:1)

Try this one:

import random
import sys
import time
from pygame import *
import pygame
pygame.init()

 #------Easy-Change Variables
mouse_visibility = False
turtlespeed = 4

 #------Font
font = pygame.font.SysFont("C:/Python34/TurtleFont.ttf", 48)

 #------Theme Music & Sound Effects
pygame.mixer.music.load('C:/Python34/Jumpshot.mp3')
pygame.mixer.music.play(1, 0.0)

chomp = pygame.mixer.Sound("C:/Python34/chomp.wav")
 #------BG, Screen & Caption
pygame.display.set_caption('Turtle Trouble!')
screen = pygame.display.set_mode((1024, 616))
bg_img = pygame.image.load("C:/Python34/icebackground.png")
screen.blit(bg_img,(0,0))
pygame.display.flip()

 #------Image Preparation
turtle = pygame.image.load("C:/Python34/turtle.png")
fish = pygame.image.load("C:/Python34/fish.png")
endgame_turtle = pygame.image.load("C:/Python34/endgame_turtle.png")

 #------Movement, Coords, Collisions & Fish
turtlepos = pygame.mouse.get_pos()
pygame.mouse.set_visible(mouse_visibility)

turtle_rect = turtle.get_rect()
fish_rect = fish.get_rect()


points = 0
fish_pos_list = []

while pygame.mouse.get_pressed()[1] == False:
    time.wait(1)

while elapsed_time <= 45== True:
    elapsed_time = time.time() - game_time
    mousepos = pygame.mouse.get_pos()

    text = font.render("Points: %s" % points, True, (0, 128, 0))
    screen.blit(text, (0, 0))

    new_fish = random.randint(0, 6)
    fish_x = random.randint(0, 943)
    fish_y = random.randint(0, 552)

    if mousepos[1] - turtlepos[1] < 0:
        turtlepos[1] -= turtlespeed
    else:
        turtlepos[1] += turtlespeed

    if mousepos[2] - turtlepos[2] < 0:
        turtlepos[2] -= turtlespeed
    else:
        turtlepos[2] += turtlespeed

    screen.blit(turtle_rect,(turtlepos[1],turtlepos[2]))
    pygame.display.flip()

    if new_fish == 0:
        screen.blit(fish_rect,(fish_x, fish_y))
        start_time = time.time()
        positions = {'x':fish_x, 'y':fish_y, 'fishtimer':start_time}
        fish_pos_list.append(positions)

    for position in fish_pos_list:
        if position['fishtimer'] >= 6:
            screen.blit(bg_img, (position['x'], position['y']), pygame.Rect(position['x'], position['y'], 81, 62))
            del fish_pos_list[0]
        else:
            break

    for position in fish_pos_list:
        if turtle_rect.colliderect(fish_rect):
            screen.blit(bg_img, (position['x'], position['y']), pygame.Rect(position['x'], position['y'], 81, 62))
            chomp.play()
            fish_pos_list.remove(position)
            points += 1

    pygame.display.update()
    mainClock.tick(40)

screen.blit(endgame_turtle,(150, 0))
pygame.display.flip()
time.sleep(2.5)
sys.exit()

ALTER TABLE <YourTable> ADD CONSTRAINT CHECK_YOURTABLE_COL CHECK(Col IN('IEU' , 'None IEU') OR Col LIKE '___/____') is the column with the Col constraint