MySql的基础知识
数据测试过程中常用的MySql语句
备份表
主要用于建表语句的表备份以及数据迁移:
- 备份表
(只备份表结构):
CREATE TABLE new_table_name like old_table _name;
(备份表结构以及数据):
CREATE TABLE new_table_name as old_table _name;
- 只备份表结构的过程中,还需要数据迁移:
INSERT INTO new_table_name select * from old_table _name;
(插入数据存在分区的情况):
set hive.exec.dynamic.partition = true;
INSERT INTO new_table _name partition(data_month) select * from old_table_name;
- 查看表结构的方法
# TDH查看别结果:
desc + table_name ;
# MySql查看表结构:
show full columns from + table_name;
# Oracle查看表结构:
select * from user_tab_columns where table_name = '表名';
- 查看建表 语句的 语法;
show create table + 表名;
- 排序去重的 语法;
# 去重data_date,并根据日期降序
SELECT DISTINCT data_date from table_name order by data_date desc;
# 根据字段owner去重
select distinct(owner) from table_name;
- 删除表以及数据的 语法;
# 删除表,一般在数据库中很少使用,用于备份表的时候用到
drop table + table_name;
# 删除表中的数据,但是不清除表结构
truncate table + table_name ;
# 根据条件删除表中的一条数据
delete from + table_name from 字段 = 值;
- 查看存过的方法
# 简单的查询
DESC plsql FUNCATION EXTENDED + 存储名称;
# 指定库名dpm查询存储的过程
select * from system.procedyres_v t where t.procedure_name
like lower('存过名称') and t.database_name='dpm';
- 检执行存过
# 执行存储过程
DECLAE
V_RETURN DECIMAL;
V_MSG STRING;
BEGIN
库名.存储名称('20230312',V_RETURN,V_MSG);
DBMS_OUTPUT.PUT_LINE(V_RETURN);
DBMS_OUTPUT.PUT_LINE(V_MSG);
END;
- 性能方面需要排查问题方法。