how to create Primary key with a specific name when creating the table itself.
I wanted to know if we can create Primary Key as part of the table name as well, and also give it a name at the same time. Though it would look very normal to all experienced developers, it can be still confusing to many.
What am I doing wrong?
CREATE TABLE [dbo].[TestTable](
[ID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](100) NULL)
GO
ALTER TABLE [dbo].[TestTable] ADD CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED
([ID] ASC)
GO
Thanks,Mahendra
foreachenumerates any object that implementsIEnumerable,IEnumerable<T>,IEnumerator, orIEnumerator<T>(or actually any object whose class implements a compatibleCurrentproperty andMoveNext()method). The syntax forforeachis:typeis the type of the newvariablevariable. (If you already have an existing variable to use then omit the type.)The body of the loop will be executed for each object in the enumerable, and the iteration variable (here
variable) will contain the object on each iteration.foris much more general-purpose, and follows this pattern:Which is almost exactly equivalent to:
You may see a
forloop when dealing with arrays, for example:This is the same thing as:
(Note that using
foreachover arrays is optimized by the compiler to use a similar index-based iteration, since this is faster. So all three approaches should have equivalent performance and runtime semantics when enumerating an array.)Further information, if you want it:
foreachis actually just syntactic sugar, and can be easily translated into awhileloop.This actually means:
Except that the
enumeratorvariable has no name and is hidden from you.It’s pretty obvious why
foreachexists. Everywhere you use it today you would otherwise have to write this horribly verbose boilerplate code.foreachtakes care of everything for you — grabbing an enumerator, testingMoveNext()on each iteration, extracting the enumerator’s current value, and disposing of the enumerator after iteration completes.1 This variable is actually semantically declared inside of the loop, starting with C# 5. This example is accurate for C# 4 and predecessors.