Getting Started With SQL Server Management Studio

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

Chapter 1: Getting Started with SQL Server Management Studio

On this part of the lesson, we will learn what is SQL Server management Studio(SSMS) and how to install
it, connect to SQL server using SSMS and the different authentications used to connect to SQL server.
We will also learn the basic creation, alteration and properties of the different common SQL server
objects.

Lesson 1: Installation of SQL Server Management Studio

First, we should get a copy first of the SQL Server management studio. So you can just open your
browser and just like what I am currently using right now, browse for the developer edition which is a
free edition of the SSMS. You can use this edition for testig and studying purposes and you can also use
it to connect to other servers outside of your testing environment.

Now after downloading the file, open it and you will be prompted to select the installation type that you
want, just click on Custom and it will download some essential files for the installation to proceed.
After downloading the needed resources, the installation will continue and it will ask you again for some
of your preferences. Go to Installation>>New SQL Server stand alone installation or add features to
existing installation.

This will ask you to specify a free edition you want to use, just select the Developer edition and there
will be no need to enter a product key, just click on next and continue with the selection of the services.
You will go through some license agreement, rules, setup file installation which won’t really need any
extra effort at all. On the feature selection just click on the Database Engine Services feature and click
next.
After installing the features, you can also specify the server name that you want for your SQL server.
This will serve as the credential instance that you will need to connect to the server.

You will also need to select the authentication mode that you want for your server. We can choose
Mixed-mode Authentication or SQL Server Authentication. If we choose Mixed-mode authentication
then the two options namely “SQL Server Authentication” and “Windows Authentication” will be
available for us. If we only chose SQL Server authentication then we will only have that option. SQL
server Authentication requires the user to enter a Username and Password while Windows
Authentication doesn’t require any of the said credentials, this is because when you want to connect to
your local server and you selected the windows authentication it will automatically get the login
credential you use to open your machine or computer and use them to validate your login to your local
database. On the other had if you are trying to connect to a different server, you need to use SQL Server
Authentication to input the credentials for the server you want to be connected with.
Just click on a lot of next and the installation will eventually finish installing the features you specified.

The other important file that you need to install will be the SQL Server management Tool or commonly
known as SQL Server management Studio(SSMS). This tool will be the media that you will use to connect
with any SQL server that you want to connect with. You just need to download and install the files and
there’s no need for other configurations.
Now we are ready to start treading with our path to being a database administrator!

Lesson 2: Connecting with SQL Server Management Studio

So now that we already have SSMS installed on our machines, let’s learn how to connect to any server
we want to connect with.

We need to know first the difference between an SQL server Management Studio and the SQL Server
database. The SQL server Database typically contains all the data, tables, and anything under the sun
SQL server management object that you created. All the object we created using the SSMS will be stored
on the Database server. SSMS on the other hand is the tool that you need to use to access the Database
server. Keep in mind that SSMS is not a server but just a client tool you use to connect to the real server.

Look for the Microsoft SSMS on your machine and open it. Once you open the SSMS, you should see a
window asking you to connect to a SQL server. This is the where we can specify the server name where
we want to connect and the credentials or authentication we need to have a successful connection.

We need to specify the server type, usually the Database Engine to connect to the Database server.
There are pther server types for SQL services and reporting services to be discussed on the following
chapters, for now let us focus on the database Engine server type. The server name can either be an IP
address or a server name. On my case I already have a test server I created on my local machine upon
installation named ‘ANJO’ which is my PC name. To connect to the local test server I created I will just
use the server name or we can use our machine’s IP address or 127.0.0.1 which is also called a loopback
IP address. In addition we can also use “.” as a server name which specifies that I want to connect to the
local server, or I can just simply type “local” as the server name and it will recognize it that we want to
connect to the local server in our machine.
After specifying the server where we want to connect, we need to choose the authentication we want to
use to connect to the server. Remember upon installation we need to choose the authentication we
want for the server.

