数据库种类sql备注mysql-- 获取所有表名、视图名
show tables
-- 获取 dev_test_data数据库 所有表、视图信息
select * from information_schema.tables where table_schema='dev_test_data'
-- 获取表名、视图名
select table_name from information_schema.tables where table_schema='dev_test_data'
-- 只获取表信息
select * from information_schema.tables where table_schema='dev_test_data' and table_type = 'BASE TABLE'达梦8
(底层是oracle)-- 获取表、视图名称
select table_name from user_tab_comments
-- 只获取表名称
select table_name from user_tab_comments where TABLE_TYPE = 'TABLE'
-- 获取表信息、视图
select * from user_tab_comments基本和oracle一样的oracle-- 获取表名
select table_name from user_tab_comments where TABLE_TYPE = 'TABLE'
-- 获取表信息
select * from user_tab_comments where TABLE_TYPE = 'TABLE'
-- 获取表、视图信息
select * from user_tab_comments2、获取当前表的 主表(外键关联的表)
数据库种类sql备注mysqlSELECT REFERENCED_TABLE_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE CONSTRAINT_SCHEMA='dev_test_data' AND TABLE_NAME = 't_c_my_dept' and REFERENCED_TABLE_NAME != 'null'获取字段:table_nameoracle、达梦8-- 根据表名获取 其主表 第一种 方法
select t1.table_name,
t2.table_name as "TABLE_NAME(R)",
t1.constraint_name,
t1.r_constraint_name as "CONSTRAINT_NAME(R)",
a1.column_name,
a2.column_name as "COLUMN_NAME(R)"
from user_constraints t1, user_constraints t2, user_cons_columns a1, user_cons_columns a2
where t1.owner = upper('CJY') and
t1.r_constraint_name = t2.constraint_name and
t1.constraint_name = a1.constraint_name and
t1.r_constraint_name = a2.constraint_name
and t1.table_name = 't_c_emp'
-- 根据表名获取 其主表 第二种 方法
select cl.table_name from user_cons_columns cl
left join user_constraints c on cl.constraint_name = c.r_constraint_name
where c.constraint_type = 'R'
and c.table_name = 't_c_dept'
and c.owner = 'CJY'--(获取其主表) ———— 外键关联的表
就是这个表中的外键关联的表
2.1、获取从表
--mysql
select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_SCHEMA = "dev_test_data" and REFERENCED_TABLE_NAME in ('t_c_my_dept') AND REFERENCED_TABLE_NAME != ""
复制代码
--oracle、达梦
获取其从表
-- 根据表名获取 其从属表的名字
select c.table_name from user_cons_columns cl
left join user_constraints c on cl.constraint_name = c.r_constraint_name
where c.constraint_type = 'R'
and cl.table_name = 't_c_dept' and c.owner = 'CJY'
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
table_schema = 'fd'
-- 筛选表名,一起查询快些
-- AND table_name = 't_autotb561'
AND (
referenced_table_name IS NOT NULL
OR
constraint_name = 'PRIMARY'
)23.2.3更新oracle/达梦8SELECT
aa.CONSTRAINT_NAME,
aa.COLUMN_NAME,
aa.CONSTRAINT_TYPE,
aa.SEARCH_CONDITION,
aa.R_CONSTRAINT_NAME,
bb.TABLE_NAME,
bb.COLUMN_NAME,
aa.TABLE_NAME
FROM
(
SELECT
A.CONSTRAINT_NAME,
A.TABLE_NAME,
A.COLUMN_NAME,
B.CONSTRAINT_TYPE,
B.SEARCH_CONDITION,
B.R_CONSTRAINT_NAME
FROM
USER_CONS_COLUMNS A,
USER_CONSTRAINTS B
WHERE
A.CONSTRAINT_NAME = B.CONSTRAINT_NAME
-- 模式名
AND A.owner = 'THEME_BY1'
-- 表名(查所有比一个个查询快)
-- AND A.TABLE_NAME = 'E_Z_CS_EMP'
) aa
LEFT JOIN USER_CONS_COLUMNS bb ON bb.CONSTRAINT_NAME = aa.R_CONSTRAINT_NAME23.2.3更新mysql
oracle
4、获取表的索引
根据表名、数据库名
数据库种类SQL备注mysqlSELECT
index_name,
column_name,
COLLATION,
non_unique,
nullable,
index_type,
index_comment,
table_name
FROM information_schema.statistics
WHERE
table_schema = 'fd'
-- 筛选表名,一起查询快些
-- AND table_name = 't_c_my_dept';23.2.3更新oracle/达梦8SELECT
t.index_name,
t.column_name,
t.descend,
i.uniqueness,
i.compression,
i.INDEX_TYPE,
i.table_type,
t.TABLE_NAME
FROM
user_ind_columns t,
user_indexes i
WHERE
t.index_name = i.index_name
AND t.table_name = i.table_name
-- 筛选表,全部查询快些
-- AND t.TABLE_NAME = 'abcdTYB_T_AUTOTB557'23.2.3更新mysql
oracle
5、case when then else end