Showing posts with label column. Show all posts
Showing posts with label column. Show all posts

Friday, March 23, 2012

Multiple indexes on the same column

Is it possible to create one clustered index and one nonclustered index on the same column of a table?

For example, if we have a table Employees (EmpID, EmpName), then will SQL Server execute the following commands successfully?

Create clustered index indx1 on Employees (EmpID)
Go

Create nonclustered index indx2 on Employees (EmpID)
Go

yes, it is possible but it is not clear why you would want to do that. Here is the output of sp_help on the table empoyee and you will notice that there are two indexes one clustered and other non-clustered on the same key.
Name Owner Type Created_datetime

-- -- - --

Employees dbo user table 2005-10-28 09:30:42.733

Column_name Type Computed Length Prec Scale Nullable TrimTrailingBlanks FixedLenNullInSource Collation

-- -- -- -- -- -- -- -- -- --

EmpID int no 4 10 0 yes (n/a) (n/a) NULL

EmpName varchar no 100 yes no yes SQL_Latin1_General_CP1_CI_AS

Identity Seed Increment Not For Replication

-- -

No identity column defined. NULL NULL NULL

RowGuidCol

--

No rowguidcol column defined.

Data_located_on_filegroup

--

PRIMARY

index_name index_description index_keys

-- -

indx1 clustered located on PRIMARY EmpID

indx2 nonclustered located on PRIMARY EmpID

No constraints are defined on object 'employees', or you do not have permissions.

No foreign keys reference table 'employees', or you do not have permissions on referencing tables.

No views with schema binding reference table 'employees'.

|||Well, I was asked this question in an interview :)
Even I am not aware about any practical aspect of the question.

Thank you for your response, anyway.|||Thinking more about it, I think it can be useful under following scenarios
(1) you have range queries on empid that retrieves large number of columns in the table so it may make sense to have clustered index on empid.
AND
(2) you have range query on empid where you need only a subset of columns. you can make those subset of columns as included columns such that your query is covered by the index. This will eliminate access to the data page.

thanks

multiple Index files

1. Is there any benifit to creating a distinct index file for each column to be indexe
Index file 1 - Field
Index File 2 - Field
Index file 3 - Field
Index file 4 - Field
2. Is there a benifit to creating multiple groups of indexes ie
Index file 1 - Field A, Field B, Filed
Index File 2 - Field B, Field
Index file 3 - Field B, FieldHi Mark.
There are benefits / limitations in taking either approach.
Multi-column indexes may "cover" more queries or join predicates than single
column queries and provide better performance that way.
Distinct indexes provide compact index width for fast join or seek
operations where query predicates or filter arguments are on those columns.
SQL Server can also intersect indexes during optimization which recovers
some of the benefits that would have come from multi-column indexes.
If your application is relatively read only (data mart / warehouse) & not
bound by disk space limitations you might find that the more indexes you
have the better, as there are simply more options for the optimizer to suit
more queries.
If your application is more of an online transaction processinng
application, you probably want to limit which indexes you add as each index
needs updating as rows are inserted, updated & deleted.
The Index Tuning Wizard can be helpful in determining which indexes are the
best for the database & queries you're using.
HTH
Regards,
Greg Linwood
SQL Server MVP
"MarkFirth" <anonymous@.discussions.microsoft.com> wrote in message
news:406C0C47-5B5C-45EF-93C2-DD721278F61F@.microsoft.com...
> 1. Is there any benifit to creating a distinct index file for each column
to be indexed
> Index file 1 - Field A
> Index File 2 - Field B
> Index file 3 - Field B
> Index file 4 - Field H
> 2. Is there a benifit to creating multiple groups of indexes ie:
> Index file 1 - Field A, Field B, Filed C
> Index File 2 - Field B, Field C
> Index file 3 - Field B, Field H
>|||Mark
It depends on what are you going to query and how.
If you issue SELECT Field A, Field B, Filed C FROM
SomeTable ORDER BY Field A, Field B, Filed C or
WHERE clause
so having covering index on these columns will gain perfomance improving.
"MarkFirth" <anonymous@.discussions.microsoft.com> wrote in message
news:406C0C47-5B5C-45EF-93C2-DD721278F61F@.microsoft.com...
> 1. Is there any benifit to creating a distinct index file for each column
to be indexed
> Index file 1 - Field A
> Index File 2 - Field B
> Index file 3 - Field B
> Index file 4 - Field H
> 2. Is there a benifit to creating multiple groups of indexes ie:
> Index file 1 - Field A, Field B, Filed C
> Index File 2 - Field B, Field C
> Index file 3 - Field B, Field H
>sql

Multiple FK or "TableName" column?

Hi!
I have a table "addresses". Address can be associate to a customer, an
employee, a supplier or a consultant. Every of these 5 have a table with an
identity primary key. My question is simple: Which of the 2 ways is the best
to link the "Addresses" table to other tables?
A- By adding 5 foreign keys, assuming that only one will be NOT NULL by reco
rd
B- Adding a column Varchar "ParentTable" and a Int column "ParentID" to make
the relation programmatically?
I need the best way to be faster and also easier to implement...
Thanks in Advance!David Parenteau wrote:
> Hi!
> I have a table "addresses". Address can be associate to a customer, an
> employee, a supplier or a consultant. Every of these 5 have a table
> with an identity primary key. My question is simple: Which of the 2
> ways is the best to link the "Addresses" table to other tables?
> A- By adding 5 foreign keys, assuming that only one will be NOT NULL
> by record B- Adding a column Varchar "ParentTable" and a Int column
> "ParentID" to make the relation programmatically?
> I need the best way to be faster and also easier to implement...
> Thanks in Advance!
Or C - Create 5 tables for CustomerAddress, EmployeeAddress, etc.
Or D - Add an AddressID to the Customer, Employee, etc. tables if they
can only have one address (probably not the case)
Or E - Create a generic Person table and a PersonAddress table. Relate
Person to Customer, Employee, etc.
I don't like option A and option B is not relational.
David Gugick
Quest Software
www.imceda.com
www.quest.com|||First of all, an IDENTITY cannot ever be a key by definition, so the
schema is screwed up from the start. Ignoring that fundamental
problem, start by asking the basic questions:
1) Is an address an attribute of a customer, an employee, a supplier
and a consultant? In which case you need a SAN (Standard Address
Number) or equivalent in a column in those tables. I am assuming that
people fall into one and only one of these roles and that they are
logically different. Otherwise, there would be a People table.
2) Is there a one-to-many relationship between an address and a
customer, an employee, a supplier or a consultant? That case, you will
need tables to model the customer-addresses, the employee-addresses,
the supplier-addresses and the consultant-addresses relationships. I
would also add type codes to the addresses (ship-to, bill-to, etc.).|||Wow... suggestion!
Yes, more than one adress per customers, etc...
For E, does the Person table has 5 FK, one for each table or you mean 5
Person table, one for customer, one for employee, and so on?
Addresses should be the same columns for each 5 entities, so Solution C
seems to be not the quickest way to choose if a change must occur in the
schema... WHat do you think about this?
"David Gugick" wrote:

> David Parenteau wrote:
> Or C - Create 5 tables for CustomerAddress, EmployeeAddress, etc.
> Or D - Add an AddressID to the Customer, Employee, etc. tables if they
> can only have one address (probably not the case)
> Or E - Create a generic Person table and a PersonAddress table. Relate
> Person to Customer, Employee, etc.
> I don't like option A and option B is not relational.
>
> --
> David Gugick
> Quest Software
> www.imceda.com
> www.quest.com
>|||First create and load this table. might want to look at Melissa Data
software to edit the addresses,
CREATE TABLE Addresses
(address_nbr INTEGER NOT NULL PRIMARY KEY,
street1 CHAR(35) NOT NULL,
street2 CHAR(35),
street3 CHAR(35),
city_name CHAR(20) NOT NULL,
state_code CHAR(2) NOT NULL,
zip4 CHAR(10) NOT NULL
CHECK (zip4 LIKE '[0-9][0-9][0-9][0-9][0-9]'
OR zip4 LIKE
'[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'),
verification_date DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL);
Next, create each of the tables with the proper key for that kind of
entity. For suppliers, the Dun & Bradsteet codes are common; employees
better have an SSN, etc.
CREATE TABLE Suppliers
(duns_nbr CHAR(9) NOT NULL PRIMARY KEY,
supplier_name CHAR(35) NOT NULL,
.);
Now create a relationship table between each of the entities and the
addresses:
CREATE TABLE SupplierAddresses
(duns_nbr CHAR(9) NOT NULL
REFERENCES Suppliers(duns_nbr)
ON UPDATE CASCADE
ON DELETE CASCADE,
address_nbr INTEGER NOT NULL,
REFERENCES Addresses(address_nbr)
ON UPDATE CASCADE
ON DELETE CASCADE,
supplier_address_type CHAR(5) INTEGER NOT NULL
CHECK (supplier_address_type IN ('ship', 'bill', 'HQ', 'serv',
...)),
PRIMARY KEY (duns_nbr, address_nbr));
Make heavy use of DRI actions to maintian data integrity. Take time to
invent good address type codes for each logical entity (i.e. a customer
does not have a service department or a headquarters).|||About the Identity that cannot be a Key, do you mean a "Natural key"? If yes
,
I'm agree.
I used to set a FieldID INT Identity has a Primary key in my tables, to
avoid multiple-field primary keys. This seems to be easier to locate a recor
d
from a web application or application... Does this a very bad thing to do? I
f
yes, why?
David
"--CELKO--" wrote:

> First of all, an IDENTITY cannot ever be a key by definition, so the
> schema is screwed up from the start. Ignoring that fundamental
> problem, start by asking the basic questions:
> 1) Is an address an attribute of a customer, an employee, a supplier
> and a consultant? In which case you need a SAN (Standard Address
> Number) or equivalent in a column in those tables. I am assuming that
> people fall into one and only one of these roles and that they are
> logically different. Otherwise, there would be a People table.
> 2) Is there a one-to-many relationship between an address and a
> customer, an employee, a supplier or a consultant? That case, you will
> need tables to model the customer-addresses, the employee-addresses,
> the supplier-addresses and the consultant-addresses relationships. I
> would also add type codes to the addresses (ship-to, bill-to, etc.).
>|||Celko,
I now have a new example: A table activity. But the "activities" will need
to be linked to all 150 tables in my database... So, is it a reason to avoid
creating 150 link tables and keep the table name in a column of the
"Activity" table?
Thanks!!
"--CELKO--" wrote:

> First of all, an IDENTITY cannot ever be a key by definition, so the
> schema is screwed up from the start. Ignoring that fundamental
> problem, start by asking the basic questions:
> 1) Is an address an attribute of a customer, an employee, a supplier
> and a consultant? In which case you need a SAN (Standard Address
> Number) or equivalent in a column in those tables. I am assuming that
> people fall into one and only one of these roles and that they are
> logically different. Otherwise, there would be a People table.
> 2) Is there a one-to-many relationship between an address and a
> customer, an employee, a supplier or a consultant? That case, you will
> need tables to model the customer-addresses, the employee-addresses,
> the supplier-addresses and the consultant-addresses relationships. I
> would also add type codes to the addresses (ship-to, bill-to, etc.).
>

Wednesday, March 21, 2012

Multiple entries script...help

Ok,
In one table, I need to find all instances of multiple entries of an
invoice number in the column InvoiceNumber.

For example, there may be more than one entrie of 321, 567, and 987
in column InvoiceNumber.

321 skin care product
321 vitamins
321 bottled water

567 rubbing alcohol
567 dental floss

987 fabric softener
987 tissue paper
987 marlboro cigaretts

Any help is appreciated.
Thanks,
Trintselect InvoiceNumber, count(*) as 'Occurrences'
from dbo.MyTable
group by InvoiceNumber
having count(*) > 1

Simon|||Simon,
Thanks!
Trint

..Net programmer
trinity.smith@.gmail.com

*** Sent via Developersdex http://www.developersdex.com ***

Multiple DISTINCTS?

Hello,
Is it possible to constrain a query so that I receive only the duplicate rows (all column data matches)? For example:
SELECT DISTINCT COL1,
DISTINCT COL2,DISTINCT COL3
FROM TABLE
I did a search on multiple DISTINCTS but came up with nothing. Thanks in advance.DISTINCT applys to the whole row of the result set, not individual columns of it. When you do aSELECT DISTINCT col1, col2, col3
FROM myTableyou will get one row for any given combination of col1, col2, and col3. You can use DISTINCT within aggragate functions to get aggrigates of the unique values, but when applied against a result set, DISTINCT applies to the whole set (row).

-PatP|||uh, huh?

SELECT DISTINCT Col1, Col2, Col3

Will give you 1 unique row..

You want to only see dups?

SELECT Col1, Col2, Col3
FROM myTable99
GROUP BY Col1, Col2, Col3
HAVING COUNT(*) > 1

Is that what your after?|||Originally posted by Brett Kaiser
uh, huh?

SELECT DISTINCT Col1, Col2, Col3

Will give you 1 unique row..

You want to only see dups?

SELECT Col1, Col2, Col3
FROM myTable99
GROUP BY Col1, Col2, Col3
HAVING COUNT(*) > 1

Is that what your after?

------------------

Thanks for your suggestions. I am looking for a resultset of:

COL1 COL2 COL3
---------
brown tall dog
brown tall dog
brown tall dog

Thanks again for your help.|||Did the code give you what you wanted...I'm not sure...|||Originally posted by Brett Kaiser
Did the code give you what you wanted...I'm not sure...

Not yet, but I could be doing something wrong.

I've tried the following:
SELECT DISTINCT COL1, COL2, COL3
FROM myTABLE

and:
SELECT COL1, COL2, COL3
FROM myTABLE
GROUP BY COL1, COL2, COL3

and:
SELECT DISTINCT COL1, COL2, COL3
FROM myTABLE
HAVING COUNT(*) > 1

and I've tried:
SELECT COL1, COL2, COL3
FROM myTABLE
GROUP BY COL1, COL2, COL3
HAVING COUNT(*) > 1|||Well that should work...

USE Northwind
GO

CREATE TABLE myTable99(Col1 varchar(25),Col2 varchar(25),Col3 varchar(25))
GO

INSERT INTO myTable99(Col1,Col2,Col3)
select 'Brown','Tall','Dog' UNION ALL
select 'Brown','Tall','Dog' UNION ALL
select 'Brown','Tall','Dog' UNION ALL
select 'Blonde','Small','Pussy cat' UNION ALL
select 'Red','Medium','Snapper Turtle'
GO

SELECT Col1, Col2, Col3
FROM myTable99
GROUP BY Col1, Col2, Col3
HAVING COUNT(*) > 1
GO

DROP TABLE myTable99
GO

Oh, are you looking for all three?|||SELECT * FROM myTable99 o WHERE EXISTS(
SELECT *
FROM myTable99 i
WHERE o.Col1 = i.Col1
AND o.Col2 = i.Col2
AND o.Col3 = i.Col3
GROUP BY Col1, Col2, Col3
HAVING COUNT(*) > 1)
GO

All three...|||Brett,
That should do it! Combined with the info in the links provided in this thread: http://www.dbforums.com/t991775.html

I should be able to put something together. I really do appreciate all your hard work.

Regards,
Americus Johnson

Thanks also to Pat!|||Hard Work...Lord no...

That's why I became a dba...

:D

PS Don't forget to take ALL of your animal freinds...ya might get lucky|||Originally posted by Brett Kaiser
Hard Work...Lord no...

That's why I became a dba...

:D

PS Don't forget to take ALL of your animal freinds...ya might get lucky

True dat.|||yup

Monday, March 19, 2012

multiple db, table, column query

Ok, if you got this far you must have an idea what im looking for. I have two DB's. I need to query four columns in two tables in two DB's at the same time to produce a list of info in order of address(last part I can do:D )example: DB1 has table "customers" with columns, address,cust number, phone. second DB2 has table "site", with columns cust number, amount owed, unique code, last bill date. I need to reference the customer number in both to match to get a result containing, "customer number, address, unique code, and amount owed.
sound easy to anyone?which database is this? select t1.custnumber
, t1.address
, t2.uniquecode
, t2.amountowed
from DB1.customers as t1
inner
join DB2.site as t2
on t2.custnumber = t1.custnumber|||This is Microsoft SQL running Enterprise Manager ver 8.0. I might be in the wrong forum|||most database systems support (some subset of) standard SQL, so no, you aren't necessarily in the wrong forum, however, there are often significant differences in the variants of SQL from one database system to another

in this case, SQL Server requires the owner name between the database name and the table name, or, at least, requires the missing period

so it would be DB1.dbo.customer or DB1..customer

Friday, March 9, 2012

Multiple count(*) on same column Same query?

Hello,
Thank you for taking time to read my question.
I have somewhat a similar problem like this person in devarticles forum
- http://shorterlink.com/?NY7F6G
In my case, I have some cities, and every city will have Status column
with values 1/2/3/4 like this:
NYC 1 11/4/2005
NYC 3 11/2/2005
CHI 3 11/1/2005
CHI 4 11/4/2005
LA 1 11/20/2005
...
...
...
I'm trying to have a query that will have a result like this:
Status1Count Status2Count Status3Count Status4Count
NYC 100 80 20 40
LA 20 40 10 25
CHI 38 35 33 20
This next query gives the count for one status. I'm expecting to get
all the count for all the status like the above table:
SELECT a.citydesc, COUNT(a.status) as Status1Count
FROM a
WHERE a.Status=1
GROUP BY a.citydesc
Right now, in the existing code, they are hitting the database for
every cell in every row. I'm researching on how best to rewrite the
code to achieve this within one trip to the DB. Is it possible to
achieve this using just a single SELECT query ? Could you please give
me some pointers?
This is w.r.t SQL Server 2000
Thank you in Advance.See if this works (untested):
select citydesc,
SUM(case status when 1 then 1 else 0 end) as Status1Count,
SUM(case status when 2 then 1 else 0 end) as Status2Count,
SUM(case status when 3 then 1 else 0 end) as Status3Count,
SUM(case status when 4 then 1 else 0 end) as Status4Count
from a
group by citydesc
<dcmetro@.gmail.com> wrote in message
news:1132682010.500230.168440@.f14g2000cwb.googlegroups.com...
> Hello,
> Thank you for taking time to read my question.
> I have somewhat a similar problem like this person in devarticles forum
> - http://shorterlink.com/?NY7F6G
> In my case, I have some cities, and every city will have Status column
> with values 1/2/3/4 like this:
> NYC 1 11/4/2005
> NYC 3 11/2/2005
> CHI 3 11/1/2005
> CHI 4 11/4/2005
> LA 1 11/20/2005
> ...
> ...
> ...
> I'm trying to have a query that will have a result like this:
> Status1Count Status2Count Status3Count Status4Count
> NYC 100 80 20 40
> LA 20 40 10 25
> CHI 38 35 33 20
> This next query gives the count for one status. I'm expecting to get
> all the count for all the status like the above table:
> SELECT a.citydesc, COUNT(a.status) as Status1Count
> FROM a
> WHERE a.Status=1
> GROUP BY a.citydesc
> Right now, in the existing code, they are hitting the database for
> every cell in every row. I'm researching on how best to rewrite the
> code to achieve this within one trip to the DB. Is it possible to
> achieve this using just a single SELECT query ? Could you please give
> me some pointers?
> This is w.r.t SQL Server 2000
> Thank you in Advance.
>|||Hello Raymond,
Thank you very much for the hint. Yes, your suggestion works for me as
it is.
I got couple of questions when I was testing it. How do we rewrite the
query if the status is dynamic and is coming from another table? Is
there any way we could get the total of each StatusCount as a row in
the same query?
Thank you very much again.|||<dcmetro@.gmail.com> wrote in message
news:1132688082.041928.277300@.g43g2000cwa.googlegroups.com...
> Hello Raymond,
> Thank you very much for the hint. Yes, your suggestion works for me as
> it is.
> I got couple of questions when I was testing it. How do we rewrite the
> query if the status is dynamic and is coming from another table? Is
> there any way we could get the total of each StatusCount as a row in
> the same query?
> Thank you very much again.
>
Take a look at ROLLUP and see if this doesn't help you.
Rick Sawtell|||Nothing simple (unless someone else can jump in).
What I would do is this and handle presentation client side.
SELECT a.citydesc, a.status, COUNT(a.status) AS status
FROM a
GROUP BY a.citydesc, a.status
ORDER BY a.citydesc, a.status
Results:
CHI status1 38
CHI status2 35
...
LA status1 20
LA status2 40
LA status3 10
...
NYC...
If you're still looking for something else, post DDL (create table
statements) and insert statements for both tables.
<dcmetro@.gmail.com> wrote in message
news:1132688082.041928.277300@.g43g2000cwa.googlegroups.com...
> Hello Raymond,
> Thank you very much for the hint. Yes, your suggestion works for me as
> it is.
> I got couple of questions when I was testing it. How do we rewrite the
> query if the status is dynamic and is coming from another table? Is
> there any way we could get the total of each StatusCount as a row in
> the same query?
> Thank you very much again.
>|||Rick: Thanks for the pointer on ROLLUP. I was looking for that exact
functionality. Thanks again.
Raymond:
Here are the DDL for the two tables I'm talking about.
CREATE TABLE tblCities (
[CityID] [int] NULL ,
[CityDesc] [varchar] (20) NULL ,
[StatusID] [int] NULL,
[CreatedDate] [datetime] NULL )
CREATE TABLE [tblStatuses] (
[StatusID] [int] NOT NULL ,
[Description] [varchar] (25) NOT NULL ,
)
and some insert statements as well.
INSERT INTO [tblStatuses]([StatusID], [Description])
VALUES(1 , 'Status1 ' )
INSERT INTO [tblStatuses]([StatusID], [Description])
VALUES(2 , 'Status2 ' )
INSERT INTO [tblStatuses]([StatusID], [Description])
VALUES(3 , 'Status3 ' )

Multiple count() from a single column

I have a table, Message, which has an FK relationshp to the table UserCDV (o
n
MessageID) and also an FK relationship to the table Document (on DocumentID)
.
Doing a join on these tables associates a message (from Message) with a
particular user (from UserCDV) and what type of document the message
involves--radiology, transcription, lab report, etc. (from the
DocumentCategoryID field of Document).
What I need to do is return, in one record, a user's ID and the count of his
messages of each of four document categories. Say for example that user
100459 has four messages with a DocumentCategoryID of 2 (Radiology), two
messages with a DocumentCategoryID of 3 (Lab), four messages with a
DocumentCategoryID of 4 (Transcription), and none with a DocumentCategoryID
of 5 (Pathology). What I need to return is a record with fields named
UserID, Rad, Lab, Tran, and Path, having the values 100459, 4, 2, 4, 0
respectively.
How can I construct a query that will select and return this information?Fred Sawtelle wrote:
> I have a table, Message, which has an FK relationshp to the table UserCDV
(on
> MessageID) and also an FK relationship to the table Document (on DocumentI
D).
> Doing a join on these tables associates a message (from Message) with a
> particular user (from UserCDV) and what type of document the message
> involves--radiology, transcription, lab report, etc. (from the
> DocumentCategoryID field of Document).
> What I need to do is return, in one record, a user's ID and the count of h
is
> messages of each of four document categories. Say for example that user
> 100459 has four messages with a DocumentCategoryID of 2 (Radiology), two
> messages with a DocumentCategoryID of 3 (Lab), four messages with a
> DocumentCategoryID of 4 (Transcription), and none with a DocumentCategoryI
D
> of 5 (Pathology). What I need to return is a record with fields named
> UserID, Rad, Lab, Tran, and Path, having the values 100459, 4, 2, 4, 0
> respectively.
You will need to do a COUNT(*) and use a group by to aggregate the count
on the user and documenttype fields. Below is an pseudo query that shows
a way to accomplish this.
SELECT
UserID,
DocumentType,
COUNT(*)
FROM users
join messages on ...
join documents on ...
GROUP BY
UserId,
DocumentType
Aaron Weiker
http://aaronweiker.com/
http://www.sqlprogrammer.org/|||Fred Sawtelle wrote:
> I have a table, Message, which has an FK relationshp to the table UserCDV
(on
> MessageID) and also an FK relationship to the table Document (on DocumentI
D).
> Doing a join on these tables associates a message (from Message) with a
> particular user (from UserCDV) and what type of document the message
> involves--radiology, transcription, lab report, etc. (from the
> DocumentCategoryID field of Document).
> What I need to do is return, in one record, a user's ID and the count of h
is
> messages of each of four document categories. Say for example that user
> 100459 has four messages with a DocumentCategoryID of 2 (Radiology), two
> messages with a DocumentCategoryID of 3 (Lab), four messages with a
> DocumentCategoryID of 4 (Transcription), and none with a DocumentCategoryI
D
> of 5 (Pathology). What I need to return is a record with fields named
> UserID, Rad, Lab, Tran, and Path, having the values 100459, 4, 2, 4, 0
> respectively.
You will need to do a COUNT(*) and use a group by to aggregate the count
on the user and documenttype fields. Below is an pseudo query that shows
a way to accomplish this.
SELECT
UserID,
DocumentType,
COUNT(*)
FROM users
join messages on ...
join documents on ...
GROUP BY
UserId,
DocumentType
Aaron Weiker
http://aaronweiker.com/
http://www.sqlprogrammer.org/|||Thanks for your answer, Aaron. That query segregates the data by
DocumentCategoryID, but it returns a separate record for each distinct
DocumentCategoryID. I need to get all four totals in a single record.
Here's how I fleshed out the pseudoquery:
SELECT
UserID,
DocumentCategoryID,
COUNT(*) as Count
FROM UserCDV u
join Message m on u.MessageID = m.MessageID
join Document d on m.DocumentID = d.DocumentID
WHERE UserID = 100459
AND DocumentCategoryID in (2, 3, 4, 5)
GROUP BY
UserID,
DocumentCategoryID
...and here's the result - three records.
UserID DocumentCategoryID Count
-- -- --
100459 2 784
100459 3 7685
100459 4 1449|||Fred Sawtelle wrote:
> Thanks for your answer, Aaron. That query segregates the data by
> DocumentCategoryID, but it returns a separate record for each distinct
> DocumentCategoryID. I need to get all four totals in a single record.
I wish we had Pivot already... (enough day dreaming).
Check out this page for some code examples.
http://www.aspfaq.com/show.asp?id=2462
Aaron Weiker
http://aaronweiker.com/
http://www.sqlprogrammer.org/|||Excellent! With the guidance of those examples i now have it working.
Thanks so much.

