ORACLE/MYSQL数据库的常用SQL命令

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunJW_2017/article/details/84023425

以下均是在创建一个成功的连接后的操作。
MySQL数据库在一个连接下是通过不同的数据库名称来进行区分的,即一个连接下可以有很多个库;Oracle数据库连接时则是通过用户名进行区分的,一个用户名下面只有一个数据库。

1. 返回某个数据库(模式)中所有表的基本信息:

ORACLE数据库:

-- 将yourschema换成指定的数据库名称
select * from all_tables where owner='yourschema'

MYSQL数据库:

-- 将yourschema换成指定的数据库名称
select * from information_schema.tables where table_schema='yourschema'
2.返回指定数据库中所有的表名

ORACLE数据库:

--将yourschema换成指定的数据库名称
select table_name from all_tables where owner='yourschema'

MYSQL数据库:

-- 将yourschema换成指定的数据库名称
select table_name from information_schema.tables where table_schema='yourschema'
3. 返回某个数据库(模式)中某张表的所有字段名称及其数据类型:

ORACLE数据库(可以不指定owner,在一些情况下制定该参数反而返回错误的结果):

--将yourschema替换为数据库名称,yourtable替换为表名
select u.column_name, u.data_type 
from user_tab_columns u
left join all_tables a 
on u.table_name = a.table_name
where a.owner='yourschema' and u.table_name='yourtable'

MYSQL数据库:

--将yourschema替换为数据库名称,yourtable替换为表名
select column_name, data_type from information_schema.columns
where table_schema='yourschema' and table_name='yourtable'

如果不指定数据库名称,在MySQL中类似的做法会引发歧义。ORACLE中并未进行测试。

4. 返回某个数据库(模式)中某张表中的具有主键约束的字段名称:

ORACLE数据库(可以不指定owner,在一些情况下制定该参数反而返回错误的结果):

--将yourschema替换为数据库名称,yourtable替换为表名
select column_name from user_cons_columns 
where constraint_name=(
	select constraint_name from user_constraints 
	where owner='yourschema' 
	and table_name='yourtable'
	and constraint_type = 'P')

MYSQL数据库:

--将yourschema替换为数据库名称,yourtable替换为表名
select column_name from information_schema.key_column_usage 
where table_schema='yourschema' 
and table_name='yourtable'
and constraint_type='PRIMARY'
5. 返回某个数据库(模式)中所有表的大小:

MYSQL数据库:

SELECT
	TABLE_NAME,
	concat(TRUNCATE (data_length / 1024 / 1024, 2),' MB') AS data_size,
	concat(TRUNCATE (index_length / 1024 / 1024, 2),' MB') AS index_size
FROM
	information_schema.TABLES
WHERE
	TABLE_SCHEMA = 'your_schema'
GROUP BY
	TABLE_NAME
ORDER BY
	data_length DESC

猜你喜欢

转载自blog.csdn.net/SunJW_2017/article/details/84023425
今日推荐