Thursday 30 July 2015

Create a Database in MS Sql Server 2008 R2

How to Create New Database 

     Wel-come to everyone in my second post related to Microsoft Sql Server, in my previous post i describe about the Microsoft sql server and it's different version and list of all version lunch year wise.

     let's start froward one step to in this post i describe about how to create new database in sql server 2008 R2 using Microsoft Management Studio for MSSql 2008 R2, also i explain about how to create new table and save table give a primary key for field in table. 

Start Sql Server Management Studio

  • Goto >>start menu >> All Programs >> Sql Server 2008 R2 >> click on Sql Server Management Studio




  •  Open a login screen for Sql server Give Server Type, Server Name, Authentication, Login and Password and then click on Connect button to Login into Sql server management studio user  Account.


  • In left pen see the Instance name who you login with user name Expand the instance and you see the some folder in this list select first folder name  "Databases" 


Right click on Database Folder >> Select  New Database....


  • open the new database creation window, in this window you can see the field "Database Name" you can give here the name of database you want to create 


Example: i give "EMPDB" for my new database name

  • you can also write a script for query to create new database


select new query >> open query editor window to write script

USE [master]
GO

/****** Object:  Database [EMPDB]    Script Date: 07/30/2015 14:24:26 ******/
CREATE DATABASE [EMPDB] ON  PRIMARY 
( NAME = N'EMPDB', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.DEVELOPER2\MSSQL\DATA\EMPDB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'EMPDB_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.DEVELOPER2\MSSQL\DATA\EMPDB_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO





  • Your database has been created Successful and his display in list of left pan database folder with Database name like "EMPDB" look like this






Create a new Table in Database

  • Expand the Database and select the "Table" folder


Select Table >> Right click >> New Table....



  • Open a design view in the Right pan for table design

i create Table_Emp and add three field

Field         DataType
Id              int
Name        Varchar
Address     Text
Phone        Varchar



  • also, using script to create a new table script is


USE [EMPDB]
GO

/****** Object:  Table [dbo].[Table_Emp]    Script Date: 07/30/2015 14:41:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Table_Emp](
[ID] [int] NOT NULL,
[Name] [varchar](50) NULL,
[Address] [text] NULL,
[Phone] [varchar](50) NULL,
 CONSTRAINT [PK_Table_Emp] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO




  • Set Primary key on ID field


select ID field  >> Right Click >>select Set Primary Key 



  • now save a table with table name "Table_Emp" look on the left pan expand the table and view the columns and key folder to see all the field and primary key in folder




  • all the set for table and database now you can see the data in table 

Right click  on  table name >> Select Top 1000 Rows...



  • Display the result of data in Right pan 
Also you can display using the query from query window

Syntax

SELECT *|<field1>,<field2> FROM <Table_NAME>

Example

select * from Table_Emp

It sql statement Display all record are there in table

        OR

SELECT TOP 1000 [ID]
      ,[Name]
      ,[Address]
      ,[Phone]
  FROM [EMPDB].[dbo].[Table_Emp]

It sql statement Display top 1000 record are there in table

now copmplete the my this post i continue with the new post on my next post...i wish to help this post to understand how to develop database and create a table

my post help full for you so please give us comment and if any query regarding this post also comment us....see you again with new post 

thanks
krunal patel



6 comments:

  1. good one budy i relay helpful for my sql server freshers learn

    ReplyDelete
  2. good one with brief intro clear

    ReplyDelete
  3. i am a new on sql server user, thanks your post helpful for my learning

    thnx krunal

    ReplyDelete
  4. nice post i also help this post to learn sql server

    thnks krunal

    ReplyDelete
  5. good blog i like it,helpful for me

    ReplyDelete
  6. Very nice and brief description of how to start career in sql server
    thanks
    krunal patel

    ReplyDelete