3

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.

4
  • 1
    after creating the database, try executing this query USE PROJECT;, and run the DDL again. Commented Nov 19, 2012 at 1:44
  • Ohhh.... It was going in the master DB... gotcha! Commented Nov 19, 2012 at 1:45
  • One drawback... After creating the PROJECT database and executing USE PROJECT; I get: Msg 911, Level 16, State 1, Line 3 Database 'PROJECT' does not exist. Make sure that the name is entered correctly. Commented Nov 19, 2012 at 1:46
  • 2
    In SSMS, you'll need a GO between the Create database, and the USE statement Commented Nov 19, 2012 at 1:46

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

4 Comments

I'm not real knowledgeable about the vendor specific T-SQL. Could you explain what GO does, in this case?
GO is not the same thing as ; in mysql. Go separates batches of commands. ; ends a statement (in both mysql and T-SQL) and many statements can be part of a batch. msdn.microsoft.com/en-us/library/ms188037.aspx
@M_M Hmm... I didn't know ; wasn't required in TSQL! Thanks!
Generally not required except using Common table expressions. The WITH must either be the first stament in a batch, or be preceded with a ;
3

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

Comments

0

Long shot but try using:

Use project;
CREATE TABLE Courses....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.