Oliver Spryn
Oliver Spryn

Reputation: 17368

"There is already an object named XXXX in the database" after creating New database

I have an SQL file which I am executing on an SQL Server instance which contains the schema for a database. The file creates a brand new database (as in, a database with this name does not exist on this server):

CREATE DATABASE PROJECT;

and begins to create a relation:

CREATE TABLE Courses (
  CourseID INT NOT NULL PRIMARY KEY,
  Name VARCHAR(64) NOT NULL UNIQUE,
  Code CHAR(4) NOT NULL UNIQUE
);

...

and here is what SQL Server tells me right off the bat:

Msg 2714, Level 16, State 6, Line 3
There is already an object named 'Courses' in the database.

Any idea why SQL Server tells me that there already exists a relation by the name of Courses when clearly there isn't?

Thank you for your time.

Upvotes: 3

Views: 10541

Answers (3)

David Lloyd Brookes
David Lloyd Brookes

Reputation: 160

Long shot but try using:

Use project;
CREATE TABLE Courses....

Upvotes: 0

John Woo
John Woo

Reputation: 263933

Check the database if you are using PROJECT

CREATE DATABASE PROJECT
GO

USE PROJECT
GO

CREATE TABLE Courses 
(
  CourseID INT NOT NULL PRIMARY KEY,
  Name VARCHAR(64) NOT NULL UNIQUE,
  Code CHAR(4) NOT NULL UNIQUE
)
GO

Upvotes: 5

Dmitry Frenkel
Dmitry Frenkel

Reputation: 1756

You are likely missing USE PROJECT statement and therefore trying to create Courses table in the master database, not in the PROJECT database.

Upvotes: 3

Related Questions