Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

How Can I Force Specific Values in a Column

$
0
0
I am building a test database to understand how constraints / triggers / & foreign keys work...

I have a table called 'zooproducts' and in that table, I have:

Code:

zoo=# \d zooproducts
                                Table "public.zooproducts"
 Column  |          Type          |                        Modifiers
---------+------------------------+----------------------------------------------------------
 id      | integer                | not null default nextval('zooproducts_id_seq'::regclass)
 model  | character varying(20)  | not null
 color  | character varying(10)  | not null
 size    | smallint              | not null
 comment | character varying(100) |
Indexes:
    "zooproducts_pkey" PRIMARY KEY, btree (id)
    "zooproducts_model_key" UNIQUE CONSTRAINT, btree (model)

Now mainly trying to focus on size of each board. I don't want someone adding in bogus values for 'size' field. I want to make sure that the sizes are only of those in which I offer:

34", 35", 36", 38", 40", 42", 44", & 46" long decks.

Now how can I enforce that only valid values are input into the table. I don't want someone entering 200" deck since that value is obviously bogus. Can someone please help me engineer a way I can enforce the available size options above when entering values into my 'public.zooproducts.size' field?

Not sure if I need a trigger (which I've never used or done before) or if I should create a separate table called 'zoosizes':

Code:

zoo=# \d zoosizes
                          Table "public.zoosizes"
 Column |  Type  |                      Modifiers
--------+----------+-------------------------------------------------------
 id    | integer  | not null default nextval('zoosizes_id_seq'::regclass)
 sizes  | smallint | not null
Indexes:
    "zoosizes_pkey" PRIMARY KEY, btree (id)

zoo=# SELECT * FROM zoosizes ORDER BY id;
 id | sizes
----+-------
  1 |    34
  2 |    35
  3 |    36
  4 |    37
  5 |    38
  6 |    40
  7 |    42
  8 |    44
  9 |    46
(9 rows)

and then make some kind of FOREIGN KEY from zooproducts.size > zoosizes.sizes.

Can someone please recommend the best / easiest way to engineer a simple problem in ANSI SQL on how I can force customers / users to enter the correct sizes for my decks that exist versus a value of -32768 to +32767.

Thank you so much!

Viewing all articles
Browse latest Browse all 13329

Trending Articles