AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 43042
Accepted
nerdinand
nerdinand
Asked: 2013-05-25 01:22:54 +0800 CST2013-05-25 01:22:54 +0800 CST 2013-05-25 01:22:54 +0800 CST

如何在 MySQL Workbench 中一次将列添加到多个表?

  • 772

我有一个属于 Ruby on Rails 应用程序的 MySQL Workbench 模型(图表)。我现在需要在每个表中添加created_at和updated_at时间戳列。什么是快速简便的方法(而不是手动操作)?我的客户不想使用 Rails 迁移,而是使用 Workbench 作为权威数据库模式,所以我坚持在 Workbench 中执行此操作。

mysql python
  • 2 2 个回答
  • 3866 Views

2 个回答

  • Voted
  1. Best Answer
    nerdinand
    2013-05-25T01:22:54+08:002013-05-25T01:22:54+08:00

    您可以在 MySQL Workbench 的 Scripting Shell 中使用此 Python 脚本。它是此处找到的脚本的改进版本:http: //mysqlworkbench.org/2012/06/mysql-workbench-script-for-adding-columns-to-all-tables-in-a-model/

    # define your columns and types here
    columns_types = {"created_at" : "DATETIME", "updated_at" : "DATETIME"}
    
    # tables you want to skip
    skip_tables = ["main_defs", "main_menu_items", "delayed_jobs", "delayed_job_workers", "delayed_job_logs"]
    
    
    
    # get a reference to the schema in the model. This will get the 1st schema in it.
    schema = grt.root.wb.doc.physicalModels[0].catalog.schemata[0]
    
    # iterate through all tables
    for table in schema.tables:
    
        # skip the current table if it is in skip_tables
        if table.name in skip_tables:
            continue
    
        # iterate through all columns to be added
        for column_name, type in columns_types.items():
    
            # skip this column_name if there is already a column with this name
            column_names = [x.name for x in table.columns]
            if column_name in column_names:
                continue
    
            # create a new column object and set its name
            column = grt.classes.db_mysql_Column()
            column.name = column_name
            # add it to the table
            table.addColumn(column)
            # set the datatype of the column
            column.setParseType(type, None)
    
    • 2
  2. NikDP
    2013-05-25T01:26:34+08:002013-05-25T01:26:34+08:00

    在 ruby​​ on rails 中,您不必分别创建 created_at 和 updated_at 列。当您运行t.timestamps迁移文件中编写的迁移时,会在表中创建created_at和updated_at列。例如

    class CreateStudents < ActiveRecord::Migration
      def change
        create_table :students do |t|
          t.string :name
          t.timestamps
        end
      end
    end
    

    这将创建一个带有列的学生表id, name, created_at and updated_at。

    • 0

相关问题

  • 是否有任何 MySQL 基准测试工具?[关闭]

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

  • 什么时候是使用 MariaDB 而不是 MySQL 的合适时机,为什么?

  • 组如何跟踪数据库架构更改?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    授予用户对所有表的访问权限

    • 5 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    pedrosanta 使用 psql 列出数据库权限 2011-08-04 11:01:21 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve