0

I'm trying to do a simple a database SQL script where I simply need tocreate a database as well as 4 tables within. So far I'm getting a very annoying error that states: There is already an object named 'DEVICE_TYPE' in the database. Why, I don't know? Any help regarding this matter will be highly appreciated.

Please see the code below:

DROP Database EliteShop
GO

CREATE Database EliteShop
GO

USE EliteShop
GO

CREATE TABLE DEVICE_TYPE
(
    Device_TypeID INT IDENTITY(1,1),
    Device_TypeID_Name VARCHAR(50),
    CONSTRAINT pk_devicetype PRIMARY KEY(Device_TypeID)
)

CREATE TABLE MANUFACTURER
(
    Manufacturer_Code VARCHAR(50),
    Manufacturer_Description VARCHAR(100),
    CONSTRAINT pk_manufacturercode PRIMARY KEY(Manufacturer_Code)
)

CREATE TABLE [PLATFORM]
(
    PlatformID INT IDENTITY(1,1),
    Platform_Description VARCHAR(50),
    CONSTRAINT pk_platID PRIMARY KEY(PlatformID)
)

CREATE TABLE DEVICE
(
    DeviceID INT IDENTITY(1,1),
    Device_TypeID INT,
    PlatformID INT,
    Manufacturer_Code VARCHAR(50),
    Model VARCHAR(50),
    InternalMemory VARCHAR(10),
    Price MONEY,
    CONSTRAINT pk_deviceID PRIMARY KEY (DeviceID),
    CONSTRAINT fk_deviceType FOREIGN KEY (Device_TypeID) REFERENCES DEVICE_TYPE(Device_TypeID),
    CONSTRAINT fk_manuFCode FOREIGN KEY (Manufacturer_Code) REFERENCES MANUFACTURER(Manufacturer_Code),
    CONSTRAINT fk_myPlatID FOREIGN KEY (PlatformID) REFERENCES [PLATFORM](PlatformID)
)
MFJones
  • 39
  • 1
  • 2
  • 8
  • Why not check i the table exists (even when you know it wont) [Check if table exists in SQL Server](http://stackoverflow.com/questions/167576/check-if-table-exists-in-sql-server) and see also [SQL Server: Check if Table or Database Already Exists](http://www.tech-recipes.com/rx/36940/sql-server-check-if-table-or-database-already-exists/) – Adriaan Stander Feb 28 '15 at 12:40
  • The table 'Device_Type' already created in your DB. Use if exists to create or drop and create the table. – SelvaS Feb 28 '15 at 12:43
  • I would be very careful. You should know why objects exist in the database before you start deleting them. – Gordon Linoff Feb 28 '15 at 12:56
  • Well I managed to solve the problem. By deleting the database and run it again. I should just not run the script more than once after all the tables are created otherwise it will think that I want to create another table that already exists. Its not the most effective way but it works when I did it like that. I will try `if exists` later on when necessary. :) – MFJones Mar 01 '15 at 09:54

0 Answers0