Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Constraints

Author  Topic 

lfmn
Posting Yak Master

141 Posts

Posted - 2003-09-17 : 11:59:08
I have a table which has a field called WBS. The field allows nulls, but other than the nulls, the value should not be repeated. I can't use a unique index because of the nulls. Any other thoughts (other than changing the table of field)?

Thanks in advance.

SQL is useful if you don't know cursors :-)

X002548
Not Just a Number

15586 Posts

Posted - 2003-09-17 : 12:01:03
Not true...you can't define a primary key..but you can have a unique index...did you try it?



Brett

8-)

SELECT @@POST=NewId()

That's correct! It's an AlphaNumeric!
Go to Top of Page

JustinBigelow
SQL Gigolo

1157 Posts

Posted - 2003-09-17 : 12:30:14
If there is already more than one null value you cannot create a unique index on a column.


create table uniquefoo
(
col1 int null,
col2 varchar(5)
)
go
insert uniquefoo values(1, 'foo1')
insert uniquefoo values(null, 'foo2')
insert uniquefoo values(null, 'foo3')
insert uniquefoo values(4, 'foo4')
go
-- alter table uniquefoo
-- add constraint myunique_constraint unique(col1)
create unique index myunique_index
on uniquefoo(col1)


Justin

"Look at it out there orbiting like it's so cool, we will rule it with an army of replicants!"
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-09-17 : 12:33:11
so maybe empty out the table to a temp location (use SELECT INTO ... syntax), add the unique index, and then re-populate?



- Jeff
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-09-17 : 13:10:42
quote:
Originally posted by lfmn

I have a table which has a field called WBS. The field allows nulls, but other than the nulls, the value should not be repeated



Uno...

quote:

SQL is useful if you don't know cursors :-)



I'd re-think the philosophy...



Brett

8-)

SELECT @@POST=NewId()

That's correct! It's an AlphaNumeric!
Go to Top of Page

lfmn
Posting Yak Master

141 Posts

Posted - 2003-09-17 : 13:11:34
quote:
Originally posted by X002548

Not true...you can't define a primary key..but you can have a unique index...did you try it?



Brett

8-)

SELECT @@POST=NewId()

That's correct! It's an AlphaNumeric!




create unique index index_name on table_name(column_name)

I did try it. Did you? If you had, you would have received an error similar to this:

Server: Msg 1505, Level 16, State 1, Line 1
CREATE UNIQUE INDEX terminated because a duplicate key was found for index ID 12. Most significant primary key is '<NULL>'.
The statement has been terminated.

I would recommend checking your facts before answering a post.

SQL is useful if you don't know cursors :-)
Go to Top of Page

lfmn
Posting Yak Master

141 Posts

Posted - 2003-09-17 : 13:14:23
quote:
Originally posted by jsmith8858

so maybe empty out the table to a temp location (use SELECT INTO ... syntax), add the unique index, and then re-populate?



- Jeff



Repopulate with the same values that would not let you create the index? When re-entering the values, you would get an error message from the index you just created because the values you are entering are not unique.

SQL is useful if you don't know cursors :-)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-09-17 : 13:20:18
First off, I was just responding to the other posts.

I feel like you really don't deserve much help, but I'll give you an idea anyway.

This effectively creates a unique constraint for all non-null values for col1:


create table uniquefoo
(
col1 int null,
col2 varchar(5),
col3 int identity,
col4 as (case when col1 is null then col3 else col1 end)
)
go

create unique index myunique_index
on uniquefoo(col4)

go

-- these all work:
insert uniquefoo values(1, 'foo1')
insert uniquefoo values(null, 'foo2')
insert uniquefoo values(4, 'foo4')

-- this works too:
insert uniquefoo values(null, 'foo3')

-- but this doesn't work:
insert uniquefoo values(4, 'foo5')


select * from uniquefoo
go
drop table uniquefoo


of course, this requires adding two columns to your table .. but it works and you'll never need to worry about the details.

- Jeff
Go to Top of Page

The Oracle
Starting Member

1 Post

Posted - 2003-09-17 : 13:22:32
Dear Ifmn,

Can you...

1. Work with an INDEXed view (that ignores NULL values)?


The Oracle has spoken.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-09-17 : 13:32:54
quote:
Originally posted by lfmn
I did try it. Did you? If you had, you would have received an error similar to this:

Server: Msg 1505, Level 16, State 1, Line 1
CREATE UNIQUE INDEX terminated because a duplicate key was found for index ID 12. Most significant primary key is '<NULL>'.
The statement has been terminated.

I would recommend checking your facts before answering a post.

SQL is useful if you don't know cursors :-)



What does that have to DO with your ORIGINAL post?

Totally different!

GOOD LUCK



Brett

8-)

SELECT @@POST=NewId()

That's correct! It's an AlphaNumeric!
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-09-17 : 13:36:04
uhh .. Brett .... he did what you told him to do and reported back the error. the error is consistent with his original post, where he said there are multiple Null values in a column, but he wants a unique index on all non-null values.

- Jeff
Go to Top of Page

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2003-09-17 : 13:37:04


Forum Score board

1 sack Slap for Brett



Jim
Users <> Logic
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-09-17 : 14:00:07
quote:
Originally posted by jsmith8858

uhh .. Brett .... he did what you told him to do and reported back the error. the error is consistent with his original post, where he said there are multiple Null values in a column, but he wants a unique index on all non-null values.

- Jeff



Not...going...to...get...in...to..this...

too much

quote:

I have a table which has a field called WBS. The field allows nulls, but other than the nulls, the value should not be repeated. I can't use a unique index because of the nulls.



He can't use a unique index because of (multiple) nulls?

quote:

I would recommend checking your facts before answering a post.



Post immediatley after I asked him if he tried it...

quote:

I feel like you really don't deserve much help,



Jeff get's in to the act

quote:

Repopulate with the same values that would not let you create the index? When re-entering the values, you would get an error message from the index you just created because the values you are entering are not unique.



The reason for Jeff's anoyance

ahh..we seem to talking apples and oranges now...

quote:

1 sack Slap for Brett



Jim: Huh...anyway, I'm Sure the score is a lot higher

Hey the board is suppose to be fun and eductaional..at least for me.







Brett

8-)

SELECT @@POST=NewId()

That's correct! It's an AlphaNumeric!
Go to Top of Page

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2003-09-17 : 14:08:18
Just figured I had to Give you the bussness. LOL

Im celibrateing, Got the PCS on line.

Jim
Users <> Logic
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-09-17 : 14:31:40
lfmn -- are you still with us? did you see my solution?

- Jeff
Go to Top of Page

lfmn
Posting Yak Master

141 Posts

Posted - 2003-09-17 : 17:38:41
quote:
Originally posted by jsmith8858

First off, I was just responding to the other posts.

I feel like you really don't deserve much help, but I'll give you an idea anyway.



- Jeff



If you don't feel like I deserve much help, next time don't bother. I'm not sure what I did to deserve your attitude. Your first solution - <so maybe empty out the table to a temp location (use SELECT INTO ... syntax), add the unique index, and then re-populate>

If The unique index wouldn't work with the data in the table, I don't understand why you would be able to load the data back into the table with the index on. The point is that there are multiple null values which preclude a unique index.

If it offends you that I pointed this out, so be it.

I saw your solution, but I wound up using a trigger on the table that checks for duplicates and then rolls back the transaction if there are duplicate values.

SQL is useful if you don't know cursors :-)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-09-17 : 18:09:08
I WAS RESPONDING TO THE PREVIOUS POSTS ABOUT DATA ALREADY BEING THERE. as I said. sorry you missed it. i just combined what Brett said and what Justin said and threw something up there. i apologize for trying to help you. Did you see i put the word "maybe" and a question mark?

What offends me is what you said to Brett. I love how people here ask for help and give people shit when they try to help you.

ARRGGGGHHHH ... sorry had to vent. feel better now. Good luck out there, kid.

- Jeff
Go to Top of Page

lfmn
Posting Yak Master

141 Posts

Posted - 2003-09-18 : 18:59:11
quote:
Originally posted by jsmith8858



What offends me is what you said to Brett. I love how people here ask for help and give people shit when they try to help you.

ARRGGGGHHHH ... sorry had to vent. feel better now. Good luck out there, kid.

- Jeff



First of all SONNY BOY, I'm 46, not a kid.

Second, Brett shot from the hip and gave an incorrect answer. It would have taken him all of 1 minute to do a quick check, but apparently that was too much trouble. If he was really trying to help he would have been more careful. Fortunately I have been writing SQL code for 7+ years so I'm able to separate the wheat from the BS. Unfortunately, there are a lot of people out there that depend on the answers they receive from this forum. If this question had been posted by someone with little experience, they could have wasted hours of their time trying to figure out what they were doing incorrectly, when actually they were right!

I love your answers. You give examples that people can cut and paste to run the code. It gives the person an example, and you know that your answer is correct. Whenever I answer a post, I ALWAYS check my answer first.

This is not aimed at you Jeff - My pet peeve is people who post just to read their brilliance on the internet. If you don't know the answer, DON'T POST! There is no shame in not knowing everything, but don't screw up someone else just to try and make yourself look like a big man.

I used to read this forum every day a few years ago. I was away for a long time and I recently posted a few questions and got lots of posts, but few answers.

SQL is useful if you don't know cursors :-)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-09-18 : 22:40:49
sorry but this is really upsetting stuff to read:

quote:

Second, Brett shot from the hip and gave an incorrect answer. It would have taken him all of 1 minute to do a quick check, but apparently that was too much trouble. If he was really trying to help he would have been more careful.
...
I used to read this forum every day a few years ago. I was away for a long time and I recently posted a few questions and got lots of posts, but few answers.
...
If you don't know the answer, DON'T POST! There is no shame in not knowing everything, but don't screw up someone else just to try and make yourself look like a big man.



People should not be discouraged from helping others in this forum. if your approach is "here's my question but don't you dare GUESS, I need the perfect answer, or don't bother, and you better have it all tested and worked out for me", then you really are in the wrong place.

MANY solutions have been uncovered by "shooting from the hip" and "wild guesses".

and then you say ...

quote:

My pet peeve is people who post just to read their brilliance on the internet



if someone guesses -- they are wasting your time. if it is a perfect answer, then you feel they are just showing off.

and to top it all off, in your very first post, here's how you ended it (read carefully):

quote:

Any other thoughts?



Sounds like you wanted to have an intelligent discussion and have people give their opinions, thoughts, and ideas.

Next time, and I am not joking or being sarcastic about this, you should phrase your question this way:

Does anyone have A FULLY tested and PERFECT solution that will solve my problem? Don't waste my time with guesses and if your solution isn't right or you don't have an answer do NOT waste my time and respond to this thread.

And that's fine ... just be clear with people. But do NOT ask for people's thoughts and then criticize them when they are nice enough to comply.

- Jeff
Go to Top of Page

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2003-09-18 : 23:34:17
Ignore him Jeff

lfmn has been an asshole here for a few years, I don't think he will change now.



Damian
Go to Top of Page

lfmn
Posting Yak Master

141 Posts

Posted - 2003-09-19 : 09:18:17
quote:
Originally posted by Merkin

Ignore him Jeff

lfmn has been an asshole here for a few years, I don't think he will change now.



Damian



Damian:

I see that when you lack an intelligent argument, you are at least able to revert to grammer school behavior, fall back on name calling and availing your self of the anonimity of the internet. When (or should I say if) you grow up, you'll understand that the same manners should apply on line that apply in person and I know that you would be afraid to call someone an asshole to their face.

Sorry that it upsets you so that I expect people to take the same amount of time I do before posting an answer.

Rather than get into a battle of wits with a defenseless man, I'll not waste my time on this forum any more.

Here's to hoping you grow up soon.

LFMN

SQL is useful if you don't know cursors :-)
Go to Top of Page
    Next Page

- Advertisement -