CreateDatabase.sql

  1/*
  2
  3This section is the Men and Mice recommended setup of the database instance. If your organization has different
  4policies then we recommend having your DBA setup the instance according to your polices and skipping to
  5the create database part below.
  6
  7*/
  8-- check NOCOUNT on at session level, perf increase in procedural execs esp
  9SET NOCOUNT ON;
 10
 11
 12EXEC sys.sp_configure N'show advanced options', N'1'  RECONFIGURE WITH OVERRIDE
 13GO
 14
 15EXEC sys.sp_configure N'min server memory (MB)', N'64'
 16GO
 17
 18RECONFIGURE WITH OVERRIDE
 19GO
 20
 21EXEC sys.sp_configure N'show advanced options', N'0'  RECONFIGURE WITH OVERRIDE
 22GO
 23
 24USE [master]
 25GO
 26
 27--set default locations for logs and data (in the example we are using C:\Data change to your setup)
 28
 29--EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultData', REG_SZ, N'<datafilepath>'   ---put path here for example 'D:\Data for the data
 30EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultData', REG_SZ, N'C:\Data'   ---put path here for example 'D:\Data for the data
 31GO
 32
 33--EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultLog', REG_SZ, N'<logfilepath>'     ---put path here for example L:\Logs for the logs
 34EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultLog', REG_SZ, N'C:\Data'     ---put path here for example L:\Logs for the logs
 35GO
 36
 37--Temp: sett default locations for temp log and data (in the examle we use C:\Data)
 38
 39use master
 40go
 41
 42--Alter database tempdb modify file (name = tempdev, filename = '<tempfilepath>\tempdb.mdf')
 43Alter database tempdb modify file (name = tempdev, filename = 'C:\Data\tempdb.mdf')
 44go
 45
 46--Alter database tempdb modify file (name = templog, filename = '<logfilepath>\templog.ldf')
 47Alter database tempdb modify file (name = templog, filename = 'C:\Data\templog.ldf')
 48go
 49
 50
 51/************************************************************************************************/
 52--Restart SQL Server....
 53/************************************************************************************************/
 54
 55
 56USE [master]
 57GO
 58
 59/*
 60add one tempfile per core upto eight cores at a fixed size per file turning off autogrow this depends
 61on how much storage space and how busy the server is, shown below create eight tempfile all 500mb in size
 62*/
 63
 64ALTER DATABASE [tempdb] MODIFY FILE ( NAME = N'tempdev', SIZE = 512000KB , FILEGROWTH = 0)
 65GO
 66
 67--ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_2', FILENAME = N'<tempfilepath>\tempdev_2.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 68ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_2', FILENAME = N'C:\Data\tempdev_2.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 69GO
 70
 71--ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_3', FILENAME = N'<tempfilepath>\tempdev_3.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 72ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_3', FILENAME = N'C:\Data\tempdev_3.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 73GO
 74
 75--ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_4', FILENAME = N'<tempfilepath>\tempdev_4.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 76ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_4', FILENAME = N'C:\Data\tempdev_4.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 77GO
 78
 79--ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_5', FILENAME = N'<tempfilepath>\tempdev_5.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 80ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_5', FILENAME = N'C:\Data\tempdev_5.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 81GO
 82
 83--ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_6', FILENAME = N'<tempfilepath>\tempdev_6.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 84ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_6', FILENAME = N'C:\Data\tempdev_6.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 85GO
 86
 87--ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_7', FILENAME = N'<tempfilepath>\tempdev_7.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 88ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_7', FILENAME = N'C:\Data\tempdev_7.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 89GO
 90
 91--ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_8', FILENAME = N'<tempfilepath>\tempdev_8.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 92ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev_8', FILENAME = N'C:\Data\tempdev_8.ndf' , SIZE = 512000KB , FILEGROWTH = 0)
 93GO
 94
 95ALTER DATABASE [tempdb] MODIFY FILE ( NAME = N'templog', SIZE = 5120000KB , MAXSIZE = 10240000KB , FILEGROWTH = 1024000KB )
 96GO
 97
 98
 99--------------------------------------- Instance level configuration ends --------------------------------------------