> http://www.aspfaq.com/show.asp?id=2462

Multiple Coumn Report goes to column 2 on group page break

I have a multiple Column report with a group that I have specified "page
break at end". The problem I am having is: if it just so happens to be
printing in column 1, the next group starts printing in column 2 instead of
on a new page.
Any ideas?Hi Ron,
> I have a multiple Column report with a group that I have specified "page
> break at end". The problem I am having is: if it just so happens to be
> printing in column 1, the next group starts printing in column 2 instead of
> on a new page.
I'm having the same problem here...
Regards,
Jens|||Just so you know, I did go through the time consuming process of creating a
Microsoft ticket to confirm my suspicion this is a bug. They had no help for
a work around. If you come up with some sort of clugy solution or whatever
to work around this let me know. I will do the same if I come up with
something.
"Jens Weiermann" wrote:
> Hi Ron,
> > I have a multiple Column report with a group that I have specified "page
> > break at end". The problem I am having is: if it just so happens to be
> > printing in column 1, the next group starts printing in column 2 instead of
> > on a new page.
> I'm having the same problem here...
> Regards,
> Jens
>|||Hi Ron,
> Just so you know, I did go through the time consuming process of creating
> a Microsoft ticket to confirm my suspicion this is a bug. They had no
> help for a work around.
thanks for the info.
> If you come up with some sort of clugy solution or whatever to work
> around this let me know. I will do the same if I come up with
> something.
I tried re-arranging my report so that I was able to put the multi-column
part into a sub-report and handle the page breaks outside of that, but just
to find out that multi-column sub-reports are also NOT working.
I guess you can imagine how much this actually s*cks. Especially for me who
was the "Let's save some $$$ - we don't need CrystalReports!" guy in our
company - and now am having a BIG problem.
Sigh...
Jens

Wednesday, March 7, 2012

Multiple Columns with different Values OR Single Column with Multiple Criteria?

Hi,

I have multiple columns in a Single Table and i want to search values in different columns. My table structure is

col1 (identity PK)
col2 (varchar(max))
col3 (varchar(max))

I have created a single FULLTEXT on col2 & col3.
suppose i want to search col2='engine' and col3='toyota' i write query as

SELECT

TBL.col2,TBL.col3

FROM

TBL

INNER JOIN

CONTAINSTABLE(TBL,col2,'engine') TBL1

ON

TBL.col1=TBL1.[key]

INNER JOIN

CONTAINSTABLE(TBL,col3,'toyota') TBL2

ON

TBL.col1=TBL2.[key]

Every thing works well if database is small. But now i have 20 million records in my database. Taking an exmaple there are 5million record with col2='engine' and only 1 record with col3='toyota', it take substantial time to find 1 record.

I was thinking this i can address this issue if i merge both columns in a Single column, but i cannot figure out what format i save it in single column that i can use query to extract correct information.
for e.g.;
i was thinking to concatinate both fields like
col4= ABengineBA + ABBToyotaBBA
and in search i use
SELECT

TBL.col4

FROM

TBL

INNER JOIN

CONTAINSTABLE(TBL,col4,' "ABengineBA" AND "ABBToyotaBBA"') TBL1

ON

TBL.col1=TBL1.[key]

Result = 1 row

But it don't work in following scenario
col4= ABengineBA + ABBCorola ToyotaBBA

SELECT

TBL.col4

FROM

TBL

INNER JOIN

CONTAINSTABLE(TBL,col4,' "ABengineBA" AND "ABB*ToyotaBBA"') TBL1

ON

TBL.col1=TBL1.[key]


Result=0 Row
Any idea how i can write second query to get result?

Before you merged the two columns could you not write your query something like this

select *

from TBL

where col2 = 'engine' and col3 = 'toyota'

You could put variables in place of the engine and toyota and create a stored procedure that passes in the values you want to search on.

I don't think there is a need to merge the columns matter of fact it will most likely run faster without being merged.

Or maybe I missed you rquestion.

Multiple Columns with different Values OR Single Column with Multiple Criteria?

Hi,

I have multiple columns in a Single Table and i want to search values in different columns. My table structure is

col1 (identity PK)
col2 (varchar(max))
col3 (varchar(max))

I have created a single FULLTEXT on col2 & col3.
suppose i want to search col2='engine' and col3='toyota' i write query as

SELECT

TBL.col2,TBL.col3

FROM

TBL

INNER JOIN

CONTAINSTABLE(TBL,col2,'engine') TBL1

ON

TBL.col1=TBL1.[key]

INNER JOIN

CONTAINSTABLE(TBL,col3,'toyota') TBL2

ON

TBL.col1=TBL2.[key]

Every thing works well if database is small. But now i have 20 million records in my database. Taking an exmaple there are 5million record with col2='engine' and only 1 record with col3='toyota', it take substantial time to find 1 record.

