• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Friday, September 1, 2017

Creating and Working with Database

 Abhishek Tomer     September 01, 2017     Management Studio, SQL Server, SSMS     1 comment   

Hi friends! In our last post we have seen different approaches how we can connect with SQL Server Management Studio(SSMS).

Today, We are going to learn how to create a database and perform tasks on database such as Creating, Altering and Dropping our Database.

So, Let's start with Creating a Database.
We can Create, Alter or Drop a database with 2 methods. Which are
  1. Graphically using SQL Server Management Studio (SSMS)
  2. Using a Query
Let's describe each method one-by-one.
Firstly, to create a database graphically we have to follow some steps
  1. Expand the Database Server by clicking on "+" icon as shown in the picture.
    Database Server
    Database Server
  2. Right Click on Databases folder in the Object Explorer
  3. Click on the New Database option
  4. You will be prompted to enter name of New Database and user to make it as the owner.
  5. In the New Database dialog box, enter the Database name (MyFirstDatabase).
    Adding name to database
    Adding name to database
  6. If You want to change the owner of Database click on the button with ... icon, for default settings we keep it untouched.
  7. Click OK.
  8. Now if you Refresh the databases folder, you can see your Database is created.
    New database created
    New database created
Now, Let's discuss the second method to create a database using a query.
Query to Create Database is:
Create database DatabaseName
Now, Let's find out the background process, what actually is getting executed behind the scenes.
So, Whenever you create a database either graphically or with the help of a Query, the following 2 files gets generated.
  • .MDF file - Data File (Contains actual data) //usually large in size
  • .LDF file - Transaction Log file (Used to recover the database)  // small in size
Now, lets start with the operation on database. To Alter database which we have already created
Alter database DatabaseName Modify Name = DatabaseName
Alternate way to achieve this task is, you can also use system stored procedure 'sp_renameDB'
Execute sp_renameDB 'OldDatabaseName','DatabaseName'
To Delete or Drop an existing database, we can use Drop Statement
Drop Database DatabaseThatYouWantToDrop
Dropping a database, deletes the LDF and MDF files.
If any database is already in use then we cannot perform drop or delete command over it.
You get an error stating - Cannot drop database "DatabaseThatYouWantToDrop" because it is currently in use.
If you still want to delete that type of databases from your server then we have an approach in SQL Server to put that Database in Single User Mode and then we can perform the drop command execution.

To bring database in Single User Mode
Alter Database DatabaseName Set SINGLE_USER With Rollback Immediate
Rollback Immediate option will rollback all the pending Transaction and closes all the connections to the database.
System databases cannot be dropped.
Summary:
So, Guys this is all about Creating and Working with Database

I Hope in this post covered all the points about how we can make a database and perform Alter, Drop and Delete commands on Databases, which will be helpful to understand the concept how SQL Server works.

Please share this post with your friends and colleagues.
For any queries please post a comment below.

Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

1 comment:

  1. AnonymousSeptember 6, 2017 at 12:10 AM

    Nice :)

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • Anonymous Types in C#
    Hi friends! Today we are going to learn about Anonymous Types in C#. Let's start with the Introduction Anonymous is a type that does...
  • Web API Attribute Routing Route Constraints
    In this article, I will discuss the Web API Attribute Routing Route Constraints with examples. We are going to work with the same example ...
  • Entity Types in Entity Framework
    In this article, I am going to discuss the Entity Types in Entity Framework in detail. Please read our previous article where we discussed...
  • What is Web API in Asp.Net
    In this article, I will be introducing you to ASP.NET WEB API Framework. At the end of this article, you will be having a very good underst...
  • Usability of SecureString object in C#
    Introduction Hi friends! In this blog we will be discussing a very interesting as well as useful topic in C# and that is Securestring objec...
  • Reverse Number Program in C# with Examples
    In this article, I am going to discuss the Reverse Number Program in C# with some examples. Please read our previous article where we discu...
  • Connecting to SQL Server using SSMS
    Introduction Hi friends! In this blog we will be discussing How to connect to the SQL Server using SQL Server Management Studio (SSMS). ...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape