Skip to content

Commit

Permalink
up dos arquivos do projeto de banco de dados de alunos
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-Vieira-R committed Mar 27, 2023
0 parents commit 9bca07b
Show file tree
Hide file tree
Showing 14 changed files with 926 additions and 0 deletions.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
## Banco de dados de Alunos, Cursos e Disciplinas

##### Modelagem de um banco de dados relacional de estudantes de uma universidade, assim como a inserção de dados de dois arquivos CSV (um para os [cursos](courses.csv) e outro para os [alunos](students.csv)) utilizando [script BASH shell](insert_data.sh) , e também a consulta de informações através de queries, em [outro script BASH shell](students_info.sh).

Utilizando o PostgreSQL construi esse banco de dados seguindo o seguinte modelo:

![modelo](images/bd_model.png)

Para a inserção dos dados, desenvolvi um script shell, que lê arquivos CSV enquanto salva em variáveis e depois insere no banco de dados em loop:
```
# Exemplo da inserção dos cursos(o método para as disciplinas e alunos é o mesmo):
PSQL="psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c"
cat courses.csv | while IFS="," read MAJOR COURSE
do
if [[ $MAJOR != "major" ]]
then
# get major_id
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")
# if not found
if [[ -z $MAJOR_ID ]]
then
# insert major
INSERT_MAJOR_RESULT=$($PSQL "INSERT INTO majors(major) VALUES('$MAJOR')")
if [[ $INSERT_MAJOR_RESULT == "INSERT 0 1" ]]
then
echo Inserted into majors, $MAJOR
fi
# get new major_id
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")
fi
```
Após a execução do script, as tabelas ficaram populadas assim:

- tabelas de cursos e disciplinas:
![cursos_e_disciplinas](images/tables_1.png)

- tabelas de alunos:
![alunos](images/tables_2.png)

- tabela de relação curso_disciplina:
![cursos_disciplina](images/tables_3.png)


---


###### Consulta de informações utilizando queries SQL:

Para consultar dados significativos com queries SQL, também desenvolvi um [script shell que realiza as queries](students_info.sh). O output no terminal desse script ficou assim:

![querie1](images/query_1.png)
![querie2](images/query_2.png)
![querie3](images/query_3.png)


---

Com esse projeto eu aprendi MUITO, manipulação de arquivos com BASH, além de piping, comparações numéricas, comparações de string, execução de expressões, variáveis e muito mais... E isso só de bash scripting...

Sobre SQL eu realmente adquiri expertise em queries, e utilizei diversas funções, JOINs, ordenação, agrupamento e bem mais.

E o mais importante de tudo, eu me diverti pra caramba com esse projeto!

Para aqueles que possuem um ambiente linux e quiserem ver esse projeto em sua máquina, siga os passos a seguir:

> 1. Instale o postgreSQL em sua máquina
>
> 2. insira o comando `psql students < students.sql` que vai ciar o BD sem os dados ou `psql students < students_full.sql` que já cria o BD populado!
>
> 3. Caso tenha criado só o modelo sem os dados, execute o script que insere os dados `./insert_data.sh`
>
> 4. Agora finalmente, execute o script com as queries `./students_info.sh`
29 changes: 29 additions & 0 deletions courses.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
major,course
Database Administration,Data Structures and Algorithms
Web Development,Web Programming
Database Administration,Database Systems
Data Science,Data Structures and Algorithms
Network Engineering,Computer Networks
Database Administration,SQL
Data Science,Machine Learning
Network Engineering,Computer Systems
Computer Programming,Computer Networks
Database Administration,Web Applications
Game Design,Artificial Intelligence
Data Science,Python
Computer Programming,Object-Oriented Programming
System Administration,Computer Systems
Game Design,Calculus
Web Development,Data Structures and Algorithms
Data Science,Calculus
Web Development,Object-Oriented Programming
Game Design,Game Architecture
System Administration,Computer Networks
Game Design,Algorithms
System Administration,UNIX
System Administration,Server Administration
Computer Programming,Computer Systems
Computer Programming,Python
Network Engineering,Network Security
Web Development,Web Applications
Network Engineering,Algorithms
Binary file added images/bd_model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/query_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/query_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/query_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tables_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tables_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tables_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions insert_data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash

