【发布时间】:2015-06-01 03:19:27
【问题描述】:
我正在一个项目中使用 C# 在数据库中创建表,但我不知道如何创建外键。
这是我要创建的 SQL:
CREATE TABLE FactSalesOrders
(ProductKey int NOT NULL REFERENCES DimProduct(ProductKey),
CustomerKey int NOT NULL REFERENCES DimCustomer(CustomerKey),
SalespersonKey int NOT NULL REFERENCES DimSalesperson(SalepersonKey),
OrderDateKey int NOT NULL REFERENCES DimDate(DateKey),
OrderNo int NOT NULL,
ItemNo int Not NULL,
Quantity int Not NULL,
SalesAmount money NOT NULL,
Cost money NOT NULL
CONSTRAINT [PK_FactSalesOrders] PRIMARY KEY NONCLUSTERED
(
[ProductKey],[CustomerKey],[SalespersonKey],[OrderDateKey],[OrderNo],[ItemNo]
)
)
例如,我只是尝试为 ProductKey 设置“REFERENCES”部分,使其引用 DimProduct 表中的 ProductKey 列。
这是我在 factTable 表中创建 ProductKey 列的代码:
//Creating Fact Table
Table factTable = new Table(myDatabase, "Fact Table");
//Column One: Product Key
Column ProductKey = new Column(factTable, "ProductKey", DataType.Int);
ProductKey.Nullable = false;
factTable.Columns.Add(ProductKey);
这是我尝试引用的 DimProduct 表的代码:
//Creating DimProduct
Table DimProduct = new Table(myDatabase, "DimProduct");
//Column One: Product Key
Column productKey = new Column(DimProduct, "ProductKey", DataType.Int);
productKey.Nullable = false;
DimProduct.Columns.Add(productKey);
//Column Two: Product Alt Key
Column productAltKey = new Column(DimProduct, "ProductAltKey", DataType.NVarChar(10));
productAltKey.Nullable = false;
DimProduct.Columns.Add(productAltKey);
//Column Three: Product Name
Column productName = new Column(DimProduct, "ProductName", DataType.NVarChar(50));
productName.Nullable = true;
DimProduct.Columns.Add(productName);
//Column Four: Product Description
Column productDescription = new Column(DimProduct, "ProductDescription", DataType.NVarChar(100));
productDescription.Nullable = true;
DimProduct.Columns.Add(productDescription);
//Column Five: Product Catagory Name
Column productCatagoryName = new Column(DimProduct, "ProductCatagoryName", DataType.NVarChar(50));
productCatagoryName.Nullable = true;
DimProduct.Columns.Add(productCatagoryName);
//Primary Key
Index primaryKeyIndex1 = new Index(DimProduct, "PK_DimProduct");
primaryKeyIndex1.IndexKeyType = IndexKeyType.DriPrimaryKey;
primaryKeyIndex1.IndexedColumns.Add(new IndexedColumn(primaryKeyIndex1, "productKey"));
DimProduct.Indexes.Add(primaryKeyIndex1);
DimProduct.Create();
我确实找到了这个链接:LINK1
它给出了以下示例,但我无法让它工作:
//Connect to the local, default instance of SQL Server.
Server srv;
srv = new Server();
//Reference the AdventureWorks2012 database.
Database db;
db = srv.Databases["AdventureWorks2012"];
//Declare another Table object variable and reference the EmployeeDepartmentHistory table.
Table tbea;
tbea = db.Tables["EmployeeDepartmentHistory", "HumanResources"];
//Define a Foreign Key object variable by supplying the EmployeeDepartmentHistory as the parent table and the foreign key name in the constructor.
ForeignKey fk;
fk = new ForeignKey(tbea, "test_foreignkey");
//Add BusinessEntityID as the foreign key column.
ForeignKeyColumn fkc;
fkc = new ForeignKeyColumn(fk, "BusinessEntityID", "BusinessEntityID");
fk.Columns.Add(fkc);
//Set the referenced table and schema.
fk.ReferencedTable = "Employee";
fk.ReferencedTableSchema = "HumanResources";
//Create the foreign key on the instance of SQL Server.
fk.Create();
任何帮助或见解将不胜感激!谢谢!
【问题讨论】:
-
...您使用的是 MySQL 还是 SQL Server? 什么对你正在尝试的东西不起作用?
-
我正在使用 SQL Server。基本上我所有的代码都可以正常工作,我只是不知道如何设置外键。
-
天哪,它也被标记为 mysql
标签: c# sql-server foreign-keys smo