Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2012
    Posts
    188

    Answered: Table Constraint

    I want to add a unique constraint on 3 fields, to only allow the value in the field ONE time. The value will NEVER be the same for anything else. This is the table structure
    Code:
    Create Table Employees
    (
    	P_Id int NOT NULL,
            InstructorName varchar(255) NOT NULL,
            CourseName varchar(100) NOT NULL,
            DataTableName varchar(100) NOT NULL
    )
    I want to create a unique constraint across the fields Instructorname, CourseName, DataTableName as their is ONLY 1 instructor per course per table so those 3 fields will ALWAYS hold unique values. I think the constraint syntax would go like so, but want to check before I go butchering some sql syntax.

    constraint syntax:
    Code:
    Alter Table Employees
    Contstraint uniqueEmployee Unique (InstructorName ,CourseName ,DataTableName )
    Thanks to all!

  2. Best Answer
    Posted by Thrasymachus

    "That does not parse. Do you not have SSMS to parse your code before you post it. Or is this freehand home work...

    Code:
    Create Table Employees
    (
    	P_Id int NOT NULL,
            InstructorName varchar(255) NOT NULL,
            CourseName varchar(100) NOT NULL,
            DataTableName varchar(100) NOT NULL
    )
    
    Alter Table Employees
    add Constraint uniqueEmployee Unique (InstructorName ,CourseName ,DataTableName )
    "


  3. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    That does not parse. Do you not have SSMS to parse your code before you post it. Or is this freehand home work...

    Code:
    Create Table Employees
    (
    	P_Id int NOT NULL,
            InstructorName varchar(255) NOT NULL,
            CourseName varchar(100) NOT NULL,
            DataTableName varchar(100) NOT NULL
    )
    
    Alter Table Employees
    add Constraint uniqueEmployee Unique (InstructorName ,CourseName ,DataTableName )
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •