### Home Classes Methods

### Files

- CHANGELOG
- MIT-LICENSE
- README.rdoc
- active_record.rdoc
- advanced_associations.rdoc
- association_basics.rdoc
- cheat_sheet.rdoc
- dataset_basics.rdoc
- dataset_filtering.rdoc
- mass_assignment.rdoc
- migration.rdoc
- model_hooks.rdoc
- opening_databases.rdoc
- prepared_statements.rdoc
- querying.rdoc
- reflection.rdoc
- 1.0.txt
- 1.1.txt
- 1.3.txt
- 1.4.0.txt
- 1.5.0.txt
- 2.0.0.txt
- 2.1.0.txt
- 2.10.0.txt
- 2.11.0.txt
- 2.12.0.txt
- 2.2.0.txt
- 2.3.0.txt
- 2.4.0.txt
- 2.5.0.txt
- 2.6.0.txt
- 2.7.0.txt
- 2.8.0.txt
- 2.9.0.txt
- 3.0.0.txt
- 3.1.0.txt
- 3.10.0.txt
- 3.11.0.txt
- 3.12.0.txt
- 3.13.0.txt
- 3.14.0.txt
- 3.15.0.txt
- 3.16.0.txt
- 3.17.0.txt
- 3.18.0.txt
- 3.19.0.txt
- 3.2.0.txt
- 3.20.0.txt
- 3.21.0.txt
- 3.22.0.txt
- 3.23.0.txt
- 3.24.0.txt
- 3.25.0.txt
- 3.26.0.txt
- 3.27.0.txt
- 3.28.0.txt
- 3.29.0.txt
- 3.3.0.txt
- 3.30.0.txt
- 3.31.0.txt
- 3.32.0.txt
- 3.33.0.txt
- 3.4.0.txt
- 3.5.0.txt
- 3.6.0.txt
- 3.7.0.txt
- 3.8.0.txt
- 3.9.0.txt
- schema_modification.rdoc
- sharding.rdoc
- sql.rdoc
- testing.rdoc
- transactions.rdoc
- validations.rdoc
- virtual_rows.rdoc

### Class Index

Quicksearch

- Sequel
- Sequel::ADO
- Sequel::ADO::Database
- Sequel::ADO::Dataset
- Sequel::ADO::MSSQL
- Sequel::ADO::MSSQL::DatabaseMethods
- Sequel::ADO::MSSQL::Dataset
- Sequel::ASTTransformer
- Sequel::Access
- Sequel::Access::DatabaseMethods
- Sequel::Access::DatasetMethods
- Sequel::Adapter
- Sequel::AdapterNotFound
- Sequel::AdapterNotFound
- Sequel::Amalgalite
- Sequel::Amalgalite::Database
- Sequel::Amalgalite::Dataset
- Sequel::Amalgalite::SequelTypeMap
- Sequel::ArbitraryServers
- Sequel::BasicObject
- Sequel::ColumnsIntrospection
- Sequel::ConnectionPool
- Sequel::ConnectionPool::ClassMethods
- Sequel::DB2
- Sequel::DB2::DB2Error
- Sequel::DB2::Database
- Sequel::DB2::DatabaseMethods
- Sequel::DB2::Dataset
- Sequel::DB2::DatasetMethods
- Sequel::DBI
- Sequel::DBI::Database
- Sequel::DBI::Dataset
- Sequel::DataObjects
- Sequel::DataObjects::Database
- Sequel::DataObjects::Dataset
- Sequel::DataObjects::MySQL
- Sequel::DataObjects::MySQL::DatabaseMethods
- Sequel::DataObjects::MySQL::Dataset
- Sequel::DataObjects::Postgres
- Sequel::DataObjects::Postgres::AdapterMethods
- Sequel::DataObjects::Postgres::DatabaseMethods
- Sequel::DataObjects::Postgres::Dataset
- Sequel::DataObjects::SQLite
- Sequel::DataObjects::SQLite::DatabaseMethods
- Sequel::DataObjects::SQLite::Dataset
- Sequel::Database
- Sequel::DatabaseConnectionError
- Sequel::DatabaseDisconnectError
- Sequel::DatabaseError
- Sequel::Dataset
- Sequel::Dataset
- Sequel::Dataset::ArgumentMapper
- Sequel::Dataset::BindArgumentMethods
- Sequel::Dataset::Pagination
- Sequel::Dataset::PreparedStatementMethods
- Sequel::Dataset::QueryBlockCopy
- Sequel::Dataset::StoredProcedureMethods
- Sequel::Dataset::StoredProcedures
- Sequel::Dataset::TYPE_TRANSLATOR
- Sequel::Dataset::UnnumberedArgumentMapper
- Sequel::EmulateOffsetWithRowNumber
- Sequel::Error
- Sequel::Firebird
- Sequel::Firebird::Database
- Sequel::Firebird::DatabaseMethods
- Sequel::Firebird::Dataset
- Sequel::Firebird::DatasetMethods
- Sequel::HookFailed
- Sequel::HookFailed
- Sequel::IBMDB
- Sequel::IBMDB::Connection
- Sequel::IBMDB::Connection::Error
- Sequel::IBMDB::Database
- Sequel::IBMDB::Dataset
- Sequel::IBMDB::Dataset::CallableStatementMethods
- Sequel::IBMDB::Dataset::PreparedStatementMethods
- Sequel::IBMDB::Statement
- Sequel::Inflections
- Sequel::Informix
- Sequel::Informix::Database
- Sequel::Informix::DatabaseMethods
- Sequel::Informix::Dataset
- Sequel::Informix::DatasetMethods
- Sequel::IntegerMigrator
- Sequel::InvalidOperation
- Sequel::InvalidOperation
- Sequel::InvalidValue
- Sequel::InvalidValue
- Sequel::JDBC
- Sequel::JDBC::AS400
- Sequel::JDBC::AS400::DatabaseMethods
- Sequel::JDBC::AS400::Dataset
- Sequel::JDBC::DB2
- Sequel::JDBC::DB2::DatabaseMethods
- Sequel::JDBC::DB2::Dataset
- Sequel::JDBC::Database
- Sequel::JDBC::Derby
- Sequel::JDBC::Derby::DatabaseMethods
- Sequel::JDBC::Derby::Dataset
- Sequel::JDBC::Firebird
- Sequel::JDBC::Firebird::DatabaseMethods
- Sequel::JDBC::Firebird::Dataset
- Sequel::JDBC::H2
- Sequel::JDBC::H2::DatabaseMethods
- Sequel::JDBC::H2::Dataset
- Sequel::JDBC::HSQLDB
- Sequel::JDBC::HSQLDB::DatabaseMethods
- Sequel::JDBC::HSQLDB::Dataset
- Sequel::JDBC::Informix
- Sequel::JDBC::Informix::DatabaseMethods
- Sequel::JDBC::Informix::Dataset
- Sequel::JDBC::JTDS
- Sequel::JDBC::JTDS::DatabaseMethods
- Sequel::JDBC::JTDS::Dataset
- Sequel::JDBC::JavaLang
- Sequel::JDBC::JavaSQL
- Sequel::JDBC::JavaxNaming
- Sequel::JDBC::MSSQL
- Sequel::JDBC::MSSQL::DatabaseMethods
- Sequel::JDBC::MySQL
- Sequel::JDBC::MySQL::DatabaseMethods
- Sequel::JDBC::MySQL::Dataset
- Sequel::JDBC::Oracle
- Sequel::JDBC::Oracle::DatabaseMethods
- Sequel::JDBC::Oracle::Dataset
- Sequel::JDBC::Postgres
- Sequel::JDBC::Postgres::AdapterMethods
- Sequel::JDBC::Postgres::DatabaseMethods
- Sequel::JDBC::Postgres::Dataset
- Sequel::JDBC::SQLServer
- Sequel::JDBC::SQLServer::DatabaseMethods
- Sequel::JDBC::SQLServer::DatabaseMethods::MetadataDatasetMethods
- Sequel::JDBC::SQLServer::Dataset
- Sequel::JDBC::SQLite
- Sequel::JDBC::SQLite::DatabaseMethods
- Sequel::JDBC::SQLite::Dataset
- Sequel::JDBC::Transactions
- Sequel::LiteralString
- Sequel::LooserTypecasting
- Sequel::MSSQL
- Sequel::MSSQL::DatabaseMethods
- Sequel::MSSQL::DatasetMethods
- Sequel::Metaprogramming
- Sequel::Migration
- Sequel::MigrationAlterTableReverser
- Sequel::MigrationDSL
- Sequel::MigrationReverser
- Sequel::Migrator
- Sequel::Migrator::Error
- Sequel::Mock
- Sequel::Mock::Connection
- Sequel::Mock::Database
- Sequel::Mock::Dataset
- Sequel::Model
- Sequel::Model::Associations
- Sequel::Model::Associations::AssociationDatasetMethods
- Sequel::Model::Associations::AssociationReflection
- Sequel::Model::Associations::ClassMethods
- Sequel::Model::Associations::DatasetMethods
- Sequel::Model::Associations::EagerGraphLoader
- Sequel::Model::Associations::InstanceMethods
- Sequel::Model::Associations::ManyToManyAssociationReflection
- Sequel::Model::Associations::ManyToOneAssociationReflection
- Sequel::Model::Associations::OneToManyAssociationReflection
- Sequel::Model::Associations::OneToOneAssociationReflection
- Sequel::Model::ClassMethods
- Sequel::Model::DatasetMethods
- Sequel::Model::Errors
- Sequel::Model::InstanceMethods
- Sequel::MySQL
- Sequel::MySQL::DatabaseMethods
- Sequel::MySQL::DatasetMethods
- Sequel::MySQL::DatasetMethods::CallableStatementMethods
- Sequel::MySQL::DatasetMethods::PreparedStatementMethods
- Sequel::MySQL::DatasetMethods::StoredProcedureMethods
- Sequel::MySQL::PreparedStatements
- Sequel::MySQL::PreparedStatements::DatabaseMethods
- Sequel::Mysql2
- Sequel::Mysql2::Database
- Sequel::Mysql2::Dataset
- Sequel::NamedTimezones
- Sequel::NoExistingObject
- Sequel::NoExistingObject
- Sequel::NotImplemented
- Sequel::ODBC
- Sequel::ODBC::DB2
- Sequel::ODBC::DB2::DatabaseMethods
- Sequel::ODBC::DB2::Dataset
- Sequel::ODBC::Database
- Sequel::ODBC::Dataset
- Sequel::ODBC::MSSQL
- Sequel::ODBC::MSSQL::DatabaseMethods
- Sequel::ODBC::MSSQL::Dataset
- Sequel::OpenBase
- Sequel::OpenBase::Database
- Sequel::OpenBase::Dataset
- Sequel::Oracle
- Sequel::Oracle::Database
- Sequel::Oracle::DatabaseMethods
- Sequel::Oracle::DatasetMethods
- Sequel::Plugins
- Sequel::Plugins::ActiveModel
- Sequel::Plugins::ActiveModel::ClassMethods
- Sequel::Plugins::ActiveModel::InstanceMethods
- Sequel::Plugins::AssociationAutoreloading
- Sequel::Plugins::AssociationAutoreloading::ClassMethods
- Sequel::Plugins::AssociationDependencies
- Sequel::Plugins::AssociationDependencies::ClassMethods
- Sequel::Plugins::AssociationDependencies::InstanceMethods
- Sequel::Plugins::AssociationPks
- Sequel::Plugins::AssociationPks::ClassMethods
- Sequel::Plugins::AssociationPks::InstanceMethods
- Sequel::Plugins::AssociationProxies
- Sequel::Plugins::AssociationProxies::AssociationProxy
- Sequel::Plugins::AssociationProxies::ClassMethods
- Sequel::Plugins::BooleanReaders
- Sequel::Plugins::BooleanReaders::ClassMethods
- Sequel::Plugins::Caching
- Sequel::Plugins::Caching::ClassMethods
- Sequel::Plugins::Caching::InstanceMethods
- Sequel::Plugins::ClassTableInheritance
- Sequel::Plugins::ClassTableInheritance::ClassMethods
- Sequel::Plugins::ClassTableInheritance::InstanceMethods
- Sequel::Plugins::Composition
- Sequel::Plugins::Composition::ClassMethods
- Sequel::Plugins::Composition::InstanceMethods
- Sequel::Plugins::DatasetAssociations
- Sequel::Plugins::DatasetAssociations::ClassMethods
- Sequel::Plugins::DatasetAssociations::DatasetMethods
- Sequel::Plugins::DefaultsSetter
- Sequel::Plugins::DefaultsSetter::ClassMethods
- Sequel::Plugins::DefaultsSetter::InstanceMethods
- Sequel::Plugins::ForceEncoding
- Sequel::Plugins::ForceEncoding::ClassMethods
- Sequel::Plugins::ForceEncoding::InstanceMethods
- Sequel::Plugins::HookClassMethods
- Sequel::Plugins::HookClassMethods::ClassMethods
- Sequel::Plugins::HookClassMethods::InstanceMethods
- Sequel::Plugins::IdentityMap
- Sequel::Plugins::IdentityMap::ClassMethods
- Sequel::Plugins::IdentityMap::InstanceMethods
- Sequel::Plugins::InstanceFilters
- Sequel::Plugins::InstanceFilters::InstanceMethods
- Sequel::Plugins::InstanceHooks
- Sequel::Plugins::InstanceHooks::InstanceMethods
- Sequel::Plugins::JsonSerializer
- Sequel::Plugins::JsonSerializer::ClassMethods
- Sequel::Plugins::JsonSerializer::DatasetMethods
- Sequel::Plugins::JsonSerializer::InstanceMethods
- Sequel::Plugins::JsonSerializer::Literal
- Sequel::Plugins::LazyAttributes
- Sequel::Plugins::LazyAttributes::ClassMethods
- Sequel::Plugins::LazyAttributes::InstanceMethods
- Sequel::Plugins::List
- Sequel::Plugins::List::ClassMethods
- Sequel::Plugins::List::InstanceMethods
- Sequel::Plugins::ManyThroughMany
- Sequel::Plugins::ManyThroughMany::ClassMethods
- Sequel::Plugins::ManyThroughMany::DatasetMethods
- Sequel::Plugins::ManyThroughMany::ManyThroughManyAssociationReflection
- Sequel::Plugins::NestedAttributes
- Sequel::Plugins::NestedAttributes::ClassMethods
- Sequel::Plugins::NestedAttributes::InstanceMethods
- Sequel::Plugins::OptimisticLocking
- Sequel::Plugins::OptimisticLocking::ClassMethods
- Sequel::Plugins::OptimisticLocking::InstanceMethods
- Sequel::Plugins::PreparedStatements
- Sequel::Plugins::PreparedStatements::ClassMethods
- Sequel::Plugins::PreparedStatements::InstanceMethods
- Sequel::Plugins::PreparedStatementsAssociations
- Sequel::Plugins::PreparedStatementsAssociations::ClassMethods
- Sequel::Plugins::PreparedStatementsAssociations::InstanceMethods
- Sequel::Plugins::PreparedStatementsSafe
- Sequel::Plugins::PreparedStatementsSafe::ClassMethods
- Sequel::Plugins::PreparedStatementsSafe::InstanceMethods
- Sequel::Plugins::PreparedStatementsWithPk
- Sequel::Plugins::PreparedStatementsWithPk::ClassMethods
- Sequel::Plugins::PreparedStatementsWithPk::DatasetMethods
- Sequel::Plugins::RcteTree
- Sequel::Plugins::Schema
- Sequel::Plugins::Schema::ClassMethods
- Sequel::Plugins::Serialization
- Sequel::Plugins::Serialization::ClassMethods
- Sequel::Plugins::Serialization::InstanceMethods
- Sequel::Plugins::SerializationModificationDetection
- Sequel::Plugins::SerializationModificationDetection::InstanceMethods
- Sequel::Plugins::Sharding
- Sequel::Plugins::Sharding::ClassMethods
- Sequel::Plugins::Sharding::DatasetMethods
- Sequel::Plugins::Sharding::InstanceMethods
- Sequel::Plugins::SingleTableInheritance
- Sequel::Plugins::SingleTableInheritance::ClassMethods
- Sequel::Plugins::SingleTableInheritance::InstanceMethods
- Sequel::Plugins::SkipCreateRefresh
- Sequel::Plugins::SkipCreateRefresh::InstanceMethods
- Sequel::Plugins::StringStripper
- Sequel::Plugins::StringStripper::InstanceMethods
- Sequel::Plugins::Subclasses
- Sequel::Plugins::Subclasses::ClassMethods
- Sequel::Plugins::TacticalEagerLoading
- Sequel::Plugins::TacticalEagerLoading::DatasetMethods
- Sequel::Plugins::TacticalEagerLoading::InstanceMethods
- Sequel::Plugins::Timestamps
- Sequel::Plugins::Timestamps::ClassMethods
- Sequel::Plugins::Timestamps::InstanceMethods
- Sequel::Plugins::Touch
- Sequel::Plugins::Touch::ClassMethods
- Sequel::Plugins::Touch::InstanceMethods
- Sequel::Plugins::Tree
- Sequel::Plugins::Tree::ClassMethods
- Sequel::Plugins::Tree::InstanceMethods
- Sequel::Plugins::Tree::SingleRoot
- Sequel::Plugins::Tree::SingleRoot::ClassMethods
- Sequel::Plugins::Tree::SingleRoot::InstanceMethods
- Sequel::Plugins::Tree::TreeMultipleRootError
- Sequel::Plugins::TypecastOnLoad
- Sequel::Plugins::TypecastOnLoad::ClassMethods
- Sequel::Plugins::TypecastOnLoad::InstanceMethods
- Sequel::Plugins::UpdatePrimaryKey
- Sequel::Plugins::UpdatePrimaryKey::ClassMethods
- Sequel::Plugins::UpdatePrimaryKey::InstanceMethods
- Sequel::Plugins::ValidationClassMethods
- Sequel::Plugins::ValidationClassMethods::ClassMethods
- Sequel::Plugins::ValidationClassMethods::ClassMethods::Generator
- Sequel::Plugins::ValidationClassMethods::InstanceMethods
- Sequel::Plugins::ValidationHelpers
- Sequel::Plugins::ValidationHelpers::InstanceMethods
- Sequel::Plugins::XmlSerializer
- Sequel::Plugins::XmlSerializer::ClassMethods
- Sequel::Plugins::XmlSerializer::DatasetMethods
- Sequel::Plugins::XmlSerializer::InstanceMethods
- Sequel::PoolTimeout
- Sequel::PoolTimeout
- Sequel::Postgres
- Sequel::Postgres::AdapterMethods
- Sequel::Postgres::DatabaseMethods
- Sequel::Postgres::DatasetMethods
- Sequel::Postgres::DatasetMethods::PreparedStatementMethods
- Sequel::PrettyTable
- Sequel::Progress
- Sequel::Progress::DatabaseMethods
- Sequel::Progress::DatasetMethods
- Sequel::Qualifier
- Sequel::Rollback
- Sequel::Rollback
- Sequel::SQL
- Sequel::SQL::AliasMethods
- Sequel::SQL::AliasedExpression
- Sequel::SQL::BitwiseMethods
- Sequel::SQL::Blob
- Sequel::SQL::BooleanConstant
- Sequel::SQL::BooleanExpression
- Sequel::SQL::BooleanMethods
- Sequel::SQL::CaseExpression
- Sequel::SQL::Cast
- Sequel::SQL::CastMethods
- Sequel::SQL::ColumnAll
- Sequel::SQL::ComplexExpression
- Sequel::SQL::ComplexExpressionMethods
- Sequel::SQL::Constant
- Sequel::SQL::Constants
- Sequel::SQL::Expression
- Sequel::SQL::Function
- Sequel::SQL::GenericComplexExpression
- Sequel::SQL::GenericExpression
- Sequel::SQL::Identifier
- Sequel::SQL::IdentifierMethods
- Sequel::SQL::InequalityMethods
- Sequel::SQL::JoinClause
- Sequel::SQL::JoinOnClause
- Sequel::SQL::JoinUsingClause
- Sequel::SQL::NegativeBooleanConstant
- Sequel::SQL::NoBooleanInputMethods
- Sequel::SQL::NumericExpression
- Sequel::SQL::NumericMethods
- Sequel::SQL::OrderMethods
- Sequel::SQL::OrderedExpression
- Sequel::SQL::PlaceholderLiteralString
- Sequel::SQL::QualifiedIdentifier
- Sequel::SQL::QualifyingMethods
- Sequel::SQL::StringConcatenationMethods
- Sequel::SQL::StringExpression
- Sequel::SQL::StringMethods
- Sequel::SQL::Subscript
- Sequel::SQL::SubscriptMethods
- Sequel::SQL::ValueList
- Sequel::SQL::ValueList
- Sequel::SQL::VirtualRow
- Sequel::SQL::Window
- Sequel::SQL::WindowFunction
- Sequel::SQLTime
- Sequel::SQLite
- Sequel::SQLite::DatabaseMethods
- Sequel::SQLite::DatasetMethods
- Sequel::Schema
- Sequel::Schema::AlterTableGenerator
- Sequel::Schema::Generator
- Sequel::ServerBlock
- Sequel::ShardedSingleConnectionPool
- Sequel::ShardedThreadedConnectionPool
- Sequel::SimpleMigration
- Sequel::SingleConnectionPool
- Sequel::Swift
- Sequel::Swift::Database
- Sequel::Swift::Dataset
- Sequel::Swift::MySQL
- Sequel::Swift::MySQL::DatabaseMethods
- Sequel::Swift::MySQL::Dataset
- Sequel::Swift::Postgres
- Sequel::Swift::Postgres::AdapterMethods
- Sequel::Swift::Postgres::DatabaseMethods
- Sequel::Swift::Postgres::Dataset
- Sequel::Swift::SQLite
- Sequel::Swift::SQLite::DatabaseMethods
- Sequel::Swift::SQLite::Dataset
- Sequel::ThreadLocalTimezones
- Sequel::ThreadedConnectionPool
- Sequel::ThreadedServerBlock
- Sequel::TimestampMigrator
- Sequel::Timezones
- Sequel::TinyTDS
- Sequel::TinyTDS::Database
- Sequel::TinyTDS::Dataset
- Sequel::TinyTDS::Dataset::ArgumentMapper
- Sequel::TinyTDS::Dataset::PreparedStatementMethods
- Sequel::ToDot
- Sequel::UnbindDuplicate
- Sequel::Unbinder
- Sequel::UndefinedAssociation
- Sequel::UnthreadedServerBlock
- Sequel::ValidationFailed
- Dataset
- Dataset::ArgumentMapper
- Dataset::BindArgumentMethods
- Dataset::PreparedStatementMethods
- String
- String::Inflections
- Array
- FalseClass
- Hash
- NilClass
- Numeric
- Object
- PGconn
- PGresult
- Proc
- Symbol
- TrueClass

No matching classes.

# Schema modification methods

Here’s a brief description of the most common schema modification methods:

## `create_table`

`create_table` is the most common schema modification method, and it’s used for adding new tables to the schema. You provide it with the name of the table as a symbol, as well a block:

      create_table(:artists) do
        primary_key :id
        String :name
      end

Not that if you want a primary key for the table, you need to specify it, Sequel does not create one by default.

### Column types

Most method calls inside the create_table block will create columns, since `method_missing` calls `column`. Columns are generally created by specifying the column type as the method name, followed by the column name symbol to use, and after that any options that should be used. If the method is a ruby class name that Sequel recognizes, Sequel will transform it into the appropriate type for the given database. So while you specified `String`, Sequel will actually use `varchar` or `text` depending on the underlying database. Here’s a list of all of ruby classes that Sequel will convert to database types:

      create_table(:columns_types) do       # common database type used
        Integer :a0                         # integer
        String :a1                          # varchar(255)
        String :a2, :size=>50               # varchar(50)
        String :a3, :fixed=>true            # char(255)
        String :a4, :fixed=>true, :size=>50 # char(50)
        String :a5, :text=>true             # text
        File :b,                            # blob
        Fixnum :c                           # integer
        Bignum :d                           # bigint
        Float :e                            # double precision
        BigDecimal :f                       # numeric
        BigDecimal :f2, :size=>10           # numeric(10)
        BigDecimal :f3, :size=>[10, 2]      # numeric(10, 2)
        Date :g                             # date
        DateTime :h                         # timestamp
        Time :i                             # timestamp
        Time :i2, :only_time=>true          # time
        Numeric :j                          # numeric
        TrueClass :k                        # boolean
        FalseClass :l                       # boolean
      end

Note that in addition to the ruby class name, Sequel also pays attention to the column options when determining which database type to use. Also note that for boolean columns, you can use either TrueClass or FalseClass, they are treated the same way (ruby doesn’t have a Boolean class).

Also note that this conversion is only done if you use a supported ruby class name. In all other cases, Sequel uses the type specified verbatim:

      create_table(:columns_types) do  # database type used
        string :a1                     # string
        datetime :a2                   # datetime
        blob :a3                       # blob
        inet :a4                       # inet
      end

In addition to specifying the types as methods, you can use the `column` method and specify the types as the second argument, either as ruby classes, symbols, or strings:

      create_table(:columns_types) do  # database type used
        column :a1, :string            # string
        column :a2, String             # varchar(255)
        column :a3, 'string'           # string
        column :a4, :datetime          # datetime
        column :a5, DateTime           # timestamp
        column :a6, 'timestamp(6)'     # timestamp(6)
      end

### Column options

When using the type name as method, the third argument is an options hash, and when using the `column` method, the fourth argument is the options hash. The following options are supported:

:default | The default value for the column.
:index | Create an index on this column.
:null | Mark the column as allowing NULL values (if true), or not allowing NULL values (if false). If unspecified, will default to whatever the database default is.
:size | The size of the column, generally used with string columns to specify the maximum number of characters the column will hold. An array of two integers can be provided to set the size and the precision, respectively, of decimal columns.
:unique | Mark the column as unique, generally has the same effect as creating a unique index on the column.
:unsigned | Make the column type unsigned, only useful for integer columns.

### Other methods

In addition to the `column` method and other methods that create columns, there are a other methods that can be used:

#### `primary_key`

You’ve seen this one used already. It’s used to create an autoincrementing integer primary key column.

      create_table(:a0){primary_key :id}

If you want to create a primary key column that doesn’t use an autoincrementing integer, you should not use this method. Instead, you should use the :primary_key option to the `column` method or type method:

      create_table(:a1){Integer :id, :primary_key=>true} # Non autoincrementing integer primary key
      create_table(:a2){String :name, :primary_key=>true} # varchar(255) primary key

If you want to create a composite primary key, you should call the `primary_key` method with an array of column symbols:

      create_table(:items) do
        Integer :group_id
        Integer :position
        primary_key [:group_id, :position]
      end

If provided with an array, `primary_key` does not create a column, it just sets up the primary key constraint.

#### `foreign_key`

`foreign_key` is used to create a foreign key column that references a column in another table (or the same table). It takes the column name as the first argument, the table it references as the second argument, and an options hash as it’s third argument. A simple example is:

      create_table(:albums) do
        primary_key :id
        foreign_key :artist_id, :artists
        String :name
      end

`foreign_key` accepts some specific options:

:deferrable | Makes the foreign key constraint checks deferrable, so they aren’t checked until the end of the transaction.
:key | For foreign key columns, the column in the associated table that this column references. Unnecessary if this column references the primary key of the associated table, at least on most databases.
:on_delete | Specify the behavior of this foreign key column when the row with the primary key it references is deleted , can be :restrict, :cascade, :set_null, or :set_default.
:on_update | Specify the behavior of this foreign key column when the row with the primary key it references modifies the value of the primary key, can be :restrict, :cascade, :set_null, or :set_default.

Like `primary_key`, if you provide `foreign_key` with an array of symbols, it will not create a column, but create a foreign key constraint:

      create_table(:artists) do
        String :name
        String :location
        primary_key [:name, :location]
      end
      create_table(:albums) do
        String :artist_name
        String :artist_location
        String :name
        foreign_key [:artist_name, :artist_location], :artists
      end

#### `index`

`index` creates indexes on the table. For single columns, calling index is the same as using the `:index` option when creating the column:

      create_table(:a){Integer :id, :index=>true}
      # Same as:
      create_table(:a) do
        Integer :id
        index :id
      end

Similar to the `primary_key` and `foreign_key` methods, calling `index` with an array of symbols will create a multiple column index:

      create_table(:albums) do
        primary_key :id
        foreign_key :artist_id, :artists
        Integer :position
        index [:artist_id, :position]
      end

The `index` method also accepts some options:

:name | The name of the index (generated based on the table and column names if not provided).
:type | The type of index to use (only supported by some databases)
:unique | Make the index unique, so duplicate values are not allowed.
:where | Create a partial index (only supported by some databases)

#### `unique`

The `unique` method creates a unique constraint on the table. A unique constraint generally operates identically to a unique index, so the following three `create_table` blocks are pretty much identical:

      create_table(:a){Integer :a, :unique=>true}

      create_table(:a) do
        Integer :a
        index :a, :unique=>true
      end

      create_table(:a) do
        Integer :a
        unique :a
      end

Just like `index`, `unique` can set up a multiple column unique constraint, where the combination of the columns must be unique:

      create_table(:a) do
        Integer :a
        Integer :b
        unique [:a, :b]
      end

#### `full_text_index` and `spatial_index`

Both of these create specialized index types supported by some databases. They both take the same options as `index`.

#### `constraint`

`constraint` creates a named table constraint:

      create_table(:artists) do
        primary_key :id
        String :name
        constraint(:name_min_length){char_length(name) > 2}
      end

Instead of using a block, you can use arguments that will be handled similarly to `Dataset#filter`:

      create_table(:artists) do
        primary_key :id
        String :name
        constraint(:name_length_range, :char_length.sql_function(:name)=>3..50)
      end

#### `check`

`check` operates just like `constraint`, except that it doesn’t take a name and it creates an unnamed constraint

      create_table(:artists) do
        primary_key :id
        String :name
        check{char_length(name) > 2}
      end

## `alter_table`

`alter_table` is used to alter existing tables, changing their columns, indexes, or constraints. It it used just like `create_table`, accepting a block which is instance_evaled, and providing its own methods:

### `add_column`

One of the most common methods, `add_column` is used to add a column to the table. Its API is similar to that of `create_table`’s `column` method, where the first argument is the column name, the second is the type, and the third is an options hash:

      alter_table(:albums) do
        add_column :copies_sold, Integer, :default=>0
      end

When adding a column, it’s a good idea to provide a default value, unless you want the value for all rows to be set to NULL.

### `drop_column`

As you may expect, `drop_column` takes a column name and drops the column. It’s often used in the `down` block of a migration to drop a column added in an `up` block:

      alter_table(:albums) do
        drop_column :copies_sold
      end

### `rename_column`

`rename_column` is used to rename a column. It takes the old column name as the first argument, and the new column name as the second argument:

      alter_table(:albums) do
        rename_column :copies_sold, :total_sales
      end

### `add_primary_key`

If you forgot to include a primary key on the table, and want to add one later, you can use `add_primary_key`. A common use of this is to make many_to_many association join tables into real models:

      alter_table(:albums_artists) do
        add_primary_key :id
      end

Just like `create_table`’s `primary_key` method, if you provide an array of symbols, Sequel will not add a column, but will add a composite primary key constraint:

      alter_table(:albums_artists) do
        add_primary_key [:album_id, :artist_id]
      end

If you just want to take an existing single column and make it a primary key, call `add_primary_key` with an array with a single symbol:

      alter_table(:artists) do
        add_primary_key [:id]
      end

### `add_foreign_key`

`add_foreign_key` can be used to add a new foreign key column or constraint to a table. Like `add_primary_key`, if you provide it with a symbol as the first argument, it creates a new column:

      alter_table(:albums) do
        add_foreign_key :artist_id, :artists
      end

If you want to add a new foreign key constraint to an existing column, you provide an array with a single element:

      alter_table(:albums) do
        add_foreign_key [:artist_id], :artists
      end

To set up a multiple column foreign key constraint, use an array with multiple column symbols:

      alter_table(:albums) do
        add_foreign_key [:artist_name, :artist_location], :artists
      end

### `add_index`

`add_index` works just like `create_table`’s `index` method, creating a new index on the table:

      alter_table(:albums) do
        add_index :artist_id
      end

It accepts the same options as `create_table`’s `index` method, and you can set up a multiple column index using an array:

      alter_table(:albums_artists) do
        add_index [:album_id, :artist_id], :unique=>true
      end

### `drop_index`

As you may expect, `drop_index` drops an existing index:

      alter_table(:albums) do
        drop_index :artist_id
      end

Just like `drop_column`, it is often used in the `down` block of a migration.

To drop an index with a specific name, use the `:name` option:

      alter_table(:albums) do
        drop_index :artist_id, :name=>:artists_id_index
      end

### `add_full_text_index`, `add_spatial_index`

Corresponding to `create_table`’s `full_text_index` and `spatial_index` methods, these two methods create new indexes on the table.

### `add_constraint`

This adds a named constraint to the table, similar to `create_table`’s `constraint` method:

      alter_table(:albums) do
        add_constraint(:name_min_length){char_length(name) > 2}
      end

There is no method to add an unnamed constraint, but you can pass nil as the first argument of `add_constraint` to do so. However, it’s not recommend to do that as it is difficult to drop such a constraint.

### `add_unique_constraint`

This adds a unique constraint to the table, similar to `create_table`’s `unique` method. This usually has the same effect as adding a unique index.

      alter_table(:albums) do
        add_unique_constraint [:artist_id, :name]
      end

### `drop_constraint`

This method drops an existing named constraint:

      alter_table(:albums) do
        drop_constraint(:name_min_length)
      end

There is no database independent method to drop an unnamed constraint. Generally, the database will give it a name automatically, and you will have to figure out what it is. For that reason, you should not add unnamed constraints that you ever might need to remove.

On MySQL, you must specify the type of constraint via a `:type` option:

      alter_table(:albums) do
        drop_constraint(:albums_pk, :type=>:primary_key)
        drop_constraint(:albums_fk, :type=>:foreign_key)
        drop_constraint(:albums_uk, :type=>:unique)
      end

### `set_column_default`

This modifies the default value of a column:

      alter_table(:albums) do
        set_column_default :copies_sold, 0
      end

### `set_column_type`

This modifies a column’s type. Most databases will attempt to convert existing values in the columns to the new type:

      alter_table(:albums) do
        set_column_type :copies_sold, Bignum
      end

You can specify the type as a string or symbol, in which case it is used verbatim, or as a supported ruby class, in which case it gets converted to an appropriate database type.

### `set_column_allow_null`

This changes the NULL or NOT NULL setting of a column:

      alter_table(:albums) do
        set_column_allow_null :artist_id, true    # NULL
        set_column_allow_null :copies_sold, false # NOT NULL
      end

## Other `Database` schema modification methods

`Sequel::Database` has many schema modification instance methods, most of which are shortcuts to the same methods in `alter_table`. The following `Database` instance methods just call `alter_table` with a block that calls the method with the same name inside the `alter_table` block with all arguments after the first argument (which is used as the table name):

- `add_column`

- `drop_column`

- `rename_column`

- `add_index`

- `drop_index`

- `set_column_default`

- `set_column_type`

For example, the following two method calls do the same thing:

      alter_table(:artists){add_column :copies_sold, Integer}
      add_column :artists, :copies_sold, Integer

There are some other schema modification methods that have no `alter_table` counterpart:

### `drop_table`

`drop_table` takes multiple arguments and treats all arguments as a table name to drop:

      drop_table(:albums_artists, :albums, :artists)

Note that when dropping tables, you may need to drop them in a specific order if you are using foreign keys and the database is enforcing referential integrity. In general, you need to drop the tables containing the foreign keys before the tables containing the primary keys they reference.

### `rename_table`

You can rename an existing table using `rename_table`. Like `rename_column`, the first argument is the current name, and the second is the new name:

      rename_table(:artist, :artists)

### `create_table!`

`create_table!` with the bang drops the table if it exists before attempting to create it, so:

      create_table!(:artists)
        primary_key :id
      end

is the same as:

      drop_table(:artists) if table_exists?(:artists)
      create_table(:artists)
        primary_key :id
      end

It should not be used inside migrations, as if the table does not exist, it may mess up the migration.

### `create_table?`

`create_table?` with a question mark only creates the table if it does not already exist, so:

      create_table?(:artists)
        primary_key :id
      end

is the same as:

      create_table(:artists)
        primary_key :id
      end unless table_exists?(:artists)

Like `create_table!`, it should not be used inside migrations.

### `create_view` and `create_or_replace_view`

These can be used to create views. The difference between them is that `create_or_replace_view` will unconditionally replace an existing view of the same name, while `create_view` will probably raise an error. Both methods take the name as the first argument, and either an string or a dataset as the second argument:

      create_view(:gold_albums, DB[:albums].filter{copies_sold > 500000})
      create_or_replace_view(:gold_albums, "SELECT * FROM albums WHERE copies_sold > 500000")

### `drop_view`

`drop_view` drops existing views. Just like `drop_table`, it can accept multiple arguments:

      drop_view(:gold_albums, :platinum_albums)

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.