Skip to content
PHP Ecosystem
Explore

Schema

API reference › yii › db › mssql › Schema

Schema is the class for retrieving metadata from MS SQL Server databases (version 2019 and above).

PropertyTypeDescriptionDefined by
$columnSchemaClassclass-string<yii\db\T>|array{class?: class-string<yii\db\T>, __class?: class-string<yii\db\T>}Default: \yii\db\mssql\ColumnSchema::class. Available since 2.0.11.Schema
$dbyii\db\ConnectionThe database connection Default: null.yii\db\Schema
$defaultSchemastringThe default schema used for the current session. Default: 'dbo'.Schema
$exceptionMaparrayMap of DB errors and corresponding exceptions If left part is found in DB error message exception class from the right part is used. Default: ['SQLSTATE[23' => 'yii\db\IntegrityException'].yii\db\Schema
$lastInsertID read-onlystringThe row ID of the last row inserted, or the last value retrieved from the sequence object.yii\db\Schema
$queryBuilder read-onlyyii\db\TQueryBuilderThe query builder for this connection.yii\db\Schema
$schemaChecks read-onlyyii\db\CheckConstraint[][]Check constraints for all tables in the database.yii\db\ConstraintFinderTrait
$schemaDefaultValues read-onlyyii\db\DefaultValueConstraint[]Default value constraints for all tables in the database.yii\db\ConstraintFinderTrait
$schemaForeignKeys read-onlyyii\db\ForeignKeyConstraint[][]Foreign keys for all tables in the database.yii\db\ConstraintFinderTrait
$schemaIndexes read-onlyyii\db\IndexConstraint[][]Indexes for all tables in the database.yii\db\ConstraintFinderTrait
$schemaNames read-onlystring[]All schema names in the database, except system schemas.yii\db\Schema
$schemaPrimaryKeys read-onlyyii\db\Constraint[]Primary keys for all tables in the database.yii\db\ConstraintFinderTrait
$schemaUniques read-onlyyii\db\IndexConstraint[][]Unique constraints for all tables in the database.yii\db\ConstraintFinderTrait
$serverVersion read-onlystringServer version as a string.yii\db\Schema
$systemSchemaNamesarrayDefault: ['guest'].Schema
$tableNames read-onlystring[]All table names in the database.yii\db\Schema
$tableSchemas read-onlyyii\db\TableSchema[]The metadata for all tables in the database.yii\db\Schema
$transactionIsolationLevel write-onlystringThe transaction isolation level to use for this transaction.yii\db\Schema
$typeMaparrayMapping from physical column types (keys) to abstract column types (values). Default: ['bigint' => self::TYPE_BIGINT, 'numeric' => self::TYPE_DECIMAL, 'bit' => self::TYPE_SMALLINT, 'smallint' => self::TYPE_SMALLINT, 'decimal' => self::TYPE_DECIMAL, 'smallmoney' => self::TYPE_MONEY, 'int' => self::TYPE_INTEGER, 'tinyint' => self::TYPE_TINYINT, 'money' => self::TYPE_MONEY, 'float' => self::TYPE_FLOAT, 'double' => self::TYPE_DOUBLE, 'real' => self::TYPE_FLOAT, 'date' => self::TYPE_DATE, 'datetimeoffset' => self::TYPE_DATETIME, 'datetime2' => self::TYPE_DATETIME, 'smalldatetime' => self::TYPE_DATETIME, 'datetime' => self::TYPE_DATETIME, 'time' => self::TYPE_TIME, 'char' => self::TYPE_CHAR, 'varchar' => self::TYPE_STRING, 'text' => self::TYPE_TEXT, 'nchar' => self::TYPE_CHAR, 'nvarchar' => self::TYPE_STRING, 'ntext' => self::TYPE_TEXT, 'binary' => self::TYPE_BINARY, 'varbinary' => self::TYPE_BINARY, 'image' => self::TYPE_BINARY, 'timestamp' => self::TYPE_TIMESTAMP, 'hierarchyid' => self::TYPE_STRING, 'uniqueidentifier' => self::TYPE_STRING, 'sql_variant' => self::TYPE_STRING, 'xml' => self::TYPE_STRING, 'table' => self::TYPE_STRING].Schema
$viewNames read-onlystring[]All view names in the database.yii\db\ViewFinderTrait
$columnQuoteCharacter protectedstring|string[]Default: ['[', ']']. Available since 2.0.14.Schema
$tableQuoteCharacter protectedstring|string[]Default: ['[', ']']. Available since 2.0.14.Schema
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
convertException()Converts a DB exception to a more concrete one if possible.yii\db\Schema
createColumnSchemaBuilder()Create a column schema builder instance giving the type and value precision.Schema
createQueryBuilder()Creates a query builder for the MSSQL database.Schema
createSavepoint()Creates a new savepoint.Schema
findUniqueIndexes()Returns all unique indexes for the given table.Schema
getLastInsertID()Returns the ID of the last inserted row or sequence value.yii\db\Schema
getPdoType()Determines the PDO type for the given PHP data value.yii\db\Schema
getQueryBuilder()yii\db\Schema
getRawTableName()Returns the actual name of a given table name.yii\db\Schema
getSchemaChecks()Returns check constraints for all tables in the database.yii\db\ConstraintFinderTrait
getSchemaDefaultValues()Returns default value constraints for all tables in the database.yii\db\ConstraintFinderTrait
getSchemaForeignKeys()Returns foreign keys for all tables in the database.yii\db\ConstraintFinderTrait
getSchemaIndexes()Returns indexes for all tables in the database.yii\db\ConstraintFinderTrait
getSchemaNames()Returns all schema names in the database, except system schemas.yii\db\Schema
getSchemaPrimaryKeys()Returns primary keys for all tables in the database.yii\db\ConstraintFinderTrait
getSchemaUniques()Returns unique constraints for all tables in the database.yii\db\ConstraintFinderTrait
getServerVersion()Returns a server version as a string comparable by version_compare().yii\db\Schema
getTableChecks()Obtains the check constraints information for the named table.yii\db\ConstraintFinderTrait
getTableDefaultValues()Obtains the default value constraints information for the named table.yii\db\ConstraintFinderTrait
getTableForeignKeys()Obtains the foreign keys information for the named table.yii\db\ConstraintFinderTrait
getTableIndexes()Obtains the indexes information for the named table.yii\db\ConstraintFinderTrait
getTableNames()Returns all table names in the database.yii\db\Schema
getTablePrimaryKey()Obtains the primary key for the named table.yii\db\ConstraintFinderTrait
getTableSchema()Obtains the metadata for the named table.yii\db\Schema
getTableSchemas()Returns the metadata for all tables in the database.yii\db\Schema
getTableUniques()Obtains the unique constraints information for the named table.yii\db\ConstraintFinderTrait
getViewNames()Returns all view names in the database.yii\db\ViewFinderTrait
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.yii\base\BaseObject
insert()Executes the INSERT command, returning primary key values.Schema
isReadQuery()Returns a value indicating whether a SQL statement is for read purpose.yii\db\Schema
quoteColumnName()Quotes a column name for use in a query.Schema
quoteSimpleColumnName()Quotes a simple column name for use in a query.yii\db\Schema
quoteSimpleTableName()Quotes a simple table name for use in a query.yii\db\Schema
quoteSystemCatalogName()Quotes the [sys] system catalog name, optionally qualified by the database catalog.Schema
quoteTableName()Qualifies single-part table names with $defaultSchema when it differs from 'dbo', ensuring unqualified DDL and DML statements target the configured schema rather than the SQL Server login default.Schema
quoteValue()Quotes a string value for use in a query.yii\db\Schema
refresh()Refreshes the schema.yii\db\Schema
refreshTableSchema()Refreshes the particular table schema.yii\db\Schema
releaseSavepoint()Releases an existing savepoint.Schema
resolveRawCatalogSchemaName()Resolves a schema argument into optional catalog and schema names without loading database metadata.Schema
resolveRawTableName()Resolves the table name and schema name (if any) without loading database metadata.yii\db\Schema
rollBackSavepoint()Rolls back to a previously created savepoint.Schema
setTransactionIsolationLevel()Sets the isolation level of the current transaction.yii\db\Schema
supportsSavepoint()yii\db\Schema
unquoteSimpleColumnName()Unquotes a simple column name.yii\db\Schema
unquoteSimpleTableName()Unquotes a simple table name.yii\db\Schema
createColumnSchema() protectedCreates a column schema for the database.yii\db\Schema
findColumns() protectedCollects the metadata of table columns.Schema
findForeignKeys() protectedCollects the foreign key column details for the given table.Schema
findPrimaryKeys() protectedCollects the primary key column details for the given table.Schema
findSchemaNames() protectedReturns all schema names in the database, including the default one but not system schemas.Schema
findTableConstraints() protectedCollects the constraint details for the given table and constraint type.Schema
findTableNames() protectedReturns all table names in the database.Schema
findViewNames() protectedSchema
getCacheKey() protectedReturns the cache key for the specified table name.yii\db\Schema
getCacheTag() protectedReturns the cache tag name.yii\db\Schema
getColumnPhpType() protectedExtracts the PHP type from abstract DB type.yii\db\Schema
getSchemaMetadata() protectedReturns the metadata of the given type for all tables in the given schema.Schema
getTableMetadata() protectedReturns the metadata of the given type for the given table.yii\db\Schema
getTableNameParts() protectedSplits full table name into parts.Schema
loadColumnSchema() protectedLoads the column information into a ColumnSchema object.Schema
loadTableChecks() protectedSchema
loadTableDefaultValues() protectedSchema
loadTableForeignKeys() protectedSchema
loadTableIndexes() protectedSchema
loadTablePrimaryKey() protectedSchema
loadTableSchema() protectedLoads the metadata for the specified table.Schema
loadTableUniques() protectedSchema
normalizePdoRowKeyCase() protectedChanges row’s array key case to lower if PDO’s one is set to uppercase.yii\db\Schema
resolveTableName() protectedResolves the table name and schema name (if any).Schema
setTableMetadata() protectedSets the metadata of the given type for the given table.yii\db\Schema
protected string|string[] $columnQuoteCharacter = ['[', ']']

An array of 2 characters can be used in case starting and ending characters are different.

Since

2.0.14

protected string|string[] $tableQuoteCharacter = ['[', ']']

An array of 2 characters can be used in case starting and ending characters are different.

Since

2.0.14

public function createColumnSchemaBuilder(mixed $type, mixed $length = null): yii\db\ColumnSchemaBuilder

Create a column schema builder instance giving the type and value precision.

This method may be overridden by child classes to create a DBMS-specific column schema builder.

Parameters

ParameterTypeDescription
$typemixedType of the column. See ColumnSchemaBuilder::$type.
$lengthmixedLength or precision of the column. See ColumnSchemaBuilder::$length.

Returns

yii\db\ColumnSchemaBuilder: Column schema builder instance

Since

2.0.6

public function createQueryBuilder(): yii\db\mssql\QueryBuilder

Creates a query builder for the MSSQL database.

Returns

QueryBuilder: Query builder interface.

public function createSavepoint(mixed $name): mixed

Creates a new savepoint.

Parameters

ParameterTypeDescription
$namemixedThe savepoint name

Returns

mixed

public function findUniqueIndexes(yii\db\mssql\TableSchema $table): array

Returns all unique indexes for the given table.

Each array element is of the following structure:

[
'IndexName1' => ['col1' [, ...]],
'IndexName2' => ['col2' [, ...]],
]

