
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 (002 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 (000 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 (000 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 (000 sec)
看下 table 的执行计划mysql-(ytt/3305)->explain table t1 order by r1 limit 2\G 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: 10000 Extra: Using filesort1 row in set, 1 warning (000 sec)
其实可以看到 TABLE 内部被 MySQL 转换为 SELECT 了。mysql-(ytt/3305)->show warnings\G 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 (000 sec)
那其实从上面简单的例子可以看到 TABLE 在内部被转成了普通的 SELECT 来处理。示例 2应用于子查询里的子表。这里要注意,内表的字段数量必须和外表过滤的字段数量一致。克隆表 t1 结构mysql-(ytt/3305)->create table t2 like t1;Query OK, 0 rows affected (002 sec)
克隆表 t1 数据mysql-(ytt/3305)->insert into t2 table t1;Query OK, 10 rows affected (000 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 (000 sec)
注意:这里如果过滤的字段数量和子表数量不一致,则会报错。
修改表名://alter table 表名 rename to 新表名alter table T rename to customer 修改字段名//alter table 表名 change 旧表名 新表名 字段约束(如类型,primary key,not null)alter table T change a b char(10)
直接将数据库表名字段作为字符类型传到存储过程中,存储过程拼接sql语句,再用exec执行。例如:
CREATE
PROCEDURE
[proc_test]
(@tablename
varchar(100))
WITH
EXECUTE
AS
CALLER
AS
declare
@sql
varchar(1000);
set
@sql='select
from
'+@tablename
exec(@sql)
GO
再直接执行
exec
proc_test
'学生表'
声明下,c不会,但是我告诉你如何查你所需要的结果:
select table_name from information_schematables where table_schema='数据库名' and table_name=concat(conv(table_name,10,10)) order by conv(table_name,10,10) desc limit 1;
ps:mysql有些版本是不支持纯数字做表名的
以上就是关于mysql中查询数据库中表名称和结构的sql语句是什么啊啊全部的内容,包括:mysql中查询数据库中表名称和结构的sql语句是什么啊啊、mysql表修改表名,字段名、mysql 存储过程 数据库表名字段作为参数传递给存储过程的方法等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)