Table

class schemaobject.table.TableSchema(name, parent)

Object representation of a single table (as per CREATE TABLE Syntax). Supports equality and inequality comparison of TableSchema.

name is the column name. parent is an instance of DatabaseSchema

Note

TableSchema objects are automatically created for you by TableSchemaBuilder and loaded under schema.databases[name].tables

Note

table options auto_increment, comment are ignored in __eq__, __neq__ comparisons.

Example

>>> schema.databases['sakila'].tables.keys()
['actor', 'address', 'category', 'city', 'country', 'customer', 'film',
  'film_actor', 'film_category', 'film_text', 'inventory', 'language',
  'payment', 'rental', 'staff', 'store']
>>> schema.databases['sakila'].tables['rental'].options.keys()
['engine', 'charset', 'collation', 'row_format', 'auto_increment', 'create_options', 'comment']
>>> schema.databases['sakila'].tables['rental'].indexes.keys()
['PRIMARY', 'rental_date', 'idx_fk_inventory_id', 'idx_fk_customer_id', 'idx_fk_staff_id']
>>> schema.databases['sakila'].tables['rental'].foreign_keys.keys()
['fk_rental_customer', 'fk_rental_inventory', 'fk_rental_staff']

Table Attributes

>>> schema.databases['sakila'].tables['rental'].name
'rental'

Table Options

  • ENGINE == options['engine']
  • CHARSET, CHARACTER SET == options['charset']
  • COLLATE == options['collation']
  • ROW_FORMAT == options['row_format']
  • AUTO_INCREMENT == options['auto_increment']
  • CREATE_OPTIONS == options['create_options']
  • COMMENT == options['comment']
alter()
Generate the SQL to alter this table
>>> schema.databases['sakila'].tables['rental'].alter()
'ALTER TABLE `rental`'
columns

Lazily loaded dictionary of all the columns within this table. See ColumnSchema for usage

>>> len(schema.databases['sakila'].tables['rental'].columns)
7
>>> schema.databases['sakila'].tables['rental'].columns.keys()
['rental_id', 'rental_date', 'inventory_id', 'customer_id', 'return_date', 'staff_id', 'last_update'
create()
Generate the SQL to create a this table
>>> schema.databases['sakila'].tables['rental'].create()
'CREATE TABLE `rental` (
`rental_id` int(11) NOT NULL AUTO_INCREMENT,
`rental_date` datetime NOT NULL,
`inventory_id` mediumint(8) unsigned NOT NULL,
`customer_id` smallint(5) unsigned NOT NULL,
`return_date` datetime DEFAULT NULL,
`staff_id` tinyint(3) unsigned NOT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`rental_id`),
UNIQUE KEY `rental_date` (`rental_date`,`inventory_id`,`customer_id`),
KEY `idx_fk_inventory_id` (`inventory_id`),
KEY `idx_fk_customer_id` (`customer_id`),
KEY `idx_fk_staff_id` (`staff_id`),
CONSTRAINT `fk_rental_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE,
CONSTRAINT `fk_rental_inventory` FOREIGN KEY (`inventory_id`) REFERENCES `inventory` (`inventory_id`) ON UPDATE CASCADE,
CONSTRAINT `fk_rental_staff` FOREIGN KEY (`staff_id`) REFERENCES `staff` (`staff_id`) ON UPDATE CASCADE)
ENGINE=InnoDB DEFAULT CHARSET=utf8;'
drop()
Generate the SQL to drop this table
>>> schema.databases['sakila'].tables['rental'].drop()
'DROP TABLE `rental`;'
foreign_keys

Lazily loaded dictionary of all the foreign keys within this table. See ForeignKeySchema for usage

>>> len(schema.databases['sakila'].tables['rental'].foreign_keys)
3
>>> schema.databases['sakila'].tables['rental'].foreign_keys.keys()
['fk_rental_customer', 'fk_rental_inventory', 'fk_rental_staff']
indexes

Lazily loaded dictionary of all the indexes within this table. See IndexSchema for usage

>>> len(schema.databases['sakila'].tables['rental'].indexes)
5
>>> schema.databases['sakila'].tables['rental'].indexes.keys()
['PRIMARY', 'rental_date', 'idx_fk_inventory_id', 'idx_fk_customer_id', 'idx_fk_staff_id']
options

Dictionary of the supported MySQL table options. See OptionSchema for usage.

  • ENGINE == options['engine']
  • CHARSET, CHARACTER SET == options['charset']
  • COLLATE == options['collation']
  • ROW_FORMAT == options['row_format']
  • AUTO_INCREMENT == options['auto_increment']
  • CREATE_OPTIONS == options['create_options']
  • COMMENT == options['comment']
schemaobject.table.TableSchemaBuilder(database)

Returns a dictionary loaded with all of the tables available in the database. database must be an instance of DatabaseSchema.

Note

This function is automatically called for you and set to schema.databases[name].tables when you create an instance of SchemaObject

Previous topic

Database

Next topic

Column

This Page