Posts

Showing posts from April 1, 2012

Composite key vs Candidate Key vs Alternate Keys

Composite Key : A composite key is a combination of more than one column to identify a unique row in a table. Exp : EmpID, EmailID, SSN in Employee table and project ID in project table .if EmpID and project id are put in projectionHours table then combination of empid and project ID called as composite key because combination of these two act as primary key in projectionHours table. Candidate Key: All keys in a table that become unique called as candidate key. Exp : EmpID , EmailID and SSN all will be always unique for any employee in that case all these three columns called as candidate keys. Alternate Key: Among of candidate keys if any single key or combination of keys made as primary key then rest candidate key called as alternate key. Exp : Suppose in employee table EmpID is primary key then Emailid and SSN are called as alternate key mean later on these key can be act as primary key without affecting existing data in table.

How many tables can be used in a single SELECT statement ?

It depends on Version SQL Server 2005: Maximum tables can be 256 SQL Server 2008: Depends on resource availability SQL Server 2008 R2: Depends on resource availability You can confirm by using script given below /*Creating 300 Tables for testing*/ Use [Master] Go Declare @I Int, @Script Varchar(500) Select @I = 1 While (@I <=300) Begin Select @Script = 'Create Table Table' + CAST(@I as varchar) + '(Id Int)' Exec(@Script) Select @I = @I + 1 End Go /*Using all the tables in SELECT statement*/ Use [Master] Go Declare @I Int, @Script Varchar(Max) Select @I = 1 Select @Script = 'Select A1.* From ' While (@I <=299) Begin if (@I >1) Select @Script = @Script + ' Join Table' + CAST(@I+1 as varchar) + ' A' + CAST(@I+1 as varchar) + ' On (' + 'A' + CAST(@I+1 as varchar) + '.Id=' + ' A' + CAST(@I as varchar) + '.Id)' else Select @Script = @Script + 'Table' + CAST