database creation with user name and password

how can i write query that will create a database. Include the creation of a user named user1 with a password of pword1
hnakumAsked:
Who is Participating?
 
MuffyBunnyCommented:
Obviously you'll want to script some tables and stuff to go with it, but here's a script that creates a login on the SQL server, then creates a blank database and assigns that login to your newly created database as a user:

USE [master]
GO

CREATE LOGIN [user1] WITH PASSWORD=N'pword1'
GO

CREATE DATABASE [whatever] ON  PRIMARY 
( NAME = N'whatever', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.IT380LOGI\MSSQL\DATA\whatever.mdf' , SIZE = 2048KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'whatever_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.IT380LOGI\MSSQL\Log\whatever_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

ALTER DATABASE [whatever] SET COMPATIBILITY_LEVEL = 100
GO

IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [whatever].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO

ALTER DATABASE [whatever] SET ANSI_NULL_DEFAULT OFF 
GO

ALTER DATABASE [whatever] SET ANSI_NULLS OFF 
GO

ALTER DATABASE [whatever] SET ANSI_PADDING OFF 
GO

ALTER DATABASE [whatever] SET ANSI_WARNINGS OFF 
GO

ALTER DATABASE [whatever] SET ARITHABORT OFF 
GO

ALTER DATABASE [whatever] SET AUTO_CLOSE OFF 
GO

ALTER DATABASE [whatever] SET AUTO_CREATE_STATISTICS ON 
GO

ALTER DATABASE [whatever] SET AUTO_SHRINK OFF 
GO

ALTER DATABASE [whatever] SET AUTO_UPDATE_STATISTICS ON 
GO

ALTER DATABASE [whatever] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO

ALTER DATABASE [whatever] SET CURSOR_DEFAULT  GLOBAL 
GO

ALTER DATABASE [whatever] SET CONCAT_NULL_YIELDS_NULL OFF 
GO

ALTER DATABASE [whatever] SET NUMERIC_ROUNDABORT OFF 
GO

ALTER DATABASE [whatever] SET QUOTED_IDENTIFIER OFF 
GO

ALTER DATABASE [whatever] SET RECURSIVE_TRIGGERS OFF 
GO

ALTER DATABASE [whatever] SET  DISABLE_BROKER 
GO

ALTER DATABASE [whatever] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO

ALTER DATABASE [whatever] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO

ALTER DATABASE [whatever] SET TRUSTWORTHY OFF 
GO

ALTER DATABASE [whatever] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO

ALTER DATABASE [whatever] SET PARAMETERIZATION SIMPLE 
GO

ALTER DATABASE [whatever] SET READ_COMMITTED_SNAPSHOT OFF 
GO

ALTER DATABASE [whatever] SET HONOR_BROKER_PRIORITY OFF 
GO

ALTER DATABASE [whatever] SET  READ_WRITE 
GO

ALTER DATABASE [whatever] SET RECOVERY FULL 
GO

ALTER DATABASE [whatever] SET  MULTI_USER 
GO

ALTER DATABASE [whatever] SET PAGE_VERIFY CHECKSUM  
GO

ALTER DATABASE [whatever] SET DB_CHAINING OFF 
GO

USE [whatever]
GO

CREATE USER [user1] FOR LOGIN [user1] WITH DEFAULT_SCHEMA=[db_owner]
GO

Open in new window

0
 
hnakumAuthor Commented:
Very good
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.