notebook/IT/SQL/SCRIPTS SQL QSA ORSYS/Solution exeo 2.sql
2020-11-26 18:38:25 +01:00

94 lines
3.1 KiB
Transact-SQL

USE DB_GRAND_HOTEL;
GO
-- création des espaces de stockage
ALTER DATABASE DB_GRAND_HOTEL ADD FILEGROUP FG_PART_OLD;
ALTER DATABASE DB_GRAND_HOTEL
ADD FILE (NAME = 'F_PART_OLD',
FILENAME = 'H:\DATABASE_SQL\SQL2019FBIN2\DATA\HOTEL_PART_OLD.ndf',
SIZE = 25 MB,
FILEGROWTH = 10 MB)
TO FILEGROUP FG_PART_OLD;
GO
ALTER DATABASE DB_GRAND_HOTEL ADD FILEGROUP FG_PART_2006;
ALTER DATABASE DB_GRAND_HOTEL
ADD FILE (NAME = 'F_PART_2006',
FILENAME = 'H:\DATABASE_SQL\SQL2019FBIN2\DATA\HOTEL_PART_2006.ndf',
SIZE = 25 MB,
FILEGROWTH = 10 MB)
TO FILEGROUP FG_PART_2006;
GO
ALTER DATABASE DB_GRAND_HOTEL ADD FILEGROUP FG_PART_2007;
ALTER DATABASE DB_GRAND_HOTEL
ADD FILE (NAME = 'F_PART_2007',
FILENAME = 'H:\DATABASE_SQL\SQL2019FBIN2\DATA\HOTEL_PART_2007.ndf',
SIZE = 25 MB,
FILEGROWTH = 10 MB)
TO FILEGROUP FG_PART_2007;
GO
ALTER DATABASE DB_GRAND_HOTEL ADD FILEGROUP FG_PART_2008;
ALTER DATABASE DB_GRAND_HOTEL
ADD FILE (NAME = 'F_PART_2008',
FILENAME = 'H:\DATABASE_SQL\SQL2019FBIN2\DATA\HOTEL_PART_2008.ndf',
SIZE = 25 MB,
FILEGROWTH = 10 MB)
TO FILEGROUP FG_PART_2008;
GO
-- 1) création de la fonction de partitionnement
CREATE PARTITION FUNCTION PF_DATE_FACTURE (DATETIME)
AS RANGE RIGHT
FOR VALUES ('2006-01-01', '2007-01-01', '2008-01-01');
-- 2) création du schéma de répartition
CREATE PARTITION SCHEME PS_DATE_FACTURE
AS PARTITION PF_DATE_FACTURE
TO (FG_PART_OLD, FG_PART_2006, FG_PART_2007, FG_PART_2008);
GO
-- 3) création de l'objet sur la partition
BEGIN TRANSACTION;
BEGIN TRY
--> il faut commencer par retirer la contrainte FK de la table T_FACTURE_ITEM_ITM
ALTER TABLE [S_CHB].[T_FACTURE_ITEM_ITM] DROP CONSTRAINT [FK_T_FACTUR_CONTIENT_T_FACTUR];
--> il faut commencer par retirer la PK !!!
ALTER TABLE [S_CHB].[T_FACTURE_FAC] DROP CONSTRAINT [PK_T_FACTURE_FAC];
--> pas possible !
CREATE UNIQUE CLUSTERED INDEX X ON [S_CHB].[T_FACTURE_FAC] ([FAC_DATE], [FAC_ID])
ON PS_DATE_FACTURE(FAC_DATE);
--> remettre la PK (ATTENTION : par défaut la PK est créée sous forme d'index clustered)
ALTER TABLE [S_CHB].[T_FACTURE_FAC]
ADD CONSTRAINT [PK_T_FACTURE_FAC] PRIMARY KEY NONCLUSTERED ([FAC_ID])
ON FG_DATA;
--> remettre la FK
ALTER TABLE [S_CHB].[T_FACTURE_ITEM_ITM]
ADD CONSTRAINT [FK_T_FACTUR_CONTIENT_T_FACTUR]
FOREIGN KEY ([FAC_ID])
REFERENCES [S_CHB].[T_FACTURE_FAC] (FAC_ID);
COMMIT;
-- tout se passe bien => COMMIT
END TRY
BEGIN CATCH
-- quelque chose se passe mal => ROLLBACK
IF XACT_STATE() <> 0
ROLLBACK;
THROW;
END CATCH
-- voir les stats de lignes des partitions
SELECT *
FROM sys.dm_db_partition_stats AS ps
JOIN sys.indexes AS i
ON ps.object_id = i.object_id AND ps.index_id = i.index_id
WHERE ps.object_id = OBJECT_ID('[S_CHB].[T_FACTURE_FAC]')
AND i.type_desc = 'CLUSTERED';