SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Unique Constraints involving two columns.


Unique Constraints involving two columns.

Author
Message
jignesh209
jignesh209
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 185
I have a Table called EmployeeDepartment,it has EmployeeDepartmentId as Primary Key as well as Identity,EmployeeId which is a foreign key from Employee Table and DepartmentId which is also a foreign key from Department Table.
I have created a Unique Constraint on EmployeeId and DepartmentId.As I want to keep a check that only one Employee will be assigned to one Department.

1)Now when I run this query,
SELECT EmployeeDepartmentId ,EmployeeId ,DepartmentId FROM EmployeeDepartment WHERE DepartmentId =12
the Execution Plan says it is an Index Scan.

2)Now when I run this second query,
SELECT EmployeeDepartmentId ,EmployeeId ,DepartmentId FROM EmployeeDepartment WHERE EmployeeId =100
the Execution Plan says it is an Index Seek-Non-Clustered.

Any idea why the first query does and Index Scan and the second does Index Seek-Non-Clustered.

How can the first query make use of the Index or have I done something wrong when specifying the Unique Constraint?

Thanks for all the help.
Eddie Wuerch
Eddie Wuerch
SSC Eights!
SSC Eights! (898 reputation)SSC Eights! (898 reputation)SSC Eights! (898 reputation)SSC Eights! (898 reputation)SSC Eights! (898 reputation)SSC Eights! (898 reputation)SSC Eights! (898 reputation)SSC Eights! (898 reputation)

Group: General Forum Members
Points: 898 Visits: 2200
Because DepartmentID is the second column in the index (unique indexes are created automatically to enforce unique constraints), the only option available to the query processer is to scan the index. Think of it this way, picture a list of employees ordered on Last Name, First Name. Now look for every employee whose first name is 'Jerry'. How would you find them? By looking through the entire list.

Eddie Wuerch
MCM: SQL
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search