• 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

  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Cross-Origin Resource Sharing in WEB API
    In this article, I am going to discuss how to enable Cross-Origin Resource Sharing in Web API which allows cross-domain AJAX calls. Pleas...
  • ASP.NET Web API Basic Authentication
    In this article, I am going to discuss how to implement the ASP.NET Web API Basic Authentication step by step with an example. Please read...
  • Entity Framework Architecture
    In this article, I am going to discuss the Entity Framework Architecture in Detail. Please read our previous article where we discussed th...
  • Comparison Between HttpModule and HttpContext
    Hi friends! Today we are going to list some of the differences between HttpModule and HttpContext. Many of my developer friends are confus...
  • ASP.NET Web API Versioning Using URI
    In this article, I am going to discuss the Web API Versioning Using URI with an example. Please read our previous article where we discuss...
  • Application Life cycle of Asp.Net MVC
    Today i am going to tell you the Application life cycle of an Asp.Net MVC Application, this post is important for those who are new to Asp.N...

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