Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

Creating a database in Microsoft SQL Server – instruction for beginners

14 September 2020

Creating a database in Microsoft SQL Server - instruction for beginners

In this article, I will talk in detail, especially for beginner programmers, about how to create a database in Microsoft SQL Server, as well as what you should know before creating a database.

Today’s material, as I said, is focused on beginner programmers who want to learn to work with Microsoft SQL Server. That’s why I will proceed from the fact that you need to create a database for training, ie, the main message of this article is that those who want to create a database in Microsoft SQL Server, after reading the article clearly know what he needs to do.

What does it take to create a database in Microsoft SQL Server?

In this section, I will present a kind of steps to create a database in Microsoft SQL Server, i.e. it is exactly what you need to know and what you should have in order to create a database:

  • You must have a Microsoft SQL Server DBMS installed. The free edition of Microsoft SQL Server Express is ideal for training. If you have not yet installed SQL Server, here is a detailed video tutorial, there I show you how to install Microsoft SQL Server 2017 in the Express edition;
  • You must have SQL Server Management Studio (SSMS) installed. SSMS is the main tool, which is used to develop databases in Microsoft SQL Server. This environment is free, if you do not have it, I also show you the installation of this environment in the above video instruction;
  • Design the database. Before you proceed to the creation of a database, you must design it, ie define all the entities that you will store, determine the characteristics that they will have, as well as define all the rules and restrictions that apply to data in the process of adding, storing and modifying them. In other words, you must determine the database structure, what tables it will contain, what relations it will have between the tables, and what columns each table will contain. In our case, i.e. during training, this stage will be more formal, because a beginner will not be able to correctly design the database. But the novice shall know that it is impossible to proceed to the creation of the database without the preliminary design, as to implement the database without a clear idea of how this database should look like in the end, most likely it will not work;
  • Create an empty database. There are two ways to create a database in SQL Server Management Studio environment: the first is using the GUI, the second is using the T-SQL language;
  • Create tables in a database. By this stage, you will already have a database, but it will be empty, as there are no tables in it yet. At this stage, you will need to create tables and appropriate restrictions;
  • Fill the database with data. The database already has tables, but they are empty, so now you can move on to adding data to the tables;
  • Create other objects in the database. You already have a database, tables, and data, so you can develop other database objects, such as:
    views, functions, procedures, triggers, which are used to implement business rules and application logic.

This is the general plan of database creation, which you should know before you start your acquaintance with Microsoft SQL Server and T-SQL language.

In this article, we will consider step 4, this is the creation of an empty database, we will consider both ways to create a database: with the help of GUI and T-SQL language.

The first three steps you should already do, i.e. you already have SQL Server and the Management Studio environment installed, and the sample database structure that you want to implement, as I said, in the training phase, you can skip this point, and in the following materials I will show how to create tables in Microsoft SQL Server, albeit with a simple, but more or less real structure.

Creating a database in SQL Server Management Studio

The first thing you need to do is run SQL Server Management Studio and connect to the SQL server.

run the SQL Server Management Studio and connect to the SQL server

The CREATE DATABASE command is used to create the database.

To create a new database, open SQL Server Management Studio. Click on the server assignment in the Object Explorer window and select New Query in the menu that appears.

The CREATE DATABASE command is used to create the database

Let’s enter the following code in the central field for entering the expressions sql:

CREATE DATABASE usersdb

In this way, we create a database that will be called “usersdb”:

CREATE DATABASE usersdb

To execute the command, click the Execute button on the toolbar or the F5 key. A new database shall appear on the server.

After the database is created, we can set it as current using the USE command:

USE usersdb;

Database attachment

It is possible that we already have a database file, which, for example, was created on another computer. The database file is a file with the extension mdf, and this file in principle we can port. However, even if we copy its computer with MS SQL Server installed, simply copied database will not appear on the server. For this purpose, it is necessary to attach the database to the server. In this case, the expression shall be used:

CREATE DATABASE_data base_name
ON PRIMARY(FILENAME="path_to_mdf_in_local_computer")
FOR ATTACH;

As a directory for the database, it is better to use a directory where the other databases of the server are stored. On Windows 10, the default directory is C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA.

For example, in my case, let’s call the data file userstoredb.mdf. And I want to add this file to the server as a database. First, it must be copied to the above directory. Then, the following command shall be used to attach the database to the server:

CREATE DATABASE contactsdb
ON PRIMARY(FILENAME='C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\userstoredb.mdf')
FOR ATTACH;

After the command is executed, the contactsdb database shall appear on the server.

Deleting the database

To delete the database, the DROP DATABASE command is used, which has the following syntax:

DROP DATABASE database_name1 [, database_name2]...

After the command, we may use a comma to list all the databases to be removed. For example, delete the contactsdb database:

DROP DATABASE contactsdb

It shall be noted that even if the deleted database was attached, all database files shall still be deleted.

How to Create MS SQL Database Using SQL Server Management Studio – For Beginners

 
Tags: , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...