I was thinking this i can address this issue if i merge both columns in a Single column, but i cannot figure out what format i save it in single column that i can use query to extract correct information.
for e.g.;
i was thinking to concatinate both fields like
col4= ABengineBA + ABBToyotaBBA
and in search i use
SELECT

TBL.col4

FROM

TBL

INNER JOIN

CONTAINSTABLE(TBL,col4,' "ABengineBA" AND "ABBToyotaBBA"') TBL1

ON

TBL.col1=TBL1.[key]

Result = 1 row

But it don't work in following scenario
col4= ABengineBA + ABBCorola ToyotaBBA

SELECT

TBL.col4

FROM

TBL

INNER JOIN

CONTAINSTABLE(TBL,col4,' "ABengineBA" AND "ABB*ToyotaBBA"') TBL1

ON

TBL.col1=TBL1.[key]


Result=0 Row
Any idea how i can write second query to get result?

Before you merged the two columns could you not write your query something like this

select *

from TBL

where col2 = 'engine' and col3 = 'toyota'

You could put variables in place of the engine and toyota and create a stored procedure that passes in the values you want to search on.

I don't think there is a need to merge the columns matter of fact it will most likely run faster without being merged.

Or maybe I missed you rquestion.

Multiple columns partitioning in SQL 2008

Will partitioning on multiple columns be supported in SQL 2008, without needing to use the computed column option?

Thanks for any help

Moved to SQL 2008 (Katmai) forums.

|||

Bassel Banbouk wrote:

Will partitioning on multiple columns be supported in SQL 2008, without needing to use the computed column option?

Thanks for any help

No. This is not planned for SQL Server 2008.

multiple columns of images

I need to be able to display multiple (external) images per row in one
control. The report that the control is on must have only one column, since
other controls (namely a table control) house enough columnar data to fill
an
entire page and then some.
I'm not trying to repeat the same image many times, I want to use space
efficiently by having several images per row. For example, instead of
having
30 images displayed in a single column, I would like to have either 15 rows
with two images per row; or 10 rows with 3 images per row.
I am using BI Report Builder in VS 2005.
I currently have a table control that to displays a single column of images
(via an image control).
I'm using image controls in the table to point to external images (hosted by
a web server). My database has a table with one column indicating the
external location of my images. This part works great.
I don't want to use the RepeatWith property because the
PDF renderer doesn't support it.
I have attempted to use the Matrix, and was able to create a single row that
spanned many pages. These spanned pages have grundles of blank, unformatted
space; which is simply unacceptable for my report's required
professionalism.
Perhaps there is a way to line feed or carriage return this long row?
Using a SubReport with multiple columns is unacceptable because subreports
inherit their parent column settings, which would be 1 in this case.
I would appreciate any help.
RobertHi Robert,
Thank you for using MSDN Managed Newsgroup Support.
Would you please make this issue more clear? I know you want to display the
images in a row. Based on my scope, you could add multiple images in
multiple columns of the table.
Hope this will be helpful.
Sincerely,
Wei Lu
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================This posting is provided "AS IS" with no warranties, and confers no rights.

Multiple Columns Index/Key (does it free me from creating a single column indexes?)

I hope i'm in the right place, but thanks anyway....

Actually i have 2 questions (regarding sql-server Indices/Keys):

1) I have an index, which is consisted of 4 columns.

I've read elsewhere that this index functions (as well) as an index (single column

index) on the first column of this multi-column index.

Does this mean that if i'd like to have (in addition) Indices on all of the 4 columns

seperately i need to define only 3?

2) I have a unique key consisted of multiple columns.

I'd like to save an index to this combination of columns as well (to speed up

things in DB...).

Does the definition of a multiple-columns key free me from defining the multiple-

columns index?

can anyone explain the main diference between Keys and Indices?


thanks,

Ran Kizi

1. Yes, the multi-column index will also function as an index for the first column. In many situations, it will also be an quasi-efficient index for the second, and perhaps other columns. I would get some response measurements before assuming that indexes were required on the lesser columns.

2. A 'unique key' is in fact, a 'Unique CONSTRAINT.

A Primary Key can be created for one or more columns, requires indexing to be efficient, and an index is automatically generated when the Primary Key is created. A Primary Key requires a value, and that the value is unique.

There is no 'Unique Key', but there is a Unique CONSTRAINT. (Perhaps in your question, you were referring to a Primary Key, and if so, the above applies.) A Unique CONSTRAINT can be created for one or more columns, and automatically creates a matching index. A Unique CONSTRAINT does not require a value, but if a value is provided, it must be unique.

A Foreign Key 'should have' an index to be efficient -but the index is NOT automatically generated when the Foreign Key is created. It is recommended that a matching index be created when the Foreign Key is created. A Foreign Key does NOT require a value, BUT if a value is provided, that value must also exist in the table with the Primary Key relationship.

|||

1. Indexes on multiple columns can also be used as index on first column. and in my experience it can not be used as index on second and following columns. you would need to create seprate indexes on those column if your queries will only use second or third column without using first column in queries.

There is a workaround even if you dont have to use first column in your query you can still use COLUMN1 = COLUMN1 only to make optimizer use index as in best of my knowledge if column1 is not present in your query index will be used for second or third column.

2. when you create UNIQUE constraint on multiple columns SQL Server will ultimately create compound unique index on those colunms to make sure uniquenes. for selectivity of index above will apply.

Primary Key does create index automaticaly (if index type is not mentioned and table is heap then will create CLUSTERED INDEX). FORIEGN KEY does not create index automatically and it is highly recomemded to create index on foriegn key column. The one unique reason is when deleting record in primary key table sql server perform a look up in all tables having foriegn key on that table and if no index presented then it will perform clustered index scan or table scan that will ultimately slow down DELETE operation for primary key table.

Multiple Columns Index/Key (does it free me from creating a single column indexes?)

I hope i'm in the right place, but thanks anyway....

Actually i have 2 questions (regarding sql-server Indices/Keys):

1) I have an index, which is consisted of 4 columns.

I've read elsewhere that this index functions (as well) as an index (single column

index) on the first column of this multi-column index.

Does this mean that if i'd like to have (in addition) Indices on all of the 4 columns

seperately i need to define only 3?

2) I have a unique key consisted of multiple columns.

I'd like to save an index to this combination of columns as well (to speed up

things in DB...).

Does the definition of a multiple-columns key free me from defining the multiple-

columns index?

