1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

Create Table SQL Server

29/07/2014

Most guides on the SQL 'Create Table' command seem to only include the most basic arguments necessary to create a table with a few columns.

They don't go on to detail how to name constraints, create indexes and add foreign keys or similar.

If you're one of the (seemingly very few) people not using code/model-first Entity Framework I've posted this small snippet to help you. It includes how to:

  • Create and name a primary key.
  • Create and name a foreign key.
  • Create and name a default constraint.
  • Create and name a non-clustered index.
  • Cascade delete.

It does not detail how to create a composite non-clustered index (a non-clustered index on multiple columns).

So, the whole statement is here (note that having a column with the same name as the table will cause problems in EF database-first, the Question property will be renamed Question1):

CREATE TABLE Dog
        (
                Id              INT IDENTITY(-2147483648, 1) CONSTRAINT PK_Dog_Id PRIMARY KEY NOT NULL,
                Title           NVARCHAR(MAX) NULL,
                Name            NVARCHAR(MAX) NOT NULL,
                OwnerId         INT INDEX IX_Dog_Owner NONCLUSTERED 
                CONSTRAINT  FK_Dog_Owner FOREIGN KEY REFERENCES dbo.Owner(Id) 
                ON DELETE CASCADE,
                UserId          INT NOT NULL INDEX IX_Dog_User NONCLUSTERED 
                CONSTRAINT FK_Users_Dog FOREIGN KEY REFERENCES dbo.AspNetUsers(Id),
                CreatedDate     DATETIME2(0) NOT NULL CONSTRAINT DF_Dog_CreatedDate DEFAULT GETDATE(),
                ModifiedDate    DATETIME2(0) NOT NULL CONSTRAINT DF_Dog_ModifiedDate DEFAULT GETDATE(),
                RowVersion      ROWVERSION
        )

To run through a few things to note:

INT IDENTITY(-2147483648, 1)
There's an interesting debate about whether to use a GUID or integer for a primary key, I've gone with integer since to me it's more readable, but a quick Google will provide more than enough reading to the interested coder. I've seeded this primary key with the minimum value for INT, this means values from -2147483648 to 1 aren't wasted.

NVARCHAR(MAX)
NVARCHAR should generally always be used for text columns because it supports Unicode. VARCHAR supports ASCII.

...

You sank my battleship

02/07/2014

The Board Game Battleship

So this is probably one of those things that everyone else but me knows and I'm just going to reveal my ignorance, but as a note to myself for future and for those who don't know, here's how you remove the need for battleship debugging (scatter the code with breakpoints and hope you hit something) in Visual Studio.

This is the same as 'Pause On Exceptions' in a Javascript debugger such as that in Google Chrome and other debuggers, but if you are new to debugging and debuggers, it's not immediately obvious.

Break on all Javascript Exceptions

...

vNext Setup

14/06/2014

ASP.NET vNext is coming and despite the lack of fanfare around the announcement, it's a challenge to describe it without resorting to meaningless buzzwords like 'paradigm-shift'. While it's hard to pick which of the awesome features of vNext to celebrate most, to me the top features are definitely:

  1. Code changes without compile (courtesy of the, also hugely exciting, new compiler Roslyn).
  2. Cross platform ASP.NET in collaboration with Mono.
  3. Open Source, the entire vNext project is open source.

While I love code changes without compile as much as the next developer I think points 2 and 3 are the most exciting in terms of how Microsoft develop and release software in the future. The unprecedented level of transparency around these projects finally puts to bed the Microsoft vs. Open Source community disputes.

One of the best server side languages (ok, I use it for a job so I'm biased) is now available for all developers on all platforms, though this might lose Microsoft some licensing costs for Server OSes and Visual Studio licenses it also opens up the majority of servers (which are not running MS OSes) to ASP.NET (not to mention, who on earth chooses not to use Visual Studio if it's available). Scott Hanselman definitely describes all the new features better than I ever could, so head on over to his site to read more.

That's enough about why vNext is great. How do we actually get it up and running? The instructions on the Git Repository are probably the best on how to install.

Install Git

The vNext project is available as a Git repo, so in order to get it you need to have Git installed. Installing Git for Windows is extremely easy, thanks to these guys.

Once you have it installed, navigate to the appropriate directory (use 'cd' to change directory, just like command line) and type git clone https://github.com/aspnet/Home.git

This should clone the repo to the correct directory, in my case I didn't change directory before clone and it copied to C:\Users\[Username]\Home

...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17