一、前言
我们查询数据库表的信息,一般都使用界面化的连接工具查看,很少使用SQL语句去查,而且不同的数据库SQL语句又各自有差异。但如果通过代码去获取数据库表的信息,这时就需要通过SQL语句去查了,这个在逆向代码生成工具中经常有使用。
二、通过SQL对表信息进行查询
1、mysql查询表的信息
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = {schema} AND TABLE_NAME = {table}
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = {schema} AND TABLE_NAME = {table}
2、mysql产线表字段的信息
SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = {schema} AND TABLE_NAME = {table} AND COLUMN_NAME = {column}
SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = {schema} AND TABLE_NAME = {table} AND COLUMN_NAME = {column}
3、postgressql查询表信息
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = {schema} AND TABLE_NAME = {table}
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = {schema} AND TABLE_NAME = {table}
4、 postgressql查询表字段信息
SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = {schema} AND TABLE_NAME = {table} AND COLUMN_NAME = {column}
SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = {schema} AND TABLE_NAME = {table} AND COLUMN_NAME = {column}
5、oracle查询表信息
SELECT OWNER TABLE_SCHEMA, TABLE_NAME, TABLE_COMMENT FROM ALL_TAB_COMMENTS
WHERE OWNER = {schema} AND TABLE_NAME = {table}
SELECT OWNER TABLE_SCHEMA, TABLE_NAME, TABLE_COMMENT FROM ALL_TAB_COMMENTS
WHERE OWNER = {schema} AND TABLE_NAME = {table}
6、oracle查询表字段信息
SELECT OWNER TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLUMN_COMMENT FROM ALL_COL_COMMENTS
WHERE OWNER = {schema} AND TABLE_NAME = {table} AND COLUMN_NAME = {column}
SELECT OWNER TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLUMN_COMMENT FROM ALL_COL_COMMENTS
WHERE OWNER = {schema} AND TABLE_NAME = {table} AND COLUMN_NAME = {column}
三、Java数据类型和数据库数据类型的对应关系
mysql
oracle
四、SQL常用统计语句
SQL 提供了多种统计语句来帮助我们分析数据库中的数据。以下是一些常用的 SQL 统计语句及其示例:
-
COUNT():计算行数
SELECT COUNT(*) FROM 表名; | |
SELECT COUNT(列名) FROM 表名 WHERE 条件; |
-
SUM():计算某列的总和
SELECT SUM(列名) FROM 表名 WHERE 条件; |
-
AVG():计算某列的平均值
SELECT AVG(列名) FROM 表名 WHERE 条件; |
-
MAX():返回某列的最大值
SELECT MAX(列名) FROM 表名 WHERE 条件; |
-
MIN():返回某列的最小值
SELECT MIN(列名) FROM 表名 WHERE 条件; |
-
GROUP BY:按一个或多个列对结果集进行分组
SELECT 列名1, 列名2, COUNT(*) | |
FROM 表名 | |
GROUP BY 列名1, 列名2 | |
HAVING 条件; |
注意:HAVING
子句用于对分组后的结果进行过滤。
7. DISTINCT:返回唯一不同的值
SELECT DISTINCT 列名 FROM 表名; |
-
JOIN(包括 INNER JOIN, LEFT JOIN, RIGHT JOIN 等):基于两个或多个表之间的相关列之间的关系,从这些表中查询数据
SELECT * | |
FROM 表1 | |
INNER JOIN 表2 ON 表1.列名 = 表2.列名; |
-
子查询:在查询中嵌套另一个查询
SELECT 列名 | |
FROM 表名 | |
WHERE 列名 IN (SELECT 列名 FROM 其他表 WHERE 条件); |
-
聚合函数与 GROUP BY 结合使用:这是非常常见的组合,用于对分组后的数据进行统计
SELECT 列名1, AVG(列名2) | |
FROM 表名 | |
GROUP BY 列名1 | |
HAVING AVG(列名2) > 某个值; |
-
排序:使用
ORDER BY
对结果进行排序
SELECT * FROM 表名 ORDER BY 列名 ASC|DESC; |
ASC 表示升序(默认),
DESC` 表示降序。