You might also encounter an error saying the tool cannot connect to the server or the server cannot be
found. To fix this, make sure that the SQL Server (MSSQLSERVER) on your computer’s Services(local) is
running.

Once the login is done, the object explorer on the left side of the window will show us all the databases,
the security, and the server objects inside the SQL server. The space on the right side of the screen will
be the space where we are going to write our queries.

Lesson 3: Databases

A database is a collection of data in one place, it can either be a hardware or a software used to run a
database – got it? The database may refer to a hardware physical computer which is used t host the
database, in this context it might be a high-end dedicated computer that host the database. As a
software it is the back-end portion of a database application which is sometimes called the instance. On
the SQL server the database can either be a System Database or a User-Defined Database. A System
Defined databases are created by the server itself and they are required for the SQL server to function
well. The User-Defined Databases are the ones we created for the purpose of storing our data.

Now that we have already installed and connected to the server we are ready to study databases.
Always remember that in SQL, most of the things can be done using 2 processes, one is clicking
graphically on the SQL Server Management Studio or you can also use a query.

Creating a Database
To create a database graphically on the SSMS, right click on Database folder on the Object Explorer
window and click New Database. A popup will be shown where you can set the database name. Just click
the OK button and the SSMS will create a query which will create the database. In the example image
below I created a database named “Practice”.

We can also create a database by writing a query. Database is also considered as a type of SQL server
objects same with tables, stored procedures, views, and many more. When we are trying to create any
object in the SQL server we use the ‘Create’ command or statement. For example:

/****To create a database we use****/

Create Database

Just press the execute button at the toolbar or press F5 and this simple query will create a database for
us, same with other objects that we want to create. Always remember to refresh the folder so that the
new database will show up on the object explorer window.

If we want to create a database or any object with a specified name we just put the name we want after
the statement:

/****To create a database named Practice2 we use****/

Create Database Practice2


I would also like you to remember that when you create a database there are two files which are
automatically being created by the server, they are called MDF and LDF files. Go to the folder path
where the SSMS is installed, in my case it’s C:\Program Files\Microsoft SQL
Server\MSSQL14.MSSQLSERVER Serve\MSSQL\DATA\ and you should see these two files. MDF or
Master Data File is actual data file and it contains the data inside your database. LDF stands for
Transaction Log File and is used to recover the database.

Renaming a Database

Again, to rename a database you can use the graphical method or you can write a query but since it is
easy to do it graphically let’s focus more on writing queries.

To modify or rename our database we just use the ‘Alter’ and ‘Modify’ command. Say for example we
want to rename our database from Practice2 to Practice1:

/****To rename database Practice2 to Practice1****/

Alter Database Practice2 Modify Name = Practice1


Or we can also use some stored procedures which is a group of command that are executed together
from our servers which we will discuss in the later part of the discussion:

/****To rename database Practice2 to Practice1****/

Execute sp_renameDB ‘Practice2’,’Practice1’

Deleting a Database

To delete our database we just use the ‘Drop’ command. You can only delete the User-Defined
Databases and not the System Databases. Say for example we want to delete our database which we
renamed as Practice1:

/****To delete database Practice1****/

Drop Database Practice1

Just make sure that when you are deleting a database, that database should not be in use by other
individuals or else you’ll get an error message that the database is in use and cannot be deleted. Also
when you delete a database, the MDF and LDF files will also be deleted.

To make sure that no one is using the database you want to delete and close all active connections to it,
use the command:

/****To set the database as a single user****/

Alter Database Practice1 Set SINGLE_USER With Rollback Immediate


Lesson 4: Tables and Data Types

The database system contains one or more objects called tables. All the data or information are stored
in these tables which consists of columns and rows and they are uniquely identified by their names. The
rows contain the data for the columns or the record that the user input on the database. The columns
contain the headers like Column name, the data type, and any other attributes given to a column.

