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?
Technology Tips and News
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?
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
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.
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