blog archive contact about feed

Rob's Blog - September 2009

Contents

Here are Rob's Blog entries for September 2009.

Blog entries for other months can be found in the main blog index.

Links for 2009-09-29

Bookmarks from del.icio.us
Entered: 2009-09-30 00:15:02
TRACKBACK - http://www.robertprice.co.uk/cgi-bin/robblog/trackback.pl?id=1047

Setting An AUTO_INCREMENT Seed In MySQL

I've used MySQL for many years and I've (nearly) always used AUTO_INCREMENT to get unique primary keys for my tables.

I came across a requirement earlier today that meant that I needed an id to start from 100000, instead of the the usual 1.

Normally I'd have just created a dummy record with an id of 100000 so the next insert would occur at 100001. This works as AUTO_INCREMENT automatically inserts the current highest value plus 1 for a column when you insert a row with NULL or 0 for the row's value. This isn't very elegant so I spent a few minutes with the MySQL manual and found that it is possible to seed the AUTO_INCREMENT to start at a specific value without having to use a dummy insert.

When creating a table you add a seed value at the end of the create statement something like this...

CREATE TABLE test( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) ) AUTO_INCREMENT = 100000;

This means the first insert will have an id value of 100001 (the highest current value plus 1).

You can also alter an existing table to set the AUTO_INCREMENT seed...

ALTER TABLE test AUTO_INCREMENT=200000;

The next insert will have an id of 200001 now.

Entered: 2009-09-29 12:03:36
TRACKBACK - http://www.robertprice.co.uk/cgi-bin/robblog/trackback.pl?id=1046

The Null-Coalescing Operator In C#

One bad habit that can occur when using a different programming language than the one you are used to, is writing code that looks very similar to the old language you are used to.

I found myself in this trap earlier, and only spotted it during a code review.

I had been using the ternary operator to compare and return results something like this in C#.

string currentStation = (selectedStation != null ? currentStation : "Q Radio");

It works, but C# provides something far nicer called the null-coalescing operator, ??, which lets you set a default value when you try to assign a nullable type to a non-nullable type.

The example above could be rewritten like this...

string currentStation = selectedStation ?? "Q Radio";

You can read more about the ?? operator for C# on the MSDN website.

Entered: 2009-09-23 20:16:24
Modified: 2009-09-23 20:16:35
TRACKBACK - http://www.robertprice.co.uk/cgi-bin/robblog/trackback.pl?id=1045