Build a Celestial Bodies Database, with bash f-string-like solution

Hello there,

I want to practise and use bash to finish the challenge ‘Build a Celestial Bodies Database’. I want to do something like:

lst_uni=(galaxy star planet moon)
for i in “${lst_uni[@]}”; do
echo “$i”
add_prim=$($PSQL “ALTER TABLE $i ADD COLUMN $i_id SERIAL PRIMARY KEY”)
done

$i_id is obviously not the correct answer. Could anyone help? Thanks in advance

To add a unique ID column to each table in your list, you could use a variable to store the name of the ID column and modify the ALTER TABLE command accordingly
#!/bin/bash

PSQL=“psql --username=freecodecamp --dbname=celestial_bodies”

lst_uni=(“galaxy” “star” “planet” “moon”)
id_column_name=“${table_name}_id”

for table_name in “${lst_uni[@]}”
do
echo “Adding $id_column_name column to $table_name table”
add_column=$($PSQL -c “ALTER TABLE $table_name ADD COLUMN $id_column_name SERIAL PRIMARY KEY;”)
done

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.