Building and Managing Your Database with SQL

When working with SQL, it’s crucial to understand how to both build and manipulate tables. This knowledge allows you to effectively manage your data, ensuring it stays clean, up-to-date and relevant. In this post, we’ll cover the basics of four essential SQL operations: CREATE, INSERT, UPDATE, and DELETE.

When writing SQL queries, there are also a couple options for where to write and execute them. A database management system (DBMS) such a PostgreSQL is great when using their built-in tools, and also for direct interaction with your databases. There is also the option of an integrated development environment (IDE), such as VS Code. An IDE will offer better file management through version control, such as Git, and it will also allow you to use other languages such as Python or JavaScript. In this post I will be using PostgreSQL, which I will get a post up soon on how to set that up on your machine, but for now, let's manipulate some data, shall we?

CREATE

Before you can store data, you need to create a table. The CREATE TABLE statement is used for this purpose. It allows you to define the name and structure of your table, including the columns and their data types (which we learned about HERE). The basic syntax for creating a table and it's structure:

-- Creating a table named 'customers'
CREATE TABLE customers (
    customer_id SERIAL PRIMARY KEY,
    customer_name VARCHAR(100) NOT NULL,
    customer_email VARCHAR(100),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Breaking this example down:

  • CREATE TABLE customers( ): simply put, we are creating a table called 'customers'. Everything that is inside the parentheses are our column headings.

  • customer_id SERIAL PRIMARY KEY: our first column name 'customer_id' and the data type is SERIAL (meaning that it will begin at number 1 and increase sequentially for each of the following data rows that are added). We also have this column as our PRIMARY KEY, meaning that we are using this as a unique identifier, so there can be no duplicates or NULL values.

  • customer_name VARCHAR(100) NOT NULL: our column name is 'customer_name', and VARCHAR is short for variable character and includes numbers, letters, spaces, punctuation marks and special characters(such as !, @ or #). NOT NULL constraint means that the column must always have a value - in this case, there must always be a customer name.

  • customer_email: column name of 'customer_email' and it's a VARHCAR data type, as described above.

  • created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP: a column name of 'created_at' and it's data type is TIMESTAMP, which is used to store date and time information. By default, this will include the year, month, day, hour, minute, second and fractional seconds.

In my current project, I am creating a table called 'grad_rates_by_state' and creating my first column as 'us_state' using the VARCHAR data type (I forgot to mention earlier, that the number in parentheses dictates the number of characters allowed - in this case, 50 characters). The following columns are all titled after the years that the data will come from, and their data type is NUMERIC, allowing me to be able to do calculations with that data.

INSERT

Once you have your table, you’ll want to add some data to it. This is where the INSERT INTO statement comes in. It allows you to insert new rows into your table. Our basic syntax for the above example -

-- Inserting a single row
INSERT INTO customers (customer_name, customer_email)
VALUES ('John Doe', 'john.doe@example.com');

-- Inserting multiple rows
INSERT INTO customers (customer_name, customer_email)
VALUES
  ('Jane Smith', 'jane.smith@example.com'),
  ('Emily Jones', 'emily.jones@example.com');

Here, we’re inserting new customers into the customers table. The first example adds a single customer, while the second adds two customers at once (separating the first and second customer with a ' , '.

In my project for graduation rates:

I find it helpful to simply comment out your previous CREATE TABLE code to be able to see the columns that you created so you don't have to go searching for them when you want to add data and need the column names.

As we see in the results that are returned with in the output, I added a state name and one numeric value in the column 'year_1980'. Trust me, it does get easier when you have full datasets that you can import directly into a table that you created. It's not always this tedious.

So, now we have created our table and added a little data. What if we need to change the data that already exists in our table? That's what the UPDATE statement is for...

UPDATE

Over time, you’ll likely need to update the data in your tables. The UPDATE statement allows you to modify existing rows. It’s essential to use the WHERE clause to specify which rows should be updated, otherwise, you’ll update all rows in the table. Here's a sample of the syntax used:

-- Updating a single column
UPDATE customers
SET customer_email = 'john.doe@newdomain.com'
WHERE customer_name = 'John Doe';

-- Updating multiple columns
UPDATE customers
SET customer_name = 'Jane Doe', customer_email = 'jane.doe@newdomain.com'
WHERE customer_id = 1;

In the first example, we’re updating the email address for a customer named John Doe. In the second, we’re updating both the name and email for the customer with an ID of 1.

Using the graduation rate project:

We first use our UPDATE statement, followed by our table name (grad_rates_by_state), then we want to SET <given column name> equal to a specific value WHERE <given row value> is currently equal to a specific value - in this case, 'Georgia'. In the graduation rates screenshots, you can see that I have changed the value in the column for year_1980 for the state of Georgia.

What happens if we want to remove the data completely? Well, we DELETE it!

DELETE

Sometimes, you’ll need to delete data from your tables. The DELETE FROM statement allows you to remove rows. As with the UPDATE statement, you should use the WHERE clause to specify which rows to delete to avoid removing all data from the table.

-- Deleting a single row
DELETE FROM customers
WHERE customer_id = 1;
--Remember, our customer_id is a SERIAL PRIMARY KEY, so there is always 
--only one of each id #. 

-- Deleting multiple rows
DELETE FROM customers
WHERE customer_name = 'John Doe';

In these examples, the first deletes the customer with an ID of 1, while the second deletes all customers named John Doe. In the graduation rates project, we want to delete the data that we previously inserted:

First, selecting our table (grad_rates_by_state), and then the row that we want to delete(the us_state that is equal to the string 'Georgia')

The resulting output is the removal of the row of data that we had previously entered. Be careful using the DELETE statement, or you could end up deleting multiple rows of data, or even your entire table.

Conclusion

Understanding how to create, insert, update, and delete data in your SQL tables is fundamental to managing a database. These operations form the backbone of your ability to maintain and manipulate data effectively. The more you practice, the more that all of these become second nature, and your ability to handle more complex databases will grow. Happy coding, y'all!

As always, please feel free to leave any comments or feedback (especially if you've made it this far! Bravo!)