I made a SQL file which makes me tables etc. There is one problem with it. It makes clustered key to table Product BUT refuses to make it to rest of tables.
Can someone please help me out?
IF OBJECT_ID(N'[dbo].[FK_Product_ProductList]', 'F') IS NOT NULL
ALTER TABLE [dbo].[Product] DROP CONSTRAINT [FK_Product_ProductList];
GO
IF OBJECT_ID(N'[dbo].[FK_ProductList_User]', 'F') IS NOT NULL
ALTER TABLE [dbo].[ProductList] DROP CONSTRAINT [FK_ProductList_User];
GO
IF OBJECT_ID(N'[dbo].[Product]', 'U') IS NOT NULL
DROP TABLE [dbo].[Product];
GO
IF OBJECT_ID(N'[dbo].[ProductList]', 'U') IS NOT NULL
DROP TABLE [dbo].[ProductList];
GO
CREATE TABLE [dbo].[Product] (
[Id] int NOT NULL,
[ProductListId] int NOT NULL,
[Name] nvarchar(50) NOT NULL,
[Weight] decimal(10,2) NOT NULL,
[Price] decimal(10,2) NOT NULL,
[Amount] int NOT NULL
);
GO
-- Creating table 'ProductList'
CREATE TABLE [dbo].[ProductList] (
[Id] int NOT NULL,
[UserId] int NOT NULL,
[Name] nvarchar(50) NOT NULL
);
ALTER TABLE [dbo].[Product]
ADD CONSTRAINT [PK_Product]
PRIMARY KEY CLUSTERED ([Id] ASC);
GO
ALTER TABLE [dbo].[ProductList]
ADD CONSTRAINT [PK_ProductList]
PRIMARY KEY CLUSTERED ([Id] ASC);
GO
ALTER TABLE [dbo].[Product]
ADD CONSTRAINT [FK_Product_ProductList]
FOREIGN KEY ([ProductListId])
REFERENCES [dbo].[ProductList]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
GO
CREATE INDEX [IX_FK_Product_ProductList]
ON [dbo].[Product]
([ProductListId]);
GO
ALTER TABLE [dbo].[ProductList]
ADD CONSTRAINT [FK_ProductList_User]
FOREIGN KEY ([UserId])
REFERENCES [dbo].[User]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
GO
CREATE INDEX [IX_FK_ProductList_User]
ON [dbo].[ProductList]
([UserId]);
GO
In practise, everything should be working fine but still I feel like i'm missing something out there.
I also tried to fix it manually from database but when I tried to save the file, I've got the message "Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created or enabled the option Prevent saving changes that require the table to be re-created.
*For purpose of this post I removed creating and editing Users table as its going same way as Product List.
User Product List"
Problem Solved. Everything was bugging because of this small change:
CREATE TABLE [dbo].[ProductList] (
[Id] int IDENTITY (1,1) NOT NULL,
[UserId] int NOT NULL,
[Name] nvarchar(50) NOT NULL
I'm really grateful for Your help!