/*============================================================================
  File:     1a_CreateFragmentation.sql

  Summary:  This script creates the GUIDTest database to show
			how design decisions can lead to fragmentation.

  Date:     April 2017

  SQL Server Version: 2005/2008/2008R2/2012/2014/2016
------------------------------------------------------------------------------
  Written by Paul S. Randal, SQLskills.com
	Modified by Erin Stellato, SQLskills.com

  (c) 2015, SQLskills.com. All rights reserved.

  For more scripts and sample code, check out 
    http://www.SQLskills.com

  You may alter this code for your own *non-commercial* purposes. You may
  republish altered code as long as you include this copyright and give due
  credit, but you must obtain prior permission before blogging this code.
  
  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF 
  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 
  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.
============================================================================*/

USE [master];
GO

IF DATABASEPROPERTYEX ('GUIDTest', 'Version') > 0
BEGIN
	DROP DATABASE [GUIDTest];
END

CREATE DATABASE GUIDTest ON PRIMARY (
    NAME = 'GUIDTest_data',
    FILENAME = N'C:\Databases\GUIDTest\GUIDTest_data.mdf',
    SIZE = 400MB,
    FILEGROWTH = 25MB)
LOG ON (
    NAME = 'GUIDTest_log',
    FILENAME = N'C:\Databases\GUIDTest\GUIDTest_log.ldf',
    SIZE = 1MB,
    FILEGROWTH = 5%);
GO

ALTER DATABASE [GUIDTest] SET RECOVERY FULL;
GO

USE [GUIDTest];
GO

SET NOCOUNT ON;
GO

/*
	Create a table with a GUID key
*/
CREATE TABLE [dbo].[BadKeyTable] (
	[c1] UNIQUEIDENTIFIER DEFAULT NEWID () ROWGUIDCOL,
    [c2] DATETIME DEFAULT GETDATE (),
	[c3] CHAR (400) DEFAULT 'a',
	[c4] VARCHAR(MAX) DEFAULT 'b');
GO

CREATE CLUSTERED INDEX [BadKeyTable_CL] ON
	[dbo].[BadKeyTable] ([c1]);
GO

CREATE NONCLUSTERED INDEX [BadKeyTable_NCL] ON
	[dbo].[BadKeyTable] ([c2]);
GO


/*
	Create another one, but using NEWSEQUENTIALID instead
*/
CREATE TABLE [dbo].[NotSoBadKeyTable] (
	[c1] UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID () ROWGUIDCOL,
    [c2] DATETIME DEFAULT GETDATE (),
	[c3] CHAR (400) DEFAULT 'a',
	[c4] VARCHAR(MAX) DEFAULT 'b');
GO

CREATE CLUSTERED INDEX [NotSoBadKeyTable_CL] ON
	[dbo].[NotSoBadKeyTable] ([c1]);
GO

CREATE NONCLUSTERED INDEX [NotSoBadKeyTable_NCL] ON
	[dbo].[NotSoBadKeyTable] ([c2]);
GO


DECLARE @a INT;
SELECT @a = 1;
WHILE (@a < 250000)
BEGIN
	INSERT INTO [dbo].[BadKeyTable] DEFAULT VALUES;
	INSERT INTO [dbo].[NotSoBadKeyTable] DEFAULT VALUES;
	SELECT @a = @a + 1;
END;
GO


SELECT TOP 100 * 
FROM [dbo].[BadKeyTable];

SELECT TOP 100 * 
FROM [dbo].[NotSoBadKeyTable];

DECLARE @BackupPath NVARCHAR(100);
SET @BackupPath = 'C:\Backups\GUIDTest_' + REPLACE(CONVERT(nvarchar(19),SYSDATETIME(), 126), ':','') + '.bak'

BACKUP DATABASE [GUIDTest] 
	TO  DISK = @BackupPath
	WITH NOFORMAT, 
	INIT,   
	COMPRESSION,  
	STATS = 10;
GO