常见数据库生成测试数据
程序员文章站
2022-06-03 10:05:27
...
目录
load文件、存储过程…
下文使用代码随机生成各种测试数据,然后通过jdbc、odbc插入数据库
测试出单次提交合适的数据量,然后多连接并发
mysql
//预先清理环境
drop table if exists table_name;
//创建测试表
create table table_name(
//......
)
//插入测试数据
insert into table_name values (
//.....
),
//...,
(
//...
);
oracle
预先删除表
declare
num number;
begin
select count(1) into num from user_tables where table_name = upper('xxxxx') ;
if num > 0 then
execute immediate 'drop table xxxxx' ;
end if;
end;
//或者
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE this_table';
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
创建表
create table table_name(
//...
);
程序代码中最后的
;
无需携带,否则报错:missing option
插入数据
insert into values方式
BEGIN
insert (...) into (...);
//....
insert (...) into (...);
END;
insert into select 方式
BEGIN
insert into table_name select 'v1',...,'vn' from dual
union all select 'v1',...,'vn' from dual
union all select 'v1',...,'vn' from dual;
END;
insert all方式
BEGIN
insert all
into table_name(xxxx)values(xxxx)
//...
into table_name(xxxx)values(xxxx)
select 1 from dual;
END;
适合于Oralce 9i以上版本
insert into select from 方式
insert into table_name
select c1,...,cn from t_inst_test_his where cx=xxx;
mssql
#在2016版本及更高版本,建表前判断表是否存在
DROP TABLE IF EXISTS myschema.mytable;
#低版本中
IF OBJECT_ID(N'myschema.mytable', N'U') IS NOT NULL DROP TABLE myschema.mytable;
create table myschema.table_name(
//......
)
insert into table_name values (
//.....
),
//...,
(
//...
);
The number of row value expressions in the INSERT statement exceeds the maximum allowed number of 1000 row values.
pgsql
DROP TABLE IF EXISTS table_name;
CREATE TABLE table_name (
//...
);
insert into table_name values (
//.....
),
//...,
(
//...
);
可以使用
COPY FROM
读取文件插入
db2
//手动确定是否需要删除表,或
select COUNT(*) from SYSCAT.TABLES where TRIM(TABNAME) = 'table_name';
//上述查询表存在大小写存在则删除
DROP TABLE table_schema.table_name;
CREATE TABLE table_schema.table_name (
//...
);
insert into table_schema.table_name values (
//.....
),
//...,
(
//...
);
下一篇: JS常用工具代码