First we need to understand that within a table there are so called Key constraints namely: Primary Key
and Foreign Key constraints. The Primary Key (PK) constraint is used or setup to tell the server that the
column, usually the first column, will be the unique identifier for each row in the table created, meaning
the server will not allow two rows with the same data under this column with a PK constraint. The
Foreign key (FK) constraint is either a colun or a combination of columns which is used to establish a link
between the data from different tables. The FK constraint is given to columns that are common on both
tables that we want to link. Note that a table can referece a maximum of 253 other tables as foreign
keys.

To create, alter and delete tables we just use the normal commands for objects same with what we did
for the database grahically. We use the ‘Create’, ‘Alter’, and ‘Drop’ commands to modify the table we
want except we need to specify the rows and columns we want to include in our tables and the
properties of each.

/****To create a table named Gender1 with column ID and column occupation and does not include null
values we use****/

Create Table Gender1


(ID int not Null Primary Key, Occupation varchar(50) )
The query statement above creates a table ‘Practice1’ with column name ‘ID’. After the column name
we specified the data type and put ‘int’ which means that the column Id will only accept integers as
values. According to an article from http://www.cs.toronto.edu/~nn/csc309-
20085/guide/pointbase/docs/html/htmlfiles/dev_datatypesandconversionsFIN.html

Data Types

CHARACTER [(length)] or CHAR [(length)]

The CHARACTER data type accepts character strings, including Unicode, of a fixed length. The length of the
character string should be specified in the data type declaration; for example, CHARACTER(n) where n represents
the desired length of the character string. If no length is specified during the declaration, the default length is 1.

The minimum length of the CHARACTER data type is 1 and it can have a maximum length up to the table page size.
Character strings that are larger than the page size of the table can be stored as a Character Large Object (CLOB).

NOTE: CHARACTER(0) is not allowed and raises an exception.

If you assign a value to a CHARACTER column containing fewer characters than the defined length, the remaining
space is filled with blanks characters. Any comparisons made to a CHARACTER column must take these trailing
spaces into account.

Attempting to assign a value containing more characters than the defined length results in the truncation of the
character string to the defined length. If any of the truncated characters are not blank, an error is raised.

Character String Examples:

CHAR(10) or CHARACTER(10)

Valid

'Race car'

'RACECAR'

'24865'

'1998-10-25'

'1998-10-25 ' (Blank characters are truncated)

Invalid

24865

1998-10-25

'Date: 1998-10-25'

VARCHAR (length)
The VARCHAR data type accepts character strings, including Unicode, of a variable length is up to the maximum
length specified in the data type declaration.

A VARCHAR declaration must include a positive integer in parentheses to define the maximum allowable character
string length. For example, VARCHAR(n) can accept any length of character string up to n characters in length. The
length parameter may take any value from 1 to the current table page size. Attempting to assign a value containing
more characters than the defined maximum length results in the truncation of the character string to the defined
length. If any of the truncated characters are not blank, an error is raised.

NOTE: VARCHAR(0) is not allowed and raises an exception.

If you need to store character strings that are longer than the current table page size, the Character Large Object
(CLOB) data type should be used.

Examples

VARCHAR(10)

Valid

'Race car'

'RACECAR'

'24865'

'1998-10-25'

'1998-10-25 '

Invalid

24865

1998-10-25

'Date: 1998-10-25'

BOOLEAN

The BOOLEAN data type supports the storage of two values: TRUE or FALSE. No parameters are required when
declaring a BOOLEAN data type.

Use the case insensitive keywords TRUE or FALSE to assign a value to a BOOLEAN data type. Comparisons using the
BOOLEAN data type should also use these keywords. If you attempt to assign any other value to a BOOLEAN data
type, an error is raised.

Examples

BOOLEAN

Valid
TRUE

true

True

False

Invalid

Yes

No

SMALLINT

The SMALLINT data type accepts numeric values with an implied scale of zero. It stores any integer value between
the range 2^ -15 and 2^15 -1. Attempting to assign values outside this range causes an error.

If you assign a numeric value with a precision and scale to a SMALLINT data type, the scale portion truncates,
without rounding.

NOTE: To store values beyond the range (2^-15) to (2^15)-1, use the INTEGER data type.

Examples

SMALLINT

Valid

-32768

-30.3 (digits to the right of the decimal point are truncated)

32767

Invalid

-33,000,567

-32769

32768

1,897,536,000

INTEGER or INT
The INTEGER data type accepts numeric values with an implied scale of zero. It stores any integer value between
the range 2^ -31 and 2^31 -1. Attempting to assign values outside this range causes an error.

If you assign a numeric value with a precision and scale to an INTEGER data type, the scale portion truncates,
without rounding.

NOTE: To store integer values beyond the range (2^-31) to (2^31)-1, use the DECIMAL data type with a scale of
zero.

Examples

INTEGER or INT

Valid

-2147483648

-1025

1025.98 (digits to the right of the decimal point are truncated)

2147483647

Invalid

-1,025,234,000,367

-2147483649

2147483648

1,025,234,000,367

DECIMAL [(p[,s])] or DEC [(p[,s])]

The DECIMAL data type accepts numeric values, for which you may define a precision and a scale in the data type
declaration. The precision is a positive integer that indicates the number of digits that the number will contain. The
scale is a positive integer that indicates the number of these digits that will represent decimal places to the right of
the decimal point. The scale for a DECIMAL cannot be larger than the precision.

DECIMAL data types can be declared in one of three different ways. The declaration of it controls how the number
is presented to an SQL query, but not how it is stored.

DECIMAL - Precision defaults to 38, Scale defaults to 0

DECIMAL(p) - Scale defaults to 0

DECIMAL(p, s) - Precision and Scale are defined by the user

In the above examples, p is an integer representing the precision and s is an integer representing the scale.
NOTE: If you exceed the number of digits expected to the left of the decimal point, an error is thrown. If you
exceed the number of expected digits to the right of the decimal point, the extra digits are truncated.

Examples

DECIMAL(10,3)

Valid

1234567

1234567.123

1234567.1234 (Final digit is truncated)

-1234567

-1234567.123

-1234567.1234 (Final digit is truncated)

Invalid

12345678

12345678.12

12345678.123

-12345678

-12345678.12

-12345678.123

NUMERIC [(p[,s])]

PointBase treats the NUMERIC data type in exactly the same way as the DECIMAL data type.

REAL

The REAL data type accepts approximate numeric values, up to a precision of 64. No parameters are required
when declaring a REAL data type. If you attempt to assign a value with a precision greater than 64 an error is
raised.

Examples

REAL

Valid

-2345
0

1E-3

1.245

123456789012345678901234567890

Invalid

123,456,789,012,345,678,901,234,567,890,123

FLOAT(p)

The FLOAT data type accepts approximate numeric values, for which you may define a precision up to a maximum
of 64. If no precision is specified during the declaration, the default precision is 64. Attempting to assign a value
lager than the declared precision will cause an error to be raised.

Examples

FLOAT(8)

Valid

12345678

1.2

123.45678

-12345678

-1.2

-123.45678

Invalid

123456789

123.456789

-123456789

-123.456789

DOUBLE PRECISION

The REAL data type accepts approximate numeric values, up to a precision of 64. No parameters are required
when declaring a DOUBLE PRECISION data type. If you attempt to assign a value with a precision greater than 64
an error is raised.

Examples
DOUBLE PRECISION

Valid

12345678901234567890123456789012345678901234567890123456

7890

-12345678901234567890123456789012345678901234567890123456

7890

Invalid

123,456,789,012,345,678,901,234,567,890,123,123,456,789,

012,345,678,901,234,567,890

-123,456,789,012,345,678,901,234,567,890,123,123,456,789,

012,345,678,901,234,567,890

DATE

The DATE data type accepts date values. No parameters are required when declaring a DATE data type. Date
values should be specified in the form: YYYY-MM-DD. However, PointBase will also accept single digits entries for
month and day values.