can anyone explain the main diference between Keys and Indices?


thanks,

Ran Kizi

1. Yes, the multi-column index will also function as an index for the first column. In many situations, it will also be an quasi-efficient index for the second, and perhaps other columns. I would get some response measurements before assuming that indexes were required on the lesser columns.

2. A 'unique key' is in fact, a 'Unique CONSTRAINT.

A Primary Key can be created for one or more columns, requires indexing to be efficient, and an index is automatically generated when the Primary Key is created. A Primary Key requires a value, and that the value is unique.

There is no 'Unique Key', but there is a Unique CONSTRAINT. (Perhaps in your question, you were referring to a Primary Key, and if so, the above applies.) A Unique CONSTRAINT can be created for one or more columns, and automatically creates a matching index. A Unique CONSTRAINT does not require a value, but if a value is provided, it must be unique.

A Foreign Key 'should have' an index to be efficient -but the index is NOT automatically generated when the Foreign Key is created. It is recommended that a matching index be created when the Foreign Key is created. A Foreign Key does NOT require a value, BUT if a value is provided, that value must also exist in the table with the Primary Key relationship.

|||

1. Indexes on multiple columns can also be used as index on first column. and in my experience it can not be used as index on second and following columns. you would need to create seprate indexes on those column if your queries will only use second or third column without using first column in queries.

There is a workaround even if you dont have to use first column in your query you can still use COLUMN1 = COLUMN1 only to make optimizer use index as in best of my knowledge if column1 is not present in your query index will be used for second or third column.

2. when you create UNIQUE constraint on multiple columns SQL Server will ultimately create compound unique index on those colunms to make sure uniquenes. for selectivity of index above will apply.

Primary Key does create index automaticaly (if index type is not mentioned and table is heap then will create CLUSTERED INDEX). FORIEGN KEY does not create index automatically and it is highly recomemded to create index on foriegn key column. The one unique reason is when deleting record in primary key table sql server perform a look up in all tables having foriegn key on that table and if no index presented then it will perform clustered index scan or table scan that will ultimately slow down DELETE operation for primary key table.

Multiple Columns Index/Key (does it free me from creating a single column indexes?)

I hope i'm in the right place, but thanks anyway....

Actually i have 2 questions (regarding sql-server Indices/Keys):

1) I have an index, which is consisted of 4 columns.

I've read elsewhere that this index functions (as well) as an index (single column

index) on the first column of this multi-column index.

Does this mean that if i'd like to have (in addition) Indices on all of the 4 columns

seperately i need to define only 3?

2) I have a unique key consisted of multiple columns.

I'd like to save an index to this combination of columns as well (to speed up

things in DB...).

Does the definition of a multiple-columns key free me from defining the multiple-

columns index?

can anyone explain the main diference between Keys and Indices?

1. I'm sure you are in the wrong place, try the SQL Server forums.

2. A Key implies unique data in the column(s) (Unique Key / Primary Key) where as an index doesn't necessarily have to have unique data in the column(s).

3. An index and a key that use the same definitions (same columns etc.) would be redundant, cost space (and possibly performance) and not desired. The optimizer can use either.

4. The first column of an index is always the most important and determines whether the index will be used or not. If you have no better indexes of the first column then it will be used.

BTW - Don't over do it with indexes!

Please ask such question in the SQL Server Forum, this forum is only for Team Edition for Database Professionals.

Alle

Multiple columns

I'm search on multiple columns useing FREETEXTTABLE, is there a way to know
which column contained the search string? Be low is my query:
SELECT C_ID, ( C_LNAME + ', ' + C_FNAME) as C_NAME, C_SIT1, C_SIT2,
C_SIT3, C_SIT4, C_SIT5, C_SIT6, C_SIT7, C_SIT8, C_SIT9, C_SIT10,
c_city, c_zip, C_hphone, c_wphone, c_cphone, c_email, c_smkcomment,
c_rank_Comment, c_wage_comment, c_edname1, c_edname2, c_edname3,
c_wkemp1, c_wkemp2, c_wkemp3, c_wkcomment, c_refname1, c_refname2,
c_refname3, c_follow, c_comment, k.rank
FROM Candidates as c
inner join FREETEXTTABLE(Candidates, *, '"2 years experience"') as k
on C.C_ID = K.[KEY]
where k.rank > 2
order by k.rank desc
Well you can do something like this, but it does not perform well.
create table Candidates (
C_ID int identity not null constraint CandidatesPK primary key ,
C_LNAME varchar(50),
C_FNAME varchar(50))
insert into candidates (C_LNAME, C_FNAME) values('james', 'bond')
insert into candidates (C_LNAME, C_FNAME) values('james bond', 'test')
insert into candidates (C_LNAME, C_FNAME) values('test','test')
insert into candidates (C_LNAME, C_FNAME) values('test','james bond')
GO
sp_fulltext_database 'enable'
GO
CREATE FULLTEXT CATALOG candidates AS DEFAULT
GO
create fulltext index on candidates(C_LNAME, C_FNAME) key index CandidatesPK
GO
select * from candidates where contains(*,'"james bond"')
declare @.string varchar(20)
set @.string='"James Bond"'
select C_ID, case when a.[KEY] is not null then 'C_LNAME' when b.[KEY] is
not null then 'C_FNAME' end,C_LNAME, C_FNAME from candidates
left join (select * from containstable(candidates, C_LNAME,@.string)) as a on
a.[KEY]=Candidates.C_ID
left join (select * from containstable(candidates, C_FNAME,@.string)) as b on
b.[KEY]=Candidates.C_ID
where a.[KEY] is not null OR b.[KEY ]is not null
Hilary Cotter
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"rhogan" <rhogan@.discussions.microsoft.com> wrote in message
news:AC8BBE41-E95C-47B5-BF6A-B5213C830157@.microsoft.com...
> I'm search on multiple columns useing FREETEXTTABLE, is there a way to
> know
> which column contained the search string? Be low is my query:
> SELECT C_ID, ( C_LNAME + ', ' + C_FNAME) as C_NAME, C_SIT1, C_SIT2,
> C_SIT3, C_SIT4, C_SIT5, C_SIT6, C_SIT7, C_SIT8, C_SIT9, C_SIT10,
> c_city, c_zip, C_hphone, c_wphone, c_cphone, c_email, c_smkcomment,
> c_rank_Comment, c_wage_comment, c_edname1, c_edname2, c_edname3,
> c_wkemp1, c_wkemp2, c_wkemp3, c_wkcomment, c_refname1, c_refname2,
> c_refname3, c_follow, c_comment, k.rank
> FROM Candidates as c
> inner join FREETEXTTABLE(Candidates, *, '"2 years experience"') as k
> on C.C_ID = K.[KEY]
> where k.rank > 2
> order by k.rank desc

