473,756 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unique constraint or index as in Oracle and "alter table alter column"

Is there any way I can define an Unique constraint or unique index
which allows more than one null values for the same column combination
in DB2?

ie, If my index is defined on (col3, col4) where both columns allow
nulls, I want col3 + col4 to be unique, if one or both the columns
have values. If both columns have nulls, it should allow more than one
such rows.

ex,

1 null -> unique
2 null -> unique
2 null -> duplicate --> reject it
null null -> unique
null null -> unique --> accept it (both values are nulls)
null 1 -> unique.

Also, is there any way to alter a INT/DEC (number) column from
NULLABLE to NOT NULLABLE and vice versa?

thanks,
Nov 12 '05 #1
3 22640
Given:

create table testuni (col3 int, col4 int);

create unique index test1 on testuni (col3, col4);

....these two statements will fail:

2nd iteration of: 2 null -> duplicate --> reject it - as you said
2nd iteration of: null null -> duplicate - rejected with SQL0803N
"One or more values in the INSERT statement, UPDATE ..."

After a table is created, you can only turn nullability on/off with a
constraint:

alter table testuni add constraint testnull check (col3 is not null)

This is not exactly the same as defining a column not null (for example,
the constraint above will not a primary key to defined against col3)

Prince Kumar wrote:
Is there any way I can define an Unique constraint or unique index
which allows more than one null values for the same column combination
in DB2?

ie, If my index is defined on (col3, col4) where both columns allow
nulls, I want col3 + col4 to be unique, if one or both the columns
have values. If both columns have nulls, it should allow more than one
such rows.

ex,

1 null -> unique
2 null -> unique
2 null -> duplicate --> reject it
null null -> unique
null null -> unique --> accept it (both values are nulls)
null 1 -> unique.

Also, is there any way to alter a INT/DEC (number) column from
NULLABLE to NOT NULLABLE and vice versa?

thanks,


Nov 12 '05 #2
Thanks Blair for the update!

Blair Adamache <ba*******@2muc hspam.yahoo.com > wrote in message news:<bp******* ***@hanover.tor olab.ibm.com>.. .
Given:

create table testuni (col3 int, col4 int);

create unique index test1 on testuni (col3, col4);

...these two statements will fail:

2nd iteration of: 2 null -> duplicate --> reject it - as you said
2nd iteration of: null null -> duplicate - rejected with SQL0803N
"One or more values in the INSERT statement, UPDATE ..."

After a table is created, you can only turn nullability on/off with a
constraint:

alter table testuni add constraint testnull check (col3 is not null)

This is not exactly the same as defining a column not null (for example,
the constraint above will not a primary key to defined against col3)

Prince Kumar wrote:
Is there any way I can define an Unique constraint or unique index
which allows more than one null values for the same column combination
in DB2?

ie, If my index is defined on (col3, col4) where both columns allow
nulls, I want col3 + col4 to be unique, if one or both the columns
have values. If both columns have nulls, it should allow more than one
such rows.

ex,

1 null -> unique
2 null -> unique
2 null -> duplicate --> reject it
null null -> unique
null null -> unique --> accept it (both values are nulls)
null 1 -> unique.

Also, is there any way to alter a INT/DEC (number) column from
NULLABLE to NOT NULLABLE and vice versa?

thanks,

Nov 12 '05 #3
AK
For example, in a police database, column VEHICLE.VIN could be NULL
(which makes sence, let's consider a situation when witnesses describe
a hit-and-run accident, then
number plate and/or VIN may be not known). How to implement a
requirement that if VIN is known, it has to be unique?

I've created a separate entity for VIN and removed VIN column from
VEHICLE table
CREATE TABLE VEHICLE(VEHICLE _ID INT NOT NULL PRIMARY KEY, ... );
CREATE TABLE VIN(VEHICLE_ID INT NOT NULL PRIMARY KEY, VIN VARCHAR(30)
UNIQUE);

The technique is called inheritance of primary key
Nov 12 '05 #4

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

Similar topics

1
7252
by: DB_2 | last post by:
Greetings, I was searching Google for ways to turn off transaction logging for some queries. I came across this old post from Feb 2003: > From: fareeda (fareeda@pspl.co.in) > Subject: Re: Question related to "CREATE TABLE AS SELECT" in DB2 > Newsgroups: comp.databases.ibm-db2 > Date: 2003-02-27 21:11:59 PST >
0
3402
by: David Garamond | last post by:
postgresql 7.4.0, redhat 7.3 (under vmware 4.0 on win2k) Windows crashed and some of the files on Redhat got corrupted, including some files in /var/lib/pgsql/data/pg_xlog/. When I tried to start postmaster, it fails with message "Invalid primary checkPoint record". I think it was trying to look for files named "0000000000000021" but only files named "0000000000000022" through "0000000000000027" were there. So I ran pg_resetxlog (this is...
2
3242
by: borajetta | last post by:
ERROR: duplicate key violates unique constraint "pg_class_oid_index" I found a post earlier but wanted to post up my info as the other persons did not look like the same type of error: I tried to cluster the isbn_table cluster isbn_index_code on isbn_table; ERROR: duplicate key violates unique constraint "pg_class_oid_index" The table looks like this
17
4721
by: Darek | last post by:
Hi, I have a table, something similar to: create table my_table ( id char(32) not null primary key, num integer not null, code varchar(2) not null, name varchar(60) not null,
9
18561
by: Tomba | last post by:
I have a really annoying problem, which i tried solving for almost 3 days now. (I googled in any variation i could think of, but i couldn't find anything that will help my stupid me understand:\) I'm trying to use a ALTER TABLE ALTER COULMN to change the datatype of a certain column in a table i'm importing by code from an .xls to my access database. I'll cut to the chase and paste the code: Option Compare Database Option Explicit ...
2
11926
by: amrhi | last post by:
Hello Guys,....! Can you help me how to export my table to excell using php code ..? thanks alot
0
5429
by: s.amann | last post by:
Hello, I get the following error when I try to insert data into a table. "SQL1477N Table "<table-name>" cannot be accessed. " The table was created with NOT LOGGED INITIALLY The import is done by a program written in C and using embedded SQL. It starts with ALTER TABLE myTable ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE. Then I prepare the statement and the inserts are done within a loop
6
4234
by: Widge | last post by:
I can't believe it but I don't have a clue how to update a table using VBA! What I'm trying to do is: 1) Do a search on the last 3 periods turnover for one client in particular and check to see if they are all over 60k 2) Go to a table which states rebate rates for each client and change that one clients rate based on whether they are over or under 60k for the 3 months I'm assuming for part 1, I'd have to set up a 3 record array and...
3
7256
by: haiminnu | last post by:
I have created Two tables 1]EmployeeTable -------------------------- EmpID EmpName AccessLevelID 2]AccessLevelTable -----------------------------
0
9275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.