Results 1 to 2 of 2

Thread: creating tables

  1. #1
    Join Date
    Sep 2003
    Location
    Sweden
    Posts
    19

    Unanswered: creating tables

    Wondering if I while I create a table I can choose certain values a field can have?
    Ex.
    CREATE TABLE test
    (
    Subject_code char(20) NOT NULL,
    Student_id char(20) NOT NULL,
    Result GOOD or BAD or SUPERGOOD (this is just an example)
    )

    If a field can have 2 values I can use bit (boolean). But is there a way to do this with several values?

  2. #2
    Join Date
    Sep 2003
    Location
    Portland, Oregon
    Posts
    11

    Re: creating tables

    Normally you would use a check constraint. Like this:
    Code:
    CREATE TABLE dbo.ExampleTable (
    	EntryID	int             NOT NULL    IDENTITY(1,1),
    	Result	varchar(32)	NOT NULL
    	CONSTRAINT ExampleTable_Result_CK CHECK (
    		Result IN ('GOOD', 'BAD', 'SUPERGOOD'))
    )
    
    INSERT INTO ExampleTable (Result) VALUES ('GOOD')
    INSERT INTO ExampleTable (Result) VALUES ('BAD')
    INSERT INTO ExampleTable (Result) VALUES ('UGLY')
    
    SELECT * FROM ExampleTable
    Originally posted by buggirl
    Wondering if I while I create a table I can choose certain values a field can have?
    Ex.
    CREATE TABLE test
    (
    Subject_code char(20) NOT NULL,
    Student_id char(20) NOT NULL,
    Result GOOD or BAD or SUPERGOOD (this is just an example)
    )

    If a field can have 2 values I can use bit (boolean). But is there a way to do this with several values?

Posting Permissions

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