multiple column values into a single value

hi all,
I am trying to take all the values from a single varchar column and to inset
them into a variable so that they are seperated with the '-' sign.
I am trying to avoid cursors.
by the way- all the rows have a column with the same value in the table
for example:
create table test (col1 int, col2 varchar(20))
insert test values(1,'aaa')
insert test values(1,'bbb')
insert test values(1,'ccc')
the result sould look like this 'aaa-bbb-ccc'
thaks,
alonA cursor/iterative solution is best suited for this.
--
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"alon" <alon@.discussions.microsoft.com> wrote in message
news:BEB95AAB-5C04-4466-B940-B00AAAAE0DE3@.microsoft.com...
> hi all,
> I am trying to take all the values from a single varchar column and to
> inset
> them into a variable so that they are seperated with the '-' sign.
> I am trying to avoid cursors.
> by the way- all the rows have a column with the same value in the table
> for example:
> create table test (col1 int, col2 varchar(20))
> insert test values(1,'aaa')
> insert test values(1,'bbb')
> insert test values(1,'ccc')
> the result sould look like this 'aaa-bbb-ccc'
> thaks,
> alon|||create table #test (col1 int, col2 varchar(20))
insert #test values(1,'aaa')
insert #test values(1,'bbb')
insert #test values(1,'ccc')
DECLARE @.concat VARCHAR(100)
SET @.concat=''
SELECT
CASE WHEN @.concat='' THEN '' ELSE '-' END + col2
FROM #test
DROP TABLE #Test
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com
"alon" <alon@.discussions.microsoft.com> wrote in message
news:BEB95AAB-5C04-4466-B940-B00AAAAE0DE3@.microsoft.com...
> hi all,
> I am trying to take all the values from a single varchar column and to
> inset
> them into a variable so that they are seperated with the '-' sign.
> I am trying to avoid cursors.
> by the way- all the rows have a column with the same value in the table
> for example:
> create table test (col1 int, col2 varchar(20))
> insert test values(1,'aaa')
> insert test values(1,'bbb')
> insert test values(1,'ccc')
> the result sould look like this 'aaa-bbb-ccc'
> thaks,
> alon|||Maybe this can help:
http://milambda.blogspot.com/2005/0...s-as-array.html
Of course you'll need to change it to meet your specific needs.
ML|||So you want to destroy First Normal Form (1NF) with a proprieetary
kludge? In a tiered archtiecture, display is done inthe front end and
not in the database. This is far more basic than just SQL programming.|||http://www.aspfaq.com/2529
"alon" <alon@.discussions.microsoft.com> wrote in message
news:BEB95AAB-5C04-4466-B940-B00AAAAE0DE3@.microsoft.com...
> hi all,
> I am trying to take all the values from a single varchar column and to
> inset
> them into a variable so that they are seperated with the '-' sign.
> I am trying to avoid cursors.
> by the way- all the rows have a column with the same value in the table
> for example:
> create table test (col1 int, col2 varchar(20))
> insert test values(1,'aaa')
> insert test values(1,'bbb')
> insert test values(1,'ccc')
> the result sould look like this 'aaa-bbb-ccc'
> thaks,
> alon

Multiple column update statement

Hi,

I'm new to SQL Server but not new to SQL because I used it with Oracle. I wonder if it is possible to do this kind of statement on SQL Server:

UPDATE TableX M
SET (M.column1, M.column2, M.column3)=
(SELECT T.column1, T.column2, G.column3)
FROM Table_Y T,
Table_Z G
WHERE join condition))
WHERE EXISTS (join condition for TableX)

Basically, I'd like to update multiple columns in one statement but for some reason I can not get it to work.

What would be the equivalent on SQL Server?

Thanks for the help,

Laszlo M

SQL Server does not support row value constructors yet. So you will have to use the TSQL UPDATE statement extension which has a FROM clause like:

UPDATE TableX

SET column1 = T.column1, column2 = T.column2, column3 = G.column3

FROM TableX as M, Table_Y as T, Table_Z as G

WHERE <join condition>

and <condition from exists>

See Books Online UPDATE statement topic for more examples and details on the syntax.

|||

Hi Laszlo,

UPDATE TableX
SET column1 = T.column1, column2 = T.column2, column3 = G.column3
FROM TableX M INNER JOIN
(TableY T INNER JOIN TableZ G ON T.column4 = G.column1)
ON M.column2 = G.column1

Regards,

Mike H.

|||

Thanks for the quick response. All of you were very helpful.

Laszlo

Multiple column unique constraint

I am new to 05 and I am searching for a way to add uniqueness to two
fields that are not the PK. I am searching for a built in way to add
uniquness that doesn't require writing a trigger or use the primary key.CREATE TABLE dbo.foo
(
col1 INT PRIMARY KEY,
col2 INT,
col3 INT
);
GO
ALTER TABLE dbo.foo
ADD CONSTRAINT myConstraint
UNIQUE (col2, col3);
GO
DROP TABLE dbo.foo;
GO
"danielhoin" <danielhoin@.hotmail.com> wrote in message
news:1145990176.065271.100630@.t31g2000cwb.googlegroups.com...
>I am new to 05 and I am searching for a way to add uniqueness to two
> fields that are not the PK. I am searching for a built in way to add
> uniquness that doesn't require writing a trigger or use the primary key.
>|||There are several ways to do this.
You could use the create index statement to create an index.
You could create a table constraint that enforces the uniqueness.
CREATE TABLE SOMETABLE (
SOMETABLE_PRIMARYKEY NUMERIC IDENTITY,
COLUMN_1_OF_UNIQUEINDEX VARCHAR(255) NULL,
COLUMN_2_OF_UNIQUEINDEX VARCHAR(255) NULL,
CONSTRAINT PK_SOMETABLE PRIMARY KEY (SOMETABLE_PRIMARYKEY),
CONSTRAINT AK_SOMETABLE_AK_SOMETABL UNIQUE (COLUMN_1_OF_UNIQUEINDEX,
COLUMN_2_OF_UNIQUEINDEX)
)