Welcome to the SQL Lessons!
The first thing you need to do is start the terminal. Do that by clicking the "hamburger" menu at the top left of the screen, going to the "terminal" section, and clicking "new terminal". Once you open a new one, type echo hello SQL
into the terminal and press enter.
- Capitalization matters
- If the tests don't run automatically, try typing
exit
into the terminal and redoing the instructions
You are started with two .csv
files with info about your computer science students. You should take a look at them. The top row in each file has titles, and the rest are values for those titles. You will be adding all that info to a PostgreSQL database. Log into the psql interactive terminal with psql --username=freecodecamp --dbname=postgres
to get started.
- Type
psql --username=freecodecamp --dbname=postgres
into the terminal and press enter
View the existing databases with the \l
shortcut command to see what's here.
- Type
\l
into the psql prompt and press enter
All the info from the CSV files will go into a single database. Create a new database named students
.
- Use the
CREATE DATABASE
keywords - Here's an example:
CREATE DATABASE <database_name>;
- Type
CREATE DATABASE students;
into the psql prompt and press enter
View the databases again to make sure it got created.
- Use the list shortcut command in the psql prompt
- Type
\l
into the psql prompt and press enter
There it is. Connect to your new database so you can start adding tables.
- Use the
\c
shortcut command - Here's an example:
\c <database_name>
- Type
\c students
in the psql prompt
The CSV files have a bunch of students with info about them, and some courses and majors. You will have four tables. One for the students and their info, one for each major, another for each course, and a final one for showing what courses are included in each major. First, create the students
table.
- Use the
CREATE TABLE
keywords - There should be parenthesis after the table name
- Here's an example:
CREATE TABLE <table_name>();
- Type
CREATE TABLE students();
into the psql prompt
The second table will be for each unique major that appears in the data. Create a table named majors
.
- Use the
CREATE TABLE
keywords - There should be parenthesis after the table name
- Here's an example:
CREATE TABLE <table_name>();
- Type
CREATE TABLE majors();
into the psql prompt
The third table is for each unique course in the data. Create another table named courses
.
- Use the
CREATE TABLE
keywords - There should be parenthesis after the table name
- Here's an example:
CREATE TABLE <table_name>();
- Type
CREATE TABLE courses();
into the psql prompt
The final table will be a junction table for the majors and courses. Create it with the name majors_courses
.
- Use the
CREATE TABLE
keywords - There should be parenthesis after the table name
- Here's an example:
CREATE TABLE <table_name>();
- Type
CREATE TABLE majors_courses();
into the psql prompt
Use the display shortcut command to view your tables to make sure your satisfied with them.
- It's the
\d
shortcut command - Type
\d
in the psql prompt
Onto the columns. The students.csv
file has four fields, you will make a column for each of those as well as an ID column. Add a column to your students
table named student_id
. Give it a type of SERIAL
so it automatically increments and make it a PRIMARY KEY
- Use the
ALTER TABLE
,ADD COLUMN
,SERIAL
andPRIMARY KEY
keywords - Here's an example:
ALTER TABLE <table_name> ADD COLUMN <column_name> <TYPE> <CONSTRAINTS>;
- Type
ALTER TABLE students ADD COLUMN student_id SERIAL PRIMARY KEY;
into the psql prompt - You can drop a column with
ALTER TABLE <table_name> DROP COLUMN <column_name>;
if you want to delete a column and try again
The first column in students.csv
is first_name
. Add a column to the students
table with that name. Make it a type of VARCHAR(50)
and give it the NOT NULL
constraint.
- Use the
ALTER TABLE
,ADD COLUMN
,VARCHAR()
andNOT NULL
keywords - Here's an example:
ALTER TABLE <table_name> ADD COLUMN <column_name> <DATA_TYPE> <CONSTRAINTS>;
- Type
ALTER TABLE students ADD COLUMN first_name VARCHAR(50) NOT NULL;
into the psql prompt - You can drop a column with
ALTER TABLE <table_name> DROP COLUMN <column_name>;
if you want to delete a column and try again
The next column in the data is last_name
. Add it to the students
table. Give it the same data type and max-length as first_name
and make sure it has the NOT NULL
constraint.
- Use the
ALTER TABLE
,ADD COLUMN
,VARCHAR()
andNOT NULL
keywords - The max-length should be
50
- Here's an example:
ALTER TABLE <table_name> ADD COLUMN <column_name> <DATA_TYPE> <CONSTRAINTS>;
- Type
ALTER TABLE students ADD COLUMN last_name VARCHAR(50) NOT NULL;
into the psql prompt - You can drop a column with
ALTER TABLE <table_name> DROP COLUMN <column_name>;
if you want to delete a column and try again
The next column is for the major. Since you will have each major in another table this column will be a foreign key that references it. Create a column in the students
table named major_id
, give it a data type of INT
for now. You will come back and set the foreign key later.
- Use the
ALTER TABLE
,ADD COLUMN
, andINT
keywords - Here's an example:
ALTER TABLE <table_name> ADD COLUMN <column_name> <DATA_TYPE>;
- Type
ALTER TABLE students ADD COLUMN major_id INT;
into the psql prompt - You can drop a column with
ALTER TABLE <table_name> DROP COLUMN <column_name>;
if you want to delete a column and try again
Create the last column, gpa
. The data in the CSV shows that they are decimals with a length of 2
and 1
number is to the right of the decimal. So give it a data type of NUMERIC(2,1)
.
- Use the
ALTER TABLE
,ADD COLUMN
, andNUMERIC()
keywords - Here's an example:
ALTER TABLE <table_name> ADD COLUMN <column_name> <DATA_TYPE>;
- Type
ALTER TABLE students ADD COLUMN gpa NUMERIC(2,1);
into the psql prompt - You can drop a column with
ALTER TABLE <table_name> DROP COLUMN <column_name>;
if you want to delete a column and try again
Use the shortcut command to display the details of the students
table to make sure you like it.
- It's the
\d
shortcut command - Add the table name after the command
- Here's an example:
\d <table_name>
- Type
\d students
in the psql prompt
The foreign key is still missing. Let's fill in the majors
table next. Add a major_id
column to it. Make it a type of SERIAL
and the PRIMARY KEY
for this table.
- Use the
ALTER TABLE
,ADD COLUMN
,SERIAL
andPRIMARY KEY
keywords - Here's an example:
ALTER TABLE <table_name> ADD COLUMN <column_name> <TYPE> <CONSTRAINTS>;
- Type
ALTER TABLE majors ADD COLUMN major_id SERIAL PRIMARY KEY;
into the psql prompt - You can drop a column with
ALTER TABLE <table_name> DROP COLUMN <column_name>;
if you want to delete a column and try again
This table will only have one other column for the name of the major. Add a column to it named major
. Make it a VARCHAR
with a max-length of 50
and give it the NOT NULL
constraint.
- Use the
ALTER TABLE
,ADD COLUMN
,VARCHAR()
andNOT NULL
keywords - The max-length of
50
should go in the parenthesis ofVARCHAR
- Here's an example:
ALTER TABLE <table_name> ADD COLUMN <column_name> <DATA_TYPE> <CONSTRAINTS>;
- Type
ALTER TABLE majors ADD COLUMN major VARCHAR(50) NOT NULL;
into the psql prompt - You can drop a column with
ALTER TABLE <table_name> DROP COLUMN <column_name>;
if you want to delete a column and try again
View the details of the majors table to make sure you like it.
- Use the display shortcut command
- Add the table name after the command
- It's the
\d
command - Here's an example:
\d <table_name>
- Type
\d majors
into the psql prompt
This table looks good. Now, set the major_id
column from the students
table as a foreign key that references the major_id
column from the majors
table. Here's an example of how to do that: ALTER TABLE <table_name> ADD FOREIGN KEY(<column_name>) REFERENCES <referenced_table_name>(<referenced_column_name>);
- Type
ALTER TABLE students ADD FOREIGN KEY(major_id) REFERENCES majors(major_id);
in the psql prompt
View the details of the students
table again to make sure the key is there.
- Use the display shortcut command
- Add the table name after the command
- It's the
\d
command - Here's an example:
\d <table_name>
- Type
\d students
into the psql prompt
Next, is the courses
table. Add a course_id
column to it. Give it a type of SERIAL
and make it the primary key.
- Use the
ALTER TABLE
,ADD COLUMN
,SERIAL
andPRIMARY KEY
keywords - Here's an example:
ALTER TABLE <table_name> ADD COLUMN <column_name> <TYPE> <CONSTRAINTS>;
- Type
ALTER TABLE courses ADD COLUMN course_id SERIAL PRIMARY KEY;
into the psql prompt - You can drop a column with
ALTER TABLE <table_name> DROP COLUMN <column_name>;
if you want to delete a column and try again
Add a course
column to the courses
table that's a type of VARCHAR
. The course names are a little longer, so give them a max-length of 100
. Also, make sure it can't accept null values.
- Use the
ALTER TABLE
,ADD COLUMN
,VARCHAR()
andNOT NULL
keywords - The max-length of
100
should go in the parenthesis ofVARCHAR
- Here's an example:
ALTER TABLE <table_name> ADD COLUMN <column_name> <DATA_TYPE> <CONSTRAINTS>;
- Type
ALTER TABLE courses ADD COLUMN course VARCHAR(100) NOT NULL;
into the psql prompt - You can drop a column with
ALTER TABLE <table_name> DROP COLUMN <column_name>;
if you want to delete a column and try again
View the details of the courses table to make sure it looks good.
- Use the display shortcut command
- Add the table name after the command
- It's the
\d
command - Here's an example:
\d <table_name>
- Type
\d courses
into the psql prompt
One more table to go. The majors_courses
junction table will have two columns, each referencing the primary key from two related table. First, add a major_id
column to it. Just give it a type of INT
for now.
- Use the
ALTER TABLE
,ADD COLUMN
, andINT
keywords - Here's an example:
ALTER TABLE <table_name> ADD COLUMN <column_name> <DATA_TYPE>;
- Type
ALTER TABLE majors_courses ADD COLUMN major_id INT;
into the psql prompt - You can drop a column with
ALTER TABLE <table_name> DROP COLUMN <column_name>;
if you want to delete a column and try again
Set the major_id
column you just created as a foreign key that references the major_id
column from the majors
table.
- Use the
ALTER TABLE
,ADD FOREIGN KEY
, andREFERENCES
keywords - Here's an example:
ALTER TABLE <table_name> ADD FOREIGN KEY(<column_name>) REFERENCES <referenced_table_name>(<referenced_column_name>);
- You previously used:
ALTER TABLE students ADD FOREIGN KEY(major_id) REFERENCES majors(major_id);
- Type
ALTER TABLE majors_courses ADD FOREIGN KEY(major_id) REFERENCES majors(major_id);
into the psql prompt
Next, add a course_id
column to the same table. Just give it a type of INT
again for now.
- It goes in the
majors_courses
table - Use the
ALTER TABLE
,ADD COLUMN
, andINT
keywords - Here's an example:
ALTER TABLE <table_name> ADD COLUMN <column_name> <DATA_TYPE>;
- Type
ALTER TABLE majors_courses ADD COLUMN course_id INT;
into the psql prompt - You can drop a column with
ALTER TABLE <table_name> DROP COLUMN <column_name>;
if you want to delete a column and try again
Set your new course_id
column as a foreign key that references the other course_id
column.
- The referenced column is
course_id
from thecourses
table - Use the
ALTER TABLE
,ADD FOREIGN KEY
, andREFERENCES
keywords - Here's an example:
ALTER TABLE <table_name> ADD FOREIGN KEY(<column_name>) REFERENCES <referenced_table_name>(<referenced_column_name>);
- You previously used:
ALTER TABLE students ADD FOREIGN KEY(major_id) REFERENCES majors(major_id);
- Type
ALTER TABLE majors_courses ADD FOREIGN KEY(course_id) REFERENCES courses(course_id);
into the psql prompt
View the details of the table you just worked on to make sure the structure is finished.
- Use the display shortcut command
- Add the table name after the command
- It's the
\d
command - Here's an example:
\d <table_name>
- Type
\d courses
into the psql prompt
There's one thing missing. This table doesn't have a primary key. The data from courses.csv
will go in this table. A single major will be in it multiple times, and same with a course. So neither of them can be a primary key. But there will never be a row with the same two values as another row. So the two columns together, are unique. You can create a composite primary key that uses more than one column as a unique pair like this: ALTER TABLE <table_name> ADD PRIMARY KEY(<column_name>, <column_name>);
Add a composite primary key to the table using the two columns.
- It's the
major_id
andcourse_id
columns from themajors_courses
table - Type
ALTER TABLE majors_courses ADD PRIMARY KEY(major_id, course_id);
into the psql prompt
View the details of the table again.
- Use the display shortcut command
- Add the table name after the command
- It's the
\d
command - Here's an example:
\d <table_name>
- Type
\d courses
into the psql prompt
Okay, now it's finished. View all the tables you ended up with.
- Use the display shortcut command
- Don't include a table name after the command
- It's the
\d
command - Type
\d
into the psql prompt
Next, you can start adding some info. Since the students
table needs a major_id
, you can add a major first. View the details of the majors
table to see what info it expects.
- Use the display shortcut command
- Add the table name after the command
- It's the
\d
command - Here's an example:
\d <table_name>
- Type
\d majors
into the psql prompt
It only needs the name of a major. The ID will be added automatically. Add the first major from the courses.csv
file into the majors
table. It's a VARCHAR
, so make sure to put the value in single quotes.
- The major is
Database Administration
- Use the
INSERT INTO
andVALUES
keywords - Here's an example:
INSERT INTO <table_name>(<column_name>) VALUES(<value>);
- Type
INSERT INTO majors(major) VALUES('Database Administration');
Use SELECT
to view all the data in the majors
table to make sure it got inserted correctly.
- Use the
SELECT
andFROM
keywords with*
to view all the columns - Here's an example:
SELECT <columns> FROM <table_name>;
- Type
SELECT * FROM majors;
into the psql prompt
Next, insert the first course from courses.csv
into the courses
table.
- Use the
INSERT INTO
andVALUES
keywords - View the details of the table with
\d courses
to see what it expects - The course name is
Data Structures and Algorithms
- Make sure to put
VARCHAR
values in single quotes - Here's an example:
INSERT INTO <table_name>(<column_name>) VALUES(<value>);
- Type
INSERT INTO courses(course) VALUES('Data Structures and Algorithms');
View all the data in the courses
table to make sure it got added.
- Use the
SELECT
andFROM
keywords with*
to view all the columns - Here's an example:
SELECT <columns> FROM <table_name>;
- Type
SELECT * FROM courses;
into the psql prompt
Next, you can add a row into the junction table. View the details of it to see what it expects.
- It's the
majors_courses
table - Use the display shortcut command
- Add the table name after the command
- It's the
\d
command - Here's an example:
\d <table_name>
- Type
\d majors_courses
into the psql prompt
It wants a major_id
and course_id
. Add a row to majors_courses
for the first entry in courses.csv
.
- Use
SELECT * FROM <table_name>;
to find the two ID's you already added - Use the
INSERT INTO
andVALUES
keywords - Here's an example:
INSERT INTO <table_name>(<column_1_name>, <column_2_name>) VALUES(<value_1>, <value_2>);
- Type
INSERT INTO majors_courses(major_id, course_id) VALUES(1, 1);
Note that your ID's may differ. You can reset the lesson to ensure they are the same
View all the data in the table you just added to.
- Use the
SELECT
andFROM
keywords with*
to view all the columns - Here's an example:
SELECT <columns> FROM <table_name>;
- Type
SELECT * FROM majors_courses;
into the psql prompt
Looks like the row got added. View the details of the students
table to remind yourself what it expects so you can add the first student to the database.
- Use the display shortcut command
- Add the table name after the command
- It's the
\d
command - Here's an example:
\d <table_name>
- Type
\d students
into the psql prompt
The output shows what the table needs. Insert the first person from students.csv
into the students
table.
- Enter
SELECT * FROM majors;
to find themajor_id
for the students major - Use the
INSERT INTO
andVALUES
keywords - Make sure to put
VARCHAR
values in single quotes - Here's an example:
INSERT INTO <table_name>(<column_1_name>, <column_N_name>) VALUES(<value_1>, <value_N>);
- The first part to insert the student looks like this:
INSERT INTO students(first_name, last_name, major_id, gpa)
- Type
INSERT INTO students(first_name, last_name, major_id, gpa) VALUES('Rhea', 'Kellems', 1, 2.5);
into the psql prompt
Looks like it worked. View all the data in the students table to make sure.
- Use the
SELECT
andFROM
keywords with*
to view all the columns - Here's an example:
SELECT <columns> FROM <table_name>;
- Type
SELECT * FROM students;
into the psql prompt
Okay, you added a row into each table. It might be wise to review the data and the database structure. Adding the rest of the info one at a time would be tedious. You are going to make a script to do it for you. I recommend "splitting" the terminal for this part. You can do that by clicking the "hamburger" menu at the top left of the window, going to the "Terminal" menu, and clicking "Split Terminal". Once you've done that, use the touch
command to create a file named insert_data.sh
in your project
folder.
- You cannot use regular terminal commands in the psql prompt. So follow the instructions to split the terminal. Then, type
touch insert_data.sh
in the new terminal and press enter - Make sure you are in the
project
folder first - You can get there by entering
cd ~/project
in the terminal
You should have two terminals open. One connected to PostgreSQL, and one for entering terminal commands. In the one for terminal commands, use the chmod
command with the +x
flag to give you new script executable permissions.
- Here's an example:
chmod +x <filename>
- Type
chmod +x insert_data.sh
in the terminal and press enter - Make sure it's the regular terminal and not the psql one
- You can log back in to psql with
psql --username=freecodecamp --dbname=students
Open your new file and add a "shebang" that uses bash
at the top. It looks like this: #!/bin/bash
.
- Add the text,
#!/bin/bash
to yourinsert_data.sh
file
Below that, add a single line comment with the text, Script to insert data from courses.csv and students.csv into students database
.
- A comment look like this:
# <comment>
- Add
# Script to insert data from courses.csv and students.csv into students database
below the "shebang" in yourinsert_data.sh
file
First, you should add all the info from the courses.csv
file since you need the major_id
for inserting the student info. cat
is a terminal command for printing the contents of a file. Here's an example: cat <filename>
. Below the comment you added, use it to print courses.csv
.
- Add
cat courses.csv
to yourinsert_data.sh
file below your comment
Run your script to see if the file contents get printed.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - Make sure you are in the
project
folder first
It worked. Instead of printing the content, you can pipe that output into a while loop so you can go through the rows one at a time. It looks like this:
cat courses.csv | while read MAJOR COURSE
do
<STATEMENTS>
done
Each new line will be read into the variables, MAJOR
and COURSE
. Add the above to your cat
command. In the STATEMENTS
area, use echo
to print the MAJOR
variable.
- You can print the suggested variable with
echo $MAJOR
- The whole loop should look like this:
cat courses.csv | while read MAJOR COURSE
do
echo $MAJOR
done
Run the script to see if it worked.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - Make sure you are in the
project
folder first
It's looping, but the MAJOR
variable is only being set to the first word. There's a default IFS
variable in bash. IFS stands for "Internal Field Separator". View it with declare -p IFS
.
- Enter
declare -p IFS
in the terminal
This variable is used to determine word boundaries. It defaults to spaces, tabs, and new lines. This is why the MAJOR
variable was set to only the first word on each line from the data. Between the while
and read
commands, set the IFS
to a comma like this: IFS=","
- Here's how it looks:
cat courses.csv | while IFS="," read MAJOR COURSE
do
echo $MAJOR
done
Now, it should use the comma in the data to separate words instead of spaces. Run the script again to see if it's working.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - Make sure you are in the
project
folder first
Looks like that worked. It prints the whole major, including the space. Print the COURSE
variable on the same line as where you print MAJOR
to make sure it's all working.
- Change the
echo
line toecho $MAJOR $COURSE
- The whole loop should look like this:
cat courses.csv | while IFS="," read MAJOR COURSE
do
echo $MAJOR $COURSE
done
Run the script again to check.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - Make sure you are in the
project
folder first
Okay, your loop is working. You can use the MAJOR
and COURSE
variables to access the major or course when you need to insert data or query the database. Delete the echo line so you can figure out what to do next.
- Delete the
echo $MAJOR $COURSE
line
It helps to plan out what you want to happen. For each loop, you will want to add the major to the database if it isn't in there yet. Same for the course. Then add a row to the majors_courses
table. Add these single line comments in your loop in this order: get major_id
, if not found
, insert major
, get new major_id
, get course_id
, if not found
, insert course
, get new course_id
, insert into majors_courses
.
- Here's an example of a single comment:
# <comment>
- Add the nine suggested single line comments, each on their own line, in the order given
- It should look like this:
do
# get major_id
# if not found
# insert major
# get new major_id
# get course_id
# if not found
# insert course
# get new course_id
# insert into majors_courses
done
You used the psql
command to log in and interact with the database. You can use it to just run a single command and exit. Above your loop, add a PSQL
variable that looks like this: PSQL="psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c"
. This will allow you to query your database from your script. The important parts are the username
, dbname
, and the -c
flag that is for running a single command and exiting. The rest of the flags are for formatting.
- Add the suggested variable between your first comment and the loop
- The suggested area should look like this:
PSQL="psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c"
Now, you can query your database using the PSQL
variable like this: $($PSQL "<query_here>")
. Below the get major_id
comment in your loop, create a MAJOR_ID
variable. Set it equal to the result of a query that gets the major_id
of the current MAJOR
in the loop. Make sure to put your MAJOR
variable in single quotes.
- Here's an example of how it looks:
MAJOR_ID=$($PSQL "<query_here>")
- For the query, you want to use the
SELECT
,FROM
, andWHERE
keywords - Here's an example of how the query part looks:
SELECT <column_name> FROM <table_name> WHERE <condition>
- The condition you want is
major_id='$MAJOR'
- Here's how the query should look:
SELECT major_id FROM majors WHERE major='$MAJOR'
- Here's how the whole line should look:
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")
Below the variable you just created, use echo
to print it so you can see it's value when you run the script.
- Add
echo $MAJOR_ID
below theMAJOR_ID
variable you created
Run the script to see what happens.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - Make sure you are in the
project
folder first
So it went through each major from the CSV file and tried to find major_id
for each one from the database. Looks like it only found the one you manually inserted earlier. The rest were empty. Below your first if not found
comment, add an if
condition that checks if the MAJOR_ID
variable is empty. You can do that with this test: [[ -z $MAJOR_ID ]]
. Place the next two comments in the statements area of the if
.
- Here's an example of an
if
:
if CONDITION
then
STATEMENTS
fi
- Make sure your
insert major
andget new major_id
comments are in the statements area in that order - Here's how it should look:
if [[ -z $MAJOR_ID ]]
then
# insert major
# get new major_id
fi
The loop will go into this if
whenever a major isn't found. Here, you will want to insert the major and then get the new id. You will need the ID for inserting data into the majors_courses
table later. Below your insert major
comment, create an INSERT_MAJOR_RESULT
variable. Set it's value to a query that inserts the current major into the database. Don't forget to use single quotes around the value.
- Here's an example of how to query the database:
INSERT_MAJOR_RESULT=$($PSQL "<query_here>")
- For the query, you want to use the
INSERT INTO
, andVALUES
keywords - Here's an example of how the query part looks:
INSERT INTO <table_name>(<column_name>) VALUES(<value>)
- You want to insert the
$MAJOR
value - Here's how the query looks:
INSERT INTO majors(major) VALUES('$MAJOR')
- Here's how the whole line should look:
INSERT_MAJOR_RESULT=$($PSQL "INSERT INTO majors(major) VALUES('$MAJOR')")
Below the variable you just created, use echo
to print it.
- Add
echo $INSERT_MAJOR_RESULT
right below where you created it
Instead of running through all the data in the CSV file, you should make some test data. In the terminal, use the copy (cp
) command to copy the courses.csv
into a new file named courses_test.csv
.
- Here's an example:
cp <filename> <new_name>
- Type
cp courses.csv courses_test.csv
in the terminal and press enter - Make sure you are using the bash terminal and not the psql one
In your new file, remove all the data except for the first five lines. Make sure there's a single empty line at the bottom.
- Remove all but the first five lines from the
courses_test.csv
file - Or, replace everything in
courses_test.csv
with the first five lines fromcourses.csv
- Make sure there's one empty line at the bottom
- The
courses_test.csv
file should look like this:
major,course
Database Administration,Data Structures and Algorithms
Web Development,Web Programming
Database Administration,Database Systems
Data Science,Data Structures and Algorithms
Back in the insert_data.sh
script, change your cat
command to loop through the test file instead of the full one.
- Change your
cat
command tocat courses_test.csv
instead ofcat courses.csv
- The suggested line should look like this:
cat courses_test.csv | while IFS="," read MAJOR COURSE
Run the script. It will go through the test data and insert a major into the database each time it doesn't find one already there and print the MAJOR_ID
and INSERT_MAJOR_RESULT
variables.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - The
majors
table should have four rows after running the script. If it doesn't, there might be something wrong in the script. You can use the reset button to reset the lesson and run the script again
Looks like found an ID that was already in the database twice and inserted three new items into the database. You don't need to print the ID anymore so delete the echo $MAJOR_ID
line.
- Delete the
echo $MAJOR_ID
line
In the psql prompt, use SELECT
to view all the data from the majors
table to see what the script added.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM majors;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
I forgot you inserted Database Administration
earlier. The script ran and inserted major
from the top line of the file. Then it added the other two that weren't already in there. You can use TRUNCATE
to delete all data from a table. In the psql prompt, try to delete all the data in the majors table by entering TRUNCATE majors;
- Enter
TRUNCATE majors;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
It says you "cannot truncate a table referenced in a foreign key constraint." The students
and majors_courses
tables use the major_id
from majors
as a foreign key. So if you want to delete the data from majors
, you need to delete the data from those two tables at the same time. Use TRUNCATE
to delete the data from those three tables. Separate the tables with commas.
- Here's an example:
TRUNCATE <table_1>, <table_2>, <table_3>;
- The three tables you want are
majors
,students
, andmajors_courses
- Enter
TRUNCATE majors, students, majors_courses;
in the psql prompt
View all the data in the majors
table to make sure it's empty.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM majors;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Looks like it worked. View all the data in the majors_courses
table to see if that one is empty.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM majors_courses;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
It is, check the students
table.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Last, check the courses
table.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM courses;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
There should still be one entry in there. Use TRUNCATE
to delete all the data from the courses
table. You will need to truncate any tables that use a column from it as a foreign key at the same time.
- Here's an example:
TRUNCATE <table_1>, <table_2>;
- The tables you want are
courses
, andmajors_courses
- Enter
TRUNCATE courses, majors_courses;
in the psql prompt
View all the data in the courses
table again.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM courses;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Now the database is completely empty. Run the script again to see what gets inserted when the database is empty.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - The
majors
table should have four rows after running the script. If it doesn't, there might be something wrong in the script. You can use the reset button to reset the lesson and run the script again
It inserted four that time. In the psql prompt, view all the data in the majors
table.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM majors;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
You won't want to add the first line from the CSV file to the database since those are just titles. In your script, add an if
condition at the top of your loop that checks if $MAJOR != major
. Put all the existing code and comments in your loop in it's statements area so it only does any of it if it's not the first line.
- Here's an example of an
if
:
if [[ CONDITION ]]
then
STATEMENTS
fi
- Your loop area should look like this:
do
if [[ $MAJOR != major ]]
then
# get major_id
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")
# if not found
if [[ -z $MAJOR_ID ]]
then
# insert major
INSERT_MAJOR_RESULT=$($PSQL "INSERT INTO majors(major) VALUES('$MAJOR')")
echo $INSERT_MAJOR_RESULT
# get new major_id
fi
# get course_id
# if not found
# insert course
# get new course_id
# insert into majors_courses
fi
done
In the psql prompt, use TRUNCATE
to delete all the data in the majors
table.
- Make sure to delete data in the tables that use any of the
majors
columns as a foreign key at the same time - Here's an example:
TRUNCATE <table_1>, <table_2>;
- You need to truncate
majors
,students
, andmajors_courses
together - Enter
TRUNCATE majors, students, majors_courses;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
View all the data in majors
table to make sure it's empty.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM majors;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Run the script to make sure it's not adding the first line anymore.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - The
majors
table should have three rows after running the script. If it doesn't, there might be something wrong in the script. You can use the reset button to reset the lesson and run the script again
It only showed three inserts, that's a good sign. View all the data in majors
table to make sure it's three you want.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM majors;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
There's three unique majors in your test data. Those were the three added to the database, so it looks like it's working. Delete the line where you print INSERT_MAJOR_RESULT
.
- Delete the
echo $INSERT_MAJOR_RESULT
line
You want a nicer message when something get's inserted so it's more informative. Below your INSERT_MAJOR_RESULT
variable, add an if
statement that checks if the variable is equal to INSERT 0 1
, which was what it was printing. Use echo
to print Inserted into majors, $MAJOR
in the statements area of the if
.
- Make sure to put the test value (
INSERT 0 1
) in double quotes since it has spaces. - The condition you want is:
[[ $INSERT_MAJOR_RESULT == "INSERT 0 1" ]]
- The
echo
part looks like this:echo "Inserted into majors, $MAJOR"
- The whole thing should look like this:
if [[ $INSERT_MAJOR_RESULT == "INSERT 0 1" ]]
then
echo "Inserted into majors, $MAJOR"
fi
In the psql prompt, truncate the majors
table again so you can run the script and see the output.
- Here's an example:
TRUNCATE <table_1>, <table_2>;
- Make sure to delete data in the tables that use any of the
majors
columns as a foreign key at the same time - You need to truncate
majors
,students
, andmajors_courses
together - Enter
TRUNCATE majors, students, majors_courses;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Check to make sure the table is empty. Then, run the script.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - The
majors
table should have three rows after running the script. If it doesn't, there might be something wrong in the script. You can use the reset button to reset the lesson and run the script again
It's starting to come together. Below your get new major_id
comment, set the MAJOR_ID
variable to a query that gets the new major_id
from the database.
- Here's an example of how to query the database:
MAJOR_ID=$($PSQL "<query_here>")
- For the query, you want to use the
SELECT
,FROM
, andWHERE
keywords - Here's an example of how the query part looks:
SELECT <column_name> FROM <table_name> WHERE <condition>
- The condition you want is
major='$MAJOR'
- Here's how the query should look:
SELECT major_id FROM majors WHERE major='$MAJOR'
- Here's how the whole line should look:
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")
- Make sure it's in the
if [[ -z $MAJOR_ID ]]
statements area
So the script will insert the majors correctly. Next are the courses. It will be the same steps as for the majors. Below your get course_id
comment, add a COURSE_ID
variable that gets the course_id
from the database. Remember that your COURSE
variable will have the current course in the loop.
- Here's an example of how to query the database:
COURSE_ID=$($PSQL "<query_here>")
- For the query, you want to use the
SELECT
,FROM
, andWHERE
keywords - Here's an example of how the query part looks:
SELECT <column_name> FROM <table_name> WHERE <condition>
- The condition you want is
course_id='$COURSE'
- Here's how the query should look:
SELECT course_id FROM courses WHERE course='$COURSE'
- Here's how the whole line should look:
COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'")
It's the same as the majors, so below the second if not found
comment, add an if
statement that checks if the query was empty so you can insert the course if needed. Place the existing insert course
and get new course_id
comments in the statements area of the if
.
- Here's an example of an
if
:
if [[ CONDITION ]]
then
STATEMENTS
fi
- Make sure your
insert course
andget new course_id
comments are in the statements area in that order - Here's how it should look:
if [[ -z $COURSE_ID ]]
then
# insert course
# get new course_id
fi
Below the insert course
comment, create an INSERT_COURSE_RESULT
variable that inserts the course into the database.
- Check the table structure in the psql prompt with
\d courses
if you need to see the columns - Here's an example of how to query the database:
INSERT_COURSE_RESULT=$($PSQL "<query_here>")
- For the query, you want to use the
INSERT INTO
, andVALUES
keywords - Here's an example of how the query part looks:
INSERT INTO <table_name>(<column_name>) VALUES(<value>)
- You want to insert the
$COURSE
value - Here's how the query looks:
INSERT INTO courses(course) VALUES('$COURSE')
- Here's how the whole line should look:
INSERT_COURSE_RESULT=$($PSQL "INSERT INTO courses(course) VALUES('$COURSE')")
The variable should be INSERT 0 1
again if something gets inserted. Below the variable you just created, add an if
condition that checks if it is and print Inserted into courses, $COURSE
using echo
in it's statements area.
- The condition you want is:
[[ $INSERT_COURSE_RESULT == "INSERT 0 1" ]]
- The
echo
part looks like this:echo "Inserted into courses, $COURSE"
- The whole thing should look like this:
if [[ $INSERT_COURSE_RESULT == "INSERT 0 1" ]]
then
echo "Inserted into courses, $COURSE"
fi
In the psql prompt, truncate the data from the majors
table so you can run the script again.
- Here's an example:
TRUNCATE <table_1>, <table_2>;
- Make sure to delete data in the tables that use any of the
majors
columns as a foreign key at the same time - You need to truncate
majors
,students
, andmajors_courses
together - Enter
TRUNCATE majors, students, majors_courses;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Run the script to see if the courses get inserted into the database.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - The
majors
andcourses
tables should have three rows each after running the script. If they don't, there might be something wrong in the script. You can use the reset button to reset the lesson and run the script again
It looks like it worked. The test data has three unique courses, and three got added to the database. View the data in the courses
table to make sure they are correct.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM courses;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Excellent. Instead of manually deleting the data each time you want to run the script, add the command to do it for you. Near the top of the file below your PSQL
variable, use echo
to query the database. In the query, truncate your four tables in this order: students
, majors
, courses
, majors_courses
.
- Here's an example:
echo $($PSQL "<query_here>")
- The query you want looks like this:
TRUNCATE students, majors, courses, majors_courses
- The whole line should look like this:
echo $($PSQL "TRUNCATE students, majors, courses, majors_courses")
Run the script to see if it works.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - The
majors
andcourses
tables should have three rows each after running the script. If they don't, there might be something wrong in the script. You can use the reset button to reset the lesson and run the script again
Awesome. That makes it easier. Below your get new course_id
comment, set the COURSE_ID
to the newly inserted course_id
.
- Here's an example:
COURSE_ID=$($PSQL "<query_here>")
- For the query, you want to use the
SELECT
,FROM
, andWHERE
keywords - Here's an example of how the query part looks:
SELECT <column_name> FROM <table_name> WHERE <condition>
- The condition you want is
course_id='$COURSE'
- Here's how the query should look:
SELECT course_id FROM courses WHERE course='$COURSE'
- Here's how the whole line should look:
COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'")
- Make sure it's in the
if [[ -z $COURSE_ID ]]
statements area
One more thing to add for this file. Below the insert into majors_courses
courses comment, create a INSERT_MAJORS_COURSES_RESULT
variable. Use it and the MAJOR_ID
and COURSE_ID
variables you created to insert a row into the majors_courses
table. Make sure the query has the major_id
column first. Also, you won't need any quotes around the values for the ID's.
- Here's an example:
INSERT_MAJORS_COURSES_RESULT=$($PSQL "<query_here>")
- For the query, you want to use the
INSERT INTO
, andVALUES
keywords - Here's an example of how the query part looks:
INSERT INTO <table_name>(<column_name>) VALUES(<value>)
- Check the table structure in the psql prompt with
\d majors_courses
if you need to see the columns - You want to add values for the
major_id
andcourse_id
columns - The query you want is:
INSERT INTO majors_courses(major_id, course_id) VALUES($MAJOR_ID, $COURSE_ID)
- Here's how the whole line should look:
INSERT_MAJORS_COURSES_RESULT=$($PSQL "INSERT INTO majors_courses(major_id, course_id) VALUES($MAJOR_ID, $COURSE_ID)")
Below the variable you just created, add an if condition that checks if it's equal to INSERT 0 1
like the others. In it's statements area, use echo
to print Inserted into majors_courses, $MAJOR : $COURSE
.
- The condition you want is:
[[ $INSERT_MAJORS_COURSES_RESULT == "INSERT 0 1" ]]
- The
echo
part looks like this:echo "Inserted into majors_courses, $MAJOR : $COURSE"
- The whole thing should look like this:
if [[ $INSERT_MAJORS_COURSES_RESULT == "INSERT 0 1" ]]
then
echo "Inserted into majors_courses, $MAJOR : $COURSE"
fi
Run the script. Your tables should get truncated and then it should go through the loop and add all the data from the courses_test.csv
into the three tables of the database.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - After running the script, the
majors
andcourses
tables should have three rows each, and themajors_courses
table should have four. If they don't, there might be something wrong in the script. You can use the reset button to reset the lesson and run the script again
Looks like it works. You better look around to make sure. View the data in the majors
table.
- Use the psql prompt
- Use the
SELECT
andFROM
keywords with*
to view all the data in themajors
table - Enter
SELECT * FROM majors;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Cool, check the courses
table.
- Use the psql prompt
- Use the
SELECT
andFROM
keywords with*
to view all the data in thecourses
table - Enter
SELECT * FROM courses;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Lastly, view the data in the majors_courses
table. There should be four rows.
- Use the psql prompt
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM majors_courses;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Alright, that part of the script is done. Next, you need to add everything from the students.csv
file. Make some test data again. In the terminal, use the copy command to copy students.csv
into a file named students_test.csv
.
cp
is the copy command- Here's an example:
cp <filename> <new_name>
- Enter
cp students.csv students_test.csv
in the terminal
In the students_test.csv
file, remove everything but the first five lines like you did for the other test file. Make sure there's an empty line at the bottom again.
- Remove all but the first five lines from the
students_test.csv
file - Or, replace everything in
students_test.csv
with the first five lines fromstudents.csv
- Make sure there's one empty line at the bottom
- The
students_test.csv
file should look like this:
first_name,last_name,major,gpa
Rhea,Kellems,Database Administration,2.5
Emma,Gilbert,null,null
Kimberly,Whitley,Web Development,3.8
Jimmy,Felipe,Database Administration,3.7
You want to loop through all this info like you did for the other CSV file. The process is the same. Below your existing loop, use cat
to print your new test file. Pipe the results into a while
loop, setting the IFS
to a comma again, and then use read
to create FIRST
, LAST
, MAJOR
and GPA
variables from the data. In the loop, use echo
to print the FIRST
variable.
- It should look real similar to your other loop like this
- Here's an example:
cat <filename> | while IFS="," read VARIABLE_1 VARIABLE_2 VARIABLE_3 VARIABLE_4
do
STATEMENTS
done
- Here's how it looks:
cat students_test.csv | while IFS="," read FIRST LAST MAJOR GPA
do
echo $FIRST
done
Run the script to see if it prints the FIRST
(first_name
) variable correctly. It will take a second since it has to go through the first loop.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - After running the script, the
majors
andcourses
tables should have three rows each, and themajors_courses
table should have four. If they don't, there might be something wrong in the script. You can use the reset button to reset the lesson and run the script again
It works 😅 It printed the first item in each row of the CSV file. It's printing the first line again, you will have to take care of that. First, delete the echo
line.
- Delete the
echo $FIRST
line
Add an if condition to the loop that checks if the FIRST
variable is not equal to first_name
so it doesn't do anything for the first line of the file. Don't put anything in the statements area for now.
- Here's an example of an
if
:
if [[ CONDITION ]]
then
STATEMENTS
fi
- The condition you want is
[[ $FIRST != "first_name" ]]
- Your second loop should look like this:
cat students_test.csv | while IFS="," read FIRST LAST MAJOR GPA
do
if [[ $FIRST != "first_name" ]]
then
fi
done
All the columns in the CSV file can be inserted directly into the database except for the major. You will need to get the major_id
again for that. There's some null
values in there as well, so you will need to use null
if the major_id
isn't found. Add four single line comments in your loop; get major_id
, if not found
, set to null
, and insert student
in that order.
- Here's an example of a single comment:
# <comment>
- Add the four suggested single line comments, each on their own line, in the order given in the
if
part of your new loop - It should look like this:
cat students_test.csv | while IFS="," read FIRST LAST MAJOR GPA
do
if [[ $FIRST != "first_name" ]]
then
# get major_id
# if not found
# set to null
# insert student
fi
done
Below the new get major_id
comment, set the MAJOR_ID
variable to a query that gets the major_id
for the current students major.
- Here's an example of how it looks:
MAJOR_ID=$($PSQL "<query_here>")
- For the query, you want to use the
SELECT
,FROM
, andWHERE
keywords - Here's an example of how the query part looks:
SELECT <column_name> FROM <table_name> WHERE <condition>
- The condition you want is
major_id='$MAJOR'
- Here's how the query should look:
SELECT major_id FROM majors WHERE major='$MAJOR'
- Here's how the whole line should look:
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")
Below that, use echo
to print the variable so you can see if it's working.
- Add
echo $MAJOR_ID
below theMAJOR_ID
variable you just created
Run the script to see what happens.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - After running the script, the
majors
andcourses
tables should have three rows each, and themajors_courses
table should have four. If they don't, there might be something wrong in the script. You can use the reset button to reset the lesson and run the script again
Looking at the test data, it found the ID for all of it except the null
value. Below the newest if not found
comment, add an if
that checks if the variable is empty. Put the set to null
comment in its statements area.
- It looks similar to the
if
condition in your first loop - The condition you want is
[[ -z $MAJOR_ID ]]
- Make sure the
set to null
comment is in the statements area - It should look like this:
if [[ -z $MAJOR_ID ]]
then
# set to null
fi
When you go to insert the student data, you want to use the MAJOR_ID
if it's found, or null
if not. Below the set to null
comment, set the MAJOR_ID
variable to null
so you can use it to insert the data.
- It should look like this:
if [[ -z $MAJOR_ID ]]
then
# set to null
MAJOR_ID=null
fi
Move the echo $MAJOR_ID
line to below the if
statement so you can run the script and see the value of the variable if the major_id
is or isn't found.
- Move the suggested line below the closing
fi
of theif [[ -z $MAJOR_ID ]]
statement
Run the script.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - After running the script, the
majors
andcourses
tables should have three rows each, and themajors_courses
table should have four. If they don't, there might be something wrong in the script. You can use the reset button to reset the lesson and run the script again
Okay, that should work for inserting the student. Delete the echo $MAJOR_ID
line.
- Delete the
echo $MAJOR_ID
line from the file
One last thing to add. In the psql prompt, view the details of the students
table so you can see what columns to add.
- Use the display shortcut command
- Add the table name after the command
- It's the
\d
command - Here's an example:
\d <table_name>
- Type
\d students
into the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
You will need to set the four columns when adding the student info. All of them except student_id
. Below the insert student
comment, create an INSERT_STUDENT_RESULT
variable that adds the student to the database. Add the columns in the order they appear in the data, and make sure to only put the two VARCHAR
columns in single quotes.
- Here's an example:
INSERT_STUDENT_RESULT=$($PSQL "<query_here>")
- For the query, you want to use the
INSERT INTO
, andVALUES
keywords - Here's an example of how the query part looks:
INSERT INTO <table_name>(<column_1>, <column_N>) VALUES(<value_1>, <value_N>)
- In your query, make sure the columns to add are in this order:
first_name
,last_name
,major_id
, andgpa
- The query you want is:
INSERT INTO students(first_name, last_name, major_id, gpa) VALUES('$FIRST', '$LAST', $MAJOR_ID, $GPA)
- Here's how the whole line should look:
INSERT_STUDENT_RESULT=$($PSQL "INSERT INTO students(first_name, last_name, major_id, gpa) VALUES('$FIRST', '$LAST', $MAJOR_ID, $GPA)")
Below the variable you just created, add an if
statement that checks if it's equal to INSERT 0 1
like the others. If it is, use echo
to print Inserted into students, <first_name> <last_name>
.
- The condition should look like this:
if [[ $INSERT_STUDENT_RESULT == "INSERT 0 1" ]]
- Use the
FIRST
andLAST
variables to print the students name - The
echo
should look like this:echo Inserted into students, $FIRST $LAST
- The whole thing should look like this:
if [[ $INSERT_STUDENT_RESULT == "INSERT 0 1" ]]
then
echo "Inserted into students, $FIRST $LAST"
fi
Run the script to see if the students are getting added.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - After running the script, the
majors
andcourses
tables should have three rows each, and themajors_courses
andstudents
tables should have four. If they don't, there might be something wrong in the script. You can use the reset button to reset the lesson and run the script again
I think it's working. View all the data in the students
table to make sure it matches the CSV file.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Excellent. It added all the students from the test data. Time to try it with the original files. Change the cat courses_test.csv
line to use the original file again.
- Change
cat courses_test.csv
tocat courses.csv
- The suggested line should look like this:
cat courses.csv | while IFS="," read MAJOR COURSE
Next, change the cat students_test.csv
line to use the original file as well.
- Change the
cat students_test.csv
tocat students.csv
- The suggested line should look like this:
cat students.csv | while IFS="," read FIRST LAST MAJOR GPA
Time for the moment of truth. Run the script and see if it works.
- Run your
insert_data.sh
script by executing it - Type
./insert_data.sh
in the terminal and press enter - After running the script, the tables should have this many rows:
majors
has 7,courses
has 17,majors_courses
has 28, andstudents
should have 31. If they don't, there might be something wrong in the script. You can use the reset button to reset the lesson and run the script again
That was cool. View all the data in the students
table to see what you ended up with.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM students;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
31 rows. That's how many are in the CSV file. Perfect. Next, check the majors
table.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM majors;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
7 rows. There must be 7 unique majors in the CSV file. View what's in the courses
table.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM courses;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
Looks like there's 17 unique courses in the CSV file. Last, view the data in majors_courses
. This should have the same number of rows at the CSV file.
- Use the
SELECT
andFROM
keywords with*
to view all the data - Enter
SELECT * FROM majors_courses;
in the psql prompt - Enter
psql --username=freecodecamp --dbname=students
in the terminal to log into the psql prompt if you aren't already
28 rows, same as the CSV file. I think all the data got added correctly. You don't need your test files anymore. In the terminal, use the list command to check what files are in your project folder.
- It's the
ls
command - Don't use any flags with the command
- Enter
ls
in the terminal
Use the remove command (rm
) to delete the students_test.csv
file.
- Here's an example
rm <filename>
- Enter
rm students_test.csv
in the terminal
Use the same command to delete the courses_test.csv
file.
- Here's an example
rm <filename>
- Enter
rm courses_test.csv
in the terminal
List the contents of the folder again to make sure they're gone.
- Use the list command
- It's the
ls
command - Don't use any flags with the command
- Enter
ls
in the terminal
The database is finished for now. The last thing you are going to do is make a "dump" of it. The pg_dump
command can do that for you. Use the --help
flag with the command to see what it can do.
- Here's an example:
<command> <flag>
- Enter
pg_dump --help
in the terminal - The bash terminal, not the psql one
- Press enter until you have seen the whole manual
This is the last step. There's quite a few options there. Enter pg_dump --clean --create --inserts --username=freecodecamp students > students.sql
in the terminal to dump the database into a students.sql
file. It will save all the commands needed to rebuild it. Take a quick look at the file when you are done.
- Enter the suggested command in the terminal
- The bash terminal, not the psql one
- Make sure you are in the
project
folder first