# Script to insert data from courses.csv and students.csv into students database

PSQL="psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c"
echo $($PSQL "TRUNCATE students, majors, courses, majors_courses")

cat courses.csv | while IFS="," read MAJOR COURSE
do
if [[ $MAJOR != "major" ]]
then
# get major_id
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")

# if not found
if [[ -z $MAJOR_ID ]]
then
# insert major
INSERT_MAJOR_RESULT=$($PSQL "INSERT INTO majors(major) VALUES('$MAJOR')")
if [[ $INSERT_MAJOR_RESULT == "INSERT 0 1" ]]
then
echo Inserted into majors, $MAJOR
fi

# get new major_id
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")
fi

# get course_id
COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'")

# if not found
if [[ -z $COURSE_ID ]]
then
# insert course
INSERT_COURSE_RESULT=$($PSQL "INSERT INTO courses(course) VALUES('$COURSE')")
if [[ $INSERT_COURSE_RESULT == "INSERT 0 1" ]]
then
echo Inserted into courses, $COURSE
fi

# get new course_id
COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'")
fi

# insert into majors_courses
INSERT_MAJORS_COURSES_RESULT=$($PSQL "INSERT INTO majors_courses(major_id, course_id) VALUES($MAJOR_ID, $COURSE_ID)")
if [[ $INSERT_MAJORS_COURSES_RESULT == "INSERT 0 1" ]]
then
echo Inserted into majors_courses, $MAJOR : $COURSE
fi
fi
done

cat students.csv | while IFS="," read FIRST LAST MAJOR GPA
do
if [[ $FIRST != "first_name" ]]
then
# get major_id
MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'")

# if not found
if [[ -z $MAJOR_ID ]]
then
# set to null
MAJOR_ID=null
fi

# insert student
INSERT_STUDENT_RESULT=$($PSQL "INSERT INTO students(first_name, last_name, major_id, gpa) VALUES('$FIRST', '$LAST', $MAJOR_ID, $GPA)")
if [[ $INSERT_STUDENT_RESULT == "INSERT 0 1" ]]
then
echo Inserted into students, $FIRST $LAST
fi
fi
done
32 changes: 32 additions & 0 deletions students.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
first_name,last_name,major,gpa
Rhea,Kellems,Database Administration,2.5
Emma,Gilbert,null,null
Kimberly,Whitley,Web Development,3.8
Jimmy,Felipe,Database Administration,3.7
Kyle,Stimson,null,2.8
Casares,Hijo,Game Design,4.0
Noe,Savage,null,3.6
Sterling,Boss,Game Design,3.9
Brian,Davis,null,2.3
Kaija,Uronen,Game Design,3.7
Faye,Conn,Game Design,2.1
Efren,Reilly,Web Development,3.9
Danh,Nhung,null,2.4
Maxine,Hagenes,Database Administration,2.9
Larry,Saunders,Data Science,2.2
Karl,Kuhar,Web Development,null
Lieke,Hazenveld,Game Design,3.5
Obie,Hilpert,Web Development,null
Peter,Booysen,null,2.9
Nathan,Turner,Database Administration,3.3
Gerald,Osiki,Data Science,2.2
Vanya,Hassanah,Game Design,4.0
Roxelana,Florescu,Database Administration,3.2
Helene,Parker,Data Science,3.4
Mariana,Russel,Web Development,1.8
Ajit,Dhungel,null,3.0
Mehdi,Vandenberghe,Database Administration,1.9
Dejon,Howell,Web Development,4.0
Aliya,Gulgowski,System Administration,2.6
Ana,Tupajic,Data Science,3.1
Hugo,Duran,null,3.8
Loading

0 comments on commit 9bca07b

Please sign in to comment.