# TableSchema

> TableSchema represents the metadata of a database table.

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

| class | `yii\db\TableSchema` |
| --- | --- |
| Extends | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/db/TableSchema.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/TableSchema.php#L22) |

1 subclass:

- [`yii\db\mssql\TableSchema`](/docs/api/yii/db/mssql/tableschema/index.md)

TableSchema represents the metadata of a database table.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-columnNames"></span>`$columnNames` read-only | `array` | List of column names. | `TableSchema` |
| [`$columns`](#property-columns) | [`ColumnSchema`](/docs/api/yii/db/columnschema/index.md)`[]` | Column metadata of this table. Default: `[]`. | `TableSchema` |
| [`$foreignKeys`](#property-foreignKeys) | `array` | Foreign keys of this table. Default: `[]`. | `TableSchema` |
| [`$fullName`](#property-fullName) | `string` | The full name of this table, which includes the schema name prefix, if any. Default: `null`. | `TableSchema` |
| [`$name`](#property-name) | `string` | The name of this table. Default: `null`. | `TableSchema` |
| <span id="property-primaryKey"></span>`$primaryKey` | `string[]` | Primary keys of this table. Default: `[]`. | `TableSchema` |
| <span id="property-schemaName"></span>`$schemaName` | `string` | The name of the schema that this table belongs to. Default: `null`. | `TableSchema` |
| [`$sequenceName`](#property-sequenceName) | `string\|null` | Sequence name for the primary key. Default: `null`. | `TableSchema` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`fixPrimaryKey()`](#method-fixPrimaryKey) | Manually specifies the primary key for this table. | `TableSchema` |
| [`getColumn()`](#method-getColumn) | Gets the named column metadata. | `TableSchema` |
| [`getColumnNames()`](#method-getColumnNames) | Returns the names of all columns in this table. | `TableSchema` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |

## Property details

### $columns <span id="property-columns"></span>

```php
public yii\db\ColumnSchema[] $columns = []
```

Column metadata of this table.

Each array element is a [`ColumnSchema`](/docs/api/yii/db/columnschema/index.md) object, indexed by column names.

### $foreignKeys <span id="property-foreignKeys"></span>

```php
public array $foreignKeys = []
```

Foreign keys of this table.

Each array element is of the following structure:

```
[
 'ForeignTableName',
 'fk1' => 'pk1',  // pk1 is in foreign table
 'fk2' => 'pk2',  // if composite foreign key
]
```

### $fullName <span id="property-fullName"></span>

```php
public string $fullName = null
```

The full name of this table, which includes the schema name prefix, if any.

Note that if the schema name is the same as the [default schema name](/docs/api/yii/db/schema/index.md#property-defaultSchema),
the schema name will not be included.

### $name <span id="property-name"></span>

```php
public string $name = null
```

The name of this table.

The schema name is not included. Use [`$fullName`](/docs/api/yii/db/tableschema/index.md#property-fullName) to get the name with schema name prefix.

### $sequenceName <span id="property-sequenceName"></span>

```php
public string|null $sequenceName = null
```

Sequence name for the primary key.

Null if no sequence.

## Method details

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

```php
public function fixPrimaryKey(string|array $keys): mixed
```

Manually specifies the primary key for this table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$keys` | `string\|array` | The primary key (can be composite) |

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if the specified key cannot be found in the table.

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

```php
public function getColumn(string $name): yii\db\ColumnSchema|null
```

Gets the named column metadata.

This is a convenient method for retrieving a named column even if it does not exist.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Column name |

**Returns**

[`ColumnSchema`](/docs/api/yii/db/columnschema/index.md)`|null`: Metadata of the named column. Null if the named column does not exist.

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

```php
public function getColumnNames(): array
```

Returns the names of all columns in this table.

**Returns**

`array`: List of column names