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
Examples
Creating a table programmatically:
Schema schema = Schema.NewSchema("my_application");
// Create a users table
Table usersTable = schema.CreateTable("users");
// Add columns
usersTable.AddColumn("user_id", DataType.Long, autoGenerate: true, notNull: true);
usersTable.AddColumn("username", DataType.VarChar, size: 100, notNull: true);
usersTable.AddColumn("email", DataType.VarChar, size: 200, notNull: true);
usersTable.AddColumn("age", DataType.Integer);
usersTable.AddColumn("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.AddColumn("order_id", DataType.Long, autoGenerate: true, notNull: true);
ordersTable.AddColumn("user_id", DataType.Long, notNull: true);
ordersTable.AddColumn("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))
{
manager.LoadSchema("my_application");
Schema schema = manager.GetSchema("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
Query query = new Query();
query.AddFromItem(usersTable);
query.AddSelectItem(usersTable["username"]);
query.AddSelectItem(usersTable["email"]);
query.AddFilter(usersTable["age"], ConditionalType.GreaterThan, 25);
using (ResultSet results = manager.Retrieve(query))
{
foreach (Result row in results)
{
Console.WriteLine($"{row.GetFieldString("username")}: {row.GetFieldString("email")}");
}
}
}
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.
Fields
Properties
- FullTextIndex
Gets the full-text index for this table, if one has been defined.
- Name
Gets the name of the table.
- 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 the collection of select items (columns or expressions) available from this FROM item.
- SpatialIndex
Gets the spatial index for this table, if one has been defined.
Methods
- CompareTo(Table)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
- CreateForeignKey(string, string[], string, string, string[], ForeignKeyRuleType, ForeignKeyRuleType)
- Equals(Table)
Indicates whether the current object is equal to another object of the same type.
- GetSelectItem(string)
Retrieves the select item with the specified name.
- HasSelectItem(string)
Determines whether this FROM item contains a select item with the specified name.
- HasSelectItem(ISelectItem)
Determines whether this FROM item contains the specified select item.
- ToString()
Returns a string that represents the current object.