Query a SQL Server using SSMS

Query a SQL Server using SSMS

Hey guyssssssssssssss.... How have we been, if this is your first time reading my article, you are welcome, do well to check other related articles by me, you might find it useful. On that note, let's get down to business.

klaus.gif

Today we would be learning how to query a SQL server using SSMS and this my friends would be a very short write-up. Microsoft SQL Server Management Studio popularly known as SSMS is an advanced development environment that enables us to configure, manage and administrate SQL Server database engines. SQL is popular and widely used majorly because it:

  • is cost free
  • has advanced user experience
  • has various add-in options
  • easy to install

SSMS provides tools to configure, manage and administer instances of Microsoft SQL Server, and it brings together a range of graphical and visual design tools and rich script editors to simplify working with SQL server. SSMS supports most of SQL Server's administrative tasks and maintains a single, integrated environment for SQL Server Database engine management and authoring.

Microsoft SQL Server Management Studio features include Object Explorer, which can view and manage all objects in a SQL Server instance; template explorer, which builds and manages files of text that can be reused to speed up query and script development.

The best way to learn is by practicing, this also applies to this article, so to get the best out of this article, practice along as you read, in that regard it is necessary that you download and install SQL Server Management Studio and SQL Server if you don't have one.

Connecting to an SQL Server instance

Open the SSMS software you downloaded, the first time you run SSMS, the 'Connect to Server' window opens. if this doesn't open, you can open it manually by clicking the 'Connect' dropdown button under the Object Explorer section, for server type, select Database Engine (this is usually the default option), this would open the 'Connect to server' window as seen below.

ssms1.JPG

For Server name, enter the name of your SQL Server instance. I am using localhost instance and 'Windows Authentication' for the authentication segment. There are other forms of authentications, for example SQL server authentication, this would require you to sign in to the SQL Server. You can also modify additional connection options such as choosing the database which you are connected to, connection timeout, etc. by clicking the 'Options' button. After you have filled in all the required fields, select the 'Connect' button to connect to the SQL server instance.

You can decide to work on already databases you have available. To see the all the available databases available to you click on the '+' button just beside 'Databases', this would show the list of all available databases.

ssms2.JPG

For this article I would be creating a new database and name it TrialOne. To do this right click on your server instance in object explorer, and then select new query. In the query window, paste the following code snippet

USE master
GO
IF NOT EXISTS (
    SELECT name
    FROM sys.databases
    WHERE name = N'TrialOne'
)
CREATE DATABASE [TrialOne]
GO

click on the execute button or F5 on your keyboard to execute the query. After the query is complete, refresh the database section under the Object explorer section, you would see that our new TrialOne database appears in the list of databases in the object explorer.

ssms4.JPG

Now that we know how to and we have created a new database, let's create a table in the new database. Now, because the query editor is still in the context of the master database, we need to switch the connection context to the TrialOne database. To do this, select the query editor section, beside the 'Execute' button, you would see a drop down of which 'master' has been selected, click on the arrow-down button to select the database you want to work with, for our case, we would choose TrialOne for this is the database we created, once done, paste the following code in the query window and then run it by selecting the 'Execute' button or the F5 on your keyboard. The code creates a new table, in this case I would call it 'Person'. If the table exists already, it drops it, else it creates the table

USE [TrialOne]
GO
CREATE TABLE PERSON
(
    PersonId    INT    NOT NULL    PRIMARY KEY,
    Name    [NVARCHAR](60)    NOT NULL,
    Address    [NVARCHAR](60)    NOT NULL,
    Email    [NVARCHAR](100)    NOT NULL
);
GO

After the query is complete, refresh Tables under the TrialOne database, there you would see your newly created Table.

ssms5.JPG

Now we know how to create database and also to create table in each database, the next step is to learn how to insert rows in each table. We would be inserting rows into the Person table. To insert rows into the table, paste the following code snippet in the query window and the execute it.

INSERT INTO dbo.Person
(
[PersonId],[Name],[Address],[Email]
)

VALUES 
(1, N'Tobe', N'Lagos', N'tobe@testing.com'),
(2, N'John', N'Earth', N'johndoe@testing.com'),
(3, N'Ciroma', N'Waec paper', N'ciromachukwumaadekunle@testing.com'),
(4, N'Hello', N'World', N'helloworld@testing.com')
GO

The results of queries are always visible below the query text window. In order to see our result, we are going to query the database and view the rows, paste the following and execute it.

SELECT * FROM dbo.Person

ssms6.JPG

Before we wrap up, a quick tip, you can change the server that your current query window is connected to. To do this, right click on the query window that you want to change its server, select Connections and then change connection. The connect to Server window would open and then you can change the server that your query uses.

impressed.gif Short I said it would be, short it was.. or wasn't it?. This just gives you basic knowledge of SSMS. As you can see, we created our database, created a table in the database created and also inserted values into the table.

You can perform more and more SQL queries in SSMS, and if you don't mind I'd advice you practice them. Try creating more tables, this time, with more column fields and data. You can then query the data set, try and get insights based on what you have. You can also work on sample databases such as AdventureWorks, you can get download them here, include them in SSMS and also perform different queries with it. Get acquainted with the tool and its several functionality and you would great with it in no time.

Do well to reach reach out on twitter if you have any questions, or you can mail me directly.

Do well also to check out other articles written my moi.

O B R I G A D O