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

SQL语句运维常用

  • 1.如何仅根据表名确定该表属于哪个数据库或者表空间?
    • 1.1 PostgreSQL
    • 1.2 Oracle
    • 1.3 SQL Server
  • 2. 查询指定的表-table
    • 2.1 Postgre SQL
    • 2.2 SQL Server
    • 2.3 Oracle
  • 3.查询指定的数据库or表空间
    • 3.1 Postgres SQL
    • 3.2 SQL Server
    • 3.3 Oracle
  • 4.查询所有表

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 SQL select * from pg_tables
Oracle select * from all_tables
SQL Server select name from sysobjects where xtype=‘u’ u是用户表s是系统表

你可能感兴趣的:(PostgreSQL,工具和生长环境,oracle,postgresql,sql)