Get field name, data type and size of database table

Estimated study time: 6 minutes. Inspect a table's structure without opening the designer.

Whenever you're working with an unfamiliar table, it's useful to pull its column names, data types, and sizes directly with a query instead of clicking through the GUI. SQL Server gives you a couple of reliable ways to do this.

Using INFORMATION_SCHEMA

The ANSI-standard INFORMATION_SCHEMA.COLUMNS view works across most relational databases, including SQL Server:

SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Employees';

Using System Catalog Views

For SQL Server–specific detail — like exact precision, scale, and identity flags — the catalog views give you more:

SELECT c.name AS ColumnName,
       t.name AS DataType,
       c.max_length AS SizeInBytes,
       c.is_nullable
FROM sys.columns c
JOIN sys.types t ON c.user_type_id = t.user_type_id
WHERE c.object_id = OBJECT_ID('Employees');

Using sp_help

SQL Server also ships a quick built-in procedure that dumps a full structural summary, including columns, indexes, and constraints:

EXEC sp_help 'Employees';
💡 Tip: max_length in sys.columns is reported in bytes, not characters — for nvarchar columns, divide by 2 to get the character count.

When to Use Which

Use INFORMATION_SCHEMA when you want portable queries that could run on other database engines too. Use the sys.* catalog views when you need SQL Server–specific detail that INFORMATION_SCHEMA doesn't expose.

The Same Task in MySQL

The queries above are written for SQL Server. If you are working in MySQL, the ideas are the same but the tools are slightly different. MySQL also implements INFORMATION_SCHEMA.COLUMNS, and you should filter by database as well as table, because the same table name can exist in several schemas:

SELECT COLUMN_NAME,
       DATA_TYPE,
       CHARACTER_MAXIMUM_LENGTH,
       NUMERIC_PRECISION,
       NUMERIC_SCALE,
       IS_NULLABLE,
       COLUMN_DEFAULT,
       COLUMN_KEY
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'company_db'
  AND TABLE_NAME   = 'employees'
ORDER BY ORDINAL_POSITION;

ORDINAL_POSITION keeps the columns in the order they were defined. COLUMN_KEY shows PRI for primary key columns, which is handy when you are learning a new schema.

For a quick look, MySQL offers shortcuts:

DESCRIBE employees;
SHOW FULL COLUMNS FROM employees;
SHOW CREATE TABLE employees;

DESCRIBE gives a compact grid of field, type, null, key, default and extra. SHOW FULL COLUMNS adds collation and comments. SHOW CREATE TABLE prints the exact statement needed to recreate the table, including indexes and constraints.

Worked Example: Reading the Output Correctly

Suppose a table has full_name VARCHAR(100), salary DECIMAL(10,2) and joined_on DATE. The metadata will show:

ColumnDATA_TYPELengthPrecision / Scale
full_namevarchar100 (characters)none
salarydecimalnone10 / 2
joined_ondatenonenone

Character length in MySQL's information schema is measured in characters. If you need the storage size in bytes, look at CHARACTER_OCTET_LENGTH, which depends on the character set; with utf8mb4 a character can take up to four bytes.

Listing Every Column in a Database

Drop the table filter to document a whole schema at once. This is useful for data dictionaries and migration planning:

SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
ORDER BY TABLE_NAME, ORDINAL_POSITION;

COLUMN_TYPE returns the full definition, such as varchar(100) or decimal(10,2), which is often more readable than splitting type and size into separate columns. DATABASE() uses whichever database you are currently connected to.

Common Mistakes When Reading Table Metadata

  • Leaving out the schema filter, so results mix columns from tables with the same name in different databases.
  • Comparing character length and byte length as if they were the same number.
  • Expecting results for tables you have no privileges on. Information schema only shows objects your account can access.
  • Assuming the order of rows matches column order without sorting by ORDINAL_POSITION.

Tip: save your favourite metadata query as a snippet. It is one of the fastest ways to understand an unfamiliar database before writing reports against it.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with Uncodemy's Data Analytics Course.

Explore Data Analytics Course →