473,387 Members | 1,520 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

Hi,

I need to create a table in db2 with 3 columns say col1,col2,col3 in db2.
For col1 [datatype is char(19)] i need to add a check constraint to check whether the first 15 characters are numbers & remaining 4 characters are blank spaces. i tried it in the following way,but its not working.

create table sample (

col2 TIMESTAMP NOT NULL,
col1 CHAR(19) NOT NULL CONSTRAINT Acc_Num_Frmt LIKE '[0-9]* ',
col3 CHAR(1) NOT NULL,
primary key(col2,col1,col3)

);

can any of u suggest me a solution ASAP

Thanks,
Vidhya
Jul 30 '07 #1
2 4939
Chrisk
5
First of all, if the 15 characters must be digits, shouldn't you use a DECIMAL(15) data type instead?

However...

Add a CHECK condition to the table which converts the digits to a DECIMAL(15) and back to a CHAR(15). Then compare the resulting string to the original. You will get a conversion SQL error if they are not digits.

It is not trivial (I think):
Expand|Select|Wrap|Line Numbers
  1. alter table sample add
  2. check(substr(char(decimal(substr(col1,1,15),15,0)),1,15) = substr(col1,1,15) and col1 like '%    ')
  3.  
Good luck.
Jul 31 '07 #2
The following alternative avoids the SQL conversion error:

Expand|Select|Wrap|Line Numbers
  1. create table sample (col2 TIMESTAMP NOT NULL,
  2.                      col1 CHAR(19) NOT NULL,
  3.                      col3 CHAR(1) NOT NULL,
  4.                      primary key(col2,col1,col3));
  5.  
  6. alter table sample add
  7.       check(translate(substr(col1,1,15),'X          ',' 0123456789') = ''
  8.         and substr(col1,16,4) = '');
  9.  
  10. insert into sample values(current timestamp, '123456 89012345    ', 'X');
  11. insert into sample values(current timestamp, '123456B89012345    ', 'X');
  12. insert into sample values(current timestamp, '1234567890123456   ', 'X');
  13. insert into sample values(current timestamp, '123456789012345    ', 'Y');
Aug 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Hari Om | last post by:
Hi ORAPERTS (ORAcle xPERTS) How can I define a CHECK CONSTRAINT on a table for a field like zip code....? Here is what I am doing: create table test ( state varchar2(5) check (state...
2
by: Doug Baroter | last post by:
Hi, DDLs and DMLs: create table #job (jobID int identity(1,1) primary key, jobName varchar(25) unique not null, jobEndDate dateTime, jobComplete bit default(0), check (( is null and = 0) OR (...
1
by: Jagdip Singh | last post by:
Hi all, I have four tables create table employee (emp_num integer not null, emp_name varchar(20), emp_commision real, emp_salary real );
0
by: gnuoytr | last post by:
this is from the UDB on-line docs: CREATE TABLE EMPLOYEE (ID SMALLINT NOT NULL , NAME VARCHAR (9), DEPT SMALLINT CHECK (DEPT BETWEEN 10 AND 100), JOB CHAR...
2
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...
6
by: Susanne Klemm | last post by:
Hi! I have a table with a check constraint. But unfortunately it does not work like I wanted. CREATE TABLE MAP ( NOT NULL, NULL,
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,...
1
by: Spectre1337 | last post by:
Hello, it seems like the check constraint validation of MS SQL Server Management Studio express is horribly, horribly broken. Either that or I'm using it wrong. I hope it's the latter. I'm...
3
by: Helen Wheels | last post by:
Can we use parentheses in a check constraint in MS-SQL-server DDL? e.g. I'm having a problem with the following statement: ALTER TABLE . ADD CONSTRAINT CHECK (( IS NULL AND IS NULL) OR (...
2
by: joerg.sailer | last post by:
Hello, is it possible to define a CHECK CONSTRAINT on a table, whereas the CHECK-OPTION will be a SELECT. With this CHECK CONSTRAINT i try to ensure, that there will allways a state only set once...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.