Parameters

ParameterTypeDescription
$tableTableSchemaThe table metadata.

Returns

array: All unique indexes for the given table.

Since

2.0.4

public function insert(mixed $table, mixed $columns): array|false

Executes the INSERT command, returning primary key values.

Retrieves the inserted row via the OUTPUT INSERTED.* block emitted by \yii\db\mssql\QueryBuilder::insert() so that IDENTITY values, computed columns, and uniqueidentifier defaults are returned to the caller.

Parameters

ParameterTypeDescription
$tablemixedThe table that new rows will be inserted into.
$columnsmixedThe column data (name => value) to be inserted into the table.

Returns

array|false: Primary key values or false if the command fails

Since

2.0.4

public function quoteColumnName(mixed $name): string

Quotes a column name for use in a query.

If the column name contains prefix, the prefix will also be properly quoted. If the column name is already fully enclosed in the quote characters, or contains ’(’, ’[[’ or ’{{’, then this method will do nothing.

Parameters

ParameterTypeDescription
$namemixedColumn name.

Returns

string: The properly quoted column name.

public function quoteSystemCatalogName(string|null $catalogName): string

Quotes the [sys] system catalog name, optionally qualified by the database catalog.

Parameters

ParameterTypeDescription
$catalogNamestring|nullThe database catalog name, or null to target the current database.

Returns

string: The [sys] schema name, prefixed with the quoted catalog when one is provided.

Since

22.0

public function quoteTableName(mixed $name): mixed

Qualifies single-part table names with $defaultSchema when it differs from 'dbo', ensuring unqualified DDL and DML statements target the configured schema rather than the SQL Server login default.

Parameters

ParameterTypeDescription
$namemixed

Returns

mixed

public function releaseSavepoint(mixed $name): mixed

Releases an existing savepoint.

Parameters

ParameterTypeDescription
$namemixedThe savepoint name

Returns

mixed

public function resolveRawCatalogSchemaName(string $schema): array{string|null, string}

Resolves a schema argument into optional catalog and schema names without loading database metadata.

Parameters

ParameterTypeDescription
$schemastringThe schema argument.

Returns

array{string|null, string}: The catalog and schema names.

Since

22.0

public function rollBackSavepoint(mixed $name): mixed

Rolls back to a previously created savepoint.

Parameters

ParameterTypeDescription
$namemixedThe savepoint name

Returns

mixed

protected function findColumns(yii\db\mssql\TableSchema $table): bool

Collects the metadata of table columns.

Parameters

ParameterTypeDescription
$tableTableSchemaThe table metadata.

Returns

bool: Whether the table exists in the database.

protected function findForeignKeys(yii\db\mssql\TableSchema $table): mixed

Collects the foreign key column details for the given table.

Parameters

ParameterTypeDescription
$tableTableSchemaThe table metadata

Returns

mixed

protected function findPrimaryKeys(yii\db\mssql\TableSchema $table): mixed

Collects the primary key column details for the given table.

Parameters

ParameterTypeDescription
$tableTableSchemaThe table metadata

Returns

mixed

protected function findSchemaNames(): array

Returns all schema names in the database, including the default one but not system schemas.

This method should be overridden by child classes in order to support this feature because the default implementation simply throws an exception.

Returns

array: All schema names in the database, except system schemas.

Throws

Since

2.0.4

See also

protected function findTableConstraints(yii\db\mssql\TableSchema $table, string $type): array

Collects the constraint details for the given table and constraint type.

Parameters

ParameterTypeDescription
$tableTableSchemaThe table metadata.
$typestringEither PK or UQ.

Returns

array: Each entry contains index_name and field_name.

Since

2.0.4

See also

protected function findTableNames(mixed $schema = ''): array

Returns all table names in the database.

This method should be overridden by child classes in order to support this feature because the default implementation simply throws an exception.

Parameters

ParameterTypeDescription
$schemamixedThe schema of the tables. Defaults to empty string, meaning the current or default schema.

Returns

array: All table names in the database. The names have NO schema name prefix.

Throws

See also

protected function findViewNames(mixed $schema = '')
protected function getSchemaMetadata(mixed $schema, mixed $type, mixed $refresh): array

Returns the metadata of the given type for all tables in the given schema.

This method will call a 'getTable' . ucfirst($type) named method with the table name and the refresh flag to obtain the metadata.

Parameters

ParameterTypeDescription
$schemamixedThe schema of the metadata. Defaults to empty string, meaning the current or default schema name.
$typemixedMetadata type.
$refreshmixedWhether to fetch the latest available table metadata. If this is false, cached data may be returned if available.

Returns

array: Array of metadata.

Since

2.0.13

protected function getTableNameParts(mixed $name): array

Splits full table name into parts.

Parameters

ParameterTypeDescription
$namemixedFull table name.

Returns

array: Table name parts.

Since

2.0.22

protected function loadColumnSchema(array $info): yii\db\mssql\T

Loads the column information into a ColumnSchema object.

Parameters

ParameterTypeDescription
$infoarrayColumn information.

Returns

yii\db\mssql\T: The column schema object.

protected function loadTableChecks(mixed $tableName)

Parameters

ParameterTypeDescription
$tableNamemixed
protected function loadTableDefaultValues(mixed $tableName)

Parameters

ParameterTypeDescription
$tableNamemixed
protected function loadTableForeignKeys(mixed $tableName)

Parameters

ParameterTypeDescription
$tableNamemixed
protected function loadTableIndexes(mixed $tableName)

Parameters

ParameterTypeDescription
$tableNamemixed
protected function loadTablePrimaryKey(mixed $tableName)

Parameters

ParameterTypeDescription
$tableNamemixed
protected function loadTableSchema(mixed $name): yii\db\TableSchema|null

Loads the metadata for the specified table.

Parameters

ParameterTypeDescription
$namemixedTable name

Returns

yii\db\TableSchema|null: DBMS-dependent table metadata, null if the table does not exist.

protected function loadTableUniques(mixed $tableName)

Parameters

ParameterTypeDescription
$tableNamemixed
protected function resolveTableName(string $name): yii\db\mssql\TableSchema

Resolves the table name and schema name (if any).

Parameters

ParameterTypeDescription
$namestringThe table name

Returns

TableSchema: Resolved table, schema, etc. names.