Question

I have following script. I don't know how to create new database in sql server 2005. I run following script, and it create tables under model instead of seperate database. it look massy.

how can I create separate database. I am copy some script here for your adive and course of action.

-----------------------------------------------------------
-- SQL Server 2000 Bible 
-- Hungry Minds 
-- Paul Nielsen

-- OBX Kites sample database - CREATE Database, Tables, and Procs

-- this script will drop an existing OBXKites database 
-- and create a fresh new installation

-- related scripts:
-- OBXKites_Populate

-- T-SQL KEYWORDS go
-- DatabaseNames   

-----------------------------------------------------------
-----------------------------------------------------------
-- Drop and Create Database


USE master
GO
IF EXISTS (SELECT * FROM SysDatabases WHERE NAME='OBXKites')
  DROP DATABASE OBXKites
go

-- This creates 1 database that uses 2 filegroups
CREATE DATABASE OBXKites
  ON PRIMARY
    (NAME = 'OBXKites', FILENAME = 'D:\SQLData\OBXKites.mdf'),
  FILEGROUP Static
    (NAME = 'OBXKitesStatic', FILENAME = 'c:\SQLData\OBXKitesStatic.ndf')
  LOG ON (NAME = 'OBXKitesLog',  FILENAME = 'c:\SQLData\OBXKites.ldf')
go

-- set to Full Log

go

SET QUOTED_IDENTIFIER ON
go
USE OBXKites
go

-----------------------------------------------------------
-----------------------------------------------------------
-- Create Tables, in order from primary to secondary

CREATE TABLE dbo.OrderPriority (
  OrderPriorityID UNIQUEIDENTIFIER NOT NULL ROWGUIDCOL DEFAULT (NEWID()) PRIMARY KEY NONCLUSTERED,
  OrderPriorityName NVARCHAR (15) NOT NULL,
  OrderPriorityCode NVARCHAR (15) NOT NULL,
  Priority INT NOT NULL
  )
  ON [Static]
go  
Was it helpful?

Solution

CREATE DATABASE OBXKites
ON PRIMARY
     (NAME = 'OBXKites', FILENAME = 'D:\SQLData\OBXKites.mdf'),  
   FILEGROUP Static
     (NAME = 'OBXKitesStatic', FILENAME = 'c:\SQLData\OBXKitesStatic.ndf')
LOG ON 
   (NAME = 'OBXKitesLog',  FILENAME = 'c:\SQLData\OBXKites.ldf')
go

This code in your script should create a new database. If no error occurrs in the process.

OTHER TIPS

Can you simplify it down? or do you have to do all of that checking?

If it can be simplified I beileve some code such as:

CREATE DATABASE OBXKites

USE OBXKites

CREATE TABLE OrderPriority(syntax)

If simple code like this doesn't work, you may have deeper issues with rights or files or something as suggested by queen3 just before me.

Afterthought: At my school we were having issues like this as well I believe... after a few classes we finally arived at the conclusion that we didn't have rights to add files to the program files directory on the lab computers and SQL wasn't reporting the error to us.

Are there any errors in output? The script itself does already do what you requested.

I'd suspect that you incorrectly specified filenames - some of them are on drive C:, and some on drive D:.

Your script in question will create the tables in the OBXKites database (see the USE OBXKites statement in your script)

Does the OBXKites database already exist in your server instance? If so, that's probably why it appears that it is creating the tables in the current database, since the script is effectively removing and recreating the OBXKites database. If you need a different name for the database, find all instances of "OBXKites" in your script above after the line that reads "This creates 1 database that uses 2 filegroups" and rename it to the new database name you desire.

The only other option could be that the script has execution errors creating the database, if your user account lacks the proper permissions to do so. However, the script continues to execute, creating the tables in the current database you are logged in to, which is why you are seeing the tables there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top