{ "id": "freeCodeCamp/learn-sql-by-building-a-student-database-part-1:v1.0.0", "version": "3.0.0", "summary": { "title": "Learn SQL by Building a Student Database: Part 1", "description": "> Welcome to the SQL Lessons!" }, "config": { "setup": { "commands": [ "./.freeCodeCamp/reset.sh", "cd .freeCodeCamp && npm install" ], "commits": [ "d42669943ee7a32a8c38f907ed7914c91953f808" ] }, "testRunner": { "command": "npm run programmatic-test", "args": { "tap": "--reporter=mocha-tap-reporter" }, "directory": ".freeCodeCamp" }, "repo": { "uri": "https://github.com/freeCodeCamp/learn-sql-by-building-a-student-database-part-1", "branch": "v3.0.0" }, "continue": { "commands": [ "./.freeCodeCamp/reset.sh" ] }, "reset": { "commands": [ "./.freeCodeCamp/reset.sh" ] }, "dependencies": [ { "name": "node", "version": ">=10" } ], "webhook": { "url": "https://api.freecodecamp.org/coderoad-challenge-completed", "events": { "init": false, "reset": false, "step_complete": false, "level_complete": false, "tutorial_complete": true } } }, "levels": [ { "id": "10", "title": "Start the Terminal", "summary": "", "content": "", "steps": [ { "id": "10.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "91fad900c0ccbbe1da8b2ec2986cba13b4ace2e4" ] }, "content": "**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.", "hints": [ "Capitalization matters", "If the tests don't run automatically, try typing `exit` into the terminal and redoing the instructions" ] } ] }, { "id": "20", "title": "psql login", "summary": "", "content": "", "steps": [ { "id": "20.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "6818ae581c459de5a23074aecc69027a1809c172" ] }, "content": "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.", "hints": [ "Type `psql --username=freecodecamp --dbname=postgres` into the terminal and press enter" ] } ] }, { "id": "30", "title": "\\l", "summary": "", "content": "", "steps": [ { "id": "30.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "ecacba1730230a0a9ae6bb224db9c90bb1548d98" ] }, "content": "View the existing databases with the `\\l` shortcut command to see what's here.", "hints": [ "Type `\\l` into the psql prompt and press enter" ] } ] }, { "id": "40", "title": "CREATE DATABASE students;", "summary": "", "content": "", "steps": [ { "id": "40.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "cb521b101a834362a8f100f5d3f6083ae2d08bc3" ] }, "content": "All the info from the CSV files will go into a single database. Create a new database named `students`.", "hints": [ "Use the `CREATE DATABASE` keywords", "Here's an example: `CREATE DATABASE ;`", "Type `CREATE DATABASE students;` into the psql prompt and press enter" ] } ] }, { "id": "50", "title": "\\l", "summary": "", "content": "", "steps": [ { "id": "50.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "3369962b4f22bee4939c0c2a427b16dad19ac3e1" ] }, "content": "View the databases again to make sure it got created.", "hints": [ "Use the **list** shortcut command in the psql prompt", "Type `\\l` into the psql prompt and press enter" ] } ] }, { "id": "60", "title": "\\c students", "summary": "", "content": "", "steps": [ { "id": "60.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "cd8738e85c50167fe4444f4ef156ccd689278c2a" ] }, "content": "There it is. Connect to your new database so you can start adding tables.", "hints": [ "Use the `\\c` shortcut command", "Here's an example: `\\c `", "Type `\\c students` in the psql prompt" ] } ] }, { "id": "70", "title": "CREATE TABLE students", "summary": "", "content": "", "steps": [ { "id": "70.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "e15a1087e5e57e8c0f6390ab52ad2fb83002bd5c" ] }, "content": "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.", "hints": [ "Use the `CREATE TABLE` keywords", "There should be parenthesis after the table name", "Here's an example: `CREATE TABLE ();`", "Type `CREATE TABLE students();` into the psql prompt" ] } ] }, { "id": "80", "title": "CREATE TABLE majors", "summary": "", "content": "", "steps": [ { "id": "80.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "19d00c78c84ace956628b8ad56dbfd8b8f8a3e7f" ] }, "content": "The second table will be for each unique major that appears in the data. Create a table named `majors`.", "hints": [ "Use the `CREATE TABLE` keywords", "There should be parenthesis after the table name", "Here's an example: `CREATE TABLE ();`", "Type `CREATE TABLE majors();` into the psql prompt" ] } ] }, { "id": "90", "title": "CREATE TABLE courses", "summary": "", "content": "", "steps": [ { "id": "90.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "681614c9795683f083cd98c70d17b813c8f81fb2" ] }, "content": "The third table is for each unique course in the data. Create another table named `courses`.", "hints": [ "Use the `CREATE TABLE` keywords", "There should be parenthesis after the table name", "Here's an example: `CREATE TABLE ();`", "Type `CREATE TABLE courses();` into the psql prompt" ] } ] }, { "id": "100", "title": "CREATE TABLE majors_courses", "summary": "", "content": "", "steps": [ { "id": "100.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "694c3459c7c3baf4ad177fe4c16ab53a9c9e427b" ] }, "content": "The final table will be a junction table for the majors and courses. Create it with the name `majors_courses`.", "hints": [ "Use the `CREATE TABLE` keywords", "There should be parenthesis after the table name", "Here's an example: `CREATE TABLE ();`", "Type `CREATE TABLE majors_courses();` into the psql prompt" ] } ] }, { "id": "110", "title": "\\d", "summary": "", "content": "", "steps": [ { "id": "110.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "caf30db914db9212e6c742f7bc6e8c8cd3200e5d" ] }, "content": "Use the **d**isplay shortcut command to view your tables to make sure your satisfied with them.", "hints": [ "It's the `\\d` shortcut command", "Type `\\d` in the psql prompt" ] } ] }, { "id": "120", "title": "Create student_id Column", "summary": "", "content": "", "steps": [ { "id": "120.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "3e8f9310559c2b1b0df72c7fdfeb056883f6e5aa" ] }, "content": "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`", "hints": [ "Use the `ALTER TABLE`, `ADD COLUMN`, `SERIAL` and `PRIMARY KEY` keywords", "Here's an example: `ALTER TABLE ADD COLUMN ;`", "Type `ALTER TABLE students ADD COLUMN student_id SERIAL PRIMARY KEY;` into the psql prompt", "You can drop a column with `ALTER TABLE DROP COLUMN ;` if you want to delete a column and try again" ] } ] }, { "id": "130", "title": "Create first_name Column", "summary": "", "content": "", "steps": [ { "id": "130.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "e72cf140588235240e540c1a9fdbd6cd0d3de56c" ] }, "content": "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.", "hints": [ "Use the `ALTER TABLE`, `ADD COLUMN`, `VARCHAR()` and `NOT NULL` keywords", "Here's an example: `ALTER TABLE ADD COLUMN ;`", "Type `ALTER TABLE students ADD COLUMN first_name VARCHAR(50) NOT NULL;` into the psql prompt", "You can drop a column with `ALTER TABLE DROP COLUMN ;` if you want to delete a column and try again" ] } ] }, { "id": "140", "title": "Create last_name Column", "summary": "", "content": "", "steps": [ { "id": "140.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "9547bc4161879ae8c98c88255abb882e39d8ff3f" ] }, "content": "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.", "hints": [ "Use the `ALTER TABLE`, `ADD COLUMN`, `VARCHAR()` and `NOT NULL` keywords", "The max-length should be `50`", "Here's an example: `ALTER TABLE ADD COLUMN ;`", "Type `ALTER TABLE students ADD COLUMN last_name VARCHAR(50) NOT NULL;` into the psql prompt", "You can drop a column with `ALTER TABLE DROP COLUMN ;` if you want to delete a column and try again" ] } ] }, { "id": "143", "title": "Create major_id Column", "summary": "", "content": "", "steps": [ { "id": "143.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "dd9afa99c9b5e64d263dd9b423f83d1e7a5071d6" ] }, "content": "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.", "hints": [ "Use the `ALTER TABLE`, `ADD COLUMN`, and `INT` keywords", "Here's an example: `ALTER TABLE ADD COLUMN ;`", "Type `ALTER TABLE students ADD COLUMN major_id INT;` into the psql prompt", "You can drop a column with `ALTER TABLE DROP COLUMN ;` if you want to delete a column and try again" ] } ] }, { "id": "146", "title": "Create gpa Column", "summary": "", "content": "", "steps": [ { "id": "146.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "06e08001970a2c6e9de25a7312d50a594997bb75" ] }, "content": "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)`.", "hints": [ "Use the `ALTER TABLE`, `ADD COLUMN`, and `NUMERIC()` keywords", "Here's an example: `ALTER TABLE ADD COLUMN ;`", "Type `ALTER TABLE students ADD COLUMN gpa NUMERIC(2,1);` into the psql prompt", "You can drop a column with `ALTER TABLE DROP COLUMN ;` if you want to delete a column and try again" ] } ] }, { "id": "150", "title": "\\d students", "summary": "", "content": "", "steps": [ { "id": "150.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "86b07f52f698d84ce2dc5cbbbfa177f82744be81" ] }, "content": "Use the shortcut command to **d**isplay the details of the `students` table to make sure you like it.", "hints": [ "It's the `\\d` shortcut command", "Add the table name after the command", "Here's an example: `\\d `", "Type `\\d students` in the psql prompt" ] } ] }, { "id": "160", "title": "Create major_id Column", "summary": "", "content": "", "steps": [ { "id": "160.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "b6be18c4e019630ab5b1e236d5ccaff60b1f2f62" ] }, "content": "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.", "hints": [ "Use the `ALTER TABLE`, `ADD COLUMN`, `SERIAL` and `PRIMARY KEY` keywords", "Here's an example: `ALTER TABLE ADD COLUMN ;`", "Type `ALTER TABLE majors ADD COLUMN major_id SERIAL PRIMARY KEY;` into the psql prompt", "You can drop a column with `ALTER TABLE DROP COLUMN ;` if you want to delete a column and try again" ] } ] }, { "id": "170", "title": "Create major Column", "summary": "", "content": "", "steps": [ { "id": "170.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "bfb05065f2d1bce73688d4d3f043a0966a29cc60" ] }, "content": "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.", "hints": [ "Use the `ALTER TABLE`, `ADD COLUMN`, `VARCHAR()` and `NOT NULL` keywords", "The max-length of `50` should go in the parenthesis of `VARCHAR`", "Here's an example: `ALTER TABLE ADD COLUMN ;`", "Type `ALTER TABLE majors ADD COLUMN major VARCHAR(50) NOT NULL;` into the psql prompt", "You can drop a column with `ALTER TABLE DROP COLUMN ;` if you want to delete a column and try again" ] } ] }, { "id": "180", "title": "\\d majors", "summary": "", "content": "", "steps": [ { "id": "180.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "8550d12cb225a62773f127d55bc617f14a714b9e" ] }, "content": "View the details of the majors table to make sure you like it.", "hints": [ "Use the **d**isplay shortcut command", "Add the table name after the command", "It's the `\\d` command", "Here's an example: `\\d `", "Type `\\d majors` into the psql prompt" ] } ] }, { "id": "183", "title": "Create major_id foreign key", "summary": "", "content": "", "steps": [ { "id": "183.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "eda35deb2e83b38211df169e761d0fac1c293c86" ] }, "content": "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 ADD FOREIGN KEY() REFERENCES ();`", "hints": [ "Type `ALTER TABLE students ADD FOREIGN KEY(major_id) REFERENCES majors(major_id);` in the psql prompt" ] } ] }, { "id": "187", "title": "\\d students", "summary": "", "content": "", "steps": [ { "id": "187.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "5b53a00b68e6bd25f9204d7acccf6fc505429fb1" ] }, "content": "View the details of the `students` table again to make sure the key is there.", "hints": [ "Use the **d**isplay shortcut command", "Add the table name after the command", "It's the `\\d` command", "Here's an example: `\\d `", "Type `\\d students` into the psql prompt" ] } ] }, { "id": "190", "title": "Create course_id Column", "summary": "", "content": "", "steps": [ { "id": "190.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "b57d2acc574fc72b281fe6579de7c7bc000ed715" ] }, "content": "Next, is the `courses` table. Add a `course_id` column to it. Give it a type of `SERIAL` and make it the primary key.", "hints": [ "Use the `ALTER TABLE`, `ADD COLUMN`, `SERIAL` and `PRIMARY KEY` keywords", "Here's an example: `ALTER TABLE ADD COLUMN ;`", "Type `ALTER TABLE courses ADD COLUMN course_id SERIAL PRIMARY KEY;` into the psql prompt", "You can drop a column with `ALTER TABLE DROP COLUMN ;` if you want to delete a column and try again" ] } ] }, { "id": "200", "title": "Create course Column", "summary": "", "content": "", "steps": [ { "id": "200.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "acc5f38d7a7bd34354ef974ce8ad6aa684822fc3" ] }, "content": "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.", "hints": [ "Use the `ALTER TABLE`, `ADD COLUMN`, `VARCHAR()` and `NOT NULL` keywords", "The max-length of `100` should go in the parenthesis of `VARCHAR`", "Here's an example: `ALTER TABLE ADD COLUMN ;`", "Type `ALTER TABLE courses ADD COLUMN course VARCHAR(100) NOT NULL;` into the psql prompt", "You can drop a column with `ALTER TABLE DROP COLUMN ;` if you want to delete a column and try again" ] } ] }, { "id": "220", "title": "\\d courses", "summary": "", "content": "", "steps": [ { "id": "220.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "a9c9c0077222bb94447f96ffbf110bde86143925" ] }, "content": "View the details of the courses table to make sure it looks good.", "hints": [ "Use the **d**isplay shortcut command", "Add the table name after the command", "It's the `\\d` command", "Here's an example: `\\d `", "Type `\\d courses` into the psql prompt" ] } ] }, { "id": "230", "title": "Create major_id column", "summary": "", "content": "", "steps": [ { "id": "230.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "4373122ccf0274d09ac7d65078561955eca5c689" ] }, "content": "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.", "hints": [ "Use the `ALTER TABLE`, `ADD COLUMN`, and `INT` keywords", "Here's an example: `ALTER TABLE ADD COLUMN ;`", "Type `ALTER TABLE majors_courses ADD COLUMN major_id INT;` into the psql prompt", "You can drop a column with `ALTER TABLE DROP COLUMN ;` if you want to delete a column and try again" ] } ] }, { "id": "240", "title": "Set major_id Foreign Key", "summary": "", "content": "", "steps": [ { "id": "240.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "07fb92fb815e0876cef6f689514b7022f69e9187" ] }, "content": "Set the `major_id` column you just created as a foreign key that references the `major_id` column from the `majors` table.", "hints": [ "Use the `ALTER TABLE`, `ADD FOREIGN KEY`, and `REFERENCES` keywords", "Here's an example: `ALTER TABLE ADD FOREIGN KEY() REFERENCES ();`", "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" ] } ] }, { "id": "250", "title": "Create course_id Column", "summary": "", "content": "", "steps": [ { "id": "250.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "caddd78d843df82e5ec68212110b004b445b6d54" ] }, "content": "Next, add a `course_id` column to the same table. Just give it a type of `INT` again for now.", "hints": [ "It goes in the `majors_courses` table", "Use the `ALTER TABLE`, `ADD COLUMN`, and `INT` keywords", "Here's an example: `ALTER TABLE ADD COLUMN ;`", "Type `ALTER TABLE majors_courses ADD COLUMN course_id INT;` into the psql prompt", "You can drop a column with `ALTER TABLE DROP COLUMN ;` if you want to delete a column and try again" ] } ] }, { "id": "260", "title": "Set course_id Foreign Key", "summary": "", "content": "", "steps": [ { "id": "260.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "3d2656daae4aead799844c94c81f2963a79f8b53" ] }, "content": "Set your new `course_id` column as a foreign key that references the other `course_id` column.", "hints": [ "The referenced column is `course_id` from the `courses` table", "Use the `ALTER TABLE`, `ADD FOREIGN KEY`, and `REFERENCES` keywords", "Here's an example: `ALTER TABLE ADD FOREIGN KEY() REFERENCES ();`", "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" ] } ] }, { "id": "270", "title": "\\d majors_courses", "summary": "", "content": "", "steps": [ { "id": "270.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "f72431860666ecbf3d8104e4611216dd5015f572" ] }, "content": "View the details of the table you just worked on to make sure the structure is finished.", "hints": [ "Use the **d**isplay shortcut command", "Add the table name after the command", "It's the `\\d` command", "Here's an example: `\\d `", "Type `\\d courses` into the psql prompt" ] } ] }, { "id": "280", "title": "Create Composite Primary Key", "summary": "", "content": "", "steps": [ { "id": "280.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "b4afdd32d77db0423f40df2b76889803ab8cb7d8" ] }, "content": "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 ADD PRIMARY KEY(, );` Add a composite primary key to the table using the two columns.", "hints": [ "It's the `major_id` and `course_id` columns from the `majors_courses` table", "Type `ALTER TABLE majors_courses ADD PRIMARY KEY(major_id, course_id);` into the psql prompt" ] } ] }, { "id": "290", "title": "\\d majors_courses", "summary": "", "content": "", "steps": [ { "id": "290.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "ad1f306c52df65047b5faff257152f41ba33e9cd" ] }, "content": "View the details of the table again.", "hints": [ "Use the **d**isplay shortcut command", "Add the table name after the command", "It's the `\\d` command", "Here's an example: `\\d `", "Type `\\d courses` into the psql prompt" ] } ] }, { "id": "300", "title": "\\d", "summary": "", "content": "", "steps": [ { "id": "300.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "14b12c3580b2209607a10e2baa56874bed89981e" ] }, "content": "Okay, now it's finished. View all the tables you ended up with.", "hints": [ "Use the **d**isplay shortcut command", "Don't include a table name after the command", "It's the `\\d` command", "Type `\\d` into the psql prompt" ] } ] }, { "id": "310", "title": "\\d majors", "summary": "", "content": "", "steps": [ { "id": "310.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "661d6141c3dd090b8c454cbbad24e8f37ab6dbbc" ] }, "content": "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.", "hints": [ "Use the **d**isplay shortcut command", "Add the table name after the command", "It's the `\\d` command", "Here's an example: `\\d `", "Type `\\d majors` into the psql prompt" ] } ] }, { "id": "320", "title": "INSERT INTO majors", "summary": "", "content": "", "steps": [ { "id": "320.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "f7f24fb1077488fc0dcf13b4f464a97834d385f2" ] }, "content": "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.", "hints": [ "The major is `Database Administration`", "Use the `INSERT INTO` and `VALUES` keywords", "Here's an example: `INSERT INTO () VALUES();`", "Type `INSERT INTO majors(major) VALUES('Database Administration');`" ] } ] }, { "id": "340", "title": "SELECT * FROM majors", "summary": "", "content": "", "steps": [ { "id": "340.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "94ecf58cf1bfacbf721a60856da4c8d8f41faa81" ] }, "content": "Use `SELECT` to view all the data in the `majors` table to make sure it got inserted correctly.", "hints": [ "Use the `SELECT` and `FROM` keywords with `*` to view all the columns", "Here's an example: `SELECT FROM ;`", "Type `SELECT * FROM majors;` into the psql prompt" ] } ] }, { "id": "350", "title": "INSERT INTO courses", "summary": "", "content": "", "steps": [ { "id": "350.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "b046ea329944bda1a6918c4a957b3d5e919f8e31" ] }, "content": "Next, insert the first course from `courses.csv` into the `courses` table.", "hints": [ "Use the `INSERT INTO` and `VALUES` 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 () VALUES();`", "Type `INSERT INTO courses(course) VALUES('Data Structures and Algorithms');`" ] } ] }, { "id": "360", "title": "SELECT * FROM courses", "summary": "", "content": "", "steps": [ { "id": "360.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "df311edf557bac3cc5e5387da58faa05924347b4" ] }, "content": "View all the data in the `courses` table to make sure it got added.", "hints": [ "Use the `SELECT` and `FROM` keywords with `*` to view all the columns", "Here's an example: `SELECT FROM ;`", "Type `SELECT * FROM courses;` into the psql prompt" ] } ] }, { "id": "370", "title": "\\d majors_courses", "summary": "", "content": "", "steps": [ { "id": "370.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "5496d6cec6e01e9b77871aa3e42735bcc3dcd72f" ] }, "content": "Next, you can add a row into the junction table. View the details of it to see what it expects.", "hints": [ "It's the `majors_courses` table", "Use the **d**isplay shortcut command", "Add the table name after the command", "It's the `\\d` command", "Here's an example: `\\d `", "Type `\\d majors_courses` into the psql prompt" ] } ] }, { "id": "380", "title": "INSERT INTO majors_courses", "summary": "", "content": "", "steps": [ { "id": "380.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "58fef6e8d9fe16ef95cab4d2273e07103a91641c" ] }, "content": "It wants a `major_id` and `course_id`. Add a row to `majors_courses` for the first entry in `courses.csv`.", "hints": [ "Use `SELECT * FROM ;` to find the two ID's you already added", "Use the `INSERT INTO` and `VALUES` keywords", "Here's an example: `INSERT INTO (, ) VALUES(, );`", "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" ] } ] }, { "id": "390", "title": "SELECT * FROM majors_courses", "summary": "", "content": "", "steps": [ { "id": "390.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "9486d0987f6c3580b0ade8fa3ff428f63bd97a4c" ] }, "content": "View all the data in the table you just added to.", "hints": [ "Use the `SELECT` and `FROM` keywords with `*` to view all the columns", "Here's an example: `SELECT FROM ;`", "Type `SELECT * FROM majors_courses;` into the psql prompt" ] } ] }, { "id": "400", "title": "\\d students", "summary": "", "content": "", "steps": [ { "id": "400.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "8e8f5bc4764ceaac359f2588f726a099b36798a6" ] }, "content": "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.", "hints": [ "Use the **d**isplay shortcut command", "Add the table name after the command", "It's the `\\d` command", "Here's an example: `\\d `", "Type `\\d students` into the psql prompt" ] } ] }, { "id": "420", "title": "INSERT INTO students", "summary": "", "content": "", "steps": [ { "id": "420.1", "setup": { "watchers": [ "../queryResults.log" ], "commits": [ "de645ace6e29a8a1680c3c2598d1e0017a2ab038" ] }, "content": "The output shows what the table needs. Insert the first person from `students.csv` into the `students` table.", "hints": [ "Enter `SELECT * FROM majors;` to find the `major_id` for the students major", "Use the `INSERT INTO` and `VALUES` keywords", "Make sure to put `VARCHAR` values in single quotes", "Here's an example: `INSERT INTO (, ) VALUES(, );`", "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" ] } ] }, { "id": "430", "title": "SELECT * FROM students", "summary": "", "content": "", "steps": [ { "id": "430.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "bd34a7a984beab0185aca6d08de7b5972b79996c" ] }, "content": "Looks like it worked. View all the data in the students table to make sure.", "hints": [ "Use the `SELECT` and `FROM` keywords with `*` to view all the columns", "Here's an example: `SELECT FROM ;`", "Type `SELECT * FROM students;` into the psql prompt" ] } ] }, { "id": "440", "title": "touch insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "440.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "956c359729eab5cce3759b511c7985d051d1f1d2" ] }, "content": "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.", "hints": [ "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" ] } ] }, { "id": "450", "title": "chmod +x insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "450.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "d62ec72561d027735f5e434c37479b39a70f8891" ] }, "content": "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.", "hints": [ "Here's an example: `chmod +x `", "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`" ] } ] }, { "id": "460", "title": "Add shebang", "summary": "", "content": "", "steps": [ { "id": "460.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "70f2542ba28af3435eda912ab1a231ae4321ef5a" ] }, "content": "Open your new file and add a \"shebang\" that uses `bash` at the top. It looks like this: `#!/bin/bash`.", "hints": [ "Add the text, `#!/bin/bash` to your `insert_data.sh` file" ] } ] }, { "id": "470", "title": "Add comment", "summary": "", "content": "", "steps": [ { "id": "470.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "89e8c81ef2be63d560cd2fdae186ef2da6fb8a68" ] }, "content": "Below that, add a single line comment with the text, `Script to insert data from courses.csv and students.csv into students database`.", "hints": [ "A comment look like this: `# `", "Add `# Script to insert data from courses.csv and students.csv into students database` below the \"shebang\" in your `insert_data.sh` file" ] } ] }, { "id": "480", "title": "Add cat courses.csv", "summary": "", "content": "", "steps": [ { "id": "480.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "43fc999d9fecc08f9fba0e5c2258d5be708f5141" ] }, "content": "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 `. Below the comment you added, use it to print `courses.csv`.", "hints": [ "Add `cat courses.csv` to your `insert_data.sh` file below your comment" ] } ] }, { "id": "490", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "490.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "e83d5e5a0f53335877a26e596acc34cb78600da5" ] }, "content": "Run your script to see if the file contents get printed.", "hints": [ "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" ] } ] }, { "id": "500", "title": "Add while read", "summary": "", "content": "", "steps": [ { "id": "500.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "d64685add19ead2b363692f6d8f04417d7ab0372" ] }, "content": "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:\n\n```sh\ncat courses.csv | while read MAJOR COURSE\ndo\n \ndone\n```\n\nEach 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.", "hints": [ "You can print the suggested variable with `echo $MAJOR`", "The whole loop should look like this:\n```sh\ncat courses.csv | while read MAJOR COURSE\ndo\n echo $MAJOR\ndone\n```" ] } ] }, { "id": "510", "title": "./insert_data", "summary": "", "content": "", "steps": [ { "id": "510.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "bb8f8b2655c5bbec4fc01888c8596a40feb75dd6" ] }, "content": "Run the script to see if it worked.", "hints": [ "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" ] } ] }, { "id": "515", "title": "declare -p IFS", "summary": "", "content": "", "steps": [ { "id": "515.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "45d11dff94d4a6076b6989ac7fa154127da26280" ] }, "content": "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`.", "hints": [ "Enter `declare -p IFS` in the terminal" ] } ] }, { "id": "520", "title": "Add IFS", "summary": "", "content": "", "steps": [ { "id": "520.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "bca09258cd3d36829884405383156584bdd80f92" ] }, "content": "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=\",\"`", "hints": [ "Here's how it looks:\n```sh\ncat courses.csv | while IFS=\",\" read MAJOR COURSE\ndo\n echo $MAJOR\ndone\n```" ] } ] }, { "id": "530", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "530.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "1149ea63026fbc3cbe72039f61a902464ca6ef04" ] }, "content": "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.", "hints": [ "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" ] } ] }, { "id": "533", "title": "Add echo all variables", "summary": "", "content": "", "steps": [ { "id": "533.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "65452be691331f5ffae652f2961a07b1e823c4e1" ] }, "content": "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.", "hints": [ "Change the `echo` line to `echo $MAJOR $COURSE`", "The whole loop should look like this:\n```sh\ncat courses.csv | while IFS=\",\" read MAJOR COURSE\ndo\n echo $MAJOR $COURSE\ndone\n```" ] } ] }, { "id": "536", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "536.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "19224436cdb4e61fcd52011d644380fd76435d10" ] }, "content": "Run the script again to check.", "hints": [ "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" ] } ] }, { "id": "540", "title": "Delete echo", "summary": "", "content": "", "steps": [ { "id": "540.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "7f3a0e4dd69d590fc76e2c003f2ec40e636ed0f7" ] }, "content": "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.", "hints": [ "Delete the `echo $MAJOR $COURSE` line" ] } ] }, { "id": "550", "title": "Add comments", "summary": "", "content": "", "steps": [ { "id": "550.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "2ce759be1255bad111e21ba49ee58683eda7f558" ] }, "content": "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`.", "hints": [ "Here's an example of a single comment: `# `", "Add the nine suggested single line comments, each on their own line, in the order given", "It should look like this:\n```sh\ndo\n # get major_id\n\n # if not found\n\n # insert major\n\n # get new major_id\n\n # get course_id\n\n # if not found\n\n # insert course\n\n # get new course_id\n\n # insert into majors_courses\n\ndone\n```" ] } ] }, { "id": "560", "title": "Add PSQL Variable", "summary": "", "content": "", "steps": [ { "id": "560.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "5a069f589681724941a6722a5f54239dfe52b964" ] }, "content": "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.", "hints": [ "Add the suggested variable between your first comment and the loop", "The suggested area should look like this:\n```sh\nPSQL=\"psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c\"\n```" ] } ] }, { "id": "562", "title": "Add MAJOR_ID", "summary": "", "content": "", "steps": [ { "id": "562.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "d1a8efaabda6f9a01e5c91aea6e8b899e051ddfa" ] }, "content": "Now, you can query your database using the `PSQL` variable like this: `$($PSQL \"\")`. The code in the parenthesis will run in a subshell, which is a separate bash process. 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.", "hints": [ "Here's an example of how it looks: `MAJOR_ID=$($PSQL \"\")`", "For the query, you want to use the `SELECT`, `FROM`, and `WHERE` keywords", "Here's an example of how the query part looks: `SELECT FROM WHERE `", "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'\")`" ] } ] }, { "id": "564", "title": "Add echo MAJOR_ID", "summary": "", "content": "", "steps": [ { "id": "564.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "f6b4a31cbc22fb9400a10db0861a2afe148e4303" ] }, "content": "Below the variable you just created, use `echo` to print it so you can see it's value when you run the script.", "hints": [ "Add `echo $MAJOR_ID` below the `MAJOR_ID` variable you created" ] } ] }, { "id": "566", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "566.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "d87bb45698e2b3c8dbe536d4b907cc2551791444" ] }, "content": "Run the script to see what happens.", "hints": [ "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" ] } ] }, { "id": "568", "title": "Add if -z MAJOR_ID", "summary": "", "content": "", "steps": [ { "id": "568.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "e7dc1ce71200db7c8a53ed7cbbc40516696c0da8" ] }, "content": "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`.", "hints": [ "Here's an example of an `if`:\n```sh\nif CONDITION\nthen\n STATEMENTS\nfi\n```", "Make sure your `insert major` and `get new major_id` comments are in the statements area in that order", "Here's how it should look:\n```sh\nif [[ -z $MAJOR_ID ]]\nthen\n # insert major\n\n # get new major_id\n\nfi\n```" ] } ] }, { "id": "570", "title": "Add INSERT_MAJOR_RESULT", "summary": "", "content": "", "steps": [ { "id": "570.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "794b797947a8d46e006282086d9f0d54c61e112f" ] }, "content": "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.", "hints": [ "Here's an example of how to query the database: `INSERT_MAJOR_RESULT=$($PSQL \"\")`", "For the query, you want to use the `INSERT INTO`, and `VALUES` keywords", "Here's an example of how the query part looks: `INSERT INTO () VALUES()`", "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')\")`" ] } ] }, { "id": "580", "title": "Add echo INSERT_MAJOR_RESULT", "summary": "", "content": "", "steps": [ { "id": "580.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "22458c649256b4e8afce252d96d231f03f39fc4d" ] }, "content": "Below the variable you just created, use `echo` to print it.", "hints": [ "Add `echo $INSERT_MAJOR_RESULT` right below where you created it" ] } ] }, { "id": "590", "title": "cp courses.csv", "summary": "", "content": "", "steps": [ { "id": "590.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "4448b1af6366e5b21bd26bb6dcf1157ebece3e86" ] }, "content": "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`.", "hints": [ "Here's an example: `cp `", "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" ] } ] }, { "id": "600", "title": "Add four records to courses_test.csv", "summary": "", "content": "", "steps": [ { "id": "600.1", "setup": { "watchers": [ "./courses_test.csv" ], "commits": [ "df974d3d06b906d84322993516c97a10659dbce9" ] }, "content": "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.", "hints": [ "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 from `courses.csv`", "Make sure there's one empty line at the bottom", "The `courses_test.csv` file should look like this:\n```csv\nmajor,course\nDatabase Administration,Data Structures and Algorithms\nWeb Development,Web Programming\nDatabase Administration,Database Systems\nData Science,Data Structures and Algorithms\n\n```" ] } ] }, { "id": "610", "title": "Change to cat courses_test.csv", "summary": "", "content": "", "steps": [ { "id": "610.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "2af29d8ee8c6943913adde9cee02868b3920d5ee" ] }, "content": "Back in the `insert_data.sh` script, change your `cat` command to loop through the test file instead of the full one.", "hints": [ "Change your `cat` command to `cat courses_test.csv` instead of `cat courses.csv`", "The suggested line should look like this:\n```sh\ncat courses_test.csv | while IFS=\",\" read MAJOR COURSE\n```" ] } ] }, { "id": "620", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "620.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "af49c9e744f6d8b262ecc54ce80bd4df37ab504e" ] }, "content": "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.", "hints": [ "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" ] } ] }, { "id": "625", "title": "Delete echo MAJOR_ID", "summary": "", "content": "", "steps": [ { "id": "625.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "eac22c9fd063121e27b796aadfc63c0a17043dab" ] }, "content": "Looks like it 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.", "hints": [ "Delete the `echo $MAJOR_ID` line" ] } ] }, { "id": "630", "title": "SELECT * FROM majors", "summary": "", "content": "", "steps": [ { "id": "630.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "ec227c9f5ab11028b726397edd87f0d94cca741c" ] }, "content": "In the psql prompt, use `SELECT` to view all the data from the `majors` table to see what the script added.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "640", "title": "TRUNCATE majors", "summary": "", "content": "", "steps": [ { "id": "640.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "505c839610f6e04f922b731c15c54b1945d64854" ] }, "content": "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;`", "hints": [ "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" ] } ] }, { "id": "660", "title": "TRUNCATE majors, students, majors_courses", "summary": "", "content": "", "steps": [ { "id": "660.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "991e7fca64cc8a68b13a5f6c9912e7ef254ce3a6" ] }, "content": "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.", "hints": [ "Here's an example: `TRUNCATE , , ;`", "The three tables you want are `majors`, `students`, and `majors_courses`", "Enter `TRUNCATE majors, students, majors_courses;` in the psql prompt" ] } ] }, { "id": "670", "title": "SELECT * FROM majors", "summary": "", "content": "", "steps": [ { "id": "670.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "f614c743e5f11a7b77eb2265db361c8ad29fba03" ] }, "content": "View all the data in the `majors` table to make sure it's empty.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "680", "title": "SELECT * FROM majors_courses", "summary": "", "content": "", "steps": [ { "id": "680.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "bb6ae971096be40758b6f78f3d39163a8917716f" ] }, "content": "Looks like it worked. View all the data in the `majors_courses` table to see if that one is empty.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "690", "title": "SELECT * FROM students", "summary": "", "content": "", "steps": [ { "id": "690.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "128c0b6634206e39898e07c67bb0573eaf8c278f" ] }, "content": "It is, check the `students` table.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "700", "title": "SELECT * FROM courses", "summary": "", "content": "", "steps": [ { "id": "700.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "dbe10947a995fe0cac93387a73ce6a3386525b8c" ] }, "content": "Last, check the `courses` table.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "710", "title": "TRUNCATE courses, majors_courses", "summary": "", "content": "", "steps": [ { "id": "710.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "90a2c0615c7a733d98aa45f31f55d45902a19f7e" ] }, "content": "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.", "hints": [ "Here's an example: `TRUNCATE , ;`", "The tables you want are `courses`, and `majors_courses`", "Enter `TRUNCATE courses, majors_courses;` in the psql prompt" ] } ] }, { "id": "720", "title": "SELECT * FROM courses", "summary": "", "content": "", "steps": [ { "id": "720.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "dd13b6916dda1fc600d2347fe401e0bbd42440c8" ] }, "content": "View all the data in the `courses` table again.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "730", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "730.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "3bcd0899d23da8c446d2236ebaaf2e93698bf3aa" ] }, "content": "Now the database is completely empty. Run the script again to see what gets inserted when the database is empty.", "hints": [ "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" ] } ] }, { "id": "740", "title": "SELECT * FROM majors", "summary": "", "content": "", "steps": [ { "id": "740.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "afe1b8a47c38760fa8d4786c2599f2bf5d77ef99" ] }, "content": "It inserted four that time. In the psql prompt, view all the data in the `majors` table.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "750", "title": "Add if major", "summary": "", "content": "", "steps": [ { "id": "750.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "1f4deda1e9d61245738abe2589dbd0d28d536e6d" ] }, "content": "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.", "hints": [ "Here's an example of an `if`:\n```sh\nif [[ CONDITION ]]\nthen\n STATEMENTS\nfi\n```", "Your loop area should look like this:\n```sh\ndo\n if [[ $MAJOR != major ]]\n then\n # get major_id\n MAJOR_ID=$($PSQL \"SELECT major_id FROM majors WHERE major='$MAJOR'\")\n\n # if not found\n if [[ -z $MAJOR_ID ]]\n then\n # insert major\n INSERT_MAJOR_RESULT=$($PSQL \"INSERT INTO majors(major) VALUES('$MAJOR')\")\n echo $INSERT_MAJOR_RESULT\n\n # get new major_id\n\n fi\n\n # get course_id\n\n # if not found\n\n # insert course\n\n # get new course_id\n\n # insert into majors_courses\n\n fi\ndone\n```" ] } ] }, { "id": "760", "title": "TRUNCATE majors CASCADE", "summary": "", "content": "", "steps": [ { "id": "760.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "dea7cc262e0fbb3a3b06bc65a6c6d9e593976552" ] }, "content": "In the psql prompt, use `TRUNCATE` to delete all the data in the `majors` table.", "hints": [ "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 , ;`", "You need to truncate `majors`, `students`, and `majors_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" ] } ] }, { "id": "770", "title": "SELECT * FROM majors", "summary": "", "content": "", "steps": [ { "id": "770.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "cc515ec176f66868eb6e8ce3f54f3757865b8c5e" ] }, "content": "View all the data in `majors` table to make sure it's empty.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "780", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "780.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "953b44fb234d93e7d57dfb31cd81e639daaedc0c" ] }, "content": "Run the script to make sure it's not adding the first line anymore.", "hints": [ "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" ] } ] }, { "id": "790", "title": "SELECT * FROM majors", "summary": "", "content": "", "steps": [ { "id": "790.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "14f165b68a6ee33a5b3c7c38e67461a65d432561" ] }, "content": "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.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "800", "title": "Delete echo INSERT_MAJOR_RESULT", "summary": "", "content": "", "steps": [ { "id": "800.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "846de7540120129992950a7a083faa74f6338d2e" ] }, "content": "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`.", "hints": [ "Delete the `echo $INSERT_MAJOR_RESULT` line" ] } ] }, { "id": "810", "title": "Add if INSERT_MAJOR_RESULT", "summary": "", "content": "", "steps": [ { "id": "810.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "6be1a9929e157b3f85c0aebc0d2204543aedbe68" ] }, "content": "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`.", "hints": [ "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:\n```sh\nif [[ $INSERT_MAJOR_RESULT == \"INSERT 0 1\" ]]\nthen\n echo \"Inserted into majors, $MAJOR\"\nfi\n```" ] } ] }, { "id": "820", "title": "TRUNCATE majors CASCADE", "summary": "", "content": "", "steps": [ { "id": "820.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "c1149c3f22acc35de31f98aa44baccce84ebd9b4" ] }, "content": "In the psql prompt, truncate the `majors` table again so you can run the script and see the output.", "hints": [ "Here's an example: `TRUNCATE , ;`", "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`, and `majors_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" ] } ] }, { "id": "830", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "830.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "8fdd458330f61eca60a9c7d4d5af993c4e2c4783" ] }, "content": "Check to make sure the table is empty. Then, run the script.", "hints": [ "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" ] } ] }, { "id": "835", "title": "Add MAJOR_ID", "summary": "", "content": "", "steps": [ { "id": "835.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "518a1470b75959f530cee7cf0d3958def18da377" ] }, "content": "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.", "hints": [ "Here's an example of how to query the database: `MAJOR_ID=$($PSQL \"\")`", "For the query, you want to use the `SELECT`, `FROM`, and `WHERE` keywords", "Here's an example of how the query part looks: `SELECT FROM WHERE `", "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" ] } ] }, { "id": "840", "title": "Add COURSE_ID", "summary": "", "content": "", "steps": [ { "id": "840.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "eef63690d8b862b17a221aee7629215bdc91b343" ] }, "content": "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.", "hints": [ "Here's an example of how to query the database: `COURSE_ID=$($PSQL \"\")`", "For the query, you want to use the `SELECT`, `FROM`, and `WHERE` keywords", "Here's an example of how the query part looks: `SELECT FROM WHERE `", "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'\")`" ] } ] }, { "id": "850", "title": "Add if -z COURSE_ID", "summary": "", "content": "", "steps": [ { "id": "850.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "415774f07c6608d9bffa30869e93566ec87ab2de" ] }, "content": "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`.", "hints": [ "Here's an example of an `if`:\n```sh\nif [[ CONDITION ]]\nthen\n STATEMENTS\nfi\n```", "Make sure your `insert course` and `get new course_id` comments are in the statements area in that order", "Here's how it should look:\n```sh\nif [[ -z $COURSE_ID ]]\nthen\n # insert course\n\n # get new course_id\n\nfi\n```" ] } ] }, { "id": "853", "title": "Add INSERT_COURSE_RESULT", "summary": "", "content": "", "steps": [ { "id": "853.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "5f65cea5949b3850d7b4c782f545f03fa2028d22" ] }, "content": "Below the `insert course` comment, create an `INSERT_COURSE_RESULT` variable that inserts the course into the database.", "hints": [ "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 \"\")`", "For the query, you want to use the `INSERT INTO`, and `VALUES` keywords", "Here's an example of how the query part looks: `INSERT INTO () VALUES()`", "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')\")`" ] } ] }, { "id": "857", "title": "Add if INSERT_COURSE_RESULT", "summary": "", "content": "", "steps": [ { "id": "857.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "a95dd7bd94cdc790485736a95d05eae68c5660dd" ] }, "content": "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.", "hints": [ "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:\n```sh\nif [[ $INSERT_COURSE_RESULT == \"INSERT 0 1\" ]]\nthen\n echo \"Inserted into courses, $COURSE\"\nfi\n```" ] } ] }, { "id": "860", "title": "TRUNCATE majors CASCADE", "summary": "", "content": "", "steps": [ { "id": "860.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "16d55b7818f93cc8741283baa96d1b0893202f56" ] }, "content": "In the psql prompt, truncate the data from the `majors` table so you can run the script again.", "hints": [ "Here's an example: `TRUNCATE , ;`", "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`, and `majors_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" ] } ] }, { "id": "870", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "870.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "1bdfa02d987c851e7aa420bdea78cea632079ba6" ] }, "content": "Run the script to see if the courses get inserted into the database.", "hints": [ "Run your `insert_data.sh` script by executing it", "Type `./insert_data.sh` in the terminal and press enter", "The `majors` and `courses` 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" ] } ] }, { "id": "877", "title": "SELECT * FROM courses", "summary": "", "content": "", "steps": [ { "id": "877.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "f0ee62ec2cabe1b6782f14391ef33b68bb9039a0" ] }, "content": "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.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "880", "title": "Add echo TRUNCATE tables", "summary": "", "content": "", "steps": [ { "id": "880.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "981fdef5be715ee443579e42017b4cf514c51c89" ] }, "content": "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`.", "hints": [ "Here's an example: `echo $($PSQL \"\")`", "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\")`" ] } ] }, { "id": "890", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "890.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "c0d02098d1e90b2c88374f251a3ad1f0d611ac98" ] }, "content": "Run the script to see if it works.", "hints": [ "Run your `insert_data.sh` script by executing it", "Type `./insert_data.sh` in the terminal and press enter", "The `majors` and `courses` 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" ] } ] }, { "id": "900", "title": "Add COURSE_ID", "summary": "", "content": "", "steps": [ { "id": "900.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "0a9afdd45a563b0d4048f613df7cb0ef0d13186c" ] }, "content": "Awesome. That makes it easier. Below your `get new course_id` comment, set the `COURSE_ID` to the newly inserted `course_id`.", "hints": [ "Here's an example: `COURSE_ID=$($PSQL \"\")`", "For the query, you want to use the `SELECT`, `FROM`, and `WHERE` keywords", "Here's an example of how the query part looks: `SELECT FROM WHERE `", "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" ] } ] }, { "id": "910", "title": "Add INSERT_MAJORS_COURSES_RESULT", "summary": "", "content": "", "steps": [ { "id": "910.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "425ac8f2b1884a72b564b321e989c35958aa6e4c" ] }, "content": "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.", "hints": [ "Here's an example: `INSERT_MAJORS_COURSES_RESULT=$($PSQL \"\")`", "For the query, you want to use the `INSERT INTO`, and `VALUES` keywords", "Here's an example of how the query part looks: `INSERT INTO () VALUES()`", "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` and `course_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)\")`" ] } ] }, { "id": "920", "title": "Add if INSERT_MAJORS_COURSES_RESULT", "summary": "", "content": "", "steps": [ { "id": "920.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "ac15ead32de2ab5686746293a3f339f817094272" ] }, "content": "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`.", "hints": [ "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:\n```sh\nif [[ $INSERT_MAJORS_COURSES_RESULT == \"INSERT 0 1\" ]]\nthen\n echo \"Inserted into majors_courses, $MAJOR : $COURSE\"\nfi\n```" ] } ] }, { "id": "930", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "930.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "5a1a8fa1e3ae919acb15c910f14df461f70fb9d8" ] }, "content": "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.", "hints": [ "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` and `courses` tables should have three rows each, and the `majors_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" ] } ] }, { "id": "950", "title": "SELECT * FROM majors", "summary": "", "content": "", "steps": [ { "id": "950.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "9f50315446ec8ae8a809fb54437679ca8d4c51d2" ] }, "content": "Looks like it works. You better look around to make sure. View the data in the `majors` table.", "hints": [ "Use the psql prompt", "Use the `SELECT` and `FROM` keywords with `*` to view all the data in the `majors` 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" ] } ] }, { "id": "960", "title": "SELECT * FROM courses", "summary": "", "content": "", "steps": [ { "id": "960.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "c96e34431c020d92fd1545b3b6440f1a7d0b9438" ] }, "content": "Cool, check the `courses` table.", "hints": [ "Use the psql prompt", "Use the `SELECT` and `FROM` keywords with `*` to view all the data in the `courses` 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" ] } ] }, { "id": "965", "title": "SELECT * FROM majors_courses", "summary": "", "content": "", "steps": [ { "id": "965.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "a747cf1f56dff52b96341c28f1cae765bf4d2e99" ] }, "content": "Lastly, view the data in the `majors_courses` table. There should be four rows.", "hints": [ "Use the psql prompt", "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "970", "title": "cp students.csv", "summary": "", "content": "", "steps": [ { "id": "970.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "e4ea82d6ffc835f8ef7afbcadfebdb88a77d3501" ] }, "content": "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`.", "hints": [ "`cp` is the copy command", "Here's an example: `cp `", "Enter `cp students.csv students_test.csv` in the terminal" ] } ] }, { "id": "975", "title": "Remove all but fours lines", "summary": "", "content": "", "steps": [ { "id": "975.1", "setup": { "watchers": [ "./students_test.csv" ], "commits": [ "a7aff3a58ffc7d15ec6d772bececa8bb134c8ff4" ] }, "content": "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.", "hints": [ "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 from `students.csv`", "Make sure there's one empty line at the bottom", "The `students_test.csv` file should look like this:\n```csv\nfirst_name,last_name,major,gpa\nRhea,Kellems,Database Administration,2.5\nEmma,Gilbert,null,null\nKimberly,Whitley,Web Development,3.8\nJimmy,Felipe,Database Administration,3.7\n\n```" ] } ] }, { "id": "980", "title": "Add cat students_test.csv", "summary": "", "content": "", "steps": [ { "id": "980.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "f496b9292f64a3a42c79f79af760b263974212c1" ] }, "content": "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.", "hints": [ "It should look real similar to your other loop like this", "Here's an example:\n```sh\ncat | while IFS=\",\" read VARIABLE_1 VARIABLE_2 VARIABLE_3 VARIABLE_4\ndo\n STATEMENTS\ndone\n```", "Here's how it looks:\n```sh\ncat students_test.csv | while IFS=\",\" read FIRST LAST MAJOR GPA\ndo\n echo $FIRST\ndone\n```" ] } ] }, { "id": "985", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "985.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "976bf8cc08bcfca3a2a8b364f2698fe995becb06" ] }, "content": "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.", "hints": [ "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` and `courses` tables should have three rows each, and the `majors_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" ] } ] }, { "id": "1020", "title": "Delete echo all variables", "summary": "", "content": "", "steps": [ { "id": "1020.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "14e5580f89e56dcc4dfd7e514e84021023f2dceb" ] }, "content": "It works :sweat_smile: 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.", "hints": [ "Delete the `echo $FIRST` line" ] } ] }, { "id": "1022", "title": "Add if first_name", "summary": "", "content": "", "steps": [ { "id": "1022.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "b5ec2e1bd78736bc491570bfbbab0753e10d3930" ] }, "content": "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.", "hints": [ "Here's an example of an `if`:\n```sh\nif [[ CONDITION ]]\nthen\n STATEMENTS\nfi\n```", "The condition you want is `[[ $FIRST != \"first_name\" ]]`", "Your second loop should look like this:\n```sh\ncat students_test.csv | while IFS=\",\" read FIRST LAST MAJOR GPA\ndo\n if [[ $FIRST != \"first_name\" ]]\n then\n\n fi\ndone\n```" ] } ] }, { "id": "1025", "title": "Add comments", "summary": "", "content": "", "steps": [ { "id": "1025.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "9cd90e6ee69622899aee124bd3ee5c36b9abbb97" ] }, "content": "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.", "hints": [ "Here's an example of a single 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:\n```sh\ncat students_test.csv | while IFS=\",\" read FIRST LAST MAJOR GPA\ndo\n if [[ $FIRST != \"first_name\" ]]\n then\n # get major_id\n\n # if not found\n\n # set to null\n\n # insert student\n\n fi\ndone\n```" ] } ] }, { "id": "1030", "title": "Add MAJOR_ID", "summary": "", "content": "", "steps": [ { "id": "1030.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "d166af7ccbd48efab47bf32425d2e7c1af174e7e" ] }, "content": "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.", "hints": [ "Here's an example of how it looks: `MAJOR_ID=$($PSQL \"\")`", "For the query, you want to use the `SELECT`, `FROM`, and `WHERE` keywords", "Here's an example of how the query part looks: `SELECT FROM WHERE `", "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'\")`" ] } ] }, { "id": "1033", "title": "Add echo MAJOR_ID", "summary": "", "content": "", "steps": [ { "id": "1033.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "aecbf287c5f9c909a26442c0403bacad5f0d4f50" ] }, "content": "Below that, use `echo` to print the variable so you can see if it's working.", "hints": [ "Add `echo $MAJOR_ID` below the `MAJOR_ID` variable you just created" ] } ] }, { "id": "1037", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "1037.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "4313921ed4308ead549d8695539f382fba24272a" ] }, "content": "Run the script to see what happens.", "hints": [ "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` and `courses` tables should have three rows each, and the `majors_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" ] } ] }, { "id": "1047", "title": "Add if -z MAJOR_ID", "summary": "", "content": "", "steps": [ { "id": "1047.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "822474e4be3ff940108af548008f7b8012cdd9b4" ] }, "content": "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.", "hints": [ "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:\n```sh\nif [[ -z $MAJOR_ID ]]\nthen\n # set to null\n\nfi\n```" ] } ] }, { "id": "1048", "title": "Set MAJOR_ID to null", "summary": "", "content": "", "steps": [ { "id": "1048.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "4e6b98a01e124e3814adb3c30a4fbc33a53661b5" ] }, "content": "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.", "hints": [ "It should look like this:\n```sh\nif [[ -z $MAJOR_ID ]]\nthen\n # set to null\n MAJOR_ID=null\nfi\n```" ] } ] }, { "id": "1049", "title": "move echo MAJOR_ID", "summary": "", "content": "", "steps": [ { "id": "1049.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "097cd6db29d158111fc7522ae9345126dde7d2c5" ] }, "content": "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.", "hints": [ "Move the suggested line below the closing `fi` of the `if [[ -z $MAJOR_ID ]]` statement" ] } ] }, { "id": "1050", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "1050.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "55b225a11eaaa4794191e8326fa7c87b5e3b4b51" ] }, "content": "Run the script.", "hints": [ "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` and `courses` tables should have three rows each, and the `majors_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" ] } ] }, { "id": "1053", "title": "Delete echo MAJOR_ID", "summary": "", "content": "", "steps": [ { "id": "1053.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "27e4e8a4f44f9a091def397a69c1242fb5c974e4" ] }, "content": "Okay, that should work for inserting the student. Delete the `echo $MAJOR_ID` line.", "hints": [ "Delete the `echo $MAJOR_ID` line from the file" ] } ] }, { "id": "1057", "title": "\\d students", "summary": "", "content": "", "steps": [ { "id": "1057.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "ce2683948c706babce7e632cd67c0dd48239a2a9" ] }, "content": "One last thing to add. In the psql prompt, view the details of the `students` table so you can see what columns to add.", "hints": [ "Use the **d**isplay shortcut command", "Add the table name after the command", "It's the `\\d` command", "Here's an example: `\\d `", "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" ] } ] }, { "id": "1060", "title": "Add INSERT_STUDENT_RESULT", "summary": "", "content": "", "steps": [ { "id": "1060.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "5c5ca37a4758bb00e129db6115bb5247f250977e" ] }, "content": "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.", "hints": [ "Here's an example: `INSERT_STUDENT_RESULT=$($PSQL \"\")`", "For the query, you want to use the `INSERT INTO`, and `VALUES` keywords", "Here's an example of how the query part looks: `INSERT INTO (, ) VALUES(, )`", "In your query, make sure the columns to add are in this order: `first_name`, `last_name`, `major_id`, and `gpa`", "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)\")`" ] } ] }, { "id": "1070", "title": "Add if INSERT_STUDENT_RESULT", "summary": "", "content": "", "steps": [ { "id": "1070.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "177a05708774ac051f5bcb484614e55fdf592cf9" ] }, "content": "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, `.", "hints": [ "The condition should look like this: `if [[ $INSERT_STUDENT_RESULT == \"INSERT 0 1\" ]]`", "Use the `FIRST` and `LAST` 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:\n```sh\nif [[ $INSERT_STUDENT_RESULT == \"INSERT 0 1\" ]]\nthen\n echo \"Inserted into students, $FIRST $LAST\"\nfi\n```" ] } ] }, { "id": "1080", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "1080.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "96a2f1cb576f9d440fbc250db24030227d8620c3" ] }, "content": "Run the script to see if the students are getting added.", "hints": [ "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` and `courses` tables should have three rows each, and the `majors_courses` and `students` 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" ] } ] }, { "id": "1090", "title": "SELECT * FROM students", "summary": "", "content": "", "steps": [ { "id": "1090.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "191ca8bdb8c50e00d428783baf5c3cafd635418c" ] }, "content": "I think it's working. View all the data in the `students` table to make sure it matches the CSV file.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "1120", "title": "Change to cat courses.csv", "summary": "", "content": "", "steps": [ { "id": "1120.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "cdd261f37e45d893446e9fce328cb5639bb25fbe" ] }, "content": "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.", "hints": [ "Change `cat courses_test.csv` to `cat courses.csv`", "The suggested line should look like this: `cat courses.csv | while IFS=\",\" read MAJOR COURSE`" ] } ] }, { "id": "1130", "title": "Change to cat students.csv", "summary": "", "content": "", "steps": [ { "id": "1130.1", "setup": { "watchers": [ "./insert_data.sh" ], "commits": [ "e143c9c0bf026475556920fe4692d30a1a5f8cc2" ] }, "content": "Next, change the `cat students_test.csv` line to use the original file as well.", "hints": [ "Change the `cat students_test.csv` to `cat students.csv`", "The suggested line should look like this: `cat students.csv | while IFS=\",\" read FIRST LAST MAJOR GPA`" ] } ] }, { "id": "1140", "title": "./insert_data.sh", "summary": "", "content": "", "steps": [ { "id": "1140.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "ccc01a901f05d86c073cf31d47693bc636b02e3b" ] }, "content": "Time for the moment of truth. Run the script and see if it works.", "hints": [ "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, and `students` 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" ] } ] }, { "id": "1150", "title": "SELECT * FROM students", "summary": "", "content": "", "steps": [ { "id": "1150.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "d4d664773009869537d0b1b5ec7856ddaadde5ed" ] }, "content": "That was cool. View all the data in the `students` table to see what you ended up with.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "1160", "title": "SELECT * FROM majors", "summary": "", "content": "", "steps": [ { "id": "1160.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "89f3a61513090d08965ca5364623f4dbd0fab0f1" ] }, "content": "31 rows. That's how many are in the CSV file. Perfect. Next, check the `majors` table.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "1170", "title": "SELECT * FROM courses", "summary": "", "content": "", "steps": [ { "id": "1170.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "84e14c005b2ad3b7cde390e43c7ea92bd9baa7d3" ] }, "content": "7 rows. There must be 7 unique majors in the CSV file. View what's in the `courses` table.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "1180", "title": "SELECT * FROM majors_courses", "summary": "", "content": "", "steps": [ { "id": "1180.1", "setup": { "watchers": [ "../pg.log" ], "commits": [ "1728a2e37652d15c1093a206d53eb40d73375ddd" ] }, "content": "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.", "hints": [ "Use the `SELECT` and `FROM` 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" ] } ] }, { "id": "1190", "title": "ls", "summary": "", "content": "", "steps": [ { "id": "1190.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "f8e7798d180403c266f5db8a3ba61f1867a7142d" ] }, "content": "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.", "hints": [ "It's the `ls` command", "Don't use any flags with the command", "Enter `ls` in the terminal" ] } ] }, { "id": "1200", "title": "rm students_test.csv", "summary": "", "content": "", "steps": [ { "id": "1200.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "f0c8279dda356a1ab48df8300fce6070e2d9b631" ] }, "content": "Use the remove command (`rm`) to delete the `students_test.csv` file.", "hints": [ "Here's an example `rm `", "Enter `rm students_test.csv` in the terminal" ] } ] }, { "id": "1205", "title": "rm courses_test.csv", "summary": "", "content": "", "steps": [ { "id": "1205.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "32dde88b2fdc7b3a7590aaa26228b6ec8de2090f" ] }, "content": "Use the same command to delete the `courses_test.csv` file.", "hints": [ "Here's an example `rm `", "Enter `rm courses_test.csv` in the terminal" ] } ] }, { "id": "1210", "title": "ls", "summary": "", "content": "", "steps": [ { "id": "1210.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "cce1804e77b07207a8b0d347604245cef6dffdc0" ] }, "content": "List the contents of the folder again to make sure they're gone.", "hints": [ "Use the **l**ist command", "It's the `ls` command", "Don't use any flags with the command", "Enter `ls` in the terminal" ] } ] }, { "id": "1220", "title": "pg_dump --help", "summary": "", "content": "", "steps": [ { "id": "1220.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "98ae97acc8e01e4713c55e12e68a5dc1b1b6e3ac" ] }, "content": "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.", "hints": [ "Here's an example: ` `", "Enter `pg_dump --help` in the terminal", "The bash terminal, not the psql one", "Press enter until you have seen the whole manual" ] } ] }, { "id": "1230", "title": "dump database", "summary": "", "content": "", "steps": [ { "id": "1230.1", "setup": { "watchers": [ "../.bash_history" ], "commits": [ "f907ff0fe7ab161189c5e61cc3657852015cb731" ] }, "content": "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.", "hints": [ "Enter the suggested command in the terminal", "The bash terminal, not the psql one", "Make sure you are in the `project` folder first" ] } ] } ] }