Working with database

In sql server a database contain some objects like Table,Stored procedure, Views etc.Here will learn how ro use databse in sql server
Create Database
CREATE DATABASE yourDataBaseName
Ex.  Create DATABASE SQLSTACK
You can create database graphically
Go to SQL Server and
1. Right Click on Databases folder in the Object explorer
2. Select New Database
3. In the New Database dialog box, enter the Database name and click OK.
When we create a database it generate 2 files
.MDF file – Data File (Contains actual data)
.LDF file – Transaction Log file (Used to recover the database)
Use Database
SQL Server Database contain many database so for using one database for performing operation there is query like following
USE DatabaseName
Ex. USE SQLSTACK
Drop DATABASE
In SQL Server we can drop a database, Dropping a database in sql server means it will permanently removes all the information stored for the database
DROP DATABASE DatabaseName
USE SQLSTACK
GO 
Ex. DROP DATABASE SQLSTACK
We can’t drop the database which is currently in use. For dropping the database we need to change the contextual database to another database.
To alter a database, once it’s created 
ALTER DATABASE olddatabaseName  modify Name = NewDatabaseName
Like my current database name is “olddatabase” and i want to change is “SQLSTACK” then the query will be
ALTER  DATABASE  olddatabase modify name = SQLSTACK
modify name is the keyword to change the name for database
System Database
System Database
Description
MASTER
Records all system-level information for an instance of SQL Server.
MSDB
Used by SQL Server Agent for scheduling alerts and jobs.
MODEL
Used as the template for all databases created on the instance of SQL Server. Modifications made to the model database, such as database size, collation, recovery model, and other database options, are applied to any databases created afterward.
TEMPDB
It is used for holding temporary objects or intermediate result-sets.



Share this

Related Posts

Previous
Next Post »