Create Tables: employees and departments
In this step, you will create two tables, employees
and departments
, and insert some sample data. These tables will be used to create and query views in the following steps.
First, open the SQLite shell by running the following command in the terminal:
sqlite3 /home/labex/project/employees.db
This command opens the SQLite shell and connects to the employees.db
database. If the database file does not exist, SQLite will create it.
Now, create the employees
table with the following SQL statement:
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
first_name TEXT,
last_name TEXT,
department TEXT,
salary REAL
);
This SQL statement creates a table named employees
with five columns: id
, first_name
, last_name
, department
, and salary
. The id
column is the primary key for the table.
Next, insert some sample data into the employees
table:
INSERT INTO employees (first_name, last_name, department, salary) VALUES
('John', 'Doe', 'Sales', 50000.00),
('Jane', 'Smith', 'Marketing', 60000.00),
('Peter', 'Jones', 'Sales', 55000.00),
('Mary', 'Brown', 'IT', 70000.00);
This SQL statement inserts four rows into the employees
table.
Now, create the departments
table with the following SQL statement:
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT,
location TEXT
);
This SQL statement creates a table named departments
with three columns: id
, name
, and location
. The id
column is the primary key for the table.
Next, insert some sample data into the departments
table:
INSERT INTO departments (name, location) VALUES
('Sales', 'New York'),
('Marketing', 'London'),
('IT', 'San Francisco');
This SQL statement inserts three rows into the departments
table.
You can verify the table creation and data insertion by querying the tables:
SELECT * FROM employees;
SELECT * FROM departments;
These commands will display the contents of the employees
and departments
tables, respectively.