You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

-- PostgreSQL数据库表结构脚本
-- 学号23210441
-- 姓名ct
-- 创建顾客表customer_cstatm23210441
-- 先删除表(如果存在)
DROP TABLE IF EXISTS customer_cstatm23210441;
-- 创建表
CREATE TABLE customer_cstatm23210441 (
-- 主键顾客ID
cid VARCHAR(20) PRIMARY KEY,
-- 顾客姓名
cname VARCHAR(100) NOT NULL,
-- 顾客PIN码密码
cpin VARCHAR(20) NOT NULL,
-- 登录时间
idatetime TIMESTAMP,
-- 登录次数
itimes INTEGER DEFAULT 0,
-- 登录失败次数
istimes INTEGER DEFAULT 0,
-- 账户状态1-正常0-锁定
status CHAR(1) DEFAULT '1',
-- 创建时间
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
-- 更新时间
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 添加索引以提高查询性能
CREATE INDEX idx_customer_id ON customer_cstatm23210441(cid);
-- 创建更新时间的触发器函数
CREATE OR REPLACE FUNCTION update_customer_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.update_time = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- 创建触发器
CREATE TRIGGER customer_update_timestamp
BEFORE UPDATE ON customer_cstatm23210441
FOR EACH ROW
EXECUTE FUNCTION update_customer_timestamp();
-- 插入示例数据
INSERT INTO customer_cstatm23210441 (cid, cname, cpin, itimes, istimes)
VALUES
('1001', '张三', '123456', 0, 0),
('1002', '李四', '654321', 0, 0),
('1003', '王五', '111111', 0, 0),
('1004', '赵六', '222222', 0, 0),
('1005', '钱七', '333333', 0, 0);
-- 查看插入的数据
SELECT * FROM customer_cstatm23210441;
-- 表结构说明
/*
表结构说明:
- cid: 顾客ID主键用于唯一标识顾客
- cname: 顾客姓名
- cpin: 顾客PIN码用于登录验证
- idatetime: 最后登录时间
- itimes: 累计登录次数
- istimes: 连续登录失败次数
- status: 账户状态,用于在多次失败后锁定账户
- create_time: 记录创建时间
- update_time: 记录最后更新时间
*/
-- 使用说明
/*
使用说明:
1. 在PostgreSQL数据库中执行此脚本创建表结构
2. 默认连接参数:
- 数据库postgres
- 用户postgres
- 密码123456
- 端口5432
3. 系统会自动管理登录次数和失败次数
*/