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 > Constraints in Tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #16 (permalink)  
Old 11-12-08, 10:54
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally Posted by r937
i think you'll find that MOD, LEN, and MID are not ANSI SQL functions
Fair enough - I was misled by a Google result on "ANSI SQL" leading me to w3schools, which I took to be documenting ANSI SQL.

But my question is - doesn't ANSI SQL have anything equivalent to these functions? I have no idea, just kind of expected it would. After all, Joe Celko must need to write check constraints to verify his VINs and whatnot? In fact, having now Googled for "celko check substring" and found this article it appears to be:
Code:
( SUBSTRING (person_Number FROM 1 FOR 1) = 'P'
AND SUBSTRING (person_Number FROM 2 FOR 1) BETWEEN '0' AND '9'
AND SUBSTRING (person_Number FROM 3 FOR 1) BETWEEN '0' AND '9'
AND SUBSTRING (person_Number FROM 4 FOR 1) BETWEEN '0' AND '9'
AND SUBSTRING (person_Number FROM 5 FOR 1) BETWEEN '0' AND '9'
AND SUBSTRING (person_Number FROM 6 FOR 1) BETWEEN '0' AND '9'
)
We don't need to test the maximum length, since the column type does that.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #17 (permalink)  
Old 11-12-08, 11:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
that's much nicer

yeah, a lot of people are misled by stuff they find on w3schools

and that forensic article by celko is a classic

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #18 (permalink)  
Old 11-12-08, 17:38
Grich Grich is offline
Registered User
 
Join Date: Aug 2008
Location: Australia
Posts: 9
Thanks andrewst for the research. Just one thing (it's going to sound lame): Were do you place this piece of code in the database script?

Quote:
Originally Posted by andrewst
Code:
( SUBSTRING (person_Number FROM 1 FOR 1) = 'P'
AND SUBSTRING (person_Number FROM 2 FOR 1) BETWEEN '0' AND '9'
AND SUBSTRING (person_Number FROM 3 FOR 1) BETWEEN '0' AND '9'
AND SUBSTRING (person_Number FROM 4 FOR 1) BETWEEN '0' AND '9'
AND SUBSTRING (person_Number FROM 5 FOR 1) BETWEEN '0' AND '9'
AND SUBSTRING (person_Number FROM 6 FOR 1) BETWEEN '0' AND '9'
)
Reply With Quote
  #19 (permalink)  
Old 11-12-08, 18:05
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
I'm not sure that this would work (I don't have an ISO compliant SQL that I can test with), but how about:
Code:
person_number BETWEEN 'P00000' AND 'P99999'
Note that this still does zilch to enforce sequence. That problem I don't know how to fix using strictly standard SQL.

-PatP
Reply With Quote
  #20 (permalink)  
Old 11-12-08, 18:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
pat, that constraint would allow P10SXR

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #21 (permalink)  
Old 11-13-08, 04:45
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally Posted by Grich
Thanks andrewst for the research. Just one thing (it's going to sound lame): Were do you place this piece of code in the database script?
Something like:
Code:
CREATE TABLE person
(
person_Number VARCHAR(6) PRIMARY KEY,
person_Name VARCHAR(50) NOT NULL,
person_UserName VARCHAR(20) NOT NULL,
person_Password VARCHAR(15) NOT NULL,
constraint person_number_chk check
  ( SUBSTRING (person_Number FROM 1 FOR 1) = 'P'
  AND SUBSTRING (person_Number FROM 2 FOR 1) BETWEEN '0' AND '9'
  AND SUBSTRING (person_Number FROM 3 FOR 1) BETWEEN '0' AND '9'
  AND SUBSTRING (person_Number FROM 4 FOR 1) BETWEEN '0' AND '9'
  AND SUBSTRING (person_Number FROM 5 FOR 1) BETWEEN '0' AND '9'
  AND SUBSTRING (person_Number FROM 6 FOR 1) BETWEEN '0' AND '9'
  )
)
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #22 (permalink)  
Old 11-13-08, 06:16
Grich Grich is offline
Registered User
 
Join Date: Aug 2008
Location: Australia
Posts: 9
Thanks andrewst . I'll try it out.
Reply With Quote
  #23 (permalink)  
Old 11-13-08, 08:48
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You can use GENERATED ALWAYS AS IDENTITY to come up with the number. (This is in ISO/IEC 9075-02:2003 in subclause 11.4, "<column definition>".) Then you build a view on top of the base table and in the view, you convert the number to a string and prepend the 'P'.

p.s: MOD is ISO SQL (which ANSI adopted as its standard, too) - but for a completely different purpose.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #24 (permalink)  
Old 12-05-08, 16:31
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by Pat Phelan
CONSTRAINT user_type LIKE 'P[0-9][0-9][0-9][0-9][0-9]'
Actually, this *is* (close to) an ANSI SQL standard, namely the SQL2003 standard for regular expressions in the new "SIMILAR" predicate:

Code:
CONSTRAINT user_type SIMILAR TO 'P[0-9][0-9][0-9][0-9][0-9]'
or slightly shorter:
Code:
CONSTRAINT user_type SIMILAR TO 'P[0-9]{5}'
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 12-05-08 at 16:34.
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