天天看点

数据库学习笔记---pgsql

创建表

drop table test_create_table;

create table test_create_table (
    id int8 not null primary key ,
    user_id int8 not null,
    user_name varchar(20) not null default '',
    user_sex boolean
)
           

给主键设置自增(需要先设置sequence)

/*增加自增索引*/
create sequence id;
alter table test_create_table alter column id set default nextval('id');
           

添加索引

/*设置更新触发器函数*/
create or replace function upd_timestamp() returns trigger as
$$
begin
    new.update_time = current_timestamp;
    return new;
end
$$
language plpgsql;

/*创建触发器*/
create trigger trigger_test_study
    before update
    on test_create_table
    for each row
execute procedure upd_timestamp();