Since I am new to setting up a database on sql server 2008 r2 and creating tables that are referenced by foreign keys, I would like to ask the following about the 4 tables that I created below:
In 3 tables the data will not change much after the data is initially loaded. However in the table called Rej_History, I will be loading data to that table daily. Thus for all the tables, can you tell me if I need to drop indexes every time I update the data. Basically can you tell me and/or point me to a reference that will tell me how to setup a script to load the data?
CREATE TABLE [dbo].[Rej_Contacts](
[MRC_Contact_ID] [int] IDENTITY(1,1) NOT NULL,
[MRC_Prefix] [varchar](30) NULL,
[MRC_Last_Name] [varchar](50) NULL,
[MRC_First_Name] [varchar](50) NULL,
[MRC_Phone_Number] [varchar](25) NULL,
[MRC_Email] [varchar](150) NULL,
[MRC_Address] [varchar](100) NULL,
[MRC_City] [varchar](50) NULL,
[MRC_State] [varchar](2) NULL,
[MRC_Zip] [varchar](10) NULL,
[MRC_Update_Date] [datetime] NULL,
[MRC_Updated_By] [varchar](50) NULL
CONSTRAINT [PK_Rej_Contacts] PRIMARY KEY CLUSTERED
(
[MRC_Contact_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]
------------
CREATE TABLE [dbo].[Rej_History](
[MRH_Id] [int] IDENTITY(1,1) NOT NULL,
[MRH_Plan_Id] [int] NULL,
[MRH_Create_Date] [datetime] NULL,
[MRH_Code_ID] [int] NULL,
[MRH_Tran_Count] [numeric](18, 0) NULL,
[MRH_Batch_Size] [numeric](18, 0) NULL,
[MRH_Tran_Code_Description] [varchar](max) NULL
CONSTRAINT [PK_Rej_History] PRIMARY KEY CLUSTERED
(
[MRH_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]
GO
ALTER TABLE [dbo].[Rej_History] WITH NOCHECK ADD CONSTRAINT [PK_MRH_RNumber] FOREIGN KEY([MRH_Plan_Id])
REFERENCES [dbo].[Rej_Plans] ([MRP_Plan_Id])
GO
ALTER TABLE [dbo].[Rej_History] WITH NOCHECK ADD CONSTRAINT [PK_MRH_Tran_Codes] FOREIGN KEY([MRH_Code_ID])
REFERENCES [dbo].[Tran_Codes] ([TRC_Code_Id])
GO
----------------
CREATE TABLE [dbo].[Rej_Plans](
[MRP_Plan_Id] [int] IDENTITY(1,1) NOT NULL,
[MRP_PLan_Number] [varchar](10) NULL,
[MRP_Contact_Id] [int] NOT NULL,
[MRP_Parent_Organization_Name] [varchar](100) NOT NULL,
[MRP_Update_Date] [datetime] NULL,
[MRP_Updated_By] [varchar](50) NULL
CONSTRAINT [PK_MRP_RNumber] PRIMARY KEY CLUSTERED
(
[MRP_Plan_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]
GO
----------------
CREATE TABLE [dbo].[Tran_Codes](
[TRC_Code_Id] [int] IDENTITY(1,1) NOT NULL,
[TRC_Tran_Code] [numeric](6, 0) NOT NULL,
[TRC_Type] [char](1) NULL,
[TRC_Title] [nvarchar](75) NULL,
[TRC_Long_Definition] [varchar](max) NULL
CONSTRAINT [PK_Tran_Codes] PRIMARY KEY CLUSTERED
(
[TRC_Code_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]
Loading
dcPosted Apr 1, 2012, 5:13 PM
The 3 extra tables that I am referring to are control tables to validate what data is loaded daily into the history table. The other 3 tables will be 3 foregin keys in the history table that refer to the other 3 tables.
The history table will contain all the daily transactions.
Basically I am tyring to decide how to how the history table on a daily basis.
Will I drop the foreign key relationships fiorst before I load the history table daily? Will I load the history table daily and not drop the foreign relationships.
Basically the history table will be loaded daily. What is the best method of referring to the other 3 control tables? Do I just use an alter statement? Can you telll me and/or point me to a reference I can use to solve the problem?
SenthilkumarPosted Mar 31, 2012, 12:05 AM
Your question is bit not clear.
Forgot about the foreign key relationships, you need to main the audit log for the other three tables and want to store on the history table?
If yes, then you need to write the trigger on these three tables and data will be inserted into history table.
If you want to take the backup of these three tables into history table and you need to clear them every day?
Then write the sql server scheduling jobs which will run on the particular scheduled time.
You can clear the table by deleting all the records in the table and you need to insert all these three tables into history table.
Please be specific with your question.