在Oracle、MySQL、DM(达梦)、PostgreSQL和SQL Server中,获取表的字段类型、长度、主键和注释的方法各有不同。以下是针对这些数据库的SQL查询示例:
Oracle:
SELECT COLUMN_NAME, DATA_TYPE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE, NULLABLE
FROM USER_TAB_COLUMNS
WHERE TABLE_NAME = 'YOUR_TABLE_NAME';
SELECT col.column_name, col.data_type, col.data_length, col.data_precision, col.data_scale,
col.nullable, com.comments
FROM user_tab_columns col
LEFT JOIN user_col_comments com ON col.table_name = com.table_name AND col.column_name = com.column_name
WHERE table_name = 'YOUR_TABLE_NAME';
MySQL:
SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'YOUR_DATABASE_NAME' AND TABLE_NAME = 'YOUR_TABLE_NAME';
SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_COMMENT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'YOUR_DATABASE_NAME' AND table_name = 'YOUR_TABLE_NAME';
DM:
SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'YOUR_DATABASE_NAME' AND TABLE_NAME = 'YOUR_TABLE_NAME';
SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, REMARKS
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'YOUR_DATABASE_NAME' AND table_name = 'YOUR_TABLE_NAME';
PostgreSQL:
SELECT column_name, data_type, character_maximum_length, numeric_precision, numeric_scale, is_nullable
FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'YOUR_TABLE_NAME';
SELECT column_name, data_type, character_maximum_length, column_default, is_nullable, col_description(table_name::regclass, ordinal_position) as comment
FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'YOUR_TABLE_NAME';
SQL Server:
SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'YOUR_DATABASE_NAME' AND TABLE_NAME = 'YOUR_TABLE_NAME';
SELECT c.name AS ColumnName, t.name AS DataType, c.max_length AS Length, c.is_nullable AS IsNullable, p.value AS Comment
FROM sys.columns c
LEFT JOIN sys.types t ON c.system_type_id = t.system_type_id
LEFT JOIN sys.extended_properties p ON p.major_id = c.object_id AND p.minor_id = c.column_id
INNER JOIN sys.objects o ON c.object_id =