Creating Microsoft SQL Server Database
Contents
There are many database management systems available to store data. The most popular of these are MySQL and Microsoft SQL Server. In this post, I will show you how to create a database with Microsoft SQL Server and the relationships between them.
First of all, you must have Microsoft SQL Server installed on your computer. If it is not installed, you can download and install it by clicking here. Since I currently have Microsoft SQL Server Management Studio 18 installed on my computer, I will continue with this version.
Connect to Server
This is how it will open when you first start Management Studio. Here, to do Windows Authentication, select Database Engine as Server Type and write your own server name for Server Name. Server Name is automatically generated.
Since we will process on our own machine here, we performed Windows Authentication. If you want to perform database operations on another server, you can do SQL Server Authentication.
Add a Database
You will now see many folders in Object Explorer. Right-click the Databases folder and click New Database. In the New Database window that opens, type the database name and click the Ok button. We are not concerned with the other settings for now.
Add a Table
After adding the database, you can expand the Databases folder and see the existing databases. Now, expand the database you just created and examine the folders inside it.
Tables folder contains tables where data will be stored. To create a new table, right-click the Tables folder and then follow the path New -> Table. This will open a window like this:
You can set Column Name, Data Type and Allow Nulls options according to the design you want to define our table. Here I want to create a table named Users with UserID, Username, Password and RegisterDate columns. Let’s examine each column one by one.
UserID
The UserID column will be the Primary Key variable for this table. To do this, right click on it and select Set Primary Key. I choose int as Variable Type and want it to automatically increment every time data is added to the table. In Column Properties, change the Identity Specification and Is Identity property to YES as shown in the figure. Also remove the Allow Nulls option if it is selected to prevent blank data entry.
Username
I want the username column to be a string of 10 characters. Again, don’t select Allow Nulls here either.
Password
The password will be a string of 50 characters and null data entry cannot be made.
RegisterDate
Select the Data Type as date in the RegisterDate column because it would be more accurate to enter our data in time and date format. Again, this column is not null.
After doing these operations, save the table with Ctrl + S. While saving, I typed the table name as Users.
Conclusion
In this post, we learned how to create a database and table using Microsoft SQL Server Management Studio. In my next post I will show you queries for adding data to tables, deleting, updating, etc.