text: /****** Object:  Table [dbo].[CDS]    Script Date: 11/26/2013 5:26:45 PM ******/
  
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CDS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[CDS](
  	[CD_ID] [bigint] NOT NULL,
   CONSTRAINT [PK_cds_CD_ID] PRIMARY KEY CLUSTERED 
  (
  	[CD_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  
  /*** _STATEMENT_BREAK_ ***/
  /****** Object:  Table [dbo].[SERDES]    Script Date: 11/26/2013 5:26:49 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SERDES]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[SERDES](
  	[SERDE_ID] [bigint] NOT NULL,
  	[NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[SLIB] [nvarchar](4000) NULL DEFAULT NULL,
   CONSTRAINT [PK_serdes_SERDE_ID] PRIMARY KEY CLUSTERED 
  (
  	[SERDE_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  /*** _STATEMENT_BREAK_ ***/
  /****** Object:  Table [dbo].[SDS]    Script Date: 11/26/2013 5:26:48 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SDS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[SDS](
  	[SD_ID] [bigint] NOT NULL,
  	[CD_ID] [bigint] NULL DEFAULT NULL,
  	[INPUT_FORMAT] [nvarchar](4000) NULL DEFAULT NULL,
  	[IS_COMPRESSED] [bit] NOT NULL,
  	[IS_STOREDASSUBDIRECTORIES] [bit] NOT NULL,
  	[LOCATION] [nvarchar](4000) NULL DEFAULT NULL,
  	[NUM_BUCKETS] [int] NOT NULL,
  	[OUTPUT_FORMAT] [nvarchar](4000) NULL DEFAULT NULL,
  	[SERDE_ID] [bigint] NULL DEFAULT NULL,
   CONSTRAINT [PK_sds_SD_ID] PRIMARY KEY CLUSTERED 
  (
  	[SD_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[SDS]  WITH CHECK ADD  CONSTRAINT [SDS_FK1] FOREIGN KEY([SERDE_ID])
  REFERENCES [dbo].[SERDES] ([SERDE_ID])
  ALTER TABLE [dbo].[SDS] CHECK CONSTRAINT [SDS_FK1]
  ALTER TABLE [dbo].[SDS]  WITH CHECK ADD  CONSTRAINT [SDS_FK2] FOREIGN KEY([CD_ID])
  REFERENCES [dbo].[CDS] ([CD_ID])
  ALTER TABLE [dbo].[SDS] CHECK CONSTRAINT [SDS_FK2]
  END
  
  /*** _STATEMENT_BREAK_ ***/
  /****** Object:  Table [dbo].[BUCKETING_COLS]    Script Date: 11/26/2013 5:26:45 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BUCKETING_COLS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[BUCKETING_COLS](
  	[SD_ID] [bigint] NOT NULL,
  	[BUCKET_COL_NAME] [nvarchar](256) NULL DEFAULT NULL,
  	[INTEGER_IDX] [int] NOT NULL,
   CONSTRAINT [PK_bucketing_cols_SD_ID] PRIMARY KEY CLUSTERED 
  (
  	[SD_ID] ASC,
  	[INTEGER_IDX] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[BUCKETING_COLS]  WITH CHECK ADD  CONSTRAINT [BUCKETING_COLS_FK1] FOREIGN KEY([SD_ID])
  REFERENCES [dbo].[SDS] ([SD_ID])
  ALTER TABLE [dbo].[BUCKETING_COLS] CHECK CONSTRAINT [BUCKETING_COLS_FK1]
  END
  
  /****** Object:  Table [dbo].[COLUMNS_V2]    Script Date: 11/26/2013 5:26:45 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[COLUMNS_V2]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[COLUMNS_V2](
  	[CD_ID] [bigint] NOT NULL,
  	[COMMENT] [nvarchar](256) NULL DEFAULT NULL,
  	[COLUMN_NAME] [nvarchar](128) NOT NULL,
  	[TYPE_NAME] [nvarchar](4000) NULL DEFAULT NULL,
  	[INTEGER_IDX] [int] NOT NULL,
   CONSTRAINT [PK_columns_v2_CD_ID] PRIMARY KEY CLUSTERED 
  (
  	[CD_ID] ASC,
  	[COLUMN_NAME] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[COLUMNS_V2]  WITH CHECK ADD  CONSTRAINT [COLUMNS_V2_FK1] FOREIGN KEY([CD_ID])
  REFERENCES [dbo].[CDS] ([CD_ID])
  ALTER TABLE [dbo].[COLUMNS_V2] CHECK CONSTRAINT [COLUMNS_V2_FK1]
  END
  
  /****** Object:  Table [dbo].[DBS]    Script Date: 11/26/2013 5:26:46 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DBS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[DBS](
  	[DB_ID] [bigint] NOT NULL,
  	[DESC] [nvarchar](4000) NULL DEFAULT NULL,
  	[DB_LOCATION_URI] [nvarchar](4000) NOT NULL,
  	[NAME] [nvarchar](128) NULL DEFAULT NULL,
   CONSTRAINT [PK_dbs_DB_ID] PRIMARY KEY CLUSTERED 
  (
  	[DB_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON),
   CONSTRAINT [UNIQUE_DATABASE] UNIQUE NONCLUSTERED 
  (
  	[NAME] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  
  /****** Object:  Table [dbo].[DATABASE_PARAMS]    Script Date: 11/26/2013 5:26:45 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DATABASE_PARAMS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[DATABASE_PARAMS](
  	[DB_ID] [bigint] NOT NULL,
  	[PARAM_KEY] [nvarchar](180) NOT NULL,
  	[PARAM_VALUE] [nvarchar](4000) NULL DEFAULT NULL,
   CONSTRAINT [PK_database_params_DB_ID] PRIMARY KEY CLUSTERED 
  (
  	[DB_ID] ASC,
  	[PARAM_KEY] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[DATABASE_PARAMS]  WITH CHECK ADD  CONSTRAINT [DATABASE_PARAMS_FK1] FOREIGN KEY([DB_ID])
  REFERENCES [dbo].[DBS] ([DB_ID])
  ALTER TABLE [dbo].[DATABASE_PARAMS] CHECK CONSTRAINT [DATABASE_PARAMS_FK1]
  END
  
  /****** Object:  Table [dbo].[DB_PRIVS]    Script Date: 11/26/2013 5:26:45 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DB_PRIVS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[DB_PRIVS](
  	[DB_GRANT_ID] [bigint] NOT NULL,
  	[CREATE_TIME] [int] NOT NULL,
  	[DB_ID] [bigint] NULL DEFAULT NULL,
  	[GRANT_OPTION] [smallint] NOT NULL,
  	[GRANTOR] [nvarchar](128) NULL DEFAULT NULL,
  	[GRANTOR_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[PRINCIPAL_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[PRINCIPAL_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[DB_PRIV] [nvarchar](128) NULL DEFAULT NULL,
   CONSTRAINT [PK_db_privs_DB_GRANT_ID] PRIMARY KEY CLUSTERED 
  (
  	[DB_GRANT_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON),
   CONSTRAINT [DBPRIVILEGEINDEX] UNIQUE NONCLUSTERED 
  (
  	[DB_ID] ASC,
  	[PRINCIPAL_NAME] ASC,
  	[PRINCIPAL_TYPE] ASC,
  	[DB_PRIV] ASC,
  	[GRANTOR] ASC,
  	[GRANTOR_TYPE] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[DB_PRIVS]  WITH CHECK ADD  CONSTRAINT [DB_PRIVS_FK1] FOREIGN KEY([DB_ID])
  REFERENCES [dbo].[DBS] ([DB_ID])
  ALTER TABLE [dbo].[DB_PRIVS] CHECK CONSTRAINT [DB_PRIVS_FK1]
  END
  
  /****** Object:  Table [dbo].[DELEGATION_TOKENS]    Script Date: 11/26/2013 5:26:46 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DELEGATION_TOKENS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[DELEGATION_TOKENS](
  	[TOKEN_IDENT] [nvarchar](767) NOT NULL,
  	[TOKEN] [nvarchar](767) NULL DEFAULT NULL,
   CONSTRAINT [PK_delegation_tokens_KEY_ID] PRIMARY KEY CLUSTERED 
  (
  	[TOKEN_IDENT] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  
  
  /****** Object:  Table [dbo].[GLOBAL_PRIVS]    Script Date: 11/26/2013 5:26:46 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GLOBAL_PRIVS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[GLOBAL_PRIVS](
  	[USER_GRANT_ID] [bigint] NOT NULL,
  	[CREATE_TIME] [int] NOT NULL,
  	[GRANT_OPTION] [smallint] NOT NULL,
  	[GRANTOR] [nvarchar](128) NULL DEFAULT NULL,
  	[GRANTOR_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[PRINCIPAL_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[PRINCIPAL_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[USER_PRIV] [nvarchar](128) NULL DEFAULT NULL,
   CONSTRAINT [PK_global_privs_USER_GRANT_ID] PRIMARY KEY CLUSTERED 
  (
  	[USER_GRANT_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON),
   CONSTRAINT [GLOBALPRIVILEGEINDEX] UNIQUE NONCLUSTERED 
  (
  	[PRINCIPAL_NAME] ASC,
  	[PRINCIPAL_TYPE] ASC,
  	[USER_PRIV] ASC,
  	[GRANTOR] ASC,
  	[GRANTOR_TYPE] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  /****** Object:  Table [dbo].[TBLS]    Script Date: 11/26/2013 5:26:51 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TBLS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[TBLS](
  	[TBL_ID] [bigint] NOT NULL,
  	[CREATE_TIME] [int] NOT NULL,
  	[DB_ID] [bigint] NULL DEFAULT NULL,
  	[LAST_ACCESS_TIME] [int] NOT NULL,
  	[OWNER] [nvarchar](767) NULL DEFAULT NULL,
  	[RETENTION] [int] NOT NULL,
  	[SD_ID] [bigint] NULL DEFAULT NULL,
  	[TBL_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[TBL_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[VIEW_EXPANDED_TEXT] [nvarchar](max) NULL,
  	[VIEW_ORIGINAL_TEXT] [nvarchar](max) NULL,
   CONSTRAINT [PK_tbls_TBL_ID] PRIMARY KEY CLUSTERED 
  (
  	[TBL_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON),
   CONSTRAINT [UNIQUETABLE] UNIQUE NONCLUSTERED 
  (
  	[TBL_NAME] ASC,
  	[DB_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[TBLS]  WITH CHECK ADD  CONSTRAINT [TBLS_FK1] FOREIGN KEY([SD_ID])
  REFERENCES [dbo].[SDS] ([SD_ID])
  ALTER TABLE [dbo].[TBLS] CHECK CONSTRAINT [TBLS_FK1]
  ALTER TABLE [dbo].[TBLS]  WITH CHECK ADD  CONSTRAINT [TBLS_FK2] FOREIGN KEY([DB_ID])
  REFERENCES [dbo].[DBS] ([DB_ID])
  ALTER TABLE [dbo].[TBLS] CHECK CONSTRAINT [TBLS_FK2]
  END
  
  
  /****** Object:  Table [dbo].[IDXS]    Script Date: 11/26/2013 5:26:46 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[IDXS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[IDXS](
  	[INDEX_ID] [bigint] NOT NULL,
  	[CREATE_TIME] [int] NOT NULL,
  	[DEFERRED_REBUILD] [bit] NOT NULL,
  	[INDEX_HANDLER_CLASS] [nvarchar](4000) NULL DEFAULT NULL,
  	[INDEX_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[INDEX_TBL_ID] [bigint] NULL DEFAULT NULL,
  	[LAST_ACCESS_TIME] [int] NOT NULL,
  	[ORIG_TBL_ID] [bigint] NULL DEFAULT NULL,
  	[SD_ID] [bigint] NULL DEFAULT NULL,
   CONSTRAINT [PK_idxs_INDEX_ID] PRIMARY KEY CLUSTERED 
  (
  	[INDEX_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON),
   CONSTRAINT [UNIQUEINDEX] UNIQUE NONCLUSTERED 
  (
  	[INDEX_NAME] ASC,
  	[ORIG_TBL_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[IDXS]  WITH CHECK ADD  CONSTRAINT [IDXS_FK1] FOREIGN KEY([ORIG_TBL_ID])
  REFERENCES [dbo].[TBLS] ([TBL_ID])
  ALTER TABLE [dbo].[IDXS] CHECK CONSTRAINT [IDXS_FK1]
  ALTER TABLE [dbo].[IDXS]  WITH CHECK ADD  CONSTRAINT [IDXS_FK2] FOREIGN KEY([SD_ID])
  REFERENCES [dbo].[SDS] ([SD_ID])
  ALTER TABLE [dbo].[IDXS] CHECK CONSTRAINT [IDXS_FK2]
  ALTER TABLE [dbo].[IDXS]  WITH CHECK ADD  CONSTRAINT [IDXS_FK3] FOREIGN KEY([INDEX_TBL_ID])
  REFERENCES [dbo].[TBLS] ([TBL_ID])
  ALTER TABLE [dbo].[IDXS] CHECK CONSTRAINT [IDXS_FK3]
  END
  
  
  /****** Object:  Table [dbo].[INDEX_PARAMS]    Script Date: 11/26/2013 5:26:46 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[INDEX_PARAMS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[INDEX_PARAMS](
  	[INDEX_ID] [bigint] NOT NULL,
  	[PARAM_KEY] [nvarchar](256) NOT NULL,
  	[PARAM_VALUE] [nvarchar](4000) NULL DEFAULT NULL,
   CONSTRAINT [PK_index_params_INDEX_ID] PRIMARY KEY CLUSTERED 
  (
  	[INDEX_ID] ASC,
  	[PARAM_KEY] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[INDEX_PARAMS]  WITH CHECK ADD  CONSTRAINT [INDEX_PARAMS_FK1] FOREIGN KEY([INDEX_ID])
  REFERENCES [dbo].[IDXS] ([INDEX_ID])
  ALTER TABLE [dbo].[INDEX_PARAMS] CHECK CONSTRAINT [INDEX_PARAMS_FK1]
  END
  
  /*** _STATEMENT_BREAK_ ***/
  /****** Object:  Table [dbo].[MASTER_KEYS]    Script Date: 11/26/2013 5:26:46 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MASTER_KEYS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[MASTER_KEYS](
  	[KEY_ID] [int] IDENTITY(1,1) NOT NULL,
  	[MASTER_KEY] [nvarchar](767) NULL DEFAULT NULL,
   CONSTRAINT [PK_master_keys_KEY_ID] PRIMARY KEY CLUSTERED 
  (
  	[KEY_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  
  SET ANSI_PADDING OFF
  
  /****** Object:  Table [dbo].[NUCLEUS_TABLES]    Script Date: 11/26/2013 5:26:46 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[NUCLEUS_TABLES]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[NUCLEUS_TABLES](
  	[CLASS_NAME] [nvarchar](128) NOT NULL,
  	[TABLE_NAME] [nvarchar](128) NOT NULL,
  	[TYPE] [nvarchar](4) NOT NULL,
  	[OWNER] [nvarchar](2) NOT NULL,
  	[VERSION] [nvarchar](20) NOT NULL,
  	[INTERFACE_NAME] [nvarchar](255) NULL DEFAULT NULL,
   CONSTRAINT [PK_nucleus_tables_CLASS_NAME] PRIMARY KEY CLUSTERED 
  (
  	[CLASS_NAME] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  
  SET ANSI_PADDING OFF
  
  /****** Object:  Table [dbo].[PART_COL_PRIVS]    Script Date: 11/26/2013 5:26:47 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PART_COL_PRIVS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[PART_COL_PRIVS](
  	[PART_COLUMN_GRANT_ID] [bigint] NOT NULL,
  	[COLUMN_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[CREATE_TIME] [int] NOT NULL,
  	[GRANT_OPTION] [smallint] NOT NULL,
  	[GRANTOR] [nvarchar](128) NULL DEFAULT NULL,
  	[GRANTOR_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[PART_ID] [bigint] NULL DEFAULT NULL,
  	[PRINCIPAL_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[PRINCIPAL_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[PART_COL_PRIV] [nvarchar](128) NULL DEFAULT NULL,
   CONSTRAINT [PK_part_col_privs_PART_COLUMN_GRANT_ID] PRIMARY KEY CLUSTERED 
  (
  	[PART_COLUMN_GRANT_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  
  /****** Object:  Table [dbo].[PARTITIONS]    Script Date: 11/26/2013 5:26:48 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PARTITIONS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[PARTITIONS](
  	[PART_ID] [bigint] NOT NULL,
  	[CREATE_TIME] [int] NOT NULL,
  	[LAST_ACCESS_TIME] [int] NOT NULL,
  	[PART_NAME] [nvarchar](767) NULL DEFAULT NULL,
  	[SD_ID] [bigint] NULL DEFAULT NULL,
  	[TBL_ID] [bigint] NULL DEFAULT NULL,
   CONSTRAINT [PK_partitions_PART_ID] PRIMARY KEY CLUSTERED 
  (
  	[PART_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON),
   CONSTRAINT [UNIQUEPARTITION] UNIQUE NONCLUSTERED 
  (
  	[PART_NAME] ASC,
  	[TBL_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[PARTITIONS]  WITH CHECK ADD  CONSTRAINT [PARTITIONS_FK1] FOREIGN KEY([TBL_ID])
  REFERENCES [dbo].[TBLS] ([TBL_ID])
  ALTER TABLE [dbo].[PARTITIONS] CHECK CONSTRAINT [PARTITIONS_FK1]
  ALTER TABLE [dbo].[PARTITIONS]  WITH CHECK ADD  CONSTRAINT [PARTITIONS_FK2] FOREIGN KEY([SD_ID])
  REFERENCES [dbo].[SDS] ([SD_ID])
  ALTER TABLE [dbo].[PARTITIONS] CHECK CONSTRAINT [PARTITIONS_FK2]
  END
  
  /****** Object:  Table [dbo].[PART_COL_STATS]    Script Date: 11/26/2013 5:26:47 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PART_COL_STATS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[PART_COL_STATS](
  	[CS_ID] [bigint] NOT NULL,
  	[DB_NAME] [nvarchar](128) NOT NULL,
  	[TABLE_NAME] [nvarchar](128) NOT NULL,
  	[PARTITION_NAME] [nvarchar](767) NOT NULL,
  	[COLUMN_NAME] [nvarchar](128) NOT NULL,
  	[COLUMN_TYPE] [nvarchar](128) NOT NULL,
  	[PART_ID] [bigint] NOT NULL,
  	[LONG_LOW_VALUE] [bigint] NULL DEFAULT NULL,
  	[LONG_HIGH_VALUE] [bigint] NULL DEFAULT NULL,
  	[DOUBLE_HIGH_VALUE] [float] NULL DEFAULT NULL,
  	[DOUBLE_LOW_VALUE] [float] NULL DEFAULT NULL,
  	[BIG_DECIMAL_LOW_VALUE] [nvarchar](4000) NULL DEFAULT NULL,
  	[BIG_DECIMAL_HIGH_VALUE] [nvarchar](4000) NULL DEFAULT NULL,
  	[NUM_NULLS] [bigint] NOT NULL,
  	[NUM_DISTINCTS] [bigint] NULL DEFAULT NULL,
  	[AVG_COL_LEN] [float] NULL DEFAULT NULL,
  	[MAX_COL_LEN] [bigint] NULL DEFAULT NULL,
  	[NUM_TRUES] [bigint] NULL DEFAULT NULL,
  	[NUM_FALSES] [bigint] NULL DEFAULT NULL,
  	[LAST_ANALYZED] [bigint] NOT NULL,
   CONSTRAINT [PK_part_col_stats_CS_ID] PRIMARY KEY CLUSTERED 
  (
  	[CS_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[PART_COL_PRIVS]  WITH CHECK ADD  CONSTRAINT [PART_COL_PRIVS_FK1] FOREIGN KEY([PART_ID])
  REFERENCES [dbo].[PARTITIONS] ([PART_ID])
  ALTER TABLE [dbo].[PART_COL_PRIVS] CHECK CONSTRAINT [PART_COL_PRIVS_FK1]
  ALTER TABLE [dbo].[PART_COL_STATS]  WITH CHECK ADD  CONSTRAINT [PART_COL_STATS_FK] FOREIGN KEY([PART_ID])
  REFERENCES [dbo].[PARTITIONS] ([PART_ID])
  ALTER TABLE [dbo].[PART_COL_STATS] CHECK CONSTRAINT [PART_COL_STATS_FK]
  END
  
  
  /****** Object:  Table [dbo].[PART_PRIVS]    Script Date: 11/26/2013 5:26:47 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PART_PRIVS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[PART_PRIVS](
  	[PART_GRANT_ID] [bigint] NOT NULL,
  	[CREATE_TIME] [int] NOT NULL,
  	[GRANT_OPTION] [smallint] NOT NULL,
  	[GRANTOR] [nvarchar](128) NULL DEFAULT NULL,
  	[GRANTOR_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[PART_ID] [bigint] NULL DEFAULT NULL,
  	[PRINCIPAL_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[PRINCIPAL_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[PART_PRIV] [nvarchar](128) NULL DEFAULT NULL,
   CONSTRAINT [PK_part_privs_PART_GRANT_ID] PRIMARY KEY CLUSTERED 
  (
  	[PART_GRANT_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[PART_PRIVS]  WITH CHECK ADD  CONSTRAINT [PART_PRIVS_FK1] FOREIGN KEY([PART_ID])
  REFERENCES [dbo].[PARTITIONS] ([PART_ID])
  ALTER TABLE [dbo].[PART_PRIVS] CHECK CONSTRAINT [PART_PRIVS_FK1]
  END
  
  /*** _STATEMENT_BREAK_ ***/
  /****** Object:  Table [dbo].[PARTITION_EVENTS]    Script Date: 11/26/2013 5:26:47 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PARTITION_EVENTS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[PARTITION_EVENTS](
  	[PART_NAME_ID] [bigint] NOT NULL,
  	[DB_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[EVENT_TIME] [bigint] NOT NULL,
  	[EVENT_TYPE] [int] NOT NULL,
  	[PARTITION_NAME] [nvarchar](767) NULL DEFAULT NULL,
  	[TBL_NAME] [nvarchar](128) NULL DEFAULT NULL,
   CONSTRAINT [PK_partition_events_PART_NAME_ID] PRIMARY KEY CLUSTERED 
  (
  	[PART_NAME_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  
  SET ANSI_PADDING OFF
  
  /****** Object:  Table [dbo].[PARTITION_KEY_VALS]    Script Date: 11/26/2013 5:26:47 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PARTITION_KEY_VALS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[PARTITION_KEY_VALS](
  	[PART_ID] [bigint] NOT NULL,
  	[PART_KEY_VAL] [nvarchar](256) NULL DEFAULT NULL,
  	[INTEGER_IDX] [int] NOT NULL,
   CONSTRAINT [PK_partition_key_vals_PART_ID] PRIMARY KEY CLUSTERED 
  (
  	[PART_ID] ASC,
  	[INTEGER_IDX] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[PARTITION_KEY_VALS]  WITH CHECK ADD  CONSTRAINT [PARTITION_KEY_VALS_FK1] FOREIGN KEY([PART_ID])
  REFERENCES [dbo].[PARTITIONS] ([PART_ID])
  ALTER TABLE [dbo].[PARTITION_KEY_VALS] CHECK CONSTRAINT [PARTITION_KEY_VALS_FK1]
  END
  
  
  /****** Object:  Table [dbo].[PARTITION_KEYS]    Script Date: 11/26/2013 5:26:47 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PARTITION_KEYS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[PARTITION_KEYS](
  	[TBL_ID] [bigint] NOT NULL,
  	[PKEY_COMMENT] [nvarchar](4000) NULL DEFAULT NULL,
  	[PKEY_NAME] [nvarchar](128) NOT NULL,
  	[PKEY_TYPE] [nvarchar](767) NOT NULL,
  	[INTEGER_IDX] [int] NOT NULL,
   CONSTRAINT [PK_partition_keys_TBL_ID] PRIMARY KEY CLUSTERED 
  (
  	[TBL_ID] ASC,
  	[PKEY_NAME] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[PARTITION_KEYS]  WITH CHECK ADD  CONSTRAINT [PARTITION_KEYS_FK1] FOREIGN KEY([TBL_ID])
  REFERENCES [dbo].[TBLS] ([TBL_ID])
  ALTER TABLE [dbo].[PARTITION_KEYS] CHECK CONSTRAINT [PARTITION_KEYS_FK1]
  END
  
  
  /****** Object:  Table [dbo].[PARTITION_PARAMS]    Script Date: 11/26/2013 5:26:48 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PARTITION_PARAMS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[PARTITION_PARAMS](
  	[PART_ID] [bigint] NOT NULL,
  	[PARAM_KEY] [nvarchar](256) NOT NULL,
  	[PARAM_VALUE] [nvarchar](4000) NULL DEFAULT NULL,
   CONSTRAINT [PK_partition_params_PART_ID] PRIMARY KEY CLUSTERED 
  (
  	[PART_ID] ASC,
  	[PARAM_KEY] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[PARTITION_PARAMS]  WITH CHECK ADD  CONSTRAINT [PARTITION_PARAMS_FK1] FOREIGN KEY([PART_ID])
  REFERENCES [dbo].[PARTITIONS] ([PART_ID])
  ALTER TABLE [dbo].[PARTITION_PARAMS] CHECK CONSTRAINT [PARTITION_PARAMS_FK1]
  END
  
  /****** Object:  Table [dbo].[ROLES]    Script Date: 11/26/2013 5:26:48 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ROLES]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[ROLES](
  	[ROLE_ID] [bigint] NOT NULL,
  	[CREATE_TIME] [int] NOT NULL,
  	[OWNER_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[ROLE_NAME] [nvarchar](128) NULL DEFAULT NULL,
   CONSTRAINT [PK_roles_ROLE_ID] PRIMARY KEY CLUSTERED 
  (
  	[ROLE_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON),
   CONSTRAINT [ROLEENTITYINDEX] UNIQUE NONCLUSTERED 
  (
  	[ROLE_NAME] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  
  /****** Object:  Table [dbo].[ROLE_MAP]    Script Date: 11/26/2013 5:26:48 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ROLE_MAP]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[ROLE_MAP](
  	[ROLE_GRANT_ID] [bigint] NOT NULL,
  	[ADD_TIME] [int] NOT NULL,
  	[GRANT_OPTION] [smallint] NOT NULL,
  	[GRANTOR] [nvarchar](128) NULL DEFAULT NULL,
  	[GRANTOR_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[PRINCIPAL_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[PRINCIPAL_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[ROLE_ID] [bigint] NULL DEFAULT NULL,
   CONSTRAINT [PK_role_map_ROLE_GRANT_ID] PRIMARY KEY CLUSTERED 
  (
  	[ROLE_GRANT_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON),
   CONSTRAINT [USERROLEMAPINDEX] UNIQUE NONCLUSTERED 
  (
  	[PRINCIPAL_NAME] ASC,
  	[ROLE_ID] ASC,
  	[GRANTOR] ASC,
  	[GRANTOR_TYPE] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[ROLE_MAP]  WITH CHECK ADD  CONSTRAINT [ROLE_MAP_FK1] FOREIGN KEY([ROLE_ID])
  REFERENCES [dbo].[ROLES] ([ROLE_ID])
  ALTER TABLE [dbo].[ROLE_MAP] CHECK CONSTRAINT [ROLE_MAP_FK1]
  END
  
  /****** Object:  Table [dbo].[SD_PARAMS]    Script Date: 11/26/2013 5:26:48 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SD_PARAMS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[SD_PARAMS](
  	[SD_ID] [bigint] NOT NULL,
  	[PARAM_KEY] [nvarchar](256) NOT NULL,
  	[PARAM_VALUE] [nvarchar](4000) NULL DEFAULT NULL,
   CONSTRAINT [PK_sd_params_SD_ID] PRIMARY KEY CLUSTERED 
  (
  	[SD_ID] ASC,
  	[PARAM_KEY] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[SD_PARAMS]  WITH CHECK ADD  CONSTRAINT [SD_PARAMS_FK1] FOREIGN KEY([SD_ID])
  REFERENCES [dbo].[SDS] ([SD_ID])
  ALTER TABLE [dbo].[SD_PARAMS] CHECK CONSTRAINT [SD_PARAMS_FK1]
  END
  
  
  /****** Object:  Table [dbo].[SEQUENCE_TABLE]    Script Date: 11/26/2013 5:26:48 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SEQUENCE_TABLE]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[SEQUENCE_TABLE](
  	[SEQUENCE_NAME] [nvarchar](255) NOT NULL,
  	[NEXT_VAL] [bigint] NOT NULL,
   CONSTRAINT [PK_sequence_table_SEQUENCE_NAME] PRIMARY KEY CLUSTERED 
  (
  	[SEQUENCE_NAME] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  
  SET ANSI_PADDING OFF
  
  /****** Object:  Table [dbo].[SERDE_PARAMS]    Script Date: 11/26/2013 5:26:49 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SERDE_PARAMS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[SERDE_PARAMS](
  	[SERDE_ID] [bigint] NOT NULL,
  	[PARAM_KEY] [nvarchar](256) NOT NULL,
  	[PARAM_VALUE] [nvarchar](4000) NULL DEFAULT NULL,
   CONSTRAINT [PK_serde_params_SERDE_ID] PRIMARY KEY CLUSTERED 
  (
  	[SERDE_ID] ASC,
  	[PARAM_KEY] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[SERDE_PARAMS]  WITH CHECK ADD  CONSTRAINT [SERDE_PARAMS_FK1] FOREIGN KEY([SERDE_ID])
  REFERENCES [dbo].[SERDES] ([SERDE_ID])
  ALTER TABLE [dbo].[SERDE_PARAMS] CHECK CONSTRAINT [SERDE_PARAMS_FK1]
  END
  
  /*** _STATEMENT_BREAK_ ***/
  
  /****** Object:  Table [dbo].[SKEWED_COL_NAMES]    Script Date: 11/26/2013 5:26:49 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SKEWED_COL_NAMES]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[SKEWED_COL_NAMES](
  	[SD_ID] [bigint] NOT NULL,
  	[SKEWED_COL_NAME] [nvarchar](256) NULL DEFAULT NULL,
  	[INTEGER_IDX] [int] NOT NULL,
   CONSTRAINT [PK_skewed_col_names_SD_ID] PRIMARY KEY CLUSTERED 
  (
  	[SD_ID] ASC,
  	[INTEGER_IDX] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[SKEWED_COL_NAMES]  WITH CHECK ADD  CONSTRAINT [SKEWED_COL_NAMES_FK1] FOREIGN KEY([SD_ID])
  REFERENCES [dbo].[SDS] ([SD_ID])
  ALTER TABLE [dbo].[SKEWED_COL_NAMES] CHECK CONSTRAINT [SKEWED_COL_NAMES_FK1]
  END
  /*** _STATEMENT_BREAK_ ***/
  /****** Object:  Table [dbo].[SKEWED_STRING_LIST]    Script Date: 11/26/2013 5:26:49 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SKEWED_STRING_LIST]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[SKEWED_STRING_LIST](
  	[STRING_LIST_ID] [bigint] NOT NULL,
   CONSTRAINT [PK_skewed_string_list_STRING_LIST_ID] PRIMARY KEY CLUSTERED 
  (
  	[STRING_LIST_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  
  /****** Object:  Table [dbo].[SKEWED_COL_VALUE_LOC_MAP]    Script Date: 11/26/2013 5:26:49 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SKEWED_COL_VALUE_LOC_MAP]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[SKEWED_COL_VALUE_LOC_MAP](
  	[SD_ID] [bigint] NOT NULL,
  	[STRING_LIST_ID_KID] [bigint] NOT NULL,
  	[LOCATION] [nvarchar](4000) NULL DEFAULT NULL,
   CONSTRAINT [PK_skewed_col_value_loc_map_SD_ID] PRIMARY KEY CLUSTERED 
  (
  	[SD_ID] ASC,
  	[STRING_LIST_ID_KID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[SKEWED_COL_VALUE_LOC_MAP]  WITH CHECK ADD  CONSTRAINT [SKEWED_COL_VALUE_LOC_MAP_FK1] FOREIGN KEY([SD_ID])
  REFERENCES [dbo].[SDS] ([SD_ID])
  ALTER TABLE [dbo].[SKEWED_COL_VALUE_LOC_MAP] CHECK CONSTRAINT [SKEWED_COL_VALUE_LOC_MAP_FK1]
  ALTER TABLE [dbo].[SKEWED_COL_VALUE_LOC_MAP]  WITH CHECK ADD  CONSTRAINT [SKEWED_COL_VALUE_LOC_MAP_FK2] FOREIGN KEY([STRING_LIST_ID_KID])
  REFERENCES [dbo].[SKEWED_STRING_LIST] ([STRING_LIST_ID])
  ALTER TABLE [dbo].[SKEWED_COL_VALUE_LOC_MAP] CHECK CONSTRAINT [SKEWED_COL_VALUE_LOC_MAP_FK2]
  END
  
  /****** Object:  Table [dbo].[SKEWED_STRING_LIST_VALUES]    Script Date: 11/26/2013 5:26:49 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SKEWED_STRING_LIST_VALUES]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[SKEWED_STRING_LIST_VALUES](
  	[STRING_LIST_ID] [bigint] NOT NULL,
  	[STRING_LIST_VALUE] [nvarchar](256) NULL DEFAULT NULL,
  	[INTEGER_IDX] [int] NOT NULL,
   CONSTRAINT [PK_skewed_string_list_values_STRING_LIST_ID] PRIMARY KEY CLUSTERED 
  (
  	[STRING_LIST_ID] ASC,
  	[INTEGER_IDX] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[SKEWED_STRING_LIST_VALUES]  WITH CHECK ADD  CONSTRAINT [SKEWED_STRING_LIST_VALUES_FK1] FOREIGN KEY([STRING_LIST_ID])
  REFERENCES [dbo].[SKEWED_STRING_LIST] ([STRING_LIST_ID])
  ALTER TABLE [dbo].[SKEWED_STRING_LIST_VALUES] CHECK CONSTRAINT [SKEWED_STRING_LIST_VALUES_FK1]
  END
  
  
  /****** Object:  Table [dbo].[SKEWED_VALUES]    Script Date: 11/26/2013 5:26:49 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SKEWED_VALUES]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[SKEWED_VALUES](
  	[SD_ID_OID] [bigint] NOT NULL,
  	[STRING_LIST_ID_EID] [bigint] NOT NULL,
  	[INTEGER_IDX] [int] NOT NULL,
   CONSTRAINT [PK_skewed_values_SD_ID_OID] PRIMARY KEY CLUSTERED 
  (
  	[SD_ID_OID] ASC,
  	[INTEGER_IDX] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  ALTER TABLE [dbo].[SKEWED_VALUES]  WITH CHECK ADD  CONSTRAINT [SKEWED_VALUES_FK1] FOREIGN KEY([SD_ID_OID])
  REFERENCES [dbo].[SDS] ([SD_ID])
  ALTER TABLE [dbo].[SKEWED_VALUES] CHECK CONSTRAINT [SKEWED_VALUES_FK1]
  ALTER TABLE [dbo].[SKEWED_VALUES]  WITH CHECK ADD  CONSTRAINT [SKEWED_VALUES_FK2] FOREIGN KEY([STRING_LIST_ID_EID])
  REFERENCES [dbo].[SKEWED_STRING_LIST] ([STRING_LIST_ID])
  ALTER TABLE [dbo].[SKEWED_VALUES] CHECK CONSTRAINT [SKEWED_VALUES_FK2]
  END
  
  /****** Object:  Table [dbo].[SORT_COLS]    Script Date: 11/26/2013 5:26:50 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SORT_COLS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[SORT_COLS](
  	[SD_ID] [bigint] NOT NULL,
  	[COLUMN_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[ORDER] [int] NOT NULL,
  	[INTEGER_IDX] [int] NOT NULL,
   CONSTRAINT [PK_sort_cols_SD_ID] PRIMARY KEY CLUSTERED 
  (
  	[SD_ID] ASC,
  	[INTEGER_IDX] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[SORT_COLS]  WITH CHECK ADD  CONSTRAINT [SORT_COLS_FK1] FOREIGN KEY([SD_ID])
  REFERENCES [dbo].[SDS] ([SD_ID])
  ALTER TABLE [dbo].[SORT_COLS] CHECK CONSTRAINT [SORT_COLS_FK1]
  END
  
  
  /****** Object:  Table [dbo].[TAB_COL_STATS]    Script Date: 11/26/2013 5:26:50 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TAB_COL_STATS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[TAB_COL_STATS](
  	[CS_ID] [bigint] NOT NULL,
  	[DB_NAME] [nvarchar](128) NOT NULL,
  	[TABLE_NAME] [nvarchar](128) NOT NULL,
  	[COLUMN_NAME] [nvarchar](128) NOT NULL,
  	[COLUMN_TYPE] [nvarchar](128) NOT NULL,
  	[TBL_ID] [bigint] NOT NULL,
  	[LONG_LOW_VALUE] [bigint] NULL DEFAULT NULL,
  	[LONG_HIGH_VALUE] [bigint] NULL DEFAULT NULL,
  	[DOUBLE_HIGH_VALUE] [float] NULL DEFAULT NULL,
  	[DOUBLE_LOW_VALUE] [float] NULL DEFAULT NULL,
  	[BIG_DECIMAL_LOW_VALUE] [nvarchar](4000) NULL DEFAULT NULL,
  	[BIG_DECIMAL_HIGH_VALUE] [nvarchar](4000) NULL DEFAULT NULL,
  	[NUM_NULLS] [bigint] NOT NULL,
  	[NUM_DISTINCTS] [bigint] NULL DEFAULT NULL,
  	[AVG_COL_LEN] [float] NULL DEFAULT NULL,
  	[MAX_COL_LEN] [bigint] NULL DEFAULT NULL,
  	[NUM_TRUES] [bigint] NULL DEFAULT NULL,
  	[NUM_FALSES] [bigint] NULL DEFAULT NULL,
  	[LAST_ANALYZED] [bigint] NOT NULL,
   CONSTRAINT [PK_tab_col_stats_CS_ID] PRIMARY KEY CLUSTERED 
  (
  	[CS_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[TAB_COL_STATS]  WITH CHECK ADD  CONSTRAINT [TAB_COL_STATS_FK] FOREIGN KEY([TBL_ID])
  REFERENCES [dbo].[TBLS] ([TBL_ID])
  ALTER TABLE [dbo].[TAB_COL_STATS] CHECK CONSTRAINT [TAB_COL_STATS_FK]
  END
  
  
  /****** Object:  Table [dbo].[TABLE_PARAMS]    Script Date: 11/26/2013 5:26:50 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TABLE_PARAMS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[TABLE_PARAMS](
  	[TBL_ID] [bigint] NOT NULL,
  	[PARAM_KEY] [nvarchar](256) NOT NULL,
  	[PARAM_VALUE] [nvarchar](4000) NULL DEFAULT NULL,
   CONSTRAINT [PK_table_params_TBL_ID] PRIMARY KEY CLUSTERED 
  (
  	[TBL_ID] ASC,
  	[PARAM_KEY] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[TABLE_PARAMS]  WITH CHECK ADD  CONSTRAINT [TABLE_PARAMS_FK1] FOREIGN KEY([TBL_ID])
  REFERENCES [dbo].[TBLS] ([TBL_ID])
  ALTER TABLE [dbo].[TABLE_PARAMS] CHECK CONSTRAINT [TABLE_PARAMS_FK1]
  END
  
  /*** _STATEMENT_BREAK_ ***/
  /****** Object:  Table [dbo].[TBL_COL_PRIVS]    Script Date: 11/26/2013 5:26:50 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TBL_COL_PRIVS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[TBL_COL_PRIVS](
  	[TBL_COLUMN_GRANT_ID] [bigint] NOT NULL,
  	[COLUMN_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[CREATE_TIME] [int] NOT NULL,
  	[GRANT_OPTION] [smallint] NOT NULL,
  	[GRANTOR] [nvarchar](128) NULL DEFAULT NULL,
  	[GRANTOR_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[PRINCIPAL_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[PRINCIPAL_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[TBL_COL_PRIV] [nvarchar](128) NULL DEFAULT NULL,
  	[TBL_ID] [bigint] NULL DEFAULT NULL,
   CONSTRAINT [PK_tbl_col_privs_TBL_COLUMN_GRANT_ID] PRIMARY KEY CLUSTERED 
  (
  	[TBL_COLUMN_GRANT_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[TBL_COL_PRIVS]  WITH CHECK ADD  CONSTRAINT [TBL_COL_PRIVS_FK1] FOREIGN KEY([TBL_ID])
  REFERENCES [dbo].[TBLS] ([TBL_ID])
  ALTER TABLE [dbo].[TBL_COL_PRIVS] CHECK CONSTRAINT [TBL_COL_PRIVS_FK1]
  END
  
  
  /****** Object:  Table [dbo].[TBL_PRIVS]    Script Date: 11/26/2013 5:26:50 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TBL_PRIVS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[TBL_PRIVS](
  	[TBL_GRANT_ID] [bigint] NOT NULL,
  	[CREATE_TIME] [int] NOT NULL,
  	[GRANT_OPTION] [smallint] NOT NULL,
  	[GRANTOR] [nvarchar](128) NULL DEFAULT NULL,
  	[GRANTOR_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[PRINCIPAL_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[PRINCIPAL_TYPE] [nvarchar](128) NULL DEFAULT NULL,
  	[TBL_PRIV] [nvarchar](128) NULL DEFAULT NULL,
  	[TBL_ID] [bigint] NULL DEFAULT NULL,
   CONSTRAINT [PK_tbl_privs_TBL_GRANT_ID] PRIMARY KEY CLUSTERED 
  (
  	[TBL_GRANT_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[TBL_PRIVS]  WITH CHECK ADD  CONSTRAINT [TBL_PRIVS_FK1] FOREIGN KEY([TBL_ID])
  REFERENCES [dbo].[TBLS] ([TBL_ID])
  ALTER TABLE [dbo].[TBL_PRIVS] CHECK CONSTRAINT [TBL_PRIVS_FK1]
  END
  
  /****** Object:  Table [dbo].[TYPES]    Script Date: 11/26/2013 5:26:51 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TYPES]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[TYPES](
  	[TYPES_ID] [bigint] NOT NULL,
  	[TYPE_NAME] [nvarchar](128) NULL DEFAULT NULL,
  	[TYPE1] [nvarchar](767) NULL DEFAULT NULL,
  	[TYPE2] [nvarchar](767) NULL DEFAULT NULL,
   CONSTRAINT [PK_types_TYPES_ID] PRIMARY KEY CLUSTERED 
  (
  	[TYPES_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON),
   CONSTRAINT [UNIQUE_TYPE] UNIQUE NONCLUSTERED 
  (
  	[TYPE_NAME] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  
  /****** Object:  Table [dbo].[TYPE_FIELDS]    Script Date: 11/26/2013 5:26:51 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TYPE_FIELDS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[TYPE_FIELDS](
  	[TYPE_NAME] [bigint] NOT NULL,
  	[COMMENT] [nvarchar](256) NULL DEFAULT NULL,
  	[FIELD_NAME] [nvarchar](128) NOT NULL,
  	[FIELD_TYPE] [nvarchar](767) NOT NULL,
  	[INTEGER_IDX] [int] NOT NULL,
   CONSTRAINT [PK_type_fields_TYPE_NAME] PRIMARY KEY CLUSTERED 
  (
  	[TYPE_NAME] ASC,
  	[FIELD_NAME] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  SET ANSI_PADDING OFF
  ALTER TABLE [dbo].[TYPE_FIELDS]  WITH CHECK ADD  CONSTRAINT [TYPE_FIELDS_FK1] FOREIGN KEY([TYPE_NAME])
  REFERENCES [dbo].[TYPES] ([TYPES_ID])
  ALTER TABLE [dbo].[TYPE_FIELDS] CHECK CONSTRAINT [TYPE_FIELDS_FK1]
  END
  
  /****** Object:  Table [dbo].[VERSION]    Script Date: 11/26/2013 5:26:51 PM ******/
  SET ANSI_NULLS ON
  
  SET QUOTED_IDENTIFIER ON
  
  SET ANSI_PADDING ON
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[VERSION]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[VERSION](
  	[VER_ID] [bigint] NOT NULL,
  	[SCHEMA_VERSION] [nvarchar](127) NOT NULL,
  	[VERSION_COMMENT] [nvarchar](255) NOT NULL,
   CONSTRAINT [VERSION_PK] PRIMARY KEY CLUSTERED 
  (
  	[VER_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  END
  
  /****** Adding indexes for tables ******/
  
  /****** Adding indexes for PART_COL_STATS ******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PART_COL_STATS_N49' AND object_id = OBJECT_ID('PART_COL_STATS'))
  BEGIN
  CREATE NONCLUSTERED INDEX PART_COL_STATS_N49
  ON [dbo].[PART_COL_STATS] ([PART_ID])
  END
  
  
  /****** Adding indexes for PART_COL_PRIVS ******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PART_COL_PRIVS_N49' AND object_id = OBJECT_ID('PART_COL_PRIVS'))
  BEGIN
  CREATE NONCLUSTERED INDEX PART_COL_PRIVS_N49
  ON [dbo].[PART_COL_PRIVS] ([PART_ID])
  END
  
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PARTITIONCOLUMNPRIVILEGEINDEX' AND object_id = OBJECT_ID('PART_COL_PRIVS'))
  BEGIN
  CREATE NONCLUSTERED INDEX PARTITIONCOLUMNPRIVILEGEINDEX
  ON [dbo].[PART_COL_PRIVS] ([PART_ID],[COLUMN_NAME],[PRINCIPAL_NAME],[PRINCIPAL_TYPE],[PART_COL_PRIV],[GRANTOR],[GRANTOR_TYPE])
  END
  
  
  /****** Adding indexes for COLUMNS_V2 ******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'COLUMNS_V2_N49' AND object_id = OBJECT_ID('COLUMNS_V2'))
  BEGIN
  CREATE NONCLUSTERED INDEX COLUMNS_V2_N49
  ON [dbo].[COLUMNS_V2] ([CD_ID])
  END
  
  
  /****** Adding indexes for PARTITION_KEY_VALS ******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PARTITION_KEY_VALS_N49' AND object_id = OBJECT_ID('PARTITION_KEY_VALS'))
  BEGIN
  CREATE NONCLUSTERED INDEX PARTITION_KEY_VALS_N49
  ON [dbo].[PARTITION_KEY_VALS] ([PART_ID])
  END
  
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PARTITION_INTEGER_INDEX' AND object_id = OBJECT_ID('PARTITION_KEY_VALS'))
  BEGIN
  CREATE NONCLUSTERED INDEX PARTITION_INTEGER_INDEX
  ON [dbo].[PARTITION_KEY_VALS] ([INTEGER_IDX])
  INCLUDE ([PART_ID],[PART_KEY_VAL])
  END
  
  /****** Adding indexes for PARTITION_PARAMS ******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PARTITION_PARAMS_N49' AND object_id = OBJECT_ID('PARTITION_PARAMS'))
  BEGIN
  CREATE NONCLUSTERED INDEX PARTITION_PARAMS_N49
  ON [dbo].[PARTITION_PARAMS] ([PART_ID])
  END
  
  
  /****** Adding indexes for PARTITION_KEYS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PARTITION_KEYS_N49' AND object_id = OBJECT_ID('PARTITION_KEYS'))
  BEGIN
  CREATE NONCLUSTERED INDEX PARTITION_KEYS_N49
  ON [dbo].[PARTITION_KEYS] ([TBL_ID])
  END
  
  
  /****** Adding indexes for PARTITIONS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PARTITIONS_N49' AND object_id = OBJECT_ID('PARTITIONS'))
  BEGIN
  CREATE NONCLUSTERED INDEX PARTITIONS_N49
  ON [dbo].[PARTITIONS] ([SD_ID])
  END
  
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PARTITIONS_N50' AND object_id = OBJECT_ID('PARTITIONS'))
  BEGIN
  CREATE NONCLUSTERED INDEX PARTITIONS_N50
  ON [dbo].[PARTITIONS] ([TBL_ID])
  END
  
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PARTITION_JOIN_INDEX' AND object_id = OBJECT_ID('PARTITIONS'))
  BEGIN
  CREATE NONCLUSTERED INDEX PARTITION_JOIN_INDEX
  ON [dbo].[PARTITIONS] ([TBL_ID])
  INCLUDE ([PART_ID],[CREATE_TIME],[LAST_ACCESS_TIME],[PART_NAME])
  END
  
  
  /****** Adding indexes for INDEX_PARAMS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'INDEX_PARAMS_N49' AND object_id = OBJECT_ID('INDEX_PARAMS'))
  BEGIN
  CREATE NONCLUSTERED INDEX INDEX_PARAMS_N49
  ON [dbo].[INDEX_PARAMS] ([INDEX_ID])
  END
  
  
  /****** Adding indexes for TBL_COL_PRIVS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'TABLECOLUMNPRIVILEGEINDEX' AND object_id = OBJECT_ID('TBL_COL_PRIVS'))
  BEGIN
  CREATE NONCLUSTERED INDEX TABLECOLUMNPRIVILEGEINDEX
  ON [dbo].[TBL_COL_PRIVS] ([TBL_ID],[COLUMN_NAME],[PRINCIPAL_NAME],[PRINCIPAL_TYPE],[TBL_COL_PRIV],[GRANTOR],[GRANTOR_TYPE])
  END
  
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'TBL_COL_PRIVS_N49' AND object_id = OBJECT_ID('TBL_COL_PRIVS'))
  BEGIN
  CREATE NONCLUSTERED INDEX TBL_COL_PRIVS_N49
  ON [dbo].[TBL_COL_PRIVS] ([TBL_ID])
  END
  
  
  /****** Adding indexes for IDXS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'IDXS_N49' AND object_id = OBJECT_ID('IDXS'))
  BEGIN
  CREATE NONCLUSTERED INDEX IDXS_N49
  ON [dbo].[IDXS] ([ORIG_TBL_ID])
  END
  
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'IDXS_N50' AND object_id = OBJECT_ID('IDXS'))
  BEGIN
  CREATE NONCLUSTERED INDEX IDXS_N50
  ON [dbo].[IDXS] ([INDEX_TBL_ID])
  END
  
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'IDXS_N51' AND object_id = OBJECT_ID('IDXS'))
  BEGIN
  CREATE NONCLUSTERED INDEX IDXS_N51
  ON [dbo].[IDXS] ([SD_ID])
  END
  
  
  /****** Adding indexes for BUCKETING_COLS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'BUCKETING_COLS_N49' AND object_id = OBJECT_ID('BUCKETING_COLS'))
  BEGIN
  CREATE NONCLUSTERED INDEX BUCKETING_COLS_N49
  ON [dbo].[BUCKETING_COLS] ([SD_ID])
  END
  
  /*** _STATEMENT_BREAK_ ***/
  /****** Adding indexes for TYPE_FIELDS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'TYPE_FIELDS_N49' AND object_id = OBJECT_ID('TYPE_FIELDS'))
  BEGIN
  CREATE NONCLUSTERED INDEX TYPE_FIELDS_N49
  ON [dbo].[TYPE_FIELDS] ([TYPE_NAME])
  END
  
  
  /****** Adding indexes for SD_PARAMS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'SD_PARAMS_N49' AND object_id = OBJECT_ID('SD_PARAMS'))
  BEGIN
  CREATE NONCLUSTERED INDEX SD_PARAMS_N49
  ON [dbo].[SD_PARAMS] ([SD_ID])
  END
  
  
  /****** Adding indexes for SDS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'SDS_N49' AND object_id = OBJECT_ID('SDS'))
  BEGIN
  CREATE NONCLUSTERED INDEX SDS_N49
  ON [dbo].[SDS] ([SERDE_ID])
  END
  
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'SDS_N50' AND object_id = OBJECT_ID('SDS'))
  BEGIN
  CREATE NONCLUSTERED INDEX SDS_N50
  ON [dbo].[SDS] ([CD_ID])
  END
  
  
  /****** Adding indexes for TABLE_PARAMS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'TABLE_PARAMS_N49' AND object_id = OBJECT_ID('TABLE_PARAMS'))
  BEGIN
  CREATE NONCLUSTERED INDEX TABLE_PARAMS_N49
  ON [dbo].[TABLE_PARAMS] ([TBL_ID])
  END
  
  
  /****** Adding indexes for SORT_COLS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'SORT_COLS_N49' AND object_id = OBJECT_ID('SORT_COLS'))
  BEGIN
  CREATE NONCLUSTERED INDEX SORT_COLS_N49
  ON [dbo].[SORT_COLS] ([SD_ID])
  END
  
  
  /****** Adding indexes for TBL_PRIVS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'TBL_PRIVS_N49' AND object_id = OBJECT_ID('TBL_PRIVS'))
  BEGIN
  CREATE NONCLUSTERED INDEX TBL_PRIVS_N49
  ON [dbo].[TBL_PRIVS] ([TBL_ID])
  END
  
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'TABLEPRIVILEGEINDEX' AND object_id = OBJECT_ID('TBL_PRIVS'))
  BEGIN
  CREATE NONCLUSTERED INDEX TABLEPRIVILEGEINDEX
  ON [dbo].[TBL_PRIVS] ([TBL_ID],[PRINCIPAL_NAME],[PRINCIPAL_TYPE],[TBL_PRIV],[GRANTOR],[GRANTOR_TYPE])
  END
  
  
  /****** Adding indexes for DATABASE_PARAMS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'DATABASE_PARAMS_N49' AND object_id = OBJECT_ID('DATABASE_PARAMS'))
  BEGIN
  CREATE NONCLUSTERED INDEX DATABASE_PARAMS_N49
  ON [dbo].[DATABASE_PARAMS] ([DB_ID])
  END
  
  
  /****** Adding indexes for ROLE_MAP******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'ROLE_MAP_N49' AND object_id = OBJECT_ID('ROLE_MAP'))
  BEGIN
  CREATE NONCLUSTERED INDEX ROLE_MAP_N49
  ON [dbo].[ROLE_MAP] ([ROLE_ID])
  END
  
  /*** _STATEMENT_BREAK_ ***/
  /****** Adding indexes for SERDE_PARAMS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'SERDE_PARAMS_N49' AND object_id = OBJECT_ID('SERDE_PARAMS'))
  BEGIN
  CREATE NONCLUSTERED INDEX SERDE_PARAMS_N49
  ON [dbo].[SERDE_PARAMS] ([SERDE_ID])
  END
  
  
  /****** Adding indexes for PART_PRIVS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PARTPRIVILEGEINDEX' AND object_id = OBJECT_ID('PART_PRIVS'))
  BEGIN
  CREATE NONCLUSTERED INDEX PARTPRIVILEGEINDEX
  ON [dbo].[PART_PRIVS] ([PART_ID],[PRINCIPAL_NAME],[PRINCIPAL_TYPE],[PART_PRIV],[GRANTOR],[GRANTOR_TYPE])
  END
  
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PART_PRIVS_N49' AND object_id = OBJECT_ID('PART_PRIVS'))
  BEGIN
  CREATE NONCLUSTERED INDEX PART_PRIVS_N49
  ON [dbo].[PART_PRIVS] ([PART_ID])
  END
  
  
  /****** Adding indexes for DB_PRIVS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'DB_PRIVS_N49' AND object_id = OBJECT_ID('DB_PRIVS'))
  BEGIN
  CREATE NONCLUSTERED INDEX DB_PRIVS_N49
  ON [dbo].[DB_PRIVS] ([DB_ID])
  END
  
  
  /****** Adding indexes for TBLS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'TBLS_N49' AND object_id = OBJECT_ID('TBLS'))
  BEGIN
  CREATE NONCLUSTERED INDEX TBLS_N49
  ON [dbo].[TBLS] ([DB_ID])
  END
  
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'TBLS_N50' AND object_id = OBJECT_ID('TBLS'))
  BEGIN
  CREATE NONCLUSTERED INDEX TBLS_N50
  ON [dbo].[TBLS] ([SD_ID])
  END
  
  
  /****** Adding indexes for PARTITION_EVENTS******/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PARTITIONEVENTINDEX' AND object_id = OBJECT_ID('PARTITION_EVENTS'))
  BEGIN
  CREATE NONCLUSTERED INDEX PARTITIONEVENTINDEX
  ON [dbo].[PARTITION_EVENTS] ([PARTITION_NAME])
  END
  
  /*==================Begin: Upgrade from 0.12 to 0.13========================*/
  /*** _STATEMENT_BREAK_ ***/
  /******** HIVE-6386 ************/
  IF NOT EXISTS (SELECT * FROM sys.columns WHERE Name = N'OWNER_NAME' and Object_ID = Object_ID(N'DBS'))
  BEGIN
  ALTER TABLE [dbo].[DBS] ADD [OWNER_NAME] [nvarchar](128) NULL;
  END
  
  IF NOT EXISTS (SELECT * FROM sys.columns WHERE Name = N'OWNER_TYPE' and Object_ID = Object_ID(N'DBS'))
  BEGIN
  ALTER TABLE [dbo].[DBS] ADD [OWNER_TYPE] [nvarchar](10) NULL;
  END
  
  /********* HIVE-6458 **********/
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FUNCS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[FUNCS](
  	[FUNC_ID] [bigint] NOT NULL,
  	[CLASS_NAME] [nvarchar](4000) NULL,
  	[CREATE_TIME] [int] NOT NULL,
  	[DB_ID] [bigint] NULL,
  	[FUNC_NAME] [nvarchar](128) NULL,
  	[FUNC_TYPE] [int] NOT NULL,
  	[OWNER_NAME] [nvarchar](128) NULL,
  	[OWNER_TYPE] [nvarchar](10) NULL,
   CONSTRAINT [FUNCS_PK] PRIMARY KEY CLUSTERED 
  (
  	[FUNC_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  )
  
  CREATE NONCLUSTERED INDEX [FUNCS_N49] ON [dbo].[FUNCS]
  (
  	[DB_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  
  CREATE UNIQUE NONCLUSTERED INDEX [UNIQUEFUNCTION] ON [dbo].[FUNCS]
  (
  	[FUNC_NAME] ASC,
  	[DB_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  
  ALTER TABLE [dbo].[FUNCS]  WITH CHECK ADD  CONSTRAINT [FUNCS_FK1] FOREIGN KEY([DB_ID])
  REFERENCES [dbo].[DBS] ([DB_ID])
  
  ALTER TABLE [dbo].[FUNCS] CHECK CONSTRAINT [FUNCS_FK1]
  END
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FUNC_RU]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[FUNC_RU](
  	[FUNC_ID] [bigint] NOT NULL,
  	[RESOURCE_TYPE] [int] NOT NULL,
  	[RESOURCE_URI] [nvarchar](4000) NULL,
  	[INTEGER_IDX] [int] NOT NULL,
   CONSTRAINT [FUNC_RU_PK] PRIMARY KEY CLUSTERED 
  (
  	[FUNC_ID] ASC,
  	[INTEGER_IDX] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  ) 
  
  CREATE NONCLUSTERED INDEX [FUNC_RU_N49] ON [dbo].[FUNC_RU]
  (
  	[FUNC_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  
  ALTER TABLE [dbo].[FUNC_RU]  WITH CHECK ADD  CONSTRAINT [FUNC_RU_FK1] FOREIGN KEY([FUNC_ID])
  REFERENCES [dbo].[FUNCS] ([FUNC_ID])
  
  ALTER TABLE [dbo].[FUNC_RU] CHECK CONSTRAINT [FUNC_RU_FK1]
  END
  
  /************** hive-txn-schema-0.13.0 ******************/
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TXNS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[TXNS](
  	[TXN_ID] [bigint] NOT NULL,
  	[TXN_STATE] [char](1) NOT NULL,
  	[TXN_STARTED] [bigint] NOT NULL,
  	[TXN_LAST_HEARTBEAT] [bigint] NOT NULL,
  	[TXN_USER] [nvarchar](128) NOT NULL,
  	[TXN_HOST] [nvarchar](128) NOT NULL,
  CONSTRAINT [TXNS_PK] PRIMARY KEY CLUSTERED 
  (
  	[TXN_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  ) 
  END
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TXN_COMPONENTS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[TXN_COMPONENTS](
  	[ID] [bigint] IDENTITY PRIMARY KEY,
  	[TC_TXNID] [bigint] NOT NULL,
  	[TC_DATABASE] [nvarchar](128) NOT NULL,
  	[TC_TABLE] [nvarchar](128) NULL,
  	[TC_PARTITION] [nvarchar](767) NULL
  ) 
  END
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[COMPLETED_TXN_COMPONENTS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[COMPLETED_TXN_COMPONENTS](
  	[ID] [bigint] IDENTITY PRIMARY KEY,
  	[CTC_TXNID] [bigint] NOT NULL,
  	[CTC_DATABASE] [nvarchar](128) NOT NULL,
  	[CTC_TABLE] [nvarchar](128) NULL,
  	[CTC_PARTITION] [nvarchar](767) NULL
  )
  END
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[NEXT_TXN_ID]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[NEXT_TXN_ID](
  	[NTXN_NEXT] [bigint] NOT NULL,
      CONSTRAINT [NEXT_TXN_ID_PK] PRIMARY KEY CLUSTERED
      (
      [NTXN_NEXT] ASC
      )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  ) 
  INSERT INTO NEXT_TXN_ID VALUES(1)
  END
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[HIVE_LOCKS]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[HIVE_LOCKS](
  	[HL_LOCK_EXT_ID] [bigint] NOT NULL,
  	[HL_LOCK_INT_ID] [bigint] NOT NULL,
  	[HL_TXNID] [bigint] NULL,
  	[HL_DB] [nvarchar](128) NOT NULL,
  	[HL_TABLE] [nvarchar](128) NULL,
  	[HL_PARTITION] [nvarchar](767) NULL,
  	[HL_LOCK_STATE] [char](1) NOT NULL,
  	[HL_LOCK_TYPE] [char](1) NOT NULL,
  	[HL_LAST_HEARTBEAT] [bigint] NOT NULL,
  	[HL_ACQUIRED_AT] [bigint] NULL,
  	[HL_USER] [nvarchar](128) NOT NULL,
  	[HL_HOST] [nvarchar](128) NOT NULL,
  PRIMARY KEY CLUSTERED 
  (
  	[HL_LOCK_EXT_ID] ASC,
  	[HL_LOCK_INT_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  ) 
  CREATE NONCLUSTERED INDEX [HL_TXNID_INDEX] ON [dbo].[HIVE_LOCKS]
  (
  	[HL_TXNID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  END
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[NEXT_LOCK_ID]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[NEXT_LOCK_ID](
  	[NL_NEXT] [bigint] NOT NULL,
      CONSTRAINT [NEXT_LOCK_ID_PK] PRIMARY KEY CLUSTERED
      (
      [NL_NEXT] ASC
      )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  ) 
  
  INSERT INTO NEXT_LOCK_ID VALUES(1)
  END
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[COMPACTION_QUEUE]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[COMPACTION_QUEUE](
  	[CQ_ID] [bigint] NOT NULL,
  	[CQ_DATABASE] [nvarchar](128) NOT NULL,
  	[CQ_TABLE] [nvarchar](128) NOT NULL,
  	[CQ_PARTITION] [nvarchar](767) NULL,
  	[CQ_STATE] [char](1) NOT NULL,
  	[CQ_TYPE] [char](1) NOT NULL,
  	[CQ_WORKER_ID] [nvarchar](128) NULL,
  	[CQ_START] [bigint] NULL,
  	[CQ_RUN_AS] [nvarchar](128) NULL,
  PRIMARY KEY CLUSTERED 
  (
  	[CQ_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  )
  END
  
  IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[NEXT_COMPACTION_QUEUE_ID]') AND type in (N'U'))
  BEGIN
  CREATE TABLE [dbo].[NEXT_COMPACTION_QUEUE_ID](
  	[NCQ_NEXT] [bigint] NOT NULL,
      CONSTRAINT [NEXT_COMPACTION_QUEUE_ID_PK] PRIMARY KEY CLUSTERED
      (
      [NCQ_NEXT] ASC
      )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  ) 
  
  INSERT INTO NEXT_COMPACTION_QUEUE_ID VALUES(1)
  END
  
  DECLARE @version [nvarchar](127);
  /* HIVE-5700 AND HIVE-6757 are not schema changes and should be run added to upgrade scripts */
  IF NOT EXISTS (SELECT * FROM [dbo].[VERSION] WHERE VER_ID = 1)
  BEGIN
  INSERT INTO [dbo].[VERSION] (VER_ID, SCHEMA_VERSION, VERSION_COMMENT) VALUES (1, '0.13.0', 'Hive release version 0.13.0');
  END
  ELSE
  BEGIN
  SELECT @version = [SCHEMA_VERSION] from [dbo].[VERSION] WHERE VER_ID = 1
  IF(@version < '0.13.0')
  BEGIN
  UPDATE [dbo].[VERSION] SET SCHEMA_VERSION='0.13.0', VERSION_COMMENT='Hive release version 0.13.0' where VER_ID = 1;
  END
  END
  /*==================End: Upgrade from 0.12 to 0.13========================*/
  
  /*==================Begin: Upgrade from 0.13 to 0.14========================*/
  SELECT @version = [SCHEMA_VERSION] from [dbo].[VERSION] WHERE VER_ID = 1
  
  /******** HIVE-7784 ************/
  IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = 'PCS_STATS_IDX' AND object_id = OBJECT_ID('PART_COL_STATS'))
  BEGIN
  CREATE INDEX PCS_STATS_IDX ON PART_COL_STATS (DB_NAME,TABLE_NAME,COLUMN_NAME,PARTITION_NAME);
  END
  
  /******** HIVE-8239 ************/
  SELECT @version = [SCHEMA_VERSION] from [dbo].[VERSION] WHERE VER_ID = 1
  IF(@version < '0.14.0')
  BEGIN
  -- Change datatype for table "COMPACTION_QUEUE"
  IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[COMPACTION_QUEUE_NEW]') AND type in (N'U'))
  BEGIN
  	DROP TABLE [dbo].[COMPACTION_QUEUE_NEW]
  END
  
  CREATE TABLE [dbo].[COMPACTION_QUEUE_NEW](
  	[CQ_ID] [bigint] NOT NULL,
  	[CQ_DATABASE] [nvarchar](128) NOT NULL,
  	[CQ_TABLE] [nvarchar](128) NOT NULL,
  	[CQ_PARTITION] [nvarchar](767) NULL,
  	[CQ_STATE] [char](1) NOT NULL,
  	[CQ_TYPE] [char](1) NOT NULL,
  	[CQ_WORKER_ID] [nvarchar](128) NULL,
  	[CQ_START] [bigint] NULL,
  	[CQ_RUN_AS] [nvarchar](128) NULL,
  PRIMARY KEY CLUSTERED 
  (
  	[CQ_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  
  INSERT INTO [dbo].[COMPACTION_QUEUE_NEW]([CQ_ID], [CQ_DATABASE], [CQ_TABLE], [CQ_PARTITION], [CQ_STATE], [CQ_TYPE], [CQ_WORKER_ID], [CQ_START], [CQ_RUN_AS]) 
  SELECT [CQ_ID], [CQ_DATABASE], [CQ_TABLE], [CQ_PARTITION], [CQ_STATE], [CQ_TYPE], [CQ_WORKER_ID], [CQ_START], [CQ_RUN_AS] 
  FROM [dbo].[COMPACTION_QUEUE]
  
  DROP TABLE [dbo].[COMPACTION_QUEUE]
  EXECUTE sp_rename N'dbo.COMPACTION_QUEUE_NEW', N'COMPACTION_QUEUE', 'OBJECT'
  
  -- Change datatype for table "COMPLETED_TXN_COMPONENTS"
  IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[COMPLETED_TXN_COMPONENTS_NEW]') AND type in (N'U'))
  BEGIN
  	DROP TABLE [dbo].[COMPLETED_TXN_COMPONENTS_NEW]
  END
  
  CREATE TABLE [dbo].[COMPLETED_TXN_COMPONENTS_NEW](
  	[ID] [bigint] IDENTITY PRIMARY KEY,
  	[CTC_TXNID] [bigint] NOT NULL,
  	[CTC_DATABASE] [nvarchar](128) NOT NULL,
  	[CTC_TABLE] [nvarchar](128) NULL,
  	[CTC_PARTITION] [nvarchar](767) NULL
  	)
  
  INSERT INTO [dbo].[COMPLETED_TXN_COMPONENTS_NEW]([CTC_TXNID], [CTC_DATABASE], [CTC_TABLE], [CTC_PARTITION])
  SELECT [CTC_TXNID], [CTC_DATABASE], [CTC_TABLE], [CTC_PARTITION] FROM [dbo].[COMPLETED_TXN_COMPONENTS]
  
  DROP TABLE [dbo].[COMPLETED_TXN_COMPONENTS]
  EXECUTE sp_rename N'dbo.COMPLETED_TXN_COMPONENTS_NEW', N'COMPLETED_TXN_COMPONENTS', 'OBJECT'
  
  -- Change datatype from table "HIVE_LOCKS"
  IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[HIVE_LOCKS_NEW]') AND type in (N'U'))
  BEGIN
  	DROP TABLE [dbo].[HIVE_LOCKS_NEW]
  END
  
  CREATE TABLE [dbo].[HIVE_LOCKS_NEW](
  	[HL_LOCK_EXT_ID] [bigint] NOT NULL,
  	[HL_LOCK_INT_ID] [bigint] NOT NULL,
  	[HL_TXNID] [bigint] NULL,
  	[HL_DB] [nvarchar](128) NOT NULL,
  	[HL_TABLE] [nvarchar](128) NULL,
  	[HL_PARTITION] [nvarchar](767) NULL,
  	[HL_LOCK_STATE] [char](1) NOT NULL,
  	[HL_LOCK_TYPE] [char](1) NOT NULL,
  	[HL_LAST_HEARTBEAT] [bigint] NOT NULL,
  	[HL_ACQUIRED_AT] [bigint] NULL,
  	[HL_USER] [nvarchar](128) NOT NULL,
  	[HL_HOST] [nvarchar](128) NOT NULL,
  PRIMARY KEY CLUSTERED 
  (
  	[HL_LOCK_EXT_ID] ASC,
  	[HL_LOCK_INT_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  ) 
  CREATE NONCLUSTERED INDEX [HL_TXNID_INDEX] ON [dbo].[HIVE_LOCKS_NEW]
  (
  	[HL_TXNID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  
  INSERT INTO [dbo].[HIVE_LOCKS_NEW]([HL_LOCK_EXT_ID], [HL_LOCK_INT_ID], [HL_TXNID], [HL_DB], [HL_TABLE], [HL_PARTITION], [HL_LOCK_STATE], [HL_LOCK_TYPE], [HL_LAST_HEARTBEAT], [HL_ACQUIRED_AT], [HL_USER], [HL_HOST])
  SELECT [HL_LOCK_EXT_ID], [HL_LOCK_INT_ID], [HL_TXNID], [HL_DB], [HL_TABLE], [HL_PARTITION], [HL_LOCK_STATE], [HL_LOCK_TYPE], [HL_LAST_HEARTBEAT], [HL_ACQUIRED_AT], [HL_USER], [HL_HOST] FROM [dbo].[HIVE_LOCKS]
  
  DROP TABLE [dbo].[HIVE_LOCKS]
  EXECUTE sp_rename N'dbo.HIVE_LOCKS_NEW', N'HIVE_LOCKS', 'OBJECT'
  
  -- Change datatype from table "NEXT_COMPACTION_QUEUE_ID"
  IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[NEXT_COMPACTION_QUEUE_ID_NEW]') AND type in (N'U'))
  BEGIN
  	DROP TABLE [dbo].[NEXT_COMPACTION_QUEUE_ID_NEW]
  END
  
  CREATE TABLE [dbo].[NEXT_COMPACTION_QUEUE_ID_NEW](
  	[NCQ_NEXT] [bigint] NOT NULL,
      CONSTRAINT [NEXT_COMPACTION_QUEUE_ID_PK_NEW] PRIMARY KEY CLUSTERED
      (
      [NCQ_NEXT] ASC
      )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  )
  
  INSERT INTO [dbo].[NEXT_COMPACTION_QUEUE_ID_NEW]([NCQ_NEXT])
  SELECT [NCQ_NEXT] FROM [dbo].[NEXT_COMPACTION_QUEUE_ID]
  
  DROP TABLE [dbo].[NEXT_COMPACTION_QUEUE_ID]
  EXECUTE sp_rename N'dbo.NEXT_COMPACTION_QUEUE_ID_NEW', N'NEXT_COMPACTION_QUEUE_ID', 'OBJECT'
  
  -- Change datatype from table "NEXT_LOCK_ID"
  IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[NEXT_LOCK_ID_NEW]') AND type in (N'U'))
  BEGIN
  	DROP TABLE [dbo].[NEXT_LOCK_ID_NEW]
  END
  
  CREATE TABLE [dbo].[NEXT_LOCK_ID_NEW](
  	[NL_NEXT] [bigint] NOT NULL,
      CONSTRAINT [NEXT_LOCK_ID_PK_NEW] PRIMARY KEY CLUSTERED
      (
      [NL_NEXT] ASC
      )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  )
  
  INSERT INTO [dbo].[NEXT_LOCK_ID_NEW]([NL_NEXT])
  SELECT [NL_NEXT] FROM [dbo].[NEXT_LOCK_ID]
  
  DROP TABLE [dbo].[NEXT_LOCK_ID]
  EXECUTE sp_rename N'dbo.NEXT_LOCK_ID_NEW', N'NEXT_LOCK_ID', 'OBJECT'
  
  -- Change datatype from table "NEXT_TXN_ID"
  IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[NEXT_TXN_ID_NEW]') AND type in (N'U'))
  BEGIN
  	DROP TABLE [dbo].[NEXT_TXN_ID_NEW]
  END
  
  CREATE TABLE [dbo].[NEXT_TXN_ID_NEW](
  	[NTXN_NEXT] [bigint] NOT NULL,
      CONSTRAINT [NEXT_TXN_ID_PK_NEW] PRIMARY KEY CLUSTERED
      (
      [NTXN_NEXT] ASC
      )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
  )
  
  INSERT INTO [dbo].[NEXT_TXN_ID_NEW]([NTXN_NEXT])
  SELECT [NTXN_NEXT] FROM [dbo].[NEXT_TXN_ID]
  
  DROP TABLE [dbo].[NEXT_TXN_ID]
  EXECUTE sp_rename N'dbo.NEXT_TXN_ID_NEW', N'NEXT_TXN_ID', 'OBJECT'
  
  -- Change datatype from table "TXN_COMPONENTS"
  IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TXN_COMPONENTS_NEW]') AND type in (N'U'))
  BEGIN
  	DROP TABLE [dbo].[TXN_COMPONENTS_NEW]
  END
  
  CREATE TABLE [dbo].[TXN_COMPONENTS_NEW](
  	[ID] [bigint] IDENTITY PRIMARY KEY,
  	[TC_TXNID] [bigint] NOT NULL,
  	[TC_DATABASE] [nvarchar](128) NOT NULL,
  	[TC_TABLE] [nvarchar](128) NULL,
  	[TC_PARTITION] [nvarchar](767) NULL
  ) 
  
  INSERT INTO [dbo].[TXN_COMPONENTS_NEW]([TC_TXNID], [TC_DATABASE], [TC_TABLE], [TC_PARTITION])
  SELECT [TC_TXNID], [TC_DATABASE], [TC_TABLE], [TC_PARTITION] FROM [dbo].[TXN_COMPONENTS]
  
  DROP TABLE [dbo].[TXN_COMPONENTS]
  EXECUTE sp_rename N'dbo.TXN_COMPONENTS_NEW', N'TXN_COMPONENTS', 'OBJECT'
  
  -- Change datatype from table "TXNS"
  
  IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TXNS_NEW]') AND type in (N'U'))
  BEGIN
  	DROP TABLE [dbo].[TXNS_NEW]
  END
  
  CREATE TABLE [dbo].[TXNS_NEW](
  	[TXN_ID] [bigint] NOT NULL,
  	[TXN_STATE] [char](1) NOT NULL,
  	[TXN_STARTED] [bigint] NOT NULL,
  	[TXN_LAST_HEARTBEAT] [bigint] NOT NULL,
  	[TXN_USER] [nvarchar](128) NOT NULL,
  	[TXN_HOST] [nvarchar](128) NOT NULL,
  CONSTRAINT [TXNS_PK_NEW] PRIMARY KEY CLUSTERED 
  (
  	[TXN_ID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) 
  ) 
  
  INSERT INTO [dbo].[TXNS_NEW]([TXN_ID], [TXN_STATE], [TXN_STARTED], [TXN_LAST_HEARTBEAT], [TXN_USER], [TXN_HOST])
  SELECT [TXN_ID], [TXN_STATE], [TXN_STARTED], [TXN_LAST_HEARTBEAT], [TXN_USER], [TXN_HOST] FROM [dbo].[TXNS]
  
  DROP TABLE [dbo].[TXNS]
  EXECUTE sp_rename N'dbo.TXNS_NEW', N'TXNS', 'OBJECT'
  
  END
  
  /******** HIVE-8550 ************/
  -- Not necessary since the columns changed in this patch is already type nvarchar in this script
  
  /******** Update MetaStore schema from 0.13.0 to 0.14.0 ************/
  IF NOT EXISTS (SELECT * FROM [dbo].[VERSION] WHERE VER_ID = 1)
  BEGIN
  INSERT INTO [dbo].[VERSION] (VER_ID, SCHEMA_VERSION, VERSION_COMMENT) VALUES (1, '0.14.0', 'Hive release version 0.14.0');
  END
  ELSE
  BEGIN
  SELECT @version = [SCHEMA_VERSION] from [dbo].[VERSION] WHERE VER_ID = 1
  IF(@version < '0.14.0')
  BEGIN
  UPDATE [dbo].[VERSION] SET SCHEMA_VERSION='0.14.0', VERSION_COMMENT='Hive release version 0.14.0' where VER_ID = 1;
  END
  END
  /*==================End: Upgrade from 0.13 to 0.14========================*/
  