28 December 2011

Database Keys


Database Keys

Super Key

A Super key is any combination of fields within a table that uniquely identifies each record within that table.

Candidate Key

A candidate is a subset of a super key. A candidate key is a single field or the least combination of fields that uniquely identifies each record in the table. The least combination of fields distinguishes a candidate key from a super key. Every table must have at least one candidate key but at the same time can have several.

Candidate Kay

As an example we might have a student_id that uniquely identifies the students in a student table. This would be a candidate key. But in the same table we might have the student’s first name and last name that also, when combined, uniquely identify the student in a student table. These would both be candidate keys.
In order to be eligible for a candidate key it must pass certain criteria.
  • It must contain unique values
  • It must not contain null values
  • It contains the minimum number of fields to ensure uniqueness
  • It must uniquely identify each record in the table

Once your candidate keys have been identified you can now select one to be your primary key

Primary Key

A primary key is a candidate key that is most appropriate to be the main reference key for the table. As its name suggests, it is the primary key of reference for the table and is used throughout the database to help establish relationships with other tables. As with any candidate key the primary key must contain unique values, must never be null and uniquely identify each record in the table.
As an example, a student id might be a primary key in a student table, a department code in a table of all departments in an organisation. This module has the code DH3D 35 that is no doubt used in a database somewhere to identify RDBMS as a unit in a table of modules. In the table below we have selected the candidate key student_id to be our most appropriate primary key

Primary Key


Secondary Key or Alternative Key

A table may have one or more choices for the primary key. Collectively these are known as candidate keys as discuss earlier. One is selected as the primary key. Those not selected are known as secondary keys or alternative keys.
For example in the table showing candidate keys above we identified two candidate keys, studentId and firstName + lastName. The studentId would be the most appropriate for a primary key leaving the other candidate key as secondary or alternative key. It should be noted for the other key to be candidate keys, we are assuming you will never have a person with the same first and last name combination. As this is unlikely we might consider fistName+lastName to be a suspect candidate key as it would be restrictive of the data you might enter. It would seem a shame to not allow John Smith onto a course just because there was already another John Smith.

Simple Key

Any of the keys described before (ie primary, secondary or foreign) may comprise one or more fields, for example if firstName and lastName was our key this would be a key of two fields where as studentId is only one. A simple key consists of a single field to uniquely identify a record. In addition the field in itself cannot be broken down into other fields, for example, studentId, which uniquely identifies a particular student, is a single field and therefore is a simple key. No two students would have the same student number.

Compound Key

A compound key consists of more than one field to uniquely identify a record. A compound key is distinguished from a composite key because each field, which makes up the primary key, is also a simple key in its own right. An example might be a table that represents the modules a student is attending. This table has a studentId and a moduleCode as its primary key. Each of the fields that make up the primary key are simple keys because each represents a unique reference when identifying a student in one instance and a module in the other.

Composite

A composite key consists of more than one field to uniquely identify a record. This differs from a compound key in that one or more of the attributes, which make up the key, are not simple keys in their own right. Taking the example from compound key, imagine we identified a student by their firstName + lastName. In our table representing students on modules our primary key would now be firstName + lastName + moduleCode. Because firstName + lastName represent a unique reference to a student, they are not each simple keys, they have to be combined in order to uniquely identify the student. Therefore the key for this table is a composite key.


23 December 2011

String is Immutable in C# and Java, Use of String builder in C#

String is Immutable in C# and Java, Use of String builder in C#

In C# a string is immutable and cannot be altered. When you alter a string, you are actually creating a new string, which in turn uses more memory than necessary, creates more work for the garbage collector and makes the code execution run slower. When a string is being modified frequently it begins to be a burden on performance .This seemingly innocent example below creates three string objects.

string msg = "Your total is ";//String object 1 is created
msg += "$500 ";            //String object 2 is created
msg += DateTime.Now();   //String object 3 is created

StringBuilder is a string-like object whose value is a mutable sequence of characters. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters. You would modify the above code like this.

StringBuilder sb = new StringBuilder();
sb.Append("Your total is ");
sb.Append("$500 ");
sb.Append(DateTime.Now());

The individual characters in the value of a StringBuilder can be accessed with the Chars property. Index positions start from zero.



21 December 2011

SQL HAVING Clause

SQL HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

for example:

SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000


19 December 2011

SqlBulkCopy in C#


SqlBulkCopy in C#

Lets you efficiently bulk load a SQL Server table with data from another source

 using (SqlBulkCopy bulkCopy =
 new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))
 {
      bulkCopy.DestinationTableName =
      "dbo.BulkCopyDemoMatchingColumns";
     try
     {
         // Write from the source to the destination may be table name or datatable.
         bulkCopy.WriteToServer(dt);
     }
    catch (Exception ex)
    {
          Console.WriteLine(ex.Message);
    }
    finally
    {
       
    }
}

Single responsibility principle in OOPS


Single responsibility principle in OOPS

In object-oriented programming, the single responsibility principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.



The single responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should therefore be in separate classes or modules. It would be a bad design to couple two things that change for different reasons at different times.
The reason it is important to keep a class focused on a single concern is that it makes the class more robust

16 December 2011

Varchar vs nvarchar in Sql Server


Varchar vs nvarchar in Sql Server

1. If your database will not be storing multilingual data you should use the varchar datatype instead. The reason for this is that nvarchar takes twice as much space as varchar.

multilingual means expressed in several languages

2.nvarchar support Unicode

  data types support Unicode data:

  nchar

  nvarchar

  ntext

Unitcode is a computing industry standard for the consistent encoding, representation and handling of text expressed in most of the world's writing systems.


13 December 2011

Session-State Modes in asp.net


Session-State Modes in asp.net


1.InProc mode
2.StateServer
3.SQLServer
4.Custom mode
5.Off mode

InProc mode, which stores session state in memory on the Web server. This is
the default.

StateServer mode, which stores session state in a separate process called
the ASP.NET state service. This ensures that session state is preserved if
the Web application is restarted and also makes session state available to
multiple Web servers in a Web farm.

SQLServer mode stores session state in a SQL Server database. This ensures
that session state is preserved if the Web application is restarted and also
makes session state available to multiple Web servers in a Web farm.

Custom mode, which enables you to specify a custom storage provider.

Off mode, which disables session state.


http://msdn.microsoft.com/en-us/library/ms178586.aspx

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...