2

I am currently trying to make a simple database but everytime I run the script, it creates the database, but does not create any of the tables, any ideas?

use master
IF EXISTS(select * from sys.databases where name = 'MyWebDB')
DROP DATABASE MyWebDB

CREATE DATABASE MyWebDB
DROP TABLE Users
CREATE TABLE Users
(
UserID int IDENTITY PRIMARY KEY,
EmailAddress varchar(100) NOT NULL,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
);
CREATE TABLE Products(
ProductID int IDENTITY PRIMARY KEY,
ProductName varchar(100) NOT NULL
);
DROP TABLE Downloads
CREATE TABLE Downloads(
DownloadID int IDENTITY PRIMARY KEY,
UserID int FOREIGN KEY REFERENCES Users(UserID),
DownloadDate datetime NOT NULL,
FileName varchar(100) NOT NULL,
ProductID int FOREIGN KEY REFERENCES Products(ProductID)
);
5
  • 3
    You get an error for the drop table and the scripts ends? How could the tables even exist if you just created the whole database Commented Jun 5, 2015 at 19:35
  • 2
    use master are the tables going there? Commented Jun 5, 2015 at 19:35
  • 2
    Looks like you're using master and then creating a new database. Your tables may be going to the master database not MyWebDB Commented Jun 5, 2015 at 19:36
  • Ohh, So you have to manually create the database, then create the script to add the tables. Is this what you're getting at? @JamesZ Commented Jun 5, 2015 at 19:38
  • Before your first drop table add a line with the word "GO", then add another line with "USE MyWebDB" Commented Jun 5, 2015 at 20:00

4 Answers 4

4

Get rid of your drop tables and make sure to change your database context by using the USE keyword and the database name. If you want to check for the existence of the table like you are doing with the database, query if exists from sys.tables.

USE master
GO
IF EXISTS(select * from sys.databases where name = 'MyWebDB')
DROP DATABASE MyWebDB

CREATE DATABASE MyWebDB
USE MyWebDB
GO
CREATE TABLE Users
(
UserID int IDENTITY PRIMARY KEY,
EmailAddress varchar(100) NOT NULL,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
);
CREATE TABLE Products(
ProductID int IDENTITY PRIMARY KEY,
ProductName varchar(100) NOT NULL
);
CREATE TABLE Downloads(
DownloadID int IDENTITY PRIMARY KEY,
UserID int FOREIGN KEY REFERENCES Users(UserID),
DownloadDate datetime NOT NULL,
FileName varchar(100) NOT NULL,
ProductID int FOREIGN KEY REFERENCES Products(ProductID)
);

Sign up to request clarification or add additional context in comments.

Comments

3
  1. You have use master so your tables are probably going there.
  2. You're dropping the Users table before you even create it. (Since you dropped the db all tables are implicitly dropped).
  3. You're missing some semicolons at the end of some lines.

1 Comment

Trying to drop nonexisting tables is probably causing an error and the rest of the script doesn't execute.
2

I agree with stazima, you're setting the database to master so your tables are probably there.

Setting the script to use the new database will help:

use master
IF EXISTS(select * from sys.databases where name = 'MyWebDB')
DROP DATABASE MyWebDB

CREATE DATABASE MyWebDB;
USE MyWebDB;
GO

DROP TABLE Users;
CREATE TABLE Users
(
UserID int IDENTITY PRIMARY KEY,
EmailAddress varchar(100) NOT NULL,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
);
CREATE TABLE Products(
ProductID int IDENTITY PRIMARY KEY,
ProductName varchar(100) NOT NULL
);
DROP TABLE Downloads;
CREATE TABLE Downloads(
DownloadID int IDENTITY PRIMARY KEY,
UserID int FOREIGN KEY REFERENCES Users(UserID),
DownloadDate datetime NOT NULL,
FileName varchar(100) NOT NULL,
ProductID int FOREIGN KEY REFERENCES Products(ProductID)
);

Comments

2
use master;
IF EXISTS(select * from sys.databases where name = 'MyWebDB')
DROP DATABASE MyWebDB;

CREATE DATABASE MyWebDB;

-- added line
USE MyWebDB;

DROP TABLE Users;
CREATE TABLE Users
(
UserID int IDENTITY PRIMARY KEY,
EmailAddress varchar(100) NOT NULL,
FirstName varchar(50) NOT NULL,
LastName varchar(50) NOT NULL,
);
CREATE TABLE Products(
ProductID int IDENTITY PRIMARY KEY,
ProductName varchar(100) NOT NULL
);

-- missing ; ?

DROP TABLE Downloads;
CREATE TABLE Downloads(
DownloadID int IDENTITY PRIMARY KEY,
UserID int FOREIGN KEY REFERENCES Users(UserID),
DownloadDate datetime NOT NULL,
FileName varchar(100) NOT NULL,
ProductID int FOREIGN KEY REFERENCES Products(ProductID)
);

5 Comments

The semicolon I'm referring to is at the end of Drop Table downloads; In the original, there was no semicolon on that line. Though, probably also need it on the other drop table lines as well. Drop Table Users; Drop Database MyWebDB; and Create Database MyWebDB;
More importantly, like Darren and Lain pointed out. You need to switch to the WebDB database after you create it.
So do I need to change use master to use MyWebDB?
No, add a line use MyWebDB; after Create Database MyWebDB;
After I did this, it says that it cannot find MyWebDB

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.