In this lab you get experience using basic SQL commands, using SQLite.
Open a text document and this SQL interpreter (clear out the default SQL supplied). Throughout this exercise, test your SQL commands in the interpreter, but keep a copy of all of your working commands in your text document.
Use the CREATE TABLE
command to match the structure given in the diagram above. Here is the first one to get you started:
CREATE TABLE Customers(
CustomerID INTEGER PRIMARY KEY,
Name TEXT NOT NULL
);
Remember to name the primary key similarly to the table (e.g. CustomerID
in the Customers
table). The other two tables each have a foreign key (e.g. the Orders
table will have a CustomerID
column). Feel free to use the lecture notes as a guide. In SQLite, the Date
can be stored as a VARCHAR(10)
of the form YYYY-MM-DD
. For price we will use the INTEGER type and store the number of cents, because floating points are prone to roundoff error hence should not be used for currency (other DBMS's support specific currency types).
Next, we will populate the tables using the INSERT command. For the customers table, use either
INSERT INTO Customers (Name) VALUES('DrKow');
or INSERT INTO Customers VALUES(NULL, 'DrKow');
for the first customer. Fill in two more customers: 'Plato' and 'Socrates'. You can view your progress by using SELECT * FROM Customers;
or another similar SELECT command.
Fill in the Orders and LineItems tables to reflect the following orders.
Now time for some SELECT
commands. Write the following 5 queries:
WHERE
clause).*
in your previous command to SUM(Price)
instead. This is known as an aggregate function.Using an UPDATE command, change the price of the USB-C adapter to $8.99 in the 2nd order.
Using a DELETE command, delete the 2nd power supply from the first order.
When you are done, show me your work to get credit for it.