Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Simulating multiple tables
Message
De
09/07/2009 03:41:43
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Conception bases de données
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01410856
Message ID:
01411091
Vues:
31
>>OK:
USE [LLNET4]
>>GO
>>/****** Object:  User [LLNETUSER]    Script Date: 07/08/2009 18:06:22 ******/
>>CREATE USER [LLNETUSER] FOR LOGIN [LLNETUSER] WITH DEFAULT_SCHEMA=[dbo]
>>GO
>>/****** Object:  Table [dbo].[Owners]    Script Date: 07/08/2009 18:06:22 ******/
>>SET ANSI_NULLS ON
>>GO
>>SET QUOTED_IDENTIFIER ON
>>GO
>>CREATE TABLE [dbo].[Owners](
>>	[Id] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_Owners_Id]  DEFAULT (newid()),
>>	[Name] [nvarchar](50) NULL,
>>	[Password] [nvarchar](50) NULL,
>> CONSTRAINT [PK_Owners] PRIMARY KEY CLUSTERED 
>>(
>>	[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
>>/****** Object:  Table [dbo].[ProductFileData]    Script Date: 07/08/2009 18:06:22 ******/
>>SET ANSI_NULLS ON
>>GO
>>SET QUOTED_IDENTIFIER ON
>>GO
>>SET ANSI_PADDING ON
>>GO
>>CREATE TABLE [dbo].[ProductFileData](
>>	[Id] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_ProductFileData_Id]  DEFAULT (newid()),
>>	[ProductFileColumnId] [uniqueidentifier] NULL,
>>	[ProductFileRowId] [uniqueidentifier] NULL,
>>	[ProductFileId] [uniqueidentifier] NULL,
>>	[ObjectData] [varbinary](max) NULL,
>>	[TextData] [nvarchar](max) NULL,
>> CONSTRAINT [PK_ProductFileData] PRIMARY KEY CLUSTERED 
>>(
>>	[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
>>SET ANSI_PADDING OFF
>>GO
>>/****** Object:  Table [dbo].[VisualObjects]    Script Date: 07/08/2009 18:06:22 ******/
>>SET ANSI_NULLS ON
>>GO
>>SET QUOTED_IDENTIFIER ON
>>GO
>>SET ANSI_PADDING ON
>>GO
>>CREATE TABLE [dbo].[VisualObjects](
>>	[Id] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_VisualObjects_Id]  DEFAULT (newid()),
>>	[OwnerId] [uniqueidentifier] NULL,
>>	[ProductFileColumnId] [uniqueidentifier] NULL,
>>	[Name] [nvarchar](50) NULL,
>>	[Object] [varbinary](max) NULL,
>>	[ObjectType] [int] NULL,
>> CONSTRAINT [PK_VisualObjects] PRIMARY KEY CLUSTERED 
>>(
>>	[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
>>SET ANSI_PADDING OFF
>>GO
>>/****** Object:  Table [dbo].[ProductFiles]    Script Date: 07/08/2009 18:06:22 ******/
>>SET ANSI_NULLS ON
>>GO
>>SET QUOTED_IDENTIFIER ON
>>GO
>>CREATE TABLE [dbo].[ProductFiles](
>>	[Id] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_ProductFiles_Id]  DEFAULT (newid()),
>>	[OwnerId] [uniqueidentifier] NULL,
>>	[Name] [nvarchar](50) NULL,
>> CONSTRAINT [PK_ProductFiles] PRIMARY KEY CLUSTERED 
>>(
>>	[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
>>/****** Object:  Table [dbo].[ProductFileColumns]    Script Date: 07/08/2009 18:06:22 ******/
>>SET ANSI_NULLS ON
>>GO
>>SET QUOTED_IDENTIFIER ON
>>GO
>>CREATE TABLE [dbo].[ProductFileColumns](
>>	[Id] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_ProductFileColumns_Id]  DEFAULT (newid()),
>>	[ProductFileId] [uniqueidentifier] NULL,
>>	[Name] [nvarchar](50) NULL,
>> CONSTRAINT [PK_ProductFileColumns] PRIMARY KEY CLUSTERED 
>>(
>>	[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
>>/****** Object:  Trigger [AddProductDataRows]    Script Date: 07/08/2009 18:06:22 ******/
>>SET ANSI_NULLS ON
>>GO
>>SET QUOTED_IDENTIFIER ON
>>GO
>>-- =============================================
>>-- Author:		Viv Phillips
>>-- Create date: 08/07/2009
>>-- Description:	Add data records for each existing row and the added column(s)
>>-- =============================================
>>CREATE TRIGGER [dbo].[AddProductDataRows]
>>   ON  [dbo].[ProductFileColumns]
>>   AFTER INSERT
>>AS 
>>BEGIN
>>    INSERT INTO ProductFileData
>>    (ProductFileColumnId,ProductFileRowId,ProductFileId)
>>    SELECT DISTINCT INSERTED.Id,ProductFileData.ProductFileRowId,INSERTED.ProductFileId 
>>    FROM INSERTED INNER JOIN ProductFileData ON ProductFileData.ProductFileId = INSERTED.ProductFileId
>>END
>>GO
>>/****** Object:  StoredProcedure [dbo].[AddRow]    Script Date: 07/08/2009 18:06:22 ******/
>>SET ANSI_NULLS ON
>>GO
>>SET QUOTED_IDENTIFIER ON
>>GO
>>-- =============================================
>>-- Author:		<Author,,Name>
>>-- Create date: <Create Date,,>
>>-- Description:	<Description,,>
>>-- =============================================
>>CREATE PROCEDURE [dbo].[AddRow]
>>    @ProductFileId uniqueidentifier
>>AS
>>BEGIN
>>    DECLARE @newid uniqueidentifier
>>    SET @newid = NEWID()
>>    INSERT INTO ProductFileData
>>    (ProductFileId,ProductFileColumnId,ProductFileRowId)
>>     SELECT @ProductFileId,ProductFileColumns.Id,@newid FROM ProductFileColumns WHERE ProductFileColumns.ProductFileId = @ProductFileId
>>END
>>GO
>>/****** Object:  ForeignKey [FK_ProductFileColumns_ProductFiles]    Script Date: 07/08/2009 18:06:22 ******/
>>ALTER TABLE [dbo].[ProductFileColumns]  WITH CHECK ADD  CONSTRAINT [FK_ProductFileColumns_ProductFiles] FOREIGN KEY([ProductFileId])
>>REFERENCES [dbo].[ProductFiles] ([Id])
>>ON DELETE CASCADE
>>GO
>>ALTER TABLE [dbo].[ProductFileColumns] CHECK CONSTRAINT [FK_ProductFileColumns_ProductFiles]
>>GO
>>/****** Object:  ForeignKey [FK_ProductFileData_ProductFileColumns]    Script Date: 07/08/2009 18:06:22 ******/
>>ALTER TABLE [dbo].[ProductFileData]  WITH CHECK ADD  CONSTRAINT [FK_ProductFileData_ProductFileColumns] FOREIGN KEY([ProductFileColumnId])
>>REFERENCES [dbo].[ProductFileColumns] ([Id])
>>ON DELETE CASCADE
>>GO
>>ALTER TABLE [dbo].[ProductFileData] CHECK CONSTRAINT [FK_ProductFileData_ProductFileColumns]
>>GO
>>/****** Object:  ForeignKey [FK_ProductFiles_Owners]    Script Date: 07/08/2009 18:06:22 ******/
>>ALTER TABLE [dbo].[ProductFiles]  WITH CHECK ADD  CONSTRAINT [FK_ProductFiles_Owners] FOREIGN KEY([OwnerId])
>>REFERENCES [dbo].[Owners] ([Id])
>>ON DELETE CASCADE
>>GO
>>ALTER TABLE [dbo].[ProductFiles] CHECK CONSTRAINT [FK_ProductFiles_Owners]
>>GO
>>/****** Object:  ForeignKey [FK_VisualObjects_ProductFileColumns]    Script Date: 07/08/2009 18:06:22 ******/
>>ALTER TABLE [dbo].[VisualObjects]  WITH CHECK ADD  CONSTRAINT [FK_VisualObjects_ProductFileColumns] FOREIGN KEY([ProductFileColumnId])
>>REFERENCES [dbo].[ProductFileColumns] ([Id])
>>ON DELETE CASCADE
>>GO
>>ALTER TABLE [dbo].[VisualObjects] CHECK CONSTRAINT [FK_VisualObjects_ProductFileColumns]
>>GO
>>The trigger here seems OK - but it still needs a ProductFileId field in ProductFileData which would not otherwise be neccessary....
>>Forget about the VisualObjects table - it's not relevant....
>>TBH I'm thinking of using the original design since it's likely to be more flexible. This might create problems as the database is properly fleshed out....
>
>Hi Viv,
>
>I created a database with these objects here, but somehow I lost the idea of what we need to accomplish. Do you think what you have right now is OK or still want to re-design?

You could think of it as a spreadsheet with multiple sheets. Whenever a row is added to a sheet then a cell needs to be created for each existing column on that sheet. Whenever a column is added a cell needs to be created for each existing row. My equivalent of a cell is a record in the ProductFileData table. The trigger here works but, as mentioned, requires the ProductFileId field in the ProductFileData table - and there's no other reason for it's presence. I suppose I could do without it by joining the ProductFileColumns table but.....
On top of that I've a suspicion that when I think this through further there may well be reasons to retain the ProductFileRows table anyway.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform