Home
%3CLINGO-SUB%20id%3D%22lingo-sub-371162%22%20slang%3D%22en-US%22%3ESQL%202016%20-%20Temporal%20Tables%20-%20Can%20you%20change%20the%20schema%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-371162%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Dec%2021%2C%202016%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3EI%20was%20recently%20asked%20if%20after%20you%20setup%20a%20Temporal%20Table%2C%20can%20you%20modify%20the%20schema%3F%26nbsp%3B%20It%20was%20a%20great%20question%20because%20if%20you%20cannot%20change%20the%20schema%2C%20then%20you%20would%20have%20to%20drop%20and%20recreate%20the%20table%20every%20time%20you%20need%20to%20change%20the%20schema%20which%20means%20you%20would%20potentially%20lose%20all%20the%20history.%26nbsp%3B%20Of%20course%2C%20there%20are%20workarounds%20to%20preserve%20that%20history%20like%20using%20views%20to%20combine%20more%20than%20one%20history%20table%2C%20but%20that%20can%20become%20a%20maintenance%20nightmare%20on%20tables%20that%20are%20changed%20frequently%20(like%20those%20in%20development%20where%20you%20want%20use%20Temporal%20Tables%20to%20track%20data%20modifications%20for%20troubleshooting).%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ETesting%20to%20find%20out%20if%20we%20can%20change%20the%20schema%20of%20a%20temporal%20table%20is%20pretty%20easy.%26nbsp%3B%20First%20we%20need%20to%20create%20a%20temporal%20table%3A%3CPRE%3E%0ACREATE%20TABLE%20%5Bdbo%5D.%5BTemporalTest%5D(%0A%20%5Bcolint%5D%20%5Bint%5D%20NOT%20NULL%2C%0A%20%5Bperiod_start%5D%20%5Bdatetime2%5D(7)%20GENERATED%20ALWAYS%20AS%20ROW%20START%20NOT%20NULL%2C%0A%20%5Bperiod_end%5D%20%5Bdatetime2%5D(7)%20GENERATED%20ALWAYS%20AS%20ROW%20END%20NOT%20NULL%2C%0APRIMARY%20KEY%20CLUSTERED%20%0A(%0A%20%5Bcolint%5D%20ASC%0A)ON%20%5BPRIMARY%5D%2C%0A%20PERIOD%20FOR%20SYSTEM_TIME%20(%5Bperiod_start%5D%2C%20%5Bperiod_end%5D)%0A)%20ON%20%5BPRIMARY%5D%0AWITH%0A(%0ASYSTEM_VERSIONING%20%3D%20ON%20(HISTORY_TABLE%20%3D%20%5Bdbo%5D.%5BTemporalTestHistory%5D%20%2C%20DATA_CONSISTENCY_CHECK%20%3D%20ON%20)%0A)%0A%3C%2FPRE%3E%0A%0A%0ANext%2C%20let%E2%80%99s%20see%20if%20we%20can%20use%20the%20Table%20Designer%20in%20SSMS.%26nbsp%3B%20Here%20is%20what%20we%20see%20when%20we%20right%20click%20on%20our%20temporal%20table%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20439px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89955i3A6874C3BA7439F9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EHmmm%2C%20the%20Design%20option%20is%20missing%2C%20this%20makes%20me%20think%20that%20you%20cannot%20change%20the%20schema.%26nbsp%3B%20Let%E2%80%99s%20see%20what%20happens%20if%20we%20use%20TSQL%20to%20add%20a%20column%3A%0A%0A%3CPRE%3E%0AALTER%20TABLE%20dbo.TemporalTest%20ADD%20HaIKnowTSQL%20VARCHAR(15)%3B%0A%3C%2FPRE%3E%0A%0AWhen%20you%20run%20the%20above%20command%2C%20to%20my%20surprise%2C%20it%20showed%20it%20completed%20successfully!%26nbsp%3B%20Let%E2%80%99s%20add%20some%20data%20and%20see%20if%20it%E2%80%99s%20populating%3A%0A%0A%3CPRE%3E%0AINSERT%20INTO%20TemporalTest%20(colint%2CHaIKnowTSQL)%0AVALUES%20(20%2C'TestSuccessful!')%3B%0AGO%0A%0ASELECT%20'TemporalTest'%20AS%20%5BTable%20Name%5D%2C%20*%0AFROM%20TemporalTest%0AWHERE%20HaIKnowTSQL%20%3D%20'TestSuccessful!'%3B%0A%3C%2FPRE%3E%0A%0A%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20641px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89956iD44B0A5595F23ED6%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESo%20far%20so%20good%2C%20but%20did%20we%20break%20the%20history%20table%3F%26nbsp%3B%20Let%E2%80%99s%20modify%20the%20data%20and%20find%20out%3A%0A%0A%3CPRE%3E%0AUPDATE%20TemporalTest%0ASET%20colint%20%3D%2021%0AWHERE%20HaIKnowTSQL%20%3D%20'TestSuccessful!'%3B%0AGO%0A%0ASELECT%20'TemporalTest'%20AS%20%5BTable%20Name%5D%2C*%0AFROM%20TemporalTest%0AWHERE%20HaIKnowTSQL%20%3D%20'TestSuccessful!'%3B%0A%0ASELECT%20'TemporalTestHistory'%20AS%20%5BTable%20Name%5D%2C*%0AFROM%20TemporalTestHistory%0AWHERE%20HaIKnowTSQL%20%3D%20'TestSuccessful!'%3B%0A%3C%2FPRE%3E%0A%0A%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20658px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89957i0188B8E0AC09E164%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3ESuccess%2C%20we%20can%20modify%20the%20schema%20of%20a%20temporal%20table!%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EOne%20limitation%20however%2C%20is%20that%20you%20cannot%20drop%20the%20primary%20key%20which%20means%20you%20cannot%20change%20it.%26nbsp%3B%20When%20I%20tried%20to%20drop%20it%2C%20I%20received%20this%20error%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20714px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F89958iF4C5B9398CC2C9A9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20more%20information%20on%20Temporal%20Tables%2C%20please%20check%20out%20this%20MSDN%20article%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20title%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fdn935015.aspx%22%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fdn935015.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fdn935015.aspx%20%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSTRONG%3E%20Links%20to%20other%20posts%20in%20this%20series%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsql_pfe_blog%2F2016%2F11%2F01%2Fsql-2016-temporal-tables-what-are-they-and-how-do-you-set-one-up%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20SQL%202016%20%E2%80%93%20Temporal%20Tables%20%E2%80%93%20What%20are%20they%20and%20how%20do%20you%20set%20one%20up%3F%20%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsql_pfe_blog%2F2017%2F01%2F19%2Fsql-2016-temporal-tables-how-do-you-drop-a-temporal-table%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20SQL%202016%20%E2%80%93%20Temporal%20Tables%20%E2%80%93%20How%20do%20you%20drop%20a%20Temporal%20Table%3F%3C%2FA%3E%20%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-371162%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20MSDN%20on%20Dec%2021%2C%202016%20I%20was%20recently%20asked%20if%20after%20you%20setup%20a%20Temporal%20Table%2C%20can%20you%20modify%20the%20schema%3F%26nbsp%3B%20It%20was%20a%20great%20question%20because%20if%20you%20cannot%20change%20the%20schema%2C%20then%20you%20would%20have%20to%20drop%20and%20recreate%20the%20table%20every%20time%20you%20need%20to%20change%20the%20schema%20which%20means%20you%20would%20potentially%20lose%20all%20the%20history.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-371162%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQL%20Server%202016%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Etemporal%20table%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

