×
By the end of this chapter, you should be able to:
When working with SQL, the commands you will be using fall into two major categories:
DDL - Data Definition Language - this refers to the SQL syntax and commands around creating, modifying and deleting tables, columns and databases.
DML - Data Manipulation Language - this refers to the SQL syntax and commands around creating, reading, modifying and deleting rows.
Let's first focus on DDL.
Open up postgres by typing psql
in your terminal. Next enter the following:
CREATE TABLE users (id SERIAL PRIMARY KEY, first_name TEXT, last_name TEXT);
In the example above, users
is the name of the table we are creating. The id
, first_name
, and last_name
are all columns in the users
table. SERIAL
and TEXT
are examples of data types, which we will talk about in detail next. PRIMARY KEY
is a constraint placed on the column.
\d+ users
should show our newly-created users
table with 3 columns: id
, first_name
, last_name
id | first_name | last_name |
---|---|---|
In relational databases like postgres, you must specify the type of data that you plan to store in a column. Here are the types in postgres:
For more on the difference between text, varchar, and char, check out this StackOverflow post.
Constraints are certain database table restrictions that are either implicitly or explicitly created via the database schema. Constraints are a key part of DDL. Violating a constraint will throw a database error and (usually) abort the intended operation.
Common constraints are:
CREATE TABLE college_students ( id SERIAL PRIMARY KEY, last_name VARCHAR(50), first_name VARCHAR(50), major VARCHAR(50) NOT NULL );
CREATE TABLE phonebook ( id SERIAL PRIMARY KEY, last_name VARCHAR(50), first_name VARCHAR(50), phone_number VARCHAR(7) UNIQUE );
CREATE TABLE users (id SERIAL PRIMARY KEY, first_name TEXT, last_name TEXT);
foreign key - we'll talk about foreign keys later when we talk about joins.
check - an expression is provided that must evaluate truthy for the operation to proceed. For example, if you have a table of products for an online shopping site, you might put a check constraint on products to have price > 0.
CREATE TABLE products ( product_no SERIAL PRIMARY KEY, name TEXT, price NUMERIC CHECK (price > 0) );
For more info about constraints and proper syntax in PostgreSQL, check out the PostgreSQL docs
ALTER TABLE users ADD COLUMN favorite_number INTEGER;
\d+ users
and we should see favorite_number
id | first_name | last_name | favorite_number |
---|
ALTER TABLE users DROP COLUMN favorite_number;
\d+ users
and favorite_number should no longer exist
id | first_name | last_name |
---|
ALTER TABLE users ADD COLUMN jobb TEXT;
id | first_name | last_name | jobb |
---|
ALTER TABLE users RENAME COLUMN jobb TO job;
\d+ users
and we should see 'job' spelled correctly now
id | first_name | last_name | job |
---|
ALTER TABLE users ADD COLUMN favorite_number TEXT; ALTER TABLE users ALTER COLUMN favorite_number SET DATA TYPE VARCHAR(100);
\d+ users
and we should see a different data type for our column
ALTER TABLE users ADD CONSTRAINT favorite_number NOT NULL;
\d+ users
and we should see favorite_number is not nullable.
When we perform CRUD operations on our rows (not columns, tables or databases) we are using DML or Data Manipulation Language.
CRUD | SQL |
---|---|
Create | INSERT |
Read | SELECT |
Update | UPDATE |
Delete | DELETE |
Let's get started with reading data from our tables using SELECT
.
--to select all rows and columns-- SELECT * FROM users; --to select specific columns-- SELECT id, first_name FROM users; --to select specific columns and rows-- SELECT id, first_name FROM users WHERE id=1;
To insert or add data to a table - we use the INSERT command
--start with the INSERT INTO commands and then specify a table(column1, column2, ...) and VALUES for each column. INSERT INTO users(first_name, last_name) VALUES ('Elie', 'Schoppik');
To update a row or multiple rows we use the UPDATE
command.
UPDATE users SET first_name = 'Elie'; -- will update all users UPDATE users SET first_name = 'Elie' WHERE id = 1; -- will update a user with an id of 1
To delete a row or multiple rows we use the DELETE FROM
command.
DELETE FROM users; -- will delete all users DELETE FROM users WHERE id=1; -- will delete a user with an id of 1
When you're ready, move on to Operators and Aggregates