Files
energy-storage-safety/docker/sql/db.sql
focus1024-wind f779275018 ADD: add users
2025-12-15 18:34:01 +08:00

30 lines
849 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";
-- 创建 users 表
CREATE TABLE users
(
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;
-- 创建触发器:在 users 表 UPDATE 时自动调用
CREATE TRIGGER trigger_update_users_updated_at
BEFORE UPDATE
ON users
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();