mysql查看錶結構及已有索引信息

需求背景是給一個表名而後給出相應的表結構信息及索引信息mysql

經常使用的命令有以下:web

desc tableName; desc employees.employees;sql

show columns from tableName; show COLUMNS from employees.employees;oracle

describe tableName; DESCRIBE employees.employees;測試

這三個顯示的結果都是同樣的,顯示錶中filed,type,null,key,default及extra。spa

show create table tableName; show CREATE TABLE employees.employees;code

這個語句會顯示這個表的建表語句。orm

select * from columns where table_name='表名';select * from information_schema.COLUMNS where TABLE_SCHEMA='employees' and TABLE_NAME='employees';htm

這個顯示的結果就比較全了。索引

接下來,來點更全的sql,這個是用來同步mysql和orac數據字典的全部sql。

mysql部分:

## 查看全部的庫
SELECT
	lower(schema_name) schema_name
FROM
	information_schema.schemata
WHERE
	schema_name NOT IN (
		'mysql',
		'information_schema',
		'test',
		'search',
		'tbsearch',
		'sbtest',
		'dev_ddl'
	)

## 產看某一個庫中的全部表
SELECT
	table_name,
	create_time updated_at,
	table_type,
	ENGINE,
	table_rows num_rows,
	table_comment,
	ceil(data_length / 1024 / 1024) store_capacity
FROM
	information_schema.TABLES
WHERE
	table_schema = 'employees'
AND table_name NOT LIKE 'tmp#_%' ESCAPE '#'

##查看某一個庫下某一個表的全部字段
SELECT
	lower(column_name) column_name,
	ordinal_position position,
	column_default dafault_value,
	substring(is_nullable, 1, 1) nullable,
	column_type data_type,
	column_comment,
	character_maximum_length data_length,
	numeric_precision data_precision,
	numeric_scale data_scale
FROM
	information_schema.COLUMNS
WHERE
	table_schema = 'employees'
AND table_name = 'employees';


## 查看某一個庫下某一張表的索引

SELECT DISTINCT
	lower(index_name) index_name,
	lower(index_type) type
FROM
	information_schema.statistics
WHERE
	table_schema = 'employees'
AND table_name = 'employees';

## 查看某一個庫下某一張表的某一個索引

SELECT
	lower(column_name) column_name,
	seq_in_index column_position
FROM
	information_schema.statistics
WHERE
	table_schema = 'employees'
AND table_name = 'employees'
AND index_name = 'primary';

## 查看某一個庫下某一個表的註釋
SELECT
	table_comment comments
FROM
	information_schema.TABLES
WHERE
	table_schema = 'employees'
AND table_name = 'employees';

## 查看某一個庫下某一個表的列的註釋
SELECT
	lower(column_name) column_name,
	column_comment comments
FROM
	COLUMNS
WHERE
	table_schema = 'employees'
AND table_name = 'employees';

oracle部分:

#table structure:
SELECT
	lower(table_name) table_name,
	TEMPORARY,
	tablespace_name,
	num_rows,
	duration,
	'ORACLE' table_type,
	partitioned,
	(
		SELECT
			ceil(sum(bytes) / 1024 / 1024)
		FROM
			dba_segments b
		WHERE
			a. OWNER = b. OWNER
		AND a.table_name = b.segment_name
	) AS store_capacity
FROM
	dba_tables a
WHERE
	OWNER = ?
AND table_name NOT LIKE 'TMP%';

SELECT
	lower(column_name) column_name,
	column_id position,
	data_type,
	data_length,
	data_precision,
	data_scale,
	nullable,
	data_default default_value,
	default_length
FROM
	dba_tab_columns
WHERE
	OWNER = ?
AND table_name = ?;

# index
SELECT
	lower(index_name) index_name,
	index_type type
FROM
	dba_indexes
WHERE
	OWNER = ?
AND table_name = ?
AND index_name NOT LIKE 'SYS_IL%';

SELECT
	lower(column_name) column_name,
	column_position,
	descend
FROM
	dba_ind_columns
WHERE
	table_owner = ?
AND table_name = ?
AND index_name = ?;

#collect description
SELECT
	comments
FROM
	dba_tab_comments
WHERE
	OWNER = ?
AND table_name = ?;

SELECT
	lower(column_name) column_name,
	comments
FROM
	dba_col_comments
WHERE
	OWNER = ?
AND table_name = ?;

#database
SELECT
	lower(username) username
FROM
	dba_users
WHERE
	username NOT IN (
		'STDBYPERF',
		'READONLY',
		'APPQOSSYS',
		'ANYSQL',
		'DBFLASH',
		'SYS',
		'SYSTEM',
		'MONITOR',
		'TBSEARCH',
		'MANAGER',
		'SYSMAN',
		'EXFSYS',
		'WMSYS',
		'DIP',
		'TSMSYS',
		'ORACLE_OCM',
		'OUTLN',
		'DBSNMP',
		'PERFSTAT',
		'SEARCH',
		'TOOLS',
		'TBDUMP',
		'DMSYS',
		'XDB',
		'ANONYMOUS',
		'DEV_DDL'
	);

#segsize
SELECT
	round(sum(bytes) / 1024 / 1024, 0) mbytes
FROM
	dba_segments
WHERE
	OWNER = ?
AND segment_name = ?;

關於oralce中的segements,能夠參考一下這個系列文章。

http://book.51cto.com/art/201108/288137.htm


總結一下,mysql中查看庫表字段信息都在information_schemal中,這些是獲取數據字典的必備sql。本文中mysql的語句都在本地測試過。另外oracle的結構也要熟悉。

相關文章
相關標籤/搜索