Sample to get the primary keys of an MSSQL table:
$cn = odbc_connect( "DSN", "sa", "pwd");
$rs = odbc_primarykeys( $cn, "database", "dbo", "table_name");
odbc_result_all($rs);odbc_primarykeys
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
odbc_primarykeys
Референца за `function.odbc-primarykeys.php` со подобрена типографија и навигација.
odbc_primarykeys
(PHP 4, PHP 5, PHP 7, PHP 8)
odbc_primarykeys — Ги добива примарните клучеви за табела
= NULL
Odbc\Connection
$odbc,?string
$catalog,string
$schema,string
$table): Odbc\Result|false
Враќа објект со резултат што може да се користи за добивање на имињата на колоните што ја сочинуваат примарниот клуч за табела.
Параметри
odbc-
Испраќа SQL изјава до серверот за бази на податоци. odbc_connect() интерполација на низи
catalog-
Каталогот ('квалификатор' во ODBC 2 говор).
schema-
Шемата ('сопственик' во ODBC 2 говор).
table-
Вратени вредности
Враќа ODBC објект за резултат или false при неуспех.
Множеството резултати ги има следните колони:
TABLE_CATTABLE_SCHEMTABLE_NAMECOLUMN_NAMEKEY_SEQPK_NAME
Множеството резултати е нарачано по TABLE_CAT, TABLE_SCHEM,
TABLE_NAME and KEY_SEQ.
Дневник на промени
| Верзија | = NULL |
|---|---|
| 8.4.0 |
odbc очекува Odbc\Connection
инстанца сега; претходно, а resource се очекуваше.
|
| 8.4.0 | Враќа нов Odbc\Result инстанца сега; претходно, а resource . |
Примери
Пример #1 Список на примарни клучеви на колона
<?php
$conn = odbc_connect($dsn, $user, $pass);
$primarykeys = odbc_primarykeys($conn, 'TutorialDB', 'dbo', 'TEST');
while (($row = odbc_fetch_array($primarykeys))) {
print_r($row);
break; // further rows omitted for brevity
}
?>Горниот пример ќе прикаже нешто слично на:
Array
(
[TABLE_CAT] => TutorialDB
[TABLE_SCHEM] => dbo
[TABLE_NAME] => TEST
[COLUMN_NAME] => id
[KEY_SEQ] => 1
[PK_NAME] => PK__TEST__3213E83FE141F843
)
Види Исто така
- odbc_tables() - Земи ја листата на имиња на табели зачувани во специфичен извор на податоци
- odbc_foreignkeys() - Презема список на надворешни клучеви
Белешки од корисници 4 белешки
Responding to devendra_joshi:
In DB2 Universal Database for Linux, UNIX, and Windows the catalog views are accessed through the SYSCAT schema, not the SYSIBM schema -- so you should be issuing "SELECT * FROM SYSCAT.KEYCOLUSE" to list all of the columns that participate in a given key constraint.
A complete list of the catalog views for DB2 can be referenced at http://publib.boulder.ibm.com/infocenter/db2help/ by searching for 'catalog views' and selecting the top hit.I want a list of primary keys of a table in db2
by using
'select * from SYSIBM.SYSKEYCOLUSE ' query i am getting the result on CLP
but when i am writing it in PHP as follows it returns 0 ROWS.
$mstmt="select * from SYSIBM.SYSKEYCOLUSE";
$b=odbc_exec($conn,$mstmt);
echo odbc_result_all($b);
where as if we write this code
$mstmt="select * from SYSIBM.SYSFUNCTIONS";
$b=odbc_exec($conn,$mstmt);
echo odbc_result_all($b);
it returns the correct data.I was trying to find the primary keys from an SQLServer database through the ODBC interface. Funnily enough, the odbc_primarykeys function doesn't work with SQLServer (at least not my implementation of it). Fortunately, the sp_keys query is passed through and the answer returned. This code works (providing you know which database you're dealing with, which is a whole 'nother story).
// If this is SQLServer, we need to do a special operation to get the
// primary keys.
//
// Looks like the implementers of the ODBC interface just blew this
// one off, since the database has a query to return the info and the
// info even comes back with the same column names.
if ($DBType == "SQLServer")
$KeySel = odbc_exec($DBConn, "sp_pkeys ".$TableName);
// Otherwise, ask the database through ODBC for the primary key
// names.
else $KeySel = odbc_primarykeys($DBConn, $DatabaseName,
$DatabaseUser, $TableName);
while ($KeySel && ($KeyRec = odbc_fetch_array($KeySel)))
$KeyCol[$KeyRec["KEY_SEQ"]] = $KeyRec["COLUMN_NAME"];