If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Force Case Sensitive Values?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-05-12, 16:03
CarlosinFL CarlosinFL is offline
Registered User
 
Join Date: Oct 2010
Location: Orlando, FL
Posts: 184
Question Force Case Sensitive Values?

I have a table called 'computers' as shown below:

Code:
holyghost=# \d computers
                                Table "public.computers"
 Column |         Type          |                       Modifiers                        
--------+-----------------------+--------------------------------------------------------
 id     | integer               | not null default nextval('computers_id_seq'::regclass)
 make   | character varying(20) | not null
 model  | character varying(40) | not null
 owner  | character varying(40) | not null
 price  | numeric(7,2)          | not null
Indexes:
    "computers_pkey" PRIMARY KEY, btree (id)
Is it possible in SQL to specify that the 1st character of the input value in the field 'make' be capital and not lower case? I'm trying to prevent users from entering data like dell, DELL, DeLl, DEll but only 'Dell'. I don't know if this is even possible considering they're all legitimate values for VARCHAR.
Reply With Quote
  #2 (permalink)  
Old 01-05-12, 17:22
Pat Phelan Pat Phelan is online now
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
The intuitive answer would be to create a table to store the valid values for Make and to put a foreign key from it into the computers table. This is by far and away the best practice for this kind of problem.

Knowing that PostgreSQL is your tool of choice, I can offer PostgreSQL Collation as a way to solve this specific problem.

The ISO Standard does define a way to deal with collation issues, which is documented by Joe Celko in SQL For Smarties Advanced SQL Programming, but other than Mimer I don't know of any SQL implementation that uses it.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #3 (permalink)  
Old 01-05-12, 17:49
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
If you want to do that in order to make searches more flexible you can either use the collation solution provided by Pat, or search by applying e.g. a lower() function:
Code:
where lower(make) like 'dell%'
Note that you will need to create a function based index to make that search fast.

Probably the best (in terms of user experience solution) would be to use PostgreSQL's full text search capabilities.
Reply With Quote
  #4 (permalink)  
Old 01-09-12, 02:42
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 68
To stop unwanted values using a check constraint:
create table computers (make character varying(20) collate english_cs,
constraint make_capitalized
check (make = upper(substring(make from 1 for 1)) || lower(substring(make from 2))))

(where english_cs is i case sensitive collation)

But I'd rather use a trigger which converts the input value at insert, so whatever case given is stored as Dell.

And, as Pat Phelan says, a foreign key to a computer brands table is a good idea.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On