MySQL cookbook第9章读书笔记

何为元数据?有时候你所需要的不仅仅是数据表中的数据值,你需要刻画或者描述这些数据的信息--那就是元数据语句。

1,获取受语句影响的数据行数目

image

2,获取设置元数据的结果

image

Statement:  select name, foods from profile
Number of rows:  10
Number of columns: 2
--- Column 0(name) ---
Type:            254
Display size:    7
Internal size:    20
Precision:        20
Scale:            0
Nullable:        0
--- Column 1(foods) ---
Type:            254
Display size:    21
Internal size:    42
Precision:        42
Scale:            0
Nullable:        1
[Finished in 0.2s]

3,列举或检查数据库或表的扩展

使用information_schema来得到相应信息。schemata表中的每一个数据项对应一个数据库,同时tables表中每一个数据行对应每个数据库中的每张表

image

image

4,返回表数据列定义

查看一张表有哪些数据列以及它们是如何被定义的

从information_schema获取数据列定义,也可以通过show语句或者是从mysqldump中得到你需要的信息。

image

查询单独一个数据列的信息:

image

import sys
import MySQLdb
import Cookbook

def get_column_names(conn, db_name,tbl_name):
	stmt="""
			select column_name from information_schema.columns
			where table_schema= %s and table_name = %s
		"""
	cursor=conn.cursor()
	cursor.execute(stmt,(db_name,tbl_name))
	names=[]
	for row in cursor.fetchall():
		names.append(row[0])
	cursor.close()
	return (names)


db_name = "cookbook"
tbl_name = "item"

try:
  conn = Cookbook.connect ()
except MySQLdb.Error, e:
  print "Message:", e.args[1]
  print "Code:", e.args[0]
  sys.exit (1)

print "Using get_column_names()";
print "Columns in %s.%s table:" % (db_name, tbl_name)
names = get_column_names (conn, db_name, tbl_name)
print ", ".join (names)

# construct "all but" statement
print "Construct statement to select all but data column:"
#@ _ALL_BUT_
names = get_column_names (conn, db_name, tbl_name)
# remove "data" from list of names; use try because remove
# raises an exception if value isn't in list
try:
  names.remove ("data")
except:
  pass
stmt = "SELECT `%s` FROM `%s`.`%s`" % ("`, `".join (names), db_name, tbl_name)
#@ _ALL_BUT_
print stmt

conn.close ()

image

image

通过create table来获取表结构

image

5,获取服务器元数据

image

检测服务器:show variables ,show status

确认服务器支持哪个存储引擎:show engines

posted @ 2014-06-11 22:17  小石头@shu  阅读(197)  评论(0编辑  收藏  举报