-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite-hr.sh
More file actions
executable file
·60 lines (55 loc) · 1.27 KB
/
sqlite-hr.sh
File metadata and controls
executable file
·60 lines (55 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/bash
# Database
readonly DB_NAME="hr.db"
# Remove the database if it exists
rm -f $DB_NAME
# Check sqlite3 is installed
if ! [ -x "$(command -v sqlite3)" ]; then
echo 'Error: sqlite3 is not installed.' >&2
exit 1
fi
# Create a database named hr.db add employee table
sqlite3 $DB_NAME <<EOF
CREATE TABLE employee (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER NOT NULL,
salary INTEGER NOT NULL,
dept_it INTEGER NOT NULL
);
EOF
# Insert data into the employee table from csv file employee.csv, --skip 1 to
# skip the header
sqlite3 $DB_NAME <<EOF
.mode csv
.separator ;
.import employee.csv --skip 1 employee
EOF
# Query the employee table
# Create table dept as for department
sqlite3 $DB_NAME <<EOF
CREATE TABLE dept (
id INTEGER PRIMARY KEY,
label TEXT NOT NULL
);
EOF
# Insert data into the dept table from csv file dept.csv, --skip 1 to skip
# the header
sqlite3 $DB_NAME <<EOF
.mode csv
.separator ;
.import dept.csv --skip 1 dept
EOF
# Join the employee and dept tables based on the dept_id column
# Print the name of the employees and the department label where the department
# id is 0
sqlite3 $DB_NAME <<EOF
.headers off
.mode column
.separator ROW "\n"
SELECT e.name, d.label
FROM employee e
JOIN dept d
ON e.dept_it = d.id
WHERE d.id = 0;
EOF