sql获取数据库表
TABLE 语句
具体语法:TABLE table_name [ORDER BY column_name] [LIMIT number [OFFSET number]]
其实从语法上看,可以排序,也可以过滤记录集,不过比较简单,没有 SELECT 那么强大。
示例 1
简单的建一张很小的表 y1,记录数为 10 条。表 t1,插入 10 条记录
mysql-(ytt/3305)->create table t1 (r1 int,r2 int);
Query OK, 0 rows affected (0.02 sec)
mysql-(ytt/3305)->insert into t1
with recursive aa(a,b) as (
select 1,1
union all
select a+1,ceil(rand()*20) from aa where a < 10
) select * from aa;
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
- 简单全表扫描mysql-(ytt/3305)->select * from t1;+------+------+| r1 | r2 |+------+------+| 1 | 1 || 2 | 9 || 3 | 9 || 4 | 17 || 5 | 17 || 6 | 16 || 7 | 6 || 8 | 1 || 9 | 10 || 10 | 3 |+------+------+10 rows in set (0.00 sec)
- TABLE 结果mysql-(ytt/3305)->table t1;+------+------+| r1 | r2 |+------+------+| 1 | 1 || 2 | 9 || 3 | 9 || 4 | 17 || 5 | 17 || 6 | 16 || 7 | 6 || 8 | 1 || 9 | 10 || 10 | 3 |+------+------+10 rows in set (0.00 sec)
- 看下 table 的执行计划mysql-(ytt/3305)->explain table t1 order by r1 limit 2G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: t1 partitions: NULL type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 10 filtered: 100.00 Extra: Using filesort1 row in set, 1 warning (0.00 sec)
- 其实可以看到 TABLE 内部被 MySQL 转换为 SELECT 了。mysql-(ytt/3305)->show warningsG*************************** 1. row *************************** Level: Note Code: 1003Message: /* select#1 */ select `ytt`.`t1`.`r1` AS `r1`,`ytt`.`t1`.`r2` AS `r2` from `ytt`.`t1` order by `ytt`.`t1`.`r1` limit 21 row in set (0.00 sec)
- 那其实从上面简单的例子可以看到 TABLE 在内部被转成了普通的 SELECT 来处理。示例 2应用于子查询里的子表。这里要注意,内表的字段数量必须和外表过滤的字段数量一致。克隆表 t1 结构mysql-(ytt/3305)->create table t2 like t1;Query OK, 0 rows affected (0.02 sec)
- 克隆表 t1 数据mysql-(ytt/3305)->insert into t2 table t1;Query OK, 10 rows affected (0.00 sec)Records: 10 Duplicates: 0 Warnings: 0
- table t1 被当做内表,表 t1 有两个字段,必须同时满足 t2 检索时过滤的字段也是两个。mysql-(ytt/3305)->select * from t2 where (r1,r2) in (table t1);+------+------+| r1 | r2 |+------+------+| 1 | 1 || 2 | 9 || 3 | 9 || 4 | 17 || 5 | 17 || 6 | 16 || 7 | 6 || 8 | 1 || 9 | 10 || 10 | 3 |+------+------+10 rows in set (0.00 sec)
- 注意:这里如果过滤的字段数量和子表数量不一致,则会报错。
❷ 怎样用SQL语句查询一个数据库中的所有表
1、打开Microsoft SQL Server 2012,选中需要查询所有表的数据库。
❸ sql查询数据库中有某个值的所有表
1、首先在电脑中打开Microsoft SQL Server,查询所有数据库。
❹ 如何通过sql获取数据库所有表数据
1.查询数据库中的所有数据库名:
SELECT Name FROM Master..SysDatabases ORDER BY Name
2.查询某个数据库中所有的表名:
SELECT Name FROM SysObjects Where XType='U' ORDER BY Name
3.查询表结构信息:
1 SELECT (case when a.colorder=1 then d.name else null end) 表名,
2 a.colorder 字段序号,a.name 字段名,
3 (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识,
4 (case when (SELECT count(*) FROM sysobjects
5 WHERE (name in (SELECT name FROM sysindexes
6 WHERE (id = a.id) AND (indid in
7 (SELECT indid FROM sysindexkeys
8 WHERE (id = a.id) AND (colid in
9 (SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name)))))))
10 AND (xtype = 'PK'))>0 then '√' else '' end) 主键,b.name 类型,a.length 占用字节数,
11 COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度,
12 isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,(case when a.isnullable=1 then '√'else '' end) 允许空,
13 isnull(e.text,'') 默认值,isnull(g.[value], ' ') AS [说明]
14 FROM syscolumns a
15 left join systypes b on a.xtype=b.xusertype
16 inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
17 left join syscomments e on a.cdefault=e.id
18 left join sys.extended_properties g on a.id=g.major_id AND a.colid=g.minor_id
19 left join sys.extended_properties f on d.id=f.class and f.minor_id=0
20 where b.name is not null
21 --WHERE d.name='要查询的表' --如果只查询指定表,加上此条件
22 order by a.id,a.colorder
❺ 怎样用SQL语句查询一个数据库中的所有表
查询数据库里所有表名和字段名的语句
SQL 查询所有表名:
SELECT NAME FROM SYSOBJECTS WHERE TYPE='U'
SELECT * FROM INFORMATION_SCHEMA.TABLES
结构化查询语言(Structured Query Language)简称SQL,结构化查询语言是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统;
sql 语句就是对数据库进行操作的一种语言。
(5)sql获取数据库表扩展阅读:
SQL语句常见语句:
1、更新:update table1 set field1=value1 where 范围;
2、查找:select * from table1 where field1 like ’%value1%’ (所有包含‘value1’这个模式的字符串);
3、排序:select * from table1 order by field1,field2 [desc];
4、求和:select sum(field1) as sumvalue from table1;
5、平均:select avg(field1) as avgvalue from table1;
6、最大:select max(field1) as maxvalue from table1;
7、最小:select min(field1) as minvalue from table1[separator]。
参考资料来源:网络-sql语句