SQL Database and Tables Creation step by step
We have installed Sql Server in our previous post here,
now we are going to see how to create database and its table in sql server.
Steps below show how to create a database and its
tables, but we need to install Sql Server Management Studio for Graphical User
Interface program to create and maintain databases.
Now download and install the Sql Server Management
Studio here,
after installing the software, we connect to the sql server using the
Management Studio as shown below
Click on the connect button as shown above after
selecting the Server type, Server name and Authentication.
Server
Type: allows you to select the server type such as Database
Engine, Reporting Services or Integration Services. But in post, we are living
it with the default Server type that is, Database Engine.
Server
name:
Let us select the server we are connecting to, we also allow the default since
we have only one instance.
Authentication: This allows us choose which
authentication to use, either Windows Authentication or SQL Server
Authentication.
Windows
Authentication allows us to use the windows Login
details to connect to the database server.
SQL
Server Authentication enable us to provide the Username and
Password set during SQL Server installation Process.
After successful connection to the Database Server, we
have the below window displayed.
From the above image, we can now see Server Explorer which
shows some of the database objects and default folders such as Databases
folder, Security Folder, Server Object Folder, Replication and the rest.
Our concern in the post is on Databases folder. Let us
expand it and see what it contained.
As we can see below, it contained the previously
created Databases
Let us now create a new Database called
csharpnaija_employees.
Right click on the Databases folder and click on New
Database as shown below
Enter the name of the database and click on ok as
shown below
Now our database is created successfully. We can now
see the database we created in list of the databases in the server explorer.
Now let us create our tables in the database created.
These tables are employees and departments.
Now expand the database that we just created named
csharpnaija_employees
From the list of folders in the expanded table, we can
see that we have Tables folder and others
To create a new Table, right click on the Tables
folder, select new and click on Table… as shown below.
Enter the Column name, Data type and either allow null
or not as shown in the image below
The column names are as follows;
First Name, Middle Name, Last Name, Address, Phone
Number, Date of Birth, Department Id
After entering the columns as shown above, we need to
specify the primary key of the table.
To make the Id field as the primary key of the table,
we right click on the Id column and click on set Primary Key as shown below
To make the Id as auto incremented primary key, click
on the Id field, go to the columns properties below the table properties,
scroll to Identity Specification, double click on the Identity Specification
and double click on ‘Is Identity’ .
Finally, to save the table, press ctrl s on your keyboard
to save the table and provides the table name as shown below.
Enter the name of the table as employees and click on
ok as shown below
We now have a table named employees in the
csharpnaija_employees database as shown below
Repeat the same procedures for creating table, to
create the second table named departments as shown below
To add records to the tables created, we right click
on the departments table and click on Edit Top 200 Rows as shown below
These are some of the records added to the Departments
table as shown below
To add records to the emloyees table, right click on
the Employees table and click on Edit Top 200 Rows as shown below
The data entered into Employees table are shown below
Thank you Tutor. It's really educative sir.
ReplyDeleteYoure welcome, just making it easier for people to be programmers
Delete