Primary Key and Index

I can't distinguish between Primary Key and Index.

How the Index is used in client code(code that access DB).

Can anybody explain difference beween those two?

February 20th, 2015 1:09pm

Hello,

in SQL Server a primary key is always a unique index; all value in the column(s) must be unique. An index is used by SQL Server database engine, not by the Client, the Client don't Need to know if an index exists or not.

See MSDN Indexes and follow-up links

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 1:26pm

Index is an ordered physical structure that contains subset of table columns. If properly designed, it can help performance. Primary key is a constraint that is physically implemented as a combination of unique index and not null constraint on all the pk columns. Client code should never refer to any index. It is a sql optimizer engine's job to find the optimal index to access the data it needs, to quickly find the execution plan that is good enough.
February 20th, 2015 1:26pm

PKs and Indexes are not generally referenced in "code that access DB".  They are used internally by the database engine to improve performance of the query you executed.

Free Windows Admin Tool Kit Click here and download it now
February 20th, 2015 3:23pm

Primary key is created to ensure data integrity and uniqueness of records. The primary key can be a single column or set of columns. Internally it creates a clustered index If there is no already existing clustered index.

Otherwise it creates a non clustered index.

From the client code you initiate your query which is executed on database. If there is a index then response from database would be optimal resulting quick response to end users.

From the client code you can define primary key and other constraints on database which internally creates the index on database as I have explained above.

workTable.PrimaryKey = new DataColumn[] {workTable.Columns["CustID"]}; // Or DataColumn[] columns = new DataColumn[1]; columns[0] = workTable.Columns["CustID"]; workTable.PrimaryKey = columns;

Please refer the MSDN article for in depth details -

https://msdn.microsoft.com/en-us/library/z24kefs8(v=vs.110).aspx

February 20th, 2015 10:18pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics