简体   繁体   English

在PostgreSQL中创建多个表

[英]Creating Multiple Tables in PostgreSQL

I want to create multiple tables (more than 100 tables) in one go. 我想一次创建多个表(超过100个表)。 I am trying to write a script using loop such as for loop for generating as many tables as I want. 我正在尝试使用诸如for循环之类的循环编写脚本,以生成所需的多个表。 I am new in PostgreSQL. 我是PostgreSQL的新手。 I would be grateful if someone could give me some tips. 如果有人可以给我一些提示,我将不胜感激。

#!/bin/sh

(
for i in 0 1 2 3 4 5 6 7 8 9; do
for j in 0 1 2 3 4 5 6 7 8 9; do
        echo "SET search_path=tmp;"
        echo "CREATE TABLE barf${i}${j}"
        echo " ( id SERIAL NOT NULL PRIMARY KEY );"
done
done) | psql -U lutsername databasename

My answer here has to be 'dont do it!' 我在这里的答案必须是“不要这样做!” and change your architechture. 并更改您的建筑。 Create a table called customer and have a customer_id along with some other info. 创建一个名为customer的表,并具有一个customer_id以及一些其他信息。 Create a second table with the columns you want here and a 'customer_id' column to refer to the customer table. 创建第二个表,其中包含您要在此处创建的列以及一个“ customer_id”列以引用客户表。 This format will allow you to store in two tables what you are trying to store in 100+ tables. 这种格式将允许您将要存储在100个以上表中的内容存储在两个表中。 You want to stardardize your data base design at this stage...otherwise you are setting yourself up for a nightmare in the not too distant future. 您希望在此阶段标准化您的数据库设计...否则,您将在不久的将来陷入一场噩梦。 Databases are not spreadsheets... 数据库不是电子表格...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM