Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Simulating multiple tables
Message
De
08/07/2009 13:10:33
 
 
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:
01410960
Vues:
40
>>>Exactly - I was going to tell that it's not going to work this way because of multiple inserts possibility.
>>>
>>>But would this work?
>>>
>>>    INSERT INTO ProductFileData
>>>    (ProductFileColumnId,ProductFileRowId,ProductFileId)
>>>    SELECT INSERTED.Id,NewID(),INSERTED.ProductFileId
>>>    FROM INSERTED INNER JOIN ProductFileData ON ProductFileData.ProductFileId = INSERTED.ProductFileId
>>>
>>>Or why not let ProductFileRowID field to have default NewID() and then you don't need to include it in insert?
>>
>>Actually what I posted above is complete rubbish. The ProductFileRowId has to be based on existing unique values for existing rows.
>>Using NEWID() is completely wrong. Using a default would be worse because all the rows would have different Ids. Let me try again....
>
>Can you post the problem definition again with the table creation scripts and some data?

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....
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform