Common operations of db2
Description: This blog is an accumulation of daily knowledge points and will be updated from time to time. If you don't like it, don't spray it. Of course, I hope it can help you
create a table.
DROP TABLE T_PATRON_INFO; CREATE TABLE T_PATRON_INFO ( USER_ID INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1), UAER_NM VARCHAR(16) NOT NULL DEFAULT , ROW_CRT_TS TIMESTAMP NOT NULL DEFAULT , primary key(USER_ID) )
The sequence operation
creates a sequence
CREATE SEQUENCE T_PATRON_INFO_SEQUENCE AS INT START WITH 1 INCREMENT BY 1 MINVALUE 1 NO MAXVALUE NO CYCLE NO CACHE ORDER;
CREATE SEQUENCE T_PATRON_INFO_SEQUENCE1 START WITH 1 INCREMENT BY 1 NOMAXVALUE NOCYCLE CACHE 24;
Query sequence (use prevval to get the current value and nextval to the next value)
select nextval for T_PATRON_INFO_SEQUENCE from sysibm.sysdummy1
select nextval for T_PATRON_INFO_SEQUENCE from T_PATRON_INFO;
delete sequence
DROP SEQUENCE T_PATRON_INFO_SEQUENCE ;
Operation operation timestamp, the time below TIMESTAMP (db2 insert time (date) type)
can also be defined as 2016-10-26 22:22:22
You can also use a function instead of current timestamp
INSERT INTO T_PATRON_INFO(UAER_NM,ROW_CRT_TS) values ('patronli','2016-10-26');
add field
ALTER TABLE t_patron_info ADD COLUMN sp_cd CHAR(6);
Modify fields
ALTER TABLE t_patron_info ALTER sp_cd SET DATA TYPE DECIMAL(3);
db2 groups by date (date to character) (but to-char is not supported in lower versions of db2)
to_char(row_crt_ts,'YYYY-MM') as transDate,count(0) as num from T_patronli_info group by to_char(row_crt_ts,'YYYY-MM')