0
Create Table ProductCategory(ProductCategory_ID int IDENTITY Primary Key,
ProductCategory varchar(50))
Insert Into Product(ProductCategory)
Select pc.ProductCategory
From ProductCategory pc left join Product p
on p.Category = pc.ProductCategory
Group By p.Category
Order By p.Category
Alter Table Product
Add Constraint FK_ProductCategoryID
Foreign Key (ProductCategoryID)
References ProductCategory(ProductCategory_ID)
据说当我这样做:SQL ID加入到外键ID
Select p.ProductCategoryID
From Product p
应该给我类别的ID从“产品分类”表。但它只是给我NULL 当我做:
Select *
From ProductCategory
我得到的所有正确的ID和类别。关于我要去哪里的任何想法都是错误的?
解释为什么你认为你需要在这里加入了什么。 –
首先,这是SQL服务器,而不是mysql,你没有显示你的产品表 – e4c5
当你运行你的'INSERT INTO'语句时出现任何错误?我以为你想运行'UPDATE'语句来更新'Product.ProductCategory',而不是插入只填充了ProductCategory字段的行,并且所有其他字段都设置为默认值或为空。 – Phylyp