First published on MSDN on Dec 21, 2016
I was recently asked if after you setup a Temporal Table, can you modify the schema?  It was a great question because if you cannot change the schema, then you would have to drop and recreate the table every time you need to change the schema which means you would potentially lose all the history.  Of course, there are workarounds to preserve that history like using views to combine more than one history table, but that can become a maintenance nightmare on tables that are changed frequently (like those in development where you want use Temporal Tables to track data modifications for troubleshooting).



Testing to find out if we can change the schema of a temporal table is pretty easy.  First we need to create a temporal table:

CREATE TABLE [dbo].[TemporalTest](
 [colint] [int] NOT NULL,
 [period_start] [datetime2](7) GENERATED ALWAYS AS ROW START NOT NULL,
 [period_end] [datetime2](7) GENERATED ALWAYS AS ROW END NOT NULL,
PRIMARY KEY CLUSTERED 
(
 [colint] ASC
)ON [PRIMARY],
 PERIOD FOR SYSTEM_TIME ([period_start], [period_end])
) ON [PRIMARY]
WITH
(
SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[TemporalTestHistory] , DATA_CONSISTENCY_CHECK = ON )
)
Next, let’s see if we can use the Table Designer in SSMS.  Here is what we see when we right click on our temporal table:







Hmmm, the Design option is missing, this makes me think that you cannot change the schema.  Let’s see what happens if we use TSQL to add a column:
ALTER TABLE dbo.TemporalTest ADD HaIKnowTSQL VARCHAR(15);
When you run the above command, to my surprise, it showed it completed successfully!  Let’s add some data and see if it’s populating:
INSERT INTO TemporalTest (colint,HaIKnowTSQL)
VALUES (20,'TestSuccessful!');
GO

SELECT 'TemporalTest' AS [Table Name], *
FROM TemporalTest
WHERE HaIKnowTSQL = 'TestSuccessful!';




So far so good, but did we break the history table?  Let’s modify the data and find out:
UPDATE TemporalTest
SET colint = 21
WHERE HaIKnowTSQL = 'TestSuccessful!';
GO

SELECT 'TemporalTest' AS [Table Name],*
FROM TemporalTest
WHERE HaIKnowTSQL = 'TestSuccessful!';

SELECT 'TemporalTestHistory' AS [Table Name],*
FROM TemporalTestHistory
WHERE HaIKnowTSQL = 'TestSuccessful!';




Success, we can modify the schema of a temporal table!



One limitation however, is that you cannot drop the primary key which means you cannot change it.  When I tried to drop it, I received this error:







For more information on Temporal Tables, please check out this MSDN article:

https://msdn.microsoft.com/en-us/library/dn935015.aspx



Links to other posts in this series

SQL 2016 – Temporal Tables – What are they and how do you set one up?

SQL 2016 – Temporal Tables – How do you drop a Temporal Table?