各关系型数据schema获取

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

mysql

获取表字段信息

SHOW FULL COLUMNS FROM data_base.table_name

sqlserver

获取表字段的信息

SELECT
	C.COLUMN_NAME,
	C.table_name,
	C.DATA_TYPE,
	C.CHARACTER_MAXIMUM_LENGTH,
	C.NUMERIC_PRECISION,
	C.NUMERIC_SCALE,
	C.IS_NULLABLE,
	CASE
WHEN Z.CONSTRAINT_NAME IS NULL THEN
	0
ELSE
	1
END AS IsPartOfPrimaryKey
FROM
	INFORMATION_SCHEMA.COLUMNS C OUTER APPLY (
		SELECT
			CCU.CONSTRAINT_NAME
		FROM
			INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
		JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU ON CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
		WHERE
			TC.TABLE_SCHEMA = C.TABLE_SCHEMA
		AND TC.TABLE_NAME = C.TABLE_NAME
		AND TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
		AND CCU.COLUMN_NAME = C.COLUMN_NAME
	) Z
WHERE
	C.TABLE_NAME = 'bact_biochem '

sqlserver获取字段信息

包含注释

SELECT
 [Table Name] = i_s.TABLE_NAME,
 [Column Name] = i_s.COLUMN_NAME,
 [DATA_TYPE] = i_s.DATA_TYPE,
 [Description] = s.value
FROM
 INFORMATION_SCHEMA.COLUMNS i_s
LEFT OUTER JOIN
 sys.extended_properties s
ON
 s.major_id = OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME)
 AND s.minor_id = i_s.ORDINAL_POSITION
 AND s.name = 'MS_Description'
WHERE
 OBJECTPROPERTY(OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME), 'IsMsShipped')=0
 --AND i_s.TABLE_NAME = 'table_name'
ORDER BY
 i_s.TABLE_NAME, i_s.ORDINAL_POSITION

sqlserver获取表名和表注释命名

SELECT
	d.name,
    d.type,//视图是v
  cast(f.value as varchar(500))  AS REMARKS
FROM
	sysobjects d
LEFT JOIN sys.extended_properties f ON d.id = f.major_id
AND f.minor_id = 0
where 
d.xtype = 'U'
AND d.name <> 'dtproperties'

sqlserver获取主外键关系

SELECT
	主表名 = object_name(b.rkeyid),
	主键列ID = b.rkey,
	主键列名 = (
		SELECT
			name
		FROM
			syscolumns
		WHERE
			colid = b.rkey
		AND id = b.rkeyid
	),
	外键表ID = b.fkeyid,
	外键表名称 = object_name(b.fkeyid),
	外键列ID = b.fkey,
	外键列名 = (
		SELECT
			name
		FROM
			syscolumns
		WHERE
			colid = b.fkey
		AND id = b.fkeyid
	)
FROM
	sysobjects a
JOIN sysforeignkeys b ON a.id = b.constid
JOIN sysobjects c ON a.parent_obj = c.id
WHERE
	a.xtype = 'f'
AND c.xtype = 'U'

oracle

获取字段的信息表包括字段是否是key

SELECT
	COL.table_name,
	col.column_name,
	col.data_type,
	COL.data_length,
	COL.nullable,
	COL.high_value,
	COL.low_value,
	COL.data_precision,
	uc.constraint_type,
	CASE uc.constraint_type
WHEN 'P' THEN
	'yes'
ELSE
	''
END "primary_key"
FROM
	user_tab_columns col
LEFT JOIN user_cons_columns ucc ON ucc.table_name = col.table_name
AND ucc.column_name = col.column_name
LEFT JOIN user_constraints uc ON uc.constraint_name = ucc.constraint_name
AND uc.constraint_type = 'P'
WHERE
	col.table_name = 'TB_24XSRCYJL'

oracle查询表名和表注释

SELECT
	ut.table_name AS NAME,
	utc.comments AS comments
FROM
	user_tables ut,
	user_tab_comments utc
WHERE
	ut.table_name = utc.table_name

oracle查询表和视图

SELECT
	UT.object_name AS NAME,
	UT.object_type AS TYPE,
	utc.comments AS comments
FROM
	user_objects ut,
	user_tab_comments utc
WHERE
	ut.object_name = utc.table_name
AND (
	UT.object_type = 'TABLE'
	OR UT.object_type = 'VIEW'
)

oracle主外键关系

SELECT
	A . OWNER 外键拥有者,
	A .table_name 外键表,
	SUBSTR (c.column_name, 1, 127) 外键列,
	b. OWNER 主键拥有者,
	b.table_name 主键表,
	SUBSTR (D .column_name, 1, 127) 主键列
FROM
	user_constraints A,
	user_constraints b,
	user_cons_columns c,
	user_cons_columns D
WHERE
	A .r_constraint_name = b.constraint_name
AND A .constraint_type = 'R'
AND b.constraint_type = 'P'
AND A .r_owner = b. OWNER
AND A .constraint_name = c.constraint_name
AND b.constraint_name = D .constraint_name
AND A . OWNER = c. OWNER
AND A .table_name = c.table_name
AND b. OWNER = D . OWNER
AND b.table_name = D .table_name

 

转载于:https://my.oschina.net/u/1760791/blog/1576093

你可能感兴趣的:(各关系型数据schema获取)