Home arrow SQL Server arrow Page 3 - Building Your First SQL Server 2000 Database
SQL SERVER

Building Your First SQL Server 2000 Database


SQL Server 2000 is one of the most popular RDBMS's for the Windows platform. Never worked with SQL Server 2000 before but dying to know how? In this article Mitchell looks at how to create your first database, and also shows you how to manipulate this database through both Enterprise Manager and Query Analyzer.

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 86
April 02, 2002
TABLE OF CONTENTS:
  1. · Building Your First SQL Server 2000 Database
  2. · Creating a database with Enterprise Manager
  3. · Creating a database with Query Analyzer
  4. · Manipulating our database
  5. · Conclusion

print this article
SEARCH DEVARTICLES

Building Your First SQL Server 2000 Database - Creating a database with Query Analyzer
(Page 3 of 5 )

Ahhh, we've arrived at the fun part of the tutorial: coding a database and its tables by hand. Don't be scared off however, because create your databases manually is not difficult at all and it's a great way to learn the TSQL syntax.

We're going to use Query Analyzer to create exactly the same database that we did with Enterprise Manager, so point to Start menu -> Programs -> Microsoft SQL Server and run Query Analyzer. When it loads, you'll be presented with the login screen. I'm assuming that you've just installed a fresh copy of SQL Server 2000, so the default username of sa and no password is fine for now. Click OK to login to your database and start working with Query Analyzer.

First off, let's delete the MyDatabase1 database that we created earlier with Enterprise Manager. The sysdatabases table in the masters database contains entries for each SQL Server 2000 database that exists on our PC. When we created the MyDatabase1 database, a new record was added to the sysdatabases table. Here's how the sysdatabases table looks:

The sysdatabases table of the master database

Using some simple TSQL statements, we can check if the MyDatabase1 database exists in the sysdatabases table of the master database. If it does, then we will delete it. Enter the following code into the Query Analyzer window:

USE MASTER

GO

-- If the database already exists, drop it

IF EXISTS(SELECT * FROM sysdatabases WHERE name='MyDatabase1')

DROP DATABASE MyDatabase1

GO


Lines that start with - are comments

In Enterprise Manager we used the tree view in the left pane to choose which database we wanted to work with. In Query Analyzer, we specify the USE [database name] statement to tell SQL Server 2000 which database we'd like to work with. The GO command executes all code up until that specific point of the script, so after the first GO command, SQL Server knows that any commands it encounters should be executed against the master database.

Obviously the MyDatabase1 database already exists, but let's pretend that we don't know that it does. By using the

SELECT * FROM sysdatabases WHERE name='MyDatabase1'

TSQL command in combination with the IF EXISTS... statement, we're setting up a condition: If any records are returned from the SELECT query to the sysdatabases table of the master database then we proceed to execute the statements that are part of the IF block:

DROP DATABASE MyDatabase1

GO


The DROP DATABASE [database name] statement removes a database from the sysdatabases table and also deletes all of its tables, stored procedures, triggers, etc. We use the DROP DATABASE command to remove the MyDatabase1 database if it exists. At this point we need to flush our TSQL commands to SQL Server, so we use the GO command to do so. We now know for sure that a database called MyDatabase1 doesn't exist.

Add the following code into the Query Analyzer window, right below the code you added previously:

-- Create the MyDatabase1 database

CREATE DATABASE MyDatabase1

GO

USE MyDatabase1

GO


Here we're creating a new database called MyDatabase1. We could specify the location of the data file and its growth settings amongst other things, but we're creating a simple database in this article, so we don't need to. Once we recreate the MyDatabase1 database, we're ready to work with it and we tell SQL Server this with the USE MyDatabase1 command.

In our Enterprise Manager example we used a table designer to create a new table and set the names, values and properties of each column within that table. We can accomplish exactly the same thing with Query Analyzer using some TSQL statements. Add the following code under the existing code in the Query Analyzer window:

-- Create the Widgets table in MyDatabase1

