⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arrays.sql

📁 PostgreSQL 8.1.4的源码 适用于Linux下的开源数据库系统
💻 SQL
字号:
---- ARRAYS--CREATE TABLE arrtest (	a 			int2[],	b 			int4[][][],	c 			name[],	d			text[][], 	e 			float8[],	f			char(5)[],	g			varchar(5)[]);---- only the 'e' array is 0-based, the others are 1-based.--INSERT INTO arrtest (a[1:5], b[1:1][1:2][1:2], c, d, f, g)   VALUES ('{1,2,3,4,5}', '{{{0,0},{1,2}}}', '{}', '{}', '{}', '{}');UPDATE arrtest SET e[0] = '1.1';UPDATE arrtest SET e[1] = '2.2';INSERT INTO arrtest (f)   VALUES ('{"too long"}');INSERT INTO arrtest (a, b[1:2][1:2], c, d, e, f, g)   VALUES ('{11,12,23}', '{{3,4},{4,5}}', '{"foobar"}',            '{{"elt1", "elt2"}}', '{"3.4", "6.7"}',           '{"abc","abcde"}', '{"abc","abcde"}');INSERT INTO arrtest (a, b[1:2], c, d[1:2])   VALUES ('{}', '{3,4}', '{foo,bar}', '{bar,foo}');SELECT * FROM arrtest;SELECT arrtest.a[1],          arrtest.b[1][1][1],          arrtest.c[1],          arrtest.d[1][1],           arrtest.e[0]   FROM arrtest;SELECT a[1], b[1][1][1], c[1], d[1][1], e[0]   FROM arrtest;SELECT a[1:3],          b[1:1][1:2][1:2],          c[1:2],           d[1:1][1:2]   FROM arrtest;SELECT array_dims(a) AS a,array_dims(b) AS b,array_dims(c) AS c   FROM arrtest;-- returns nothing SELECT *   FROM arrtest   WHERE a[1] < 5 and          c = '{"foobar"}'::_name;UPDATE arrtest  SET a[1:2] = '{16,25}'  WHERE NOT a = '{}'::_int2;UPDATE arrtest  SET b[1:1][1:1][1:2] = '{113, 117}',      b[1:1][1:2][2:2] = '{142, 147}'  WHERE array_dims(b) = '[1:1][1:2][1:2]';UPDATE arrtest  SET c[2:2] = '{"new_word"}'  WHERE array_dims(c) is not null;SELECT a,b,c FROM arrtest;SELECT a[1:3],          b[1:1][1:2][1:2],          c[1:2],           d[1:1][2:2]   FROM arrtest;---- array expressions and operators---- table creation and INSERTsCREATE TEMP TABLE arrtest2 (i integer ARRAY[4], f float8[], n numeric[], t text[], d timestamp[]);INSERT INTO arrtest2 VALUES(  ARRAY[[[113,142],[1,147]]],  ARRAY[1.1,1.2,1.3]::float8[],  ARRAY[1.1,1.2,1.3],  ARRAY[[['aaa','aab'],['aba','abb'],['aca','acb']],[['baa','bab'],['bba','bbb'],['bca','bcb']]],  ARRAY['19620326','19931223','19970117']::timestamp[]);-- some more test dataCREATE TEMP TABLE arrtest_f (f0 int, f1 text, f2 float8);insert into arrtest_f values(1,'cat1',1.21);insert into arrtest_f values(2,'cat1',1.24);insert into arrtest_f values(3,'cat1',1.18);insert into arrtest_f values(4,'cat1',1.26);insert into arrtest_f values(5,'cat1',1.15);insert into arrtest_f values(6,'cat2',1.15);insert into arrtest_f values(7,'cat2',1.26);insert into arrtest_f values(8,'cat2',1.32);insert into arrtest_f values(9,'cat2',1.30);CREATE TEMP TABLE arrtest_i (f0 int, f1 text, f2 int);insert into arrtest_i values(1,'cat1',21);insert into arrtest_i values(2,'cat1',24);insert into arrtest_i values(3,'cat1',18);insert into arrtest_i values(4,'cat1',26);insert into arrtest_i values(5,'cat1',15);insert into arrtest_i values(6,'cat2',15);insert into arrtest_i values(7,'cat2',26);insert into arrtest_i values(8,'cat2',32);insert into arrtest_i values(9,'cat2',30);-- expressionsSELECT t.f[1][3][1] AS "131", t.f[2][2][1] AS "221" FROM (  SELECT ARRAY[[[111,112],[121,122],[131,132]],[[211,212],[221,122],[231,232]]] AS f) AS t;SELECT ARRAY[[[[[['hello'],['world']]]]]];SELECT ARRAY[ARRAY['hello'],ARRAY['world']];SELECT ARRAY(select f2 from arrtest_f order by f2) AS "ARRAY";-- functionsSELECT array_append(array[42], 6) AS "{42,6}";SELECT array_prepend(6, array[42]) AS "{6,42}";SELECT array_cat(ARRAY[1,2], ARRAY[3,4]) AS "{1,2,3,4}";SELECT array_cat(ARRAY[1,2], ARRAY[[3,4],[5,6]]) AS "{{1,2},{3,4},{5,6}}";SELECT array_cat(ARRAY[[3,4],[5,6]], ARRAY[1,2]) AS "{{3,4},{5,6},{1,2}}";-- operatorsSELECT a FROM arrtest WHERE b = ARRAY[[[113,142],[1,147]]];SELECT NOT ARRAY[1.1,1.2,1.3] = ARRAY[1.1,1.2,1.3] AS "FALSE";SELECT ARRAY[1,2] || 3 AS "{1,2,3}";SELECT 0 || ARRAY[1,2] AS "{0,1,2}";SELECT ARRAY[1,2] || ARRAY[3,4] AS "{1,2,3,4}";SELECT ARRAY[[['hello','world']]] || ARRAY[[['happy','birthday']]] AS "ARRAY";SELECT ARRAY[[1,2],[3,4]] || ARRAY[5,6] AS "{{1,2},{3,4},{5,6}}";SELECT ARRAY[0,0] || ARRAY[1,1] || ARRAY[2,2] AS "{0,0,1,1,2,2}";SELECT 0 || ARRAY[1,2] || 3 AS "{0,1,2,3}";-- array castsSELECT ARRAY[1,2,3]::text[]::int[]::float8[] AS "{1,2,3}";SELECT ARRAY[1,2,3]::text[]::int[]::float8[] is of (float8[]) as "TRUE";SELECT ARRAY[['a','bc'],['def','hijk']]::text[]::varchar[] AS "{{a,bc},{def,hijk}}";SELECT ARRAY[['a','bc'],['def','hijk']]::text[]::varchar[] is of (varchar[]) as "TRUE";SELECT CAST(ARRAY[[[[[['a','bb','ccc']]]]]] as text[]) as "{{{{{{a,bb,ccc}}}}}}";-- scalar op any/all (array)select 33 = any ('{1,2,3}');select 33 = any ('{1,2,33}');select 33 = all ('{1,2,33}');select 33 >= all ('{1,2,33}');-- boundary casesselect null::int >= all ('{1,2,33}');select null::int >= all ('{}');select null::int >= any ('{}');-- cross-datatypeselect 33.4 = any (array[1,2,3]);select 33.4 > all (array[1,2,3]);-- errorsselect 33 * any ('{1,2,3}');select 33 * any (44);-- test indexes on arrayscreate temp table arr_tbl (f1 int[] unique);insert into arr_tbl values ('{1,2,3}');insert into arr_tbl values ('{1,2}');-- failure expected:insert into arr_tbl values ('{1,2,3}');insert into arr_tbl values ('{2,3,4}');insert into arr_tbl values ('{1,5,3}');insert into arr_tbl values ('{1,2,10}');set enable_seqscan to off;set enable_bitmapscan to off;select * from arr_tbl where f1 > '{1,2,3}' and f1 <= '{1,5,3}';-- note: if above select doesn't produce the expected tuple order,-- then you didn't get an indexscan plan, and something is busted.reset enable_seqscan;reset enable_bitmapscan;-- test [not] (like|ilike) (any|all) (...)select 'foo' like any (array['%a', '%o']); -- tselect 'foo' like any (array['%a', '%b']); -- fselect 'foo' like all (array['f%', '%o']); -- tselect 'foo' like all (array['f%', '%b']); -- fselect 'foo' not like any (array['%a', '%b']); -- tselect 'foo' not like all (array['%a', '%o']); -- fselect 'foo' ilike any (array['%A', '%O']); -- tselect 'foo' ilike all (array['F%', '%O']); -- t---- General array parser tests---- none of the following should be acceptedselect '{{1,{2}},{2,3}}'::text[];select '{{},{}}'::text[];select E'{{1,2},\\{2,3}}'::text[];select '{{"1 2" x},{3}}'::text[];select '{}}'::text[];select '{ }}'::text[];-- none of the above should be accepted-- all of the following should be acceptedselect '{}'::text[];select '{{{1,2,3,4},{2,3,4,5}},{{3,4,5,6},{4,5,6,7}}}'::text[];select '{0 second  ,0 second}'::interval[];select '{ { "," } , { 3 } }'::text[];select '  {   {  "  0 second  "   ,  0 second  }   }'::text[];select '{           0 second,           @ 1 hour @ 42 minutes @ 20 seconds         }'::interval[];-- all of the above should be accepted-- tests for array aggregatesCREATE TEMP TABLE arraggtest ( f1 INT[], f2 TEXT[][], f3 FLOAT[]);INSERT INTO arraggtest (f1, f2, f3) VALUES('{1,2,3,4}','{{grey,red},{blue,blue}}','{1.6, 0.0}');INSERT INTO arraggtest (f1, f2, f3) VALUES('{1,2,3}','{{grey,red},{grey,blue}}','{1.6}');SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;INSERT INTO arraggtest (f1, f2, f3) VALUES('{3,3,2,4,5,6}','{{white,yellow},{pink,orange}}','{2.1,3.3,1.8,1.7,1.6}');SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;INSERT INTO arraggtest (f1, f2, f3) VALUES('{2}','{{black,red},{green,orange}}','{1.6,2.2,2.6,0.4}');SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;INSERT INTO arraggtest (f1, f2, f3) VALUES('{4,2,6,7,8,1}','{{red},{black},{purple},{blue},{blue}}',NULL);SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;INSERT INTO arraggtest (f1, f2, f3) VALUES('{}','{{pink,white,blue,red,grey,orange}}','{2.1,1.87,1.4,2.2}');SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -