• 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

  • Web API Versioning using Query String Parameter
    In this article, I am going to discuss Web API Versioning using the Query String Parameter with an example. Please read our previous artic...
  • Static Files Middleware in ASP.NET Core
    In this article, I am going to discuss how to serve static files using Static Files Middleware in ASP.NET Core Application. Please read ou...
  • 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...
  • Custom Method Names in Web API
    In this article, I am going to discuss how to create Custom Method Names in Web API application. Please read our previous article before p...
  • Web API Versioning Using Accept Header
    In this article, I am going to discuss Web API Versioning Using Accept Header with an example. This is a continuation part of our previous...
  • Authentication and Authorization in Web API
    In this article, I am going to discuss the Authentication and Authorization in Web API . Here I will give you an overview of Authentication...
  • Parameter Binding in ASP.NET Web API
    In this article, I am going to discuss one of the most important concepts i.e. Parameter Binding in ASP.NET Web API Application . Please re...

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