PowerDesigner 给表统一增加默认字段(id,创建时间,创建人,修改时间,修改人)...

本文介绍了一种使用VBA脚本在数据模型中批量为表格添加包含创建和更新时间戳的字段的方法。此脚本可以自动检测每个表格,并在必要时创建指定的四个字段:create_tm、create_emp、update_tm 和 update_emp。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将下面脚本粘贴到tools->execute commands->edit/run script里运行。

 

 

Option   Explicit    
ValidationMode   =   True    
InteractiveMode   =   im_Batch    
  
Dim   mdl   '   the   current   model    
  
'   get   the   current   active   model    
Set   mdl   =   ActiveModel    
If   (mdl   Is   Nothing)   Then    
      MsgBox   "There   is   no   current   Model "    
ElseIf   Not   mdl.IsKindOf(PdPDM.cls_Model)   Then    
      MsgBox   "The   current   model   is   not   an   Physical   Data   model. "    
Else    
      ProcessFolder   mdl    
End   If    
  
Private   sub   ProcessFolder(folder)    
On Error Resume Next   
      Dim   Tab   'running     table    
      for   each   Tab   in   folder.tables    
            if   not   tab.isShortcut   then    
                  'if tab.comment="" then
                     'tab.comment   =  tab.name
                  'end if   
                  Dim isNeedAdd:isNeedAdd = true
                  Dim   col   '   running   column    
                  for   each   col   in   tab.columns    
                     if col.code="create_tm" then   
                         isNeedAdd = false   
                     end if  
                  next
                  if isNeedAdd then
                     Set col = tab.columns.CreateNew '创建一列/字段
                     If not col is Nothing then
                        col.name = "create_tm"
                        col.code = "create_tm"
                        col.DataType = "datetime"
                        output " col.name: " + col.name
                     End If
                     set col = nothing
                     
                     Set col = tab.columns.CreateNew '创建一列/字段
                     If not col is Nothing then
                        col.name = "create_emp"
                        col.code = "create_emp"
                        col.DataType = "varchar(32)"
                        output " col.name: " + col.name
                     End If
                     set col = nothing
                     
                     Set col = tab.columns.CreateNew '创建一列/字段
                     If not col is Nothing then
                        col.name = "update_tm"
                        col.code = "update_tm"
                        col.DataType = "datetime"
                        output " col.name: " + col.name
                     End If
                     set col = nothing
                     
                     Set col = tab.columns.CreateNew '创建一列/字段
                     If not col is Nothing then
                        col.name = "update_emp"
                        col.code = "update_emp"
                        col.DataType = "varchar(32)"
                        output " col.name: " + col.name
                     End If
                     set col = nothing
                  end if  
            end   if    
      next    
  
      Dim   view   'running   view    
      for   each   view   in   folder.Views    
            if   not   view.isShortcut   then    
                  view.name   =   view.comment    
            end   if    
      next    
  
      '   go   into   the   sub-packages    
      Dim   f   '   running   folder    
      For   Each   f   In   folder.Packages    
            if   not   f.IsShortcut   then    
                  ProcessFolder   f    
            end   if    
      Next    
end   sub 
 

 

create table address_book ( id bigint auto_increment comment '主键' primary key, user_id bigint not null comment '用户id', consignee varchar(50) null comment '收货', sex varchar(2) null comment '性别', phone varchar(11) not null comment '手机号', city varchar(32) charset utf8mb4 null comment '城市', shippingAddress varchar(200) charset utf8mb4 null comment '详细地址', label varchar(100) charset utf8mb4 null comment '标签', is_default tinyint(1) default 0 not null comment '默认 0 否 1是' ) comment '地址簿' collate = utf8mb3_bin; create table category ( id bigint auto_increment comment '主键' primary key, name varchar(32) not null comment '分类名称', sort int default 0 not null comment '顺序', create_time datetime null comment '创建时间', constraint idx_category_name unique (name) ) comment '物品分类' collate = utf8mb3_bin; create table employee ( id bigint auto_increment comment '主键' primary key, name varchar(32) not null comment '姓名', username varchar(32) not null comment '用户名', password varchar(64) not null comment '密码', phone varchar(11) not null comment '手机号', sex varchar(2) not null comment '性别', status int default 1 not null comment '状态 0:禁用,1:启用', create_time datetime null comment '创建时间', update_time datetime null comment '更新时间', create_user bigint null comment '创建人', update_user bigint null comment '修改', constraint idx_username unique (username) ) comment '员工信息' collate = utf8mb3_bin; create table user ( id bigint auto_increment comment '主键' primary key, username varchar(32) not null comment '姓名', phone varchar(11) null comment '手机号', sex varchar(2) null comment '性别', create_time datetime null comment '注册时间', status int default 1 not null comment '0禁用 1启用', password varchar(32) not null comment '密码' ) comment '用户信息' collate = utf8mb3_bin; create table goods ( id bigint auto_increment comment '主键' primary key, name varchar(32) not null comment '物品名称', category_id bigint not null comment '物品分类id', price decimal(10, 2) null comment '物品价格', image varchar(255) null comment '图片', description varchar(255) null comment '描述信息', create_time datetime null comment '创建时间', update_time datetime null comment '更新时间', create_user varchar(32) null comment '创建人', update_user varchar(32) null comment '修改', adress varchar(32) null comment '详细地址', user_id bigint not null comment '用户id', phone varchar(11) null comment '电话号码', remark varchar(64) not null comment '备注信息', constraint fk_goods_category foreign key (category_id) references category (id), constraint fk_goods_user foreign key (user_id) references user (id) on delete cascade ) comment '物品' collate = utf8mb3_bin; create table orders ( id bigint auto_increment comment '主键' primary key, status int default 1 not null comment '状态:0待回收 1进行中 2已完成 3已取消', user_id bigint not null comment '用户id', goods_id bigint not null comment '物品id', phone varchar(11) null comment '手机号', address varchar(255) null comment '地址', user_name varchar(32) null comment '用户名称', remark varchar(64) null comment '备注信息', price decimal(10, 2) null comment '价格', image varchar(255) null comment '图片展示', category_id bigint not null comment '分类id', goods_name varchar(32) null comment '物品名称', constraint fk_orders_user foreign key (user_id) references user (id) on delete cascade ) comment '订单' collate = utf8mb3_bin; create index orders___goods on orders (goods_id); 可以帮我把这些联系起来然后生成er图发给我吗
07-23
CREATE TABLE school ( school_id INT PRIMARY KEY AUTO_INCREMENT, -- 学校id school_name VARCHAR(100) NOT NULL UNIQUE -- 学校名称 ); CREATE TABLE user ( user_id INT PRIMARY KEY AUTO_INCREMENT, -- 用户id,主键 username VARCHAR(50) NOT NULL UNIQUE, -- 登录用户名 password VARCHAR(100) NOT NULL, -- 用户密码 school_id INT, -- 所属学校ID role VARCHAR(20) NOT NULL, -- 用户角色(学生/教职工/商业士) gender VARCHAR(10), -- 性别 qq VARCHAR(200), -- qq email VARCHAR(200), -- email register_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 注册时间 login_count INT DEFAULT 0, -- 登录次数 space_click INT DEFAULT 0, -- 空间点击量 avatar_url VARCHAR(255), -- 头像图片路径 space_title VARCHAR(100), -- 个空间标题 space_intro TEXT, -- 个空间介绍 FOREIGN KEY (school_id) REFERENCES school(school_id) ON DELETE SET NULL -- 外键关联学校 ) ; CREATE TABLE blog ( blog_id INT PRIMARY KEY AUTO_INCREMENT, -- 日志id, 主键 user_id INT NOT NULL, -- 日志作者ID blog_title VARCHAR(200) NOT NULL, -- 日志标题 category VARCHAR(50) NOT NULL, -- 日志分类 content TEXT NOT NULL, -- 日志正文 image_url VARCHAR(255), -- 日志配图 publish_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 发时间 view_count INT DEFAULT 0, -- 日志查看次数 FOREIGN KEY (user_id) REFERENCES user(user_id) ON DELETE CASCADE -- 外键关联用户 ); CREATE TABLE album ( album_id INT PRIMARY KEY AUTO_INCREMENT, -- 相册/照片唯一标识,自增 user_id INT NOT NULL, -- 所有者ID,关联用户 name VARCHAR(100) NOT NULL, -- 相册名/照片名 perm VARCHAR(30) DEFAULT '所有', -- 浏览权限(所有/好友/同校) upload_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建/上传时间默认当前时间 FOREIGN KEY (user_id) REFERENCES user(user_id) ON DELETE CASCADE -- 外键关联用户 ) ; CREATE TABLE circle ( circle_id INT PRIMARY KEY AUTO_INCREMENT, -- 圈子id,主键 c_creator_id INT NOT NULL, -- 创建id name VARCHAR(100) NOT NULL, -- 圈子名称 intro TEXT, -- 圈子简介 category VARCHAR(50), -- 圈子分类 join_type VARCHAR(20) DEFAULT '自由加入', -- 加入方式 create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建时间 FOREIGN KEY (c_creator_id) REFERENCES user(user_id) ON DELETE CASCADE -- 外键关联创建); CREATE TABLE activity ( act_id INT PRIMARY KEY AUTO_INCREMENT, -- 活动id a_creator_id INT NOT NULL, -- 创建ID title VARCHAR(200) NOT NULL, -- 活动主题 content TEXT NOT NULL, -- 活动详细内容 start_time DATETIME NOT NULL, -- 活动开始时间 end_time DATETIME NOT NULL, -- 活动结束时间 location VARCHAR(200) NOT NULL, -- 活动地点 FOREIGN KEY (a_creator_id) REFERENCES user(user_id) ON DELETE CASCADE -- 外键关联创建) ; 给我er图
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值