473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

check constraint on char field

I have a field in my pgsql 7.4.2 table that is char(6) type. This holds
an ID that contains only numbers, but must be six characters in length.
First two chars are the year followed by a sequential number. For
instance, a number entered for the time this year would be '040001'. I
was trying to setup a check constraint to make sure only number were
used, this is what I have, but it is allowing alphanumerics:

projectno >= '000000' AND projectno <= '999999'

Or maybe just a trigger to create the number itself would be better?

--
Robert
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #1
2 3306
On Wed, 2004-06-09 at 10:42, Robert Fitzpatrick wrote:
I have a field in my pgsql 7.4.2 table that is char(6) type. This holds
an ID that contains only numbers, but must be six characters in length.
First two chars are the year followed by a sequential number. For
instance, a number entered for the time this year would be '040001'. I
was trying to setup a check constraint to make sure only number were
used, this is what I have, but it is allowing alphanumerics:

projectno >= '000000' AND projectno <= '999999'

Or maybe just a trigger to create the number itself would be better?


You've chosen the wrong type. char(6) will ALWAYS be 6 characters long,
no matter how short a string you try to put it in. I.e. if you insert
'abc' it will become 'abc ' on insert. You also need to cast. Try
this:

smarlowe=> create table test (id varchar(6) check (id::text::int>0 and
id::text::int<1000000 and length(id)=6));
CREATE TABLE
smarlowe=> insert into test values ('6');
ERROR: new row for relation "test" violates check constraint "test_id"
smarlowe=> insert into test values ('601');
ERROR: new row for relation "test" violates check constraint "test_id"
smarlowe=> insert into test values ('abc123');
ERROR: invalid input syntax for integer: "abc123"
smarlowe=> insert into test values ('000123');
INSERT 17263 1

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #2
On Wed, 2004-06-09 at 10:42, Robert Fitzpatrick wrote:
I have a field in my pgsql 7.4.2 table that is char(6) type. This holds
an ID that contains only numbers, but must be six characters in length.
First two chars are the year followed by a sequential number. For
instance, a number entered for the time this year would be '040001'. I
was trying to setup a check constraint to make sure only number were
used, this is what I have, but it is allowing alphanumerics:

projectno >= '000000' AND projectno <= '999999'

Or maybe just a trigger to create the number itself would be better?


You've chosen the wrong type. char(6) will ALWAYS be 6 characters long,
no matter how short a string you try to put it in. I.e. if you insert
'abc' it will become 'abc ' on insert. You also need to cast. Try
this:

smarlowe=> create table test (id varchar(6) check (id::text::int>0 and
id::text::int<1000000 and length(id)=6));
CREATE TABLE
smarlowe=> insert into test values ('6');
ERROR: new row for relation "test" violates check constraint "test_id"
smarlowe=> insert into test values ('601');
ERROR: new row for relation "test" violates check constraint "test_id"
smarlowe=> insert into test values ('abc123');
ERROR: invalid input syntax for integer: "abc123"
smarlowe=> insert into test values ('000123');
INSERT 17263 1

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: RAD | last post by:
I am working with an evaluation copy of SQL Server 2000 for the first time; my DB experience lies with MS Access. I have a simple table in SQL Server (tblCompany) that has a field called...
3
by: Mary Walker | last post by:
Hi, I am new to database development and am writing a database as part of a university course I have created a table as below called CableWire - the table is created ok. CREATE TABLE...
1
by: Dmitry Martynov | last post by:
Hi I have a question whether XmlValidatingReader doesn't check keyref constrain (the same with key constraint) or I do smth wrong. I have the following schema <?xml version="1.0"...
2
by: kaming | last post by:
Dear all, Is it possible to define a check constraint on a field that the field value must exist in another table non-unique field? how to write the create table DDL? i.e TABLE A TYPE,...
0
by: Robert Fitzpatrick | last post by:
I have a field in my pgsql 7.4.2 table that is char(6) type. This holds an ID that contains only numbers, but must be six characters in length. First two chars are the year followed by a sequential...
3
by: ferg | last post by:
I have a Customer table. The table has two different CHECK constraints. Then there is the Customer details dialog, which provides the user with an UI for changing users. I have some UPDATE sql,...
5
by: new | last post by:
Sorry to bug you with what is a simple question but if you could point me in the right direction I can follow up. I have a sql table field we can call XXX it is the least significant of a four...
2
by: vidhyat | last post by:
Hi, I need to create a table in db2 with 3 columns say col1,col2,col3 in db2. For col1 i need to add a check constraint to check whether the first 15 characters are numbers & remaining 4...
1
by: thesti | last post by:
hi, i have a field named 'nim', in my table which is a char(10) field. i want to create a constraint that checks the field so the field must be 10 characters long and each character is a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.