Files
energy-storage-safety/docker/sql/db.sql
focus1024-wind 8d1cba57ca ADD: user login
2025-12-23 11:52:24 +08:00

30 lines
859 B
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 确保启用 uuid-ossp 扩展(用于生成 UUID
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- 创建 account 表
CREATE TABLE account
(
id CHAR(36) PRIMARY KEY DEFAULT uuid_generate_v4()::varchar,
username VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
nickname VARCHAR(255),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- 创建触发器函数:自动更新 updated_at
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS
$$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- 创建触发器:在 account 表 UPDATE 时自动调用
CREATE TRIGGER trigger_update_account_updated_at
BEFORE UPDATE
ON account
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();