Foreign Keys
var country = Table.Create("Country",
"ID".Int().Identity(1, 1).PrimaryKey(),
"Alpha2Code".Char(2),
"Name".NVarChar(100));
var customer = Table.Create("Customer",
"ID".Int().PrimaryKey(),
"Country".Char(2).NotNull().References(country["Alpha2Code"]));
CREATE TABLE [Country] (
[ID] INT IDENTITY(1, 1) NOT NULL PRIMARY KEY,
[Alpha2Code] CHAR(2),
[Name] NVARCHAR(100)
);
CREATE TABLE [Customer] (
[ID] INT NOT NULL PRIMARY KEY,
[Country] CHAR(2) NOT NULL REFERENCES [Country] ( [Alpha2Code] )
);
You don’t need to define the “Country” table fully. You can make a simple Table
variable and reference columns by name. Fluent SQL doesn’t check that the column exists.
var country = Table.Create("Country");
var customer = Table.Create("Customer",
"ID".Int().PrimaryKey(),
"Country".Char(2).NotNull().References(country["Alpha2Code"]));
CREATE TABLE [Customer] (
[ID] INT NOT NULL PRIMARY KEY,
[Country] CHAR(2) NOT NULL REFERENCES [Country] ( [Alpha2Code] )
);
Named constraints and cascade clauses are not yet supported.