SQL Server2019 数据库查询所有数据库名、表名、表结构、表字段、主键方法演示
- 第一章:查询方法
- ① 查询所有数据库名
- ② 查询所有表名方法
- ③ 查询表结构、表字段方法
- ④ 查询主键方法
- 第一章:报错信息
- ① 对象名 'user_cons_columns' 无效
第一章:查询方法
对象名 'user_tab_columns'
和 'user_cons_columns'
都属于 oracle 数据库里的,sqlserver 里没有,所以使用会报错。
① 查询所有数据库名
SQL 语句示例:
-- 查询所有数据库名
select name as '数据库名'
from master..sysdatabases;
查询效果展示:
② 查询所有表名方法
SQL 语句示例:
-- 查询所有表名
select name as '表名'
from sysobjects
where xtype='U';
查询效果展示:
③ 查询表结构、表字段方法
SQL 语句示例:
-- 查询表结构、表字段
select * from information_schema.columns where table_name = 'SM_USERGROUP';
查询效果展示:
④ 查询主键方法
SQL 语句示例:
-- 查询表主键
select table_name as '表名', column_name as '主键'
from information_schema.key_column_usage
where table_name = 'SM_USERGROUP';
查询效果展示:
第一章:报错信息
① 对象名 ‘user_cons_columns’ 无效
使用 oracle 的对象名会报错。
com.microsoft.sqlserver.jdbc.SQLServerException: 对象名 'user_cons_columns' 无效。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:254)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1608)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:859)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:759)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7240)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2869)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:243)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:218)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeQuery(SQLServerStatement.java:688)
喜欢的点个赞❤吧!