Home > front end >  Error Code: 1007. Can't create database 'CustomerDataService'; database exists 0.0002
Error Code: 1007. Can't create database 'CustomerDataService'; database exists 0.0002

Time:10-08

I am new to using MYSQL and MYSQL workbench. I have the error code above and not sure how to resolve that. Here is my code below

CREATE DATABASE CustomerDataService;

USE CustomerDataService;

CREATE TABLE Customers(
    CustomerId INT NOT NULL AUTO_INCREMENT,
    FirstName VARCHAR(1000) NOT NULL,
    MiddleName VARCHAR(1000),
    LastName VARCHAR(1000) NOT NULL,
    PRIMARY KEY (CustomerId)
);

CREATE TABLE Emails(
    EmailId INT NOT NULL AUTO_INCREMENT,
EmailAddress VARCHAR(1000) NOT NULL,
    IsSubscribed BOOLEAN NOT NULL,
PRIMARY KEY (EmailId)
);

ALTER TABLE Customers ADD COLUMN EmailId INT;
ALTER TABLE Customers ADD CONSTRAINT FK_CustomerEmail FOREIGN KEY (EmailId) REFERENCES Emails(EmailID);
INSERT INTO Emails (EmailAddress, IsSubscribed) VALUES("[email protected]", TRUE);
INSERT INTO Customers (FirstName, LastName, EmailId) VALUES("Fname", "Lname", 1);
SELECT * FROM Customers;
SELECT * FROM Emails;

CodePudding user response:

Either remove the CREATE DATABASE line, or replace it with

CREATE DATABASE IF NOT EXISTS `CustomerDataService`;

Chances are that the scriptlet had already been run, and the tables already exist, too, so you may then run into Table already exists errors next.

The other, brute force, alternative would be to:

DROP DATABASE `CustomerDataService`;

But only try that if you are really sure you want to delete what is already there, and have a good backup just in case ...

  • Related