100
101
102/*
103Here starts the database creation and configuration for the Men and Mice database.
104*/
105
106/*
107      After creating the database we recommend that these minimum database settings are completed:
108              -Configure the the backup settings according to your organizations backup policy.
109              -You create a sheduled job to reindex the database (e.g. every saturday at 4:00).
110              -
111*/
112
113-- check instance default collation settings
114select convert(sysname, serverproperty('collation'))
115/*
116MM requires case sensitive and accent sensitive collation - We recomend case sensitive and accent sensitive wersion of default collation.
117select the correct collation name :select replace( replace(convert(sysname, serverproperty('collation')), '_CI_', '_CS_'), '_AI', '_AS')
118The collation can be set at database level later in this script (Se CREATE DATABASE command)
119*/
120
121/*
122Create the database instance
123      If you have preference to where the data files should be then you can change this script
124      (you will have to make sure that the path entered exists)
125*/
126
127/*    This deletes the database - please make absolutely certain that this is a database that
128      can be deleted.
129*/
130IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'mmsuite')
131begin
132      USE [master]
133      ALTER DATABASE [mmsuite] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE
134      DROP DATABASE [mmsuite]
135end
136GO
137
138/*
139      This create statement is intended for small scale database. For larger database setups (> tens of GBs) then you must
140      consult with your database administrators, and have them contact Men and Mice support team for additional information.
141*/
142CREATE DATABASE [mmsuite] ON  PRIMARY
143(
144NAME = N'mmsuite', FILENAME = N'C:\Data\mmDatabase\mmsuite.mdf' , SIZE = 20480KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ),
145 FILEGROUP [mmsuiteData]  DEFAULT
146      ( NAME = N'mmsuite_data', FILENAME = N'C:\Data\mmDatabase\mmsuite_data.ndf' , SIZE = 20480KB , MAXSIZE = 200GB, FILEGROWTH = 1024KB )
147 LOG ON
148      ( NAME = N'mmsuite_log', FILENAME = N'C:\Data\mmDatabase\mmsuiteLog.ldf' , SIZE = 20480KB , MAXSIZE = 20GB , FILEGROWTH = 1024KB)
149 COLLATE SQL_Latin1_General_CP1_CS_AS -- CI is required so chosing the default with CS_AS ending (case sensitive and Accent sensitive)
150GO
151EXEC dbo.sp_dbcmptlevel @dbname=N'mmsuite', @new_cmptlevel=100 /*ms*/
152GO
153
154IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
155begin
156      EXEC [mmsuite].[dbo].[sp_fulltext_database] @action = 'disable'
157end
158GO
159
160/*
161      Database setings
162*/
163ALTER DATABASE [mmsuite] SET ANSI_NULL_DEFAULT OFF
164GO
165
166ALTER DATABASE [mmsuite] SET ANSI_NULLS OFF
167GO
168
169ALTER DATABASE [mmsuite] SET ANSI_PADDING OFF
170GO
171
172ALTER DATABASE [mmsuite] SET ANSI_WARNINGS OFF
173GO
174
175ALTER DATABASE [mmsuite] SET ARITHABORT OFF
176GO
177
178ALTER DATABASE [mmsuite] SET AUTO_CLOSE OFF
179GO
180
181ALTER DATABASE [mmsuite] SET AUTO_CREATE_STATISTICS ON
182GO
183
184ALTER DATABASE [mmsuite] SET AUTO_SHRINK OFF
185GO
186
187ALTER DATABASE [mmsuite] SET AUTO_UPDATE_STATISTICS ON
188GO
189
190ALTER DATABASE [mmsuite] SET CURSOR_CLOSE_ON_COMMIT OFF
191GO
192
193ALTER DATABASE [mmsuite] SET CURSOR_DEFAULT  GLOBAL
194GO
195
196ALTER DATABASE [mmsuite] SET CONCAT_NULL_YIELDS_NULL OFF
197GO
198
199ALTER DATABASE [mmsuite] SET NUMERIC_ROUNDABORT OFF
200GO
201
202ALTER DATABASE [mmsuite] SET QUOTED_IDENTIFIER OFF
203GO
204
205ALTER DATABASE [mmsuite] SET RECURSIVE_TRIGGERS OFF
206GO
207
208ALTER DATABASE [mmsuite] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
209GO
210
211ALTER DATABASE [mmsuite] SET DATE_CORRELATION_OPTIMIZATION OFF
212GO
213
214ALTER DATABASE [mmsuite] SET PARAMETERIZATION SIMPLE
215GO
216
217ALTER DATABASE [mmsuite] SET  READ_WRITE
218GO
219
220/* Set the recovery mode - recommended FULL */
221ALTER DATABASE [mmsuite] SET RECOVERY FULL
222GO
223
224ALTER DATABASE [mmsuite] SET  MULTI_USER
225GO
226
227ALTER DATABASE [mmsuite] SET PAGE_VERIFY CHECKSUM
228GO
229
230/*
231      Creates the login for the mmCentral service - this is a MSSQL authenticated login so the server needs
232      to be set to allow such. You can either change the password in this segment or change the password for
233      this user in the database later. DO NOT use the default password given in this file. Experation
234      policy is set to off as this is a service not a user account.
235
236      If you are using Windows integrated authentication then you need to accociate the DBUser (on the mmsuite
237      (defined later))with the Windows user running mmCentral instead of the mmSuiteDBUser login.
238*/
239
240IF  EXISTS (SELECT * FROM sys.server_principals WHERE name = N'mmSuiteDBUser')
241DROP LOGIN [mmSuiteDBUser]
242GO
243
244USE [mmsuite]
245GO
246
247/* NOTE change the password according to your company policy */
248CREATE LOGIN [mmSuiteDBUser] WITH PASSWORD=N'1234',
249      DEFAULT_DATABASE=[mmsuite],
250      CHECK_EXPIRATION=OFF,
251      CHECK_POLICY=OFF
252 GO
253
254/*
255Create a user on the new database. If you are using Windows login then the login needs to point to the Windows user,
256running the mmCentral service.
257*/
258
259USE [mmsuite]
260GO
261 CREATE USER [mmSuiteUser] FOR LOGIN [mmSuiteDBUser]
262 GO
263/*
264Create a role for mm user.
265*/
266CREATE ROLE mmSuiteRole
267
268/*
269Grant normal access
270*/
271USE [mmsuite];
272EXEC sp_addrolemember db_datawriter, mmSuiteRole
273EXEC sp_addrolemember db_datareader, mmSuiteRole
274
275/*
276Authorization, the mmCentral service handles updates of the database when a new version of the system is started for the first time
277To make that possible the following needs to be granted. If youre database administration policy does not agree with granting these
278rights permanently, then please contact Men and Mice support for alternatives.
279
280The mmCentral service runs upgrade scripts on start up, - if needed. This only occurs when starting new version, so ddladmin only
281needs to be given to the role when the system is being run for the first time or is being upgrated. ddladmin can therfore be revoked
282when the system has been started and then given to the role only on upgrades.
283Another alternative is to remove ddladmin rights and get an upgrade script from Men and Mice that has to be run by the DBA.
284The same considerations apply to Grant Create Table.
285*/
286
287ALTER AUTHORIZATION ON SCHEMA::[db_ddladmin] TO mmSuiteRole
288GO
289
290USE [mmsuite]
291GO
292GRANT CREATE TABLE TO mmSuiteRole
293GO
294
295/*Add our user to the mmCentral role*/
296USE [mmsuite]
297GO
298EXEC sp_addrolemember mmSuiteRole, mmSuiteUser
299GO
300
301/*
302Create the default schema for the new user. It is important that the user runing mmCentral has the schema as default schema.
303*/
304CREATE SCHEMA mmCentral AUTHORIZATION mmSuiteUser
305GO
306ALTER user mmSuiteUser with default_schema = mmCentral
307GO
308
309/**
310Now you can start mmCentral, it starts by creating the tables needed.
311**/