SQL 102 — CREATE DATABASE

The most fundamental task you’ll have to learn to do as a Database Administrator is to create a database. Please remember it’s probably a good idea to make sure you’re connected to the master database on your server when creating databases. Don’t change your connection to your database until you’ve closed out your CREATE DATABASE script. To create a database in SQL, use the following formula:

CREATE DATABASE DatabaseName

Here is an example:

CREATE DATABASE JabbaPalaceInventory;

If you want the name of the database to be in different words, include them in square brackets. Here is an example:

CREATE DATABASE [Jabba Palace Inventory];

Be extremely careful when using spaces in your database names. While it is supported, you can cause problems for your developers. They could build connection strings in such a way that the spaces could be read as terminators and the spaces would break the connection strings. As your developers if they’ve ever had to deal with a database name with spaces, and you’ll usually get a sneer out of them!

If you’re not familiar with my DBA Rules, then you’ll want to read up on rule 4, script everything (and save it). Based on that rule, I’m including my generic CREATE DATABASE script.

/* ----------------------------------------------------------------------------
-- Create database template
-- yyyymmdd sdl: initial commit
-- yyyymmdd sdl: change x
---------------------------------------------------------------------------- */
USE master
GO
-- Make sure your database doesn't already exist
IF  EXISTS (
  SELECT
	name
  FROM sys.databases
  WHERE
    name = N'JabbaPalaceInventory'
)
BEGIN
  PRINT 'The database already exists, DO NOT CONTINUE!'
ELSE
  CREATE DATABASE JabbaPalaceInventory
GO

If you want to create your database visually, please refer to the msdn for those instructions. I’m not going to be the one to hand a monkey a handgun, and expect him not to shoot someone.

If you have any questions, please let me know. I’m here to help!

Tags: , ,

No comments yet.

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.