Вы находитесь на странице: 1из 5

3

( )
1.
2. "" (, ),
; .
create table ( serial, int[],
constraint pk_ primary key ());
3.
) get_array(),
-10..40.
create or replace function get_array()
returns int[]
as
$$
declare
A int[];
i integer;
n integer;
begin
n=(5*random())::int+2;
for i in 1..n
loop
A= array_append(A,(51*random()-10)::int);
end loop;
return A;
end
$$
language PLpgsql;
select get_array();
) insert_data, N N
"". 12 .
create or replace function INSERT_DATA(n int)
returns void
as $$
begin
for i in 1..n
loop
insert into () values(get_array());
end loop;
end;
$$
language PLpgsql;
select insert_data(12);

4. "", .
select * from
order by ;
5. , 0.
select from
where 0=ANY();
6. , 0.
select from
where [1] =0;
7. .
select , (Select sum(elem) from unnest() as elem)
from ;
2. ""
1. "" .
2. pm/edu//PostgreSQL "AllBooks2.sql"
3. sql-
4. . ,
, .
5. , .
select .,.,COUNT(*) as
from ,
where .=Any(.)
group by ;

select *, array_length(,1)
from ;
6. , ( ).
select ., count(*) AS
From ,
where .=ANY(.)
Group BY .
Order By .;
7. , .
.
select ., count(*) AS
From ,
where .=All(.)
Group BY .
Order By .;

9. , ,
.
' ..' ' ..'
create or replace function knigi(N int)
returns table( int, int, varchar(200), kolichestvo int) as
$$
begin
return query (select N, ., .,
array_length(.,1) as kolichestvo
FROM
WHERE N=Any(.));
end;
$$ Language plpgsql;
select knigi(2);
10. , :
)
select *
from
where to_tsvector('russian',)
@@to_tsquery('russian',' & |');
)
select *
from
where to_tsvector('russian',)
@@to_tsquery('russian',' | & ');
)
select *
from
where to_tsvector('russian',)
@@to_tsquery('russian',' & | & ');

4
( PL/pgSQL. , , )
1. ""
1. "",
(, , , , , )
(, 1, 2, , ).
2014.txt
"".
_2014.txt
2014 A, B, C,, H. :
: :
A,B,C,D
:
A : B
C : D
2. PL/pgSQL, "",
"" ( ),
"".
create or replace function Copir()
returns void
as $$
declare
state int;
sline text;
gr text;
n1 int;
n2 int;
line1 text;
line2 text;
n int;
begin
n=1;
for sline in (select From ) loop
sline:=replace(sline,'-''','--');
if sline like '%' then
state=1; gr:=substring (sline from 8 for 1);
elsif sline =':' then
state=2;
elsif state=1 then
insert into values (sline, gr, 0, 0, 0, 0);
elsif state=2 then
line1 = split_part(sline, ' ',2);
n1 = split_part (line1,':',1);
n2 = split_part (line1, ':',2);
line1=split_part (sline, ' ',1);
line2=split_part (sline, ' ',3);
insert into values (n,line1,line2,n1,n2);
n=n+1;
end if;
end loop;
end;
$$ Language plpgsql;
delete from ;
select Copir();

3. PL/pgSQL, , ,
"",
= 3* + .
create or replace function copir2()
returns void
AS $$
declare
Game %rowtype;
komanda1 text;
begin
for game IN (SELECT * FROM ) loop
if game.>game. then
UPDATE SET =+1,=+3 WHERE
=game.1;
UPDATE SET =+1 WHERE =game.2;
elsif game.<game. then
UPDATE SET =+1,=+3 WHERE
=game.2;
UPDATE SET =+1 WHERE =game.1;
elsif game.=game. then
UPDATE SET =+1,=+1 WHERE
=game.1;
UPDATE SET =+1,=+1 WHERE
=game.2;
end if;
end loop;
end;
$$ Language plpgsql;
SELECT copir2();

Оценить