Month values must be between 1 and 12, day values should be between 1 and 31 depending on the month and
year values should be between 0 and 9999.

Values assigned to the DATE data type should be enclosed in single quotes, preceded by the case insensitive
keyword DATE; for example, DATE '1999-04-04'.

Examples

DATE

Valid

DATE '1999-01-01'

DATE '2000-2-2'

date '0-1-1'
Invalid

DATE '1999-13-1'

date '2000-2-30'

'2000-2-27'

date 2000-2-27

TIME

The TIME data type accepts time values. No parameters are required when declaring a TIME data type. Date values
should be specified in the form: HH:MM:SS. An optional fractional value can be used to represent nanoseconds.

The minutes and seconds values must be two digits. Hour values should be between zero 0 and 23, minute values
should be between 00 and 59 and second values should be between 00 and 61.999999.

Values assigned to the TIME data type should be enclosed in single quotes, preceded by the case insensitive
keyword TIME; for example, TIME '07:30:00'.

Examples

TIME

Valid

TIME '00:00:00'

TIME '1:00:00'

TIME '23:59:59'

time '23:59:59.99'

Invalid

TIME '00:62:00'

TIME '00:3:00'

TIME '23:01'

'24:01:00'

TIMESTAMP

The TIMESTAMP data type accepts timestamp values, which are a combination of a DATE value and a TIME value.
No parameters are required when declaring a TIMESTAMP data type. Timestamp values should be specified in the
form: YYYY-MM-DD HH:MM:SS. There is a space separator between the date and time portions of the timestamp.

All specifications and restrictions noted for the DATE and TIME data types also apply to the TIMESTAMP data type.
Values assigned to the TIMESTAMP data type should be enclosed in single quotes, preceded by the case insensitive
keyword TIMESTAMP; for example, TIMESTAMP '1999-04-04 07:30:00'.

Examples

TIMESTAMP

Valid

TIMESTAMP `1999-12-31 23:59:59.99'

TIMESTAMP `0-01-01 00:00:00'

Invalid

1999-00-00 00:00:00

TIMESTAMP `1999-01-01 00:64:00'

CLOB [(length)] or CHARACTER LARGE OBJECT [(length)] or CHAR LARGE OBJECT [(length)]

The Character Large Object (CLOB) data type accepts character strings longer than those that are allowed in the
CHARACTER [(length)] or VARCHAR (length) data types. The CLOB declaration uses the following syntax to specify
the length of the CLOB in bytes:

n [K | M | G]

In the above syntax, n is an unsigned integer that represents the length. K, M, and G correspond to Kilobytes,
Megabytes or Gigabytes, respectively. If K, M, or G is specified in addition to n, then the actual length of n is the
following:

K = n * 1024

M = n * 1,048,576

G = n * 1,073,741,824

The maximum size allowed for CLOB data types is 2 gigabytes. If a length is not specified, then a default length of
one byte is used. CLOB values can vary in length from one byte up to the specified length.

NOTE: The CLOB data type supports Unicode data.

BLOB [(length)] or BINARY LARGE OBJECT [(length)]

The Binary Large Object (BLOB) data type accepts binary values. The BLOB declaration uses the following syntax to
specify the length in bytes:

n [K | M | G]

In the above syntax, n is an unsigned integer that represents the length. K, M, and G correspond to Kilobytes,
Megabytes or Gigabytes, respectively. If K, M, or G is specified in addition to n, then the actual length of n is the
following:
K = n * 1024

M = n * 1,048,576

G = n * 1,073,741,824

The maximum size allowed for BLOB data types is 2 gigabytes. If a length is not specified, then a default length of
one byte is used. BLOB values can vary in length from one byte up to the specified length.

NOTE: BLOB data types cannot be used with SQL scalar functions.

/****To rename table Practice to Practice1****/

Alter Table Practice Modify Name = Practice1

/****To delete Table Practice1****/

Drop Table Practice1

Let us now focus more on the functions and usage of the key constraints for each table on an SQL server
database.

You might also like