SQL Server 2005 – Export table programmatically (run a .sql file to rebuild it)

This functionality is already built in to Sql Server Management Studio 2008. Just download the trial and only install the client tools (which shouldn’t expire). Use Management Studio 2008 to connect to your 2005 database (its backwards compatible). Right click your database Choose Tasks > Generate Scripts Press Next, select your database again On the … Read more

PostgreSQL create table if not exists

This feature has been implemented in Postgres 9.1: CREATE TABLE IF NOT EXISTS myschema.mytable (i integer); For older versions, here is a function to work around it: CREATE OR REPLACE FUNCTION create_mytable() RETURNS void LANGUAGE plpgsql AS $func$ BEGIN IF EXISTS (SELECT FROM pg_catalog.pg_tables WHERE schemaname=”myschema” AND tablename=”mytable”) THEN RAISE NOTICE ‘Table myschema.mytable already exists.’; … Read more

Is there any reason to worry about the column order in a table?

Column order had a big performance impact on some of the databases I’ve tuned, spanning Sql Server, Oracle, and MySQL. This post has good rules of thumb: Primary key columns first Foreign key columns next. Frequently searched columns next Frequently updated columns later Nullable columns last. Least used nullable columns after more frequently used nullable … Read more

Why use multiple columns as primary keys (composite primary key)

Your understanding is correct. You would do this in many cases. One example is in a relationship like OrderHeader and OrderDetail. The PK in OrderHeader might be OrderNumber. The PK in OrderDetail might be OrderNumber AND LineNumber. If it was either of those two, it would not be unique, but the combination of the two … Read more