Showing posts with label Sql server 2008. Show all posts
Showing posts with label Sql server 2008. Show all posts

Thursday, 10 July 2014

What are the difference between Primary Keys and Foreign Keys?



What are the difference between Primary Keys and Foreign Keys?

Primary key:
Primary keys are the unique identifiers for each row. They must contain unique values and cannot be null. Due to their importance in relational databases, Primary keys are the most fundamental aspect of all keys and constraints. A table can have only one primary key.

  • The PRIMARY KEY constraint uniquely identifies each record in a database table.
  •  Primary keys must contain unique values.
  • A primary key column cannot contain NULL values.
  •   Each table should have a primary key, and each table can have only ONE primary key.

Foreign key:
Foreign keys are a method of ensuring data integrity and manifestation of the relationship between tables. A FOREIGN KEY in one table points to a PRIMARY KEY in another table.

Difference between Primary Key & Foreign Key:

Primary Key
Foreign Key
  •  Primary key uniquely identify a record in the table.
Foreign key is a field in the table that is primary key in another table.
  • Primary Key can't accept null values.
Foreign key can accept multiple null value.
  •  By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index.
Foreign key do not automatically create an index, clustered or non-clustered. You can manually create an index on foreign key.
  • We can have only one Primary key in a table.
We can have more than one foreign key in a table.


Wednesday, 9 July 2014

Difference between Copying and Cloning the DataTable



 Copying and Cloning the DataTable: 


DataTable.Clone() - Copies only the structure of the source table with the constraints, the data is not copied from the source table.

DataTable.Copy() - Copies the structure and data from the source table.

For example, we may want to assign a DataTable object to a GridView control to allow a user to edit the data, but you also may want to provide a cancel button that aborts all changes on the Web page.

A simple way to implement this functionality is to create a copy of your DataTable object and use the copy for editing. If the user clicks the cancel button, the DataTable copy is thrown out.

If the user decides to keep the changes, you can replace the original DataTable object with the edited copy.

To create a copy of a DataTable object, use the Copy method on the DataTable, which copies the DataTable object schema and data.

DataTable copy = employee.Copy( );  
  

You often require a copy of the DataTable schema without the data. You can accomplish this by invoking the Clone method on the DataTable. Use this method when an empty copy of the DataTable is required and to which DataRow objects will be added at a later time.

DataTable clone = employee.Clone( ); 

Reset Identity Column Value to 1 in SQL Server



Reset Identity Column Value to 1 in SQL Server

To reset identity column value in SQL server or change or rest identity column value to 1 in SQL server.

To reset the identity below is the syntax:


SYNTAX: DBCC CHECKIDENT (Table_Name, RESEED, New_Reseed_Value)

Table_Name : Is name of your identity column table.

RESEED : It specifies that the current identity value should be changed.

New_Reseed_Value : Is the new value to use as the current value of the identity column.   

  

For Example: DBCC CHECKIDENT ('UserDetails', RESEED, 0)