【PostgreSQL/SQL Server/Oracle】SQL语句运维常用

1.如何仅根据表名确定该表属于哪个数据库或者表空间?

1.1 PostgreSQL

select * from information_schema.tables where table_name ilike ‘%pG_statistic%’;

postgres=# select * from information_schema.tables where table_name ilike '%pG_statistic%';
-[ RECORD 1 ]----------------+-----------------
table_catalog                | postgres
table_schema                 | pg_catalog
table_name                   | pg_statistic
table_type                   | BASE TABLE
self_referencing_column_name |
reference_generation         |
user_defined_type_catalog    |
user_defined_type_schema     |
user_defined_type_name       |
is_insertable_into           | YES
is_typed                     | NO
commit_action                |
-[ RECORD 2 ]----------------+-----------------
table_catalog                | postgres
table_schema                 | pg_catalog
table_name                   | pg_statistic_ext
table_type                   | BASE TABLE
self_referencing_column_name |
reference_generation         |
user_defined_type_catalog    |
user_defined_type_schema     |
user_defined_type_name       |
is_insertable_into           | YES
is_typed                     | NO
commit_action                |

postgres=#

1.2 Oracle

SELECT * FROM all_tables WHERE upper(table_name) LIKE ‘%mytable%’;

1.3 SQL Server

exec sp_MSforeachdb @command1=‘USE ? if exists(SELECT 1 from sysobjects where id=object_id(’‘表名’‘)) PRINT ‘’?’‘’

2. 查询指定的表-table

2.1 Postgre SQL

psql命令行模式下两种方式,注意这个是查询的指定database的表信息:
testdb=# select * from pg_tables where tablename = ‘test_student’;
testdb=# \dt *student;
查询表定义(包括table所属用户信息):
\d + 表名
修改table所属的owner:
alter table xxx_table owner to xxx_db_user;
*列名、表名、数据库名中带有特殊字符的时候,使用双引号",否则执行会报错。

2.2 SQL Server

指定到具体的database查询:
select name from sysobjects where xtype=‘u’
查询表定义:
sp_help + 表名
sp_columns + 表名

2.3 Oracle

查询是基于所有表空间:
select * from all_tables
查询表定义:
desc + 表名

3.查询指定的数据库or表空间

3.1 Postgres SQL

查询数据库信息:
psql命令行2种方式查询:
\l
select datname from pg_database;
使用\c命令可以具体切换到相关的数据库进行操作。

3.2 SQL Server

查询数据库信息:
select * from master…sysdatabases

3.3 Oracle

查询表空间信息:
select * from dba_tablespaces
查看表空间的名称及大小
SELECT t.tablespace_name, round(SUM(bytes / (1024 * 1024)), 0) ts_size
FROM dba_tablespaces t, dba_data_files d
WHERE t.tablespace_name = d.tablespace_name
GROUP BY t.tablespace_name;
查看表空间物理文件的名称及大小
SELECT tablespace_name,
file_id,
file_name,
round(bytes / (1024 * 1024), 0) total_space
FROM dba_data_files
ORDER BY tablespace_name;

4.查询所有表

数据库类型语句说明
Postgre SQLselect * from pg_tables
Oracleselect * from all_tables
SQL Serverselect name from sysobjects where xtype=‘u’u是用户表s是系统表
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值