# SchemaBuilderTrait

> SchemaBuilderTrait contains shortcut methods to create instances of yii\db\ColumnSchemaBuilder.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › SchemaBuilderTrait

| trait | `yii\db\SchemaBuilderTrait` |
| --- | --- |
| Since | 2.0.6 |
| Source | [`framework/db/SchemaBuilderTrait.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/SchemaBuilderTrait.php#L36) |

Used by 1 type:

- [`Migration`](/docs/api/yii/db/migration/index.md)

SchemaBuilderTrait contains shortcut methods to create instances of [`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md).

These can be used in database migrations to define database schema types using a PHP interface.
This is useful to define a schema in a DBMS independent way so that the application may run on
different DBMS the same way.

For example you may use the following code inside your migration files:

```
$this->createTable('example_table', [
  'id' => $this->primaryKey(),
  'name' => $this->string(64)->notNull(),
  'type' => $this->integer()->notNull()->defaultValue(10),
  'description' => $this->text(),
  'rule_name' => $this->string(64),
  'data' => $this->text(),
  'created_at' => $this->datetime()->notNull(),
  'updated_at' => $this->datetime(),
]);
```

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`bigInteger()`](#method-bigInteger) | Creates a bigint column. | `SchemaBuilderTrait` |
| [`bigPrimaryKey()`](#method-bigPrimaryKey) | Creates a big primary key column. | `SchemaBuilderTrait` |
| [`binary()`](#method-binary) | Creates a binary column. | `SchemaBuilderTrait` |
| [`boolean()`](#method-boolean) | Creates a boolean column. | `SchemaBuilderTrait` |
| [`char()`](#method-char) | Creates a char column. | `SchemaBuilderTrait` |
| [`date()`](#method-date) | Creates a date column. | `SchemaBuilderTrait` |
| [`dateTime()`](#method-dateTime) | Creates a datetime column. | `SchemaBuilderTrait` |
| [`decimal()`](#method-decimal) | Creates a decimal column. | `SchemaBuilderTrait` |
| [`double()`](#method-double) | Creates a double column. | `SchemaBuilderTrait` |
| [`float()`](#method-float) | Creates a float column. | `SchemaBuilderTrait` |
| [`integer()`](#method-integer) | Creates an integer column. | `SchemaBuilderTrait` |
| [`json()`](#method-json) | Creates a JSON column. | `SchemaBuilderTrait` |
| [`money()`](#method-money) | Creates a money column. | `SchemaBuilderTrait` |
| [`primaryKey()`](#method-primaryKey) | Creates a primary key column. | `SchemaBuilderTrait` |
| [`smallInteger()`](#method-smallInteger) | Creates a smallint column. | `SchemaBuilderTrait` |
| [`string()`](#method-string) | Creates a string column. | `SchemaBuilderTrait` |
| [`text()`](#method-text) | Creates a text column. | `SchemaBuilderTrait` |
| [`time()`](#method-time) | Creates a time column. | `SchemaBuilderTrait` |
| [`timestamp()`](#method-timestamp) | Creates a timestamp column. | `SchemaBuilderTrait` |
| [`tinyInteger()`](#method-tinyInteger) | Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used. | `SchemaBuilderTrait` |
| [`getDb()`](#method-getDb) protected abstract |  | `SchemaBuilderTrait` |

## Method details

### bigInteger() <span id="method-bigInteger"></span>

```php
public function bigInteger(int|null $length = null): yii\db\ColumnSchemaBuilder
```

Creates a bigint column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$length` | `int\|null` | Column size or precision definition. This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### bigPrimaryKey() <span id="method-bigPrimaryKey"></span>

```php
public function bigPrimaryKey(int|null $length = null): yii\db\ColumnSchemaBuilder
```

Creates a big primary key column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$length` | `int\|null` | Column size or precision definition. This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### binary() <span id="method-binary"></span>

```php
public function binary(int|null $length = null): yii\db\ColumnSchemaBuilder
```

Creates a binary column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$length` | `int\|null` | Column size or precision definition. This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### boolean() <span id="method-boolean"></span>

```php
public function boolean(): yii\db\ColumnSchemaBuilder
```

Creates a boolean column.

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### char() <span id="method-char"></span>

```php
public function char(int|null $length = null): yii\db\ColumnSchemaBuilder
```

Creates a char column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$length` | `int\|null` | Column size definition i.e. the maximum string length. This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.8

### date() <span id="method-date"></span>

```php
public function date(): yii\db\ColumnSchemaBuilder
```

Creates a date column.

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### dateTime() <span id="method-dateTime"></span>

```php
public function dateTime(int|null $precision = null): yii\db\ColumnSchemaBuilder
```

Creates a datetime column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$precision` | `int\|null` | Column value precision. First parameter passed to the column type, e.g. DATETIME(precision). This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### decimal() <span id="method-decimal"></span>

```php
public function decimal(int|null $precision = null, int|null $scale = null): yii\db\ColumnSchemaBuilder
```

Creates a decimal column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$precision` | `int\|null` | Column value precision, which is usually the total number of digits. First parameter passed to the column type, e.g. DECIMAL(precision, scale). This parameter will be ignored if not supported by the DBMS. |
| `$scale` | `int\|null` | Column value scale, which is usually the number of digits after the decimal point. Second parameter passed to the column type, e.g. DECIMAL(precision, scale). This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### double() <span id="method-double"></span>

```php
public function double(int|null $precision = null): yii\db\ColumnSchemaBuilder
```

Creates a double column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$precision` | `int\|null` | Column value precision. First parameter passed to the column type, e.g. DOUBLE(precision). This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### float() <span id="method-float"></span>

```php
public function float(int|null $precision = null): yii\db\ColumnSchemaBuilder
```

Creates a float column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$precision` | `int\|null` | Column value precision. First parameter passed to the column type, e.g. FLOAT(precision). This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### integer() <span id="method-integer"></span>

```php
public function integer(int|null $length = null): yii\db\ColumnSchemaBuilder
```

Creates an integer column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$length` | `int\|null` | Column size or precision definition. This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### json() <span id="method-json"></span>

```php
public function json(): yii\db\ColumnSchemaBuilder
```

Creates a JSON column.

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.14

### money() <span id="method-money"></span>

```php
public function money(int|null $precision = null, int|null $scale = null): yii\db\ColumnSchemaBuilder
```

Creates a money column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$precision` | `int\|null` | Column value precision, which is usually the total number of digits. First parameter passed to the column type, e.g. DECIMAL(precision, scale). This parameter will be ignored if not supported by the DBMS. |
| `$scale` | `int\|null` | Column value scale, which is usually the number of digits after the decimal point. Second parameter passed to the column type, e.g. DECIMAL(precision, scale). This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### primaryKey() <span id="method-primaryKey"></span>

```php
public function primaryKey(int|null $length = null): yii\db\ColumnSchemaBuilder
```

Creates a primary key column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$length` | `int\|null` | Column size or precision definition. This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### smallInteger() <span id="method-smallInteger"></span>

```php
public function smallInteger(int|null $length = null): yii\db\ColumnSchemaBuilder
```

Creates a smallint column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$length` | `int\|null` | Column size or precision definition. This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### string() <span id="method-string"></span>

```php
public function string(int|null $length = null): yii\db\ColumnSchemaBuilder
```

Creates a string column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$length` | `int\|null` | Column size definition i.e. the maximum string length. This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### text() <span id="method-text"></span>

```php
public function text(): yii\db\ColumnSchemaBuilder
```

Creates a text column.

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### time() <span id="method-time"></span>

```php
public function time(int|null $precision = null): yii\db\ColumnSchemaBuilder
```

Creates a time column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$precision` | `int\|null` | Column value precision. First parameter passed to the column type, e.g. TIME(precision). This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### timestamp() <span id="method-timestamp"></span>

```php
public function timestamp(int|null $precision = null): yii\db\ColumnSchemaBuilder
```

Creates a timestamp column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$precision` | `int\|null` | Column value precision. First parameter passed to the column type, e.g. TIMESTAMP(precision). This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.6

### tinyInteger() <span id="method-tinyInteger"></span>

```php
public function tinyInteger(int|null $length = null): yii\db\ColumnSchemaBuilder
```

Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$length` | `int\|null` | Column size or precision definition. This parameter will be ignored if not supported by the DBMS. |

**Returns**

[`ColumnSchemaBuilder`](/docs/api/yii/db/columnschemabuilder/index.md): The column instance which can be further customized.

**Since**

2.0.14

### getDb() <span id="method-getDb"></span>

```php
abstract protected function getDb(): yii\db\Connection
```

**Returns**

[`Connection`](/docs/api/yii/db/connection/index.md): The database connection to be used for schema building.