Class Table
- Namespace
- YndigoBlue.Velocity.Model
- Assembly
- YndigoBlue.Velocity.dll
Represents a database table with columns, constraints, and indexes.
public class Table : IFromItem, IEquatable<Table>, IComparable<Table>
- Inheritance
-
Table
- Implements
- Derived
Remarks
Table is the primary container for structured data in a database schema. Tables contain columns that define the data structure, constraints that enforce data integrity rules, and indexes that optimize query performance.
Tables are created within a Schema using CreateTable(string) or loaded from an existing database or schema file.
Examples
Creating a table programmatically:
Schema schema = Schema.NewSchema("my_application");
// Create a users table
Table usersTable = schema.CreateTable("users");
// Add columns
usersTable.CreateColumn("user_id", DataType.Long, autoGenerate: true, notNull: true);
usersTable.CreateColumn("username", DataType.VarChar, size: 100, notNull: true);
usersTable.CreateColumn("email", DataType.VarChar, size: 200, notNull: true);
usersTable.CreateColumn("age", DataType.Integer);
usersTable.CreateColumn("created_at", DataType.DateTime);
// Add primary key constraint
usersTable.AddPrimaryKey("pk_users", usersTable["user_id"]);
// Add unique constraint
usersTable.AddUnique("uk_username", usersTable["username"]);
// Add index for performance
Index ageIndex = usersTable.AddIndex("idx_age");
ageIndex.AddIndexItem(usersTable["age"]);
// Add check constraint
usersTable.AddCheck("ck_age", usersTable["age"], CheckOperator.GreaterThanOrEquals, 18);
Adding a foreign key relationship:
Table ordersTable = schema.CreateTable("orders");
ordersTable.CreateColumn("order_id", DataType.Long, autoGenerate: true, notNull: true);
ordersTable.CreateColumn("user_id", DataType.Long, notNull: true);
ordersTable.CreateColumn("total", DataType.Decimal, precision: 10, scale: 2);
// Add foreign key to users table
ordersTable.AddForeignKey(
"fk_orders_users",
ordersTable["user_id"],
usersTable["user_id"]
);
Working with table data:
using (Manager manager = new Manager(connection))
{
Schema schema = manager.LoadSchema("my_application");
Table usersTable = schema["users"];
// Insert a record
Record record = new Record(usersTable);
record.SetFieldString("username", "john_doe");
record.SetFieldString("email", "john@example.com");
record.SetFieldInt("age", 30);
record.SetFieldDateTime("created_at", DateTime.UtcNow);
manager.AddRecord(record);
// Query the table
var query = new Query()
.Select([ usersTable["username"], usersTable["email"] ])
.From(usersTable)
.Where(new Criterion<int>(usersTable["age"], ConditionalType.GreaterThan, 25));
using (ResultSet results = manager.Retrieve(query))
{
foreach (Result row in results)
{
Console.WriteLine($"{row.GetFieldString("username")}: {row.GetFieldString("email")}");
}
}
}
Properties
- Columns
Gets the ordered collection of columns defined on this table.
- Constraints
Gets the collection of constraints (primary keys, foreign keys, unique, check, default) defined on this table.
- FullTextIndex
Gets the full-text index for this table, if one has been defined.
- Indexes
Gets the collection of indexes defined on this table.
- this[int]
Gets the column at the specified zero-based index.
- this[string]
Gets the column with the specified name from this table.
- Name
Gets the name of the table.
- NotNullColumns
Gets the collection of columns that have a NOT NULL constraint and are not auto-generated.
- SchemaName
Gets or sets the name of the schema that contains this table.
- SchemaUpdate
Gets or sets a value indicating whether this table is part of a schema update operation.
- SelectItems
Gets all columns in this table as a collection of ISelectItem instances, for use in query building.
- SpatialIndex
Gets the spatial index for this table, if one has been defined.
Methods
- AddColumn(string, DataType, bool, bool, int, byte, byte)
Adds a new column to an already-deployed table as a schema migration operation.
- AddColumn(string, DataType, DefaultConstraintType, IDefaultItem, bool, bool, int, byte, byte)
Adds a new column to an already-deployed table as a schema migration operation, with a default value applied to existing rows.
- Clone(string)
Creates a deep clone of this table including all columns, constraints, and indexes.
- CloneForStaging(string, bool)
Creates a staging copy of this table suitable for bulk import, excluding auto-generated columns unless
keepIdentityistrue. All NOT NULL constraints are relaxed on the copy to allow partial data loading.
- CompareTo(Table)
Compares this table to another by name for ordering purposes.
- CreateCheckConstraint(string, IEnumerable<ICheckItem>)
Creates a CHECK constraint defined by a collection of check items.
- CreateCheckConstraint(string, ICheckItem)
Creates a CHECK constraint defined by a single check item.
- CreateCheckConstraint(string, ICheckItem[])
Creates a CHECK constraint defined by an array of check items.
- CreateColumn(string, DataType, bool, bool, int, byte, byte)
Defines a new column for this table as part of the initial schema definition.
- CreateDefaultConstraint(string, IDefaultItem, DefaultConstraintType, DataType)
Creates a DEFAULT constraint for a column, specifying the default value applied when no value is provided.
- CreateForeignKey(string, IEnumerable<string>, string, string, IEnumerable<string>, ForeignKeyRuleType, ForeignKeyRuleType)
Creates a composite foreign key constraint referencing columns in another table.
- CreateForeignKey(string, string, string, string, string, ForeignKeyRuleType, ForeignKeyRuleType)
Creates a foreign key constraint referencing a column in another table.
- CreateForeignKey(string, string[], string, string, string[], ForeignKeyRuleType, ForeignKeyRuleType)
Creates a composite foreign key constraint referencing columns in another table.
- CreateFullTextIndex(string, IEnumerable<string>)
Creates a full-text index across the specified columns on this table.
- CreateIndex(string)
Creates an empty index that columns can be added to via AddIndexColumn(string, IndexOrderType).
- CreateIndex(string, string, IndexOrderType)
Creates an index on a single column with the specified ordering.
- CreatePrimaryKey(string, IEnumerable<string>)
Creates a composite primary key constraint across multiple columns.
- CreatePrimaryKey(string, string)
Creates a primary key constraint on a single column.
- CreatePrimaryKey(string, string[])
Creates a composite primary key constraint across multiple columns.
- CreateSpatialIndex(string, string)
Creates a spatial index on a geometry or geography column with zeroed bounding box and grid sizes, suitable for databases that auto-compute the extent.
- CreateSpatialIndex(string, string, double, double, double, double, double, double, double)
Creates a spatial index on a geometry or geography column with an explicit bounding box and multi-level grid sizes.
- CreateSpatialIndexLatLong(string, string, double, double, double, double, double, double, double)
Creates a spatial index on a geography column using WGS 84 (latitude/longitude) world-coverage defaults.
- CreateUniqueConstraint(string, IEnumerable<string>)
Creates a UNIQUE constraint across multiple columns.
- CreateUniqueConstraint(string, string)
Creates a UNIQUE constraint on a single column.
- CreateUniqueConstraint(string, string[])
Creates a UNIQUE constraint across multiple columns.
- Equals(Table)
Performs a deep structural comparison of two tables, comparing columns, constraints, and indexes.
- GetAutoIncrementColumn()
Returns the first auto-increment (identity/sequence) column on this table.
- GetConstraint(string)
Returns the constraint with the specified name.
- GetIndex(string)
Returns the index with the specified name.
- GetSelectItem(string)
Returns the column with the specified name as an ISelectItem.
- GetTypeMap()
Returns a read-only dictionary mapping column names to their DataType values for all columns in this table.
- HasAutoIncrementColumn()
Determines whether this table has at least one auto-increment (identity/sequence) column.
- HasColumn(string)
Determines whether a column with the specified name exists in this table.
- HasIndex(string)
Determines whether an index with the specified name exists on this table.
- HasSelectItem(string)
Determines whether a column with the specified name exists in this table.
- HasSelectItem(ISelectItem)
Determines whether the specified select item corresponds to a column in this table.
- ToString()
Returns the name of this table.