简体   繁体   中英

Creating Multiple Tables in PostgreSQL

I want to create multiple tables (more than 100 tables) in one go. I am trying to write a script using loop such as for loop for generating as many tables as I want. I am new in 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. Create a second table with the columns you want here and a 'customer_id' column to refer to the customer table. This format will allow you to store in two tables what you are trying to store in 100+ tables. 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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