Showing posts with label col2. Show all posts
Showing posts with label col2. Show all posts

Thursday, March 22, 2012

can unique index be created if the include col makes the index unique?

create index idx_tab1_col1col2

on dbo.tab1([col1])

include([col2])

on tab1_index

My question is this. I require this index to be created. col1 doesn't have a lot of unique values but col2 is our pk and adding col2 to the index makes it unique. Can I make this a unique index then? Also, can someone explaing me a bit as to why it's possible or y not? Thank you very much for your help.

I dare say that you could have created the index as unique in less time than it took to navigate to here and write the question. But you didn't so let's try it together.

Create Table dbo.tab1 (Col1 varchar(5) null, Col2 int not null primary key)

Insert Into dbo.tab1
Select 'aaaaa', 1
Union
Select 'bbbbb', 2
Union
Select 'ccccc', 3
Union
Select 'ccccc', 4
Union
Select 'ddddd', 5

create unique index idx_tab1_col1col2
on dbo.tab1([col1])
include([col2])

Msg 1505, Level 16, State 1, Line 2
CREATE UNIQUE INDEX terminated because a duplicate key was found for object name 'dbo.tab1' and index name 'idx_tab1_col1col2'. The duplicate key value is (ccccc).
The statement has been terminated.

So, no, you can't. Uniqueness must exist within the indexed fields without regard to any included columns.

|||Include column can not make index unique. Because include column is not part of index key.|||Hey Robert. Yes, you are correct but at that time, I was in middle of implememnting a checkdb process for my company, which was never in place. Well, thank you very much for your time and effort and the answer.

Friday, February 24, 2012

can someone check this code for a nOOb

I've managed to get the following code to do 99% of what I want it to:

SELECT tbl3.col1, tbl3.col11, tbl3.col2, tbl3.col3, tbl3.col4, tbl3.col5, tbl3.col6, tbl3.col7, tbl3.col8, tbl3.col13
FROM tbl3
WHERE ((tbl3.col13 = 1) AND ((tbl3.col11 = colcheck) AND (tbl3.col2 LIKE '%colname%' AND tbl3.col6 LIKE '%colcity%' AND tbl3.col7 LIKE '%colstate%' AND tbl3.col4 LIKE '%coladdress%' AND tbl3.col8 LIKE '%colzip%')))
ORDER BY tbl3.col2 ASC

It for an advanced search form. col13 is set manually in the code, col11 is a radio button, and the rest of the fields are optional. It's for finding contacts in a table. I have colname which is the company name, but also a want to check it against col3 (not only col2). Same with coladdress, I'd like to check it against col5 (not only col4). Does anyone know how to accomplish this?can you use better column names and resend

:)