CREATE TABLE Widgets

(

widgetId INT NOT NULL PRIMARY KEY IDENTITY(1,1),

widgetName VARCHAR(50) NOT NULL,

widgetPrice MONEY NOT NULL DEFAULT 0

)

GO


In the code above we've used the CREATE TABLE command to recreate our widgets table. Notice how the widgetId field is specified as a primary key and also as a unique identity. The IDENTITY(1,1) part of the code tells SQL Server that the widgetId field should have a start value of 1 and should be incremented by 1 every time a new record is added to the widgets table. The widgetName field is declared as a varchar of 50 characters in length and is also declared as null with the NOT NULL expression. The last field, widgetPrice is a money field that's not null. I've also used the DEFAULT keyword to set its default value to 0.

Just to recap, here's the code that your query analyzer window should contain:

USE MASTER

GO

-- If the database already exists, drop it

IF EXISTS(SELECT * FROM sysdatabases WHERE name='MyDatabase1')

DROP DATABASE MyDatabase1

GO

-- Create the MyDatabase1 database

CREATE DATABASE MyDatabase1

GO

USE MyDatabase1

GO

-- Create the Widgets table in MyDatabase1

CREATE TABLE Widgets

(

widgetId INT NOT NULL PRIMARY KEY IDENTITY(1,1),

widgetName VARCHAR(50) NOT NULL,

widgetPrice MONEY NOT NULL DEFAULT 0

)

GO


TSQL is not case sensitive.

To execute our TSQL code either press the F5 key or click on the green play button. You should see the following result in the bottom pane of the Query Analyzer window:

The results of executing our TSQL in Query Analyzer

We've just dropped our original MyDatabase1 database and recreated both its structure and its widgets table. The one thing we're missing are the records of the actual widgets. Clear the code in the query analyzer window and enter the following code there:

USE MYDATABASE1

GO

INSERT INTO Widgets(widgetName, widgetPrice) VALUES('Red Widget', 9.95)

INSERT INTO Widgets(widgetName, widgetPrice) VALUES('Blue Widget', 14.50)

INSERT INTO Widgets(widgetName, widgetPrice) VALUES('Green Widget', 24.95)

INSERT INTO Widgets(widgetName) VALUES('Orange Widget')

INSERT INTO Widgets(widgetName, widgetPrice) VALUES('Purple Widget', 1.95)

INSERT INTO Widgets(widgetName) VALUES('Yellow Widget')

GO


Execute the code once again by pressing F5 or clicking on the green play button. For each new record that's added to the widgets table, SQL Server 2000 should respond with "(1 row(s) affected)" in the bottom pane of the query analyzer window.

Lastly, to view the records in the widgets table, clear the code in Query Analyzer and enter and execute the following TSQL code:

USE MYDATABASE1

GO

SELECT * FROM Widgets

ORDER BY widgetName ASC


You should see a list of records from the widgets table in the lower pane of Query Analyzer, just like this:

The result of running our INSERT queries

And there you have it, one fully functional database that you now know how to create with both Enterprise Manager and Query Analyzer. Let's look at using some TSQL statements to manipulate our database on the next page.
blog comments powered by Disqus
SQL SERVER ARTICLES

- Executing SQL Server Stored Procedure from P...
- How to Search for Date and Time Values Using...
- Replication: SQL Server 2000 - Part 2
- Replication: SQL Server 2000 - Part 1
- SQL Sever: Storing Code in Binary or Text Fi...
- Execute SQL on Multiple Tables/Columns - New...
- How to Connect to a SQL Server from Visual F...
- SQL Server Hardware Tuning and Performance M...
- Primary Key on Multiple Tables New RDBMS C...
- Migrating from Sybase to SQL Server
- What's Best for DBAs? GUI or T-SQL Comma...
- How to Perform a SQL Server Performance Audit
- An Introduction To The Bulk Copy Utility
- SQL Server Stored Procedures 101
- Building Your First SQL Server 2000 Database

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials