Main Content

Retrieve Database Metadata

This example shows how to retrieve database information using the connection object and the sqlfind function.

The example assumes that you are connecting to a MySQL® database that contains a table named productTable.

Connect to Database

Create an ODBC database connection to a MySQL database with a user name and password.

datasource = "MySQL ODBC";
username = "username";
password = "password";
conn = database(datasource,username,password);

Find Catalogs and Schemas

Display the catalogs in the database by using the Catalogs property of the connection object.

conn.Catalogs
ans =

  1×7 cell array

  Columns 1 through 4

    {'information_sch…'}    {'detsdb'}    {'mysql'}    {'performance_sch…'}

  Columns 5 through 7

    {'sys'}    {'toy_store'}    {'toystore_doc'}

Display the schemas in the database by using the Schemas property of the connection object.

conn.Schemas
ans =

  0×0 empty cell array

Find Table Types

Find all table types in the database by using the sqlfind function with the connection object.

tables = sqlfind(conn,'');

Display the first three table types.

tables(1:3,:)
ans =

  3×5 table

        Catalog           Schema            Table             Columns        Type   
    ________________    __________    __________________    ___________    _________

    {'toystore_doc'}    {0×0 char}    {'Person'        }    {1×5  cell}    {'TABLE'}
    {'toystore_doc'}    {0×0 char}    {'airlinesmall'  }    {1×29 cell}    {'TABLE'}
    {'toystore_doc'}    {0×0 char}    {'inventoryTable'}    {1×4  cell}    {'TABLE'}

Find the table type of the table productTable.

tablename = 'productTable';
data = sqlfind(conn,tablename);
data.Type
ans =

  1×1 cell array

    {'TABLE'}

Find Table Columns

Find all columns in the database table productTable and display them.

data = sqlfind(conn,tablename);
data.Columns{:}
ans =

  1×5 cell array

  Columns 1 through 4

    {'productNumber'}    {'stockNumber'}    {'supplierNumber'}    {'unitCost'}

  Column 5

    {'productDescript…'}

Close Database Connection

close(conn)

See Also

| | |

Related Topics