SQL (Structured Query Language) is a programming language that allows us to manage data in a relational database.relational database is when two or more tables have some kind of relationship between them.Postgres app and download it.In .zshrc add:
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/latest/bin
psql to enter the psql shell.help to see the available commands.\l to list all databases.To create a database:
CREATE DATABASE test;
In terminal, type:
psql -h localhost -p 5432 -U amigoscode test
Alternatively, type psql to enter the psql shell and then to connect to the database, type:
\c test
In terminal, type:
DROP DATABASE test;
This is a generic way you can create a table (without constraints):
CREATE TABLE table_name (
column_name data_type
)
Example:
CREATE TABLE person (
id INT,
first_name VARCHAR(50),
last_name VARCHAR(50),
gender VARCHAR(7),
date_of_birth DATE,
);
\d (for describe) to see the tables in the database.\d person to see the columns of the person table.This is a generic way you can create a table with constraints:
CREATE TABLE table_name (
column_name data_type column_constraints,
)
Example:
CREATE TABLE person (
id BIGSERIAL NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
gender VARCHAR(7) NOT NULL,
date_of_birth DATE NOT NULL,
);
Note: BIGSERIAL is a signed eight-byte integer. It auto increments automatically for you.
To drop a table, type:
DROP TABLE person;
Let’s say we want to insert a new person into our table:
INSERT INTO person (
first_name,
last_name,
gender,
date_of_birth
)
VALUES ('Anne', 'Smith', 'FEMALE', DATE '1988-01-09');
Notice here we didn’t specify the id column. This is because the BIGSERIAL data type auto increments for us.
Note: To list only the tables, type \dt.
id), click on Download Data, Select SQL..sql file.cd to the directory where the file is.psql to enter the psql shell.DROP TABLE person;.\i person.sql to execute the file.SELECT * FROM person; to see the data.SELECT * FROM person;
SELECT first_name, last_name FROM person;
SELECT * FROM person ORDER BY first_name; -- it's ASC by default
SELECT * FROM person ORDER BY first_name DESC;
SELECT DISTINCT countr_of_birth FROM person ORDER BY countr_of_birth;
WHERE Clause and ANDSELECT * FROM person WHERE gender = 'Female';
SELECT * FROM person WHERE gender = 'Female' AND country_of_birth = 'Poland';
SELECT 1 = 1; -- equals
SELECT 1 <> 1; -- not equals
SELECT 1 > 1; -- greater than
SELECT 1 < 1; -- less than
SELECT 1 >= 1; -- greater than or equal
SELECT 1 <= 1; -- less than or equal
SELECT * FROM person LIMIT 10;
SELECT * FROM person OFFSET 10 LIMIT 10;
SELECT * FROM person OFFSET 10 FETCH FIRST 10 ROW ONLY; -- this is original way of limiting in sql
SELECT * FROM person WHERE country_of_birth IN ('Poland', 'Germany');
SELECT * FROM person WHERE date_of_birth BETWEEN DATE '1990-01-01' AND DATE '1999-12-31';
SELECT * FROM person WHERE email LIKE '%@bloomberg.com';
SELECT * FROM person WHERE email LIKE '_______@%'; -- 7 characters before @
SELECT * FROM person WHERE email ILIKE '%@bloomberg.com'; -- case insensitive
SELECT country_of_birth, COUNT(*) FROM person GROUP BY country_of_birth;
SELECT country_of_birth, COUNT(*) FROM person GROUP BY country_of_birth HAVING COUNT(*) > 5;
-- ORDER BY must come after HAVING
SELECT country_of_birth, COUNT(*) FROM person GROUP BY country_of_birth HAVING COUNT(*) > 5 ORDER BY country_of_birth;
SELECT MAX(price) FROM car;
SELECT MIN(price) FROM car;
SELECT AVG(price) FROM car;
SELECT ROUND(AVG(price)) FROM car;
SELECT make, model, MIN(price) FROM car GROUP BY make, model;
SELECT SUM(price) FROM car;
SELECT make, SUM(price) FROM car GROUP BY make;
SELECT 1 + 1; -- addition
SELECT 1 - 1; -- subtraction
SELECT 1 * 1; -- multiplication
SELECT 1 / 1; -- division
SELECT 1 ^ 1; -- power
SELECT 1!; -- factorial
SELECT 1 % 1; -- modulo
SELECT ROUND(1.5); -- 2
SELECT ROUND(1.43465, 2); -- 1.43
SELECT first_name AS name FROM person;
SELECT COALESCE(NULL, 'Amigoscode'); -- Amigoscode
SELECT COALESCE(NULL, NULL, 'Amigoscode'); -- Amigoscode
Returns null if the arguments are equal.
SELECT NULLIF(1, 2); -- 1
SELECT NULLIF(1, 1); -- NULL
SELECT 10 / 0; -- ERROR
SELECT 10 / NULL; -- NULL
SELECT 10 / NULLIF(0, 0); -- NULL
SELECT COALESCE(10 / NULLIF(0, 0), 0); -- 0
SELECT NOW(); -- 2021-01-09 16:00:00.000000
SELECT NOW()::DATE; -- 2021-01-09
SELECT NOW()::TIME; -- 16:00:00.000000
SELECT NOW() - INTERVAL '1 YEAR';
SELECT NOW() - INTERVAL '10 YEARS';
SELECT NOW() - INTERVAL '1 MONTH';
SELECT NOW() - INTERVAL '1 DAY';
SELECT (NOW() - INTERVAL '1 DAY')::DATE;
SELECT EXTRACT(YEAR FROM NOW()); -- 2021
SELECT EXTRACT(MONTH FROM NOW()); -- 1
SELECT EXTRACT(DAY FROM NOW()); -- 9
SELECT EXTRACT(DOW FROM NOW()); -- 6
SELECT AGE('1990-01-01'); -- 31 years 8 days
SELECT AGE(NOW(), date_of_birth) AS age FROM person;
ALTER TABLE person DROP CONSTRAINT person_pkey;
ALTER TABLE person ADD PRIMARY KEY (id);
A unique constraint is a single field or combination of fields that uniquely defines a record.
-- Adding Unique Constraints
ALTER TABLE person ADD CONSTRAINT person_email_key UNIQUE (email);
ALTER TABLE person ADD UNIQUE (email);
-- Dropping Unique Constraints
ALTER TABLE person DROP CONSTRAINT person_email_key;
A check constraint is used to limit the value range that can be placed in a column.
-- Adding Check Constraints
ALTER TABLE person ADD CONSTRAINT gender_constraint CHECK(gender = 'Female' OR gender = 'Male');
DELETE FROM person WHERE id = 1;
UPDATE person SET first_name = 'Anne' WHERE id = 2;
INSERT INTO person (id, first_name, last_name, gender, email, date_of_birth, country_of_birth)
VALUES (2017, 'Russ' "Ruddoch', 'Male', 'rruddoch7@hhs.gov', DATE 1952-09-25', 'Norway')
ON CONFLICT (id) DO NOTHING;
INSERT INTO person (id, first_name, last_name, gender, email, date_of_birth, country_of_birth)
VALUES (2017, 'Russ' "Ruddoch', 'Male', 'rruddoch7@hhs.gov', DATE 1952-09-25', 'Norway')
ON CONFLICT (id) DO UPDATE SET email = EXCLUDED.email, date_of_birth = EXCLUDED.date_of_birth;
create table car (
id BIGSERIAL NOT NULL PRIMARY KEY,
make VARCHAR(100) NOT NULL,
model VARCHAR(100) NOT NULL,
price NUMERIC(19, 2) NOT NULL
);
create table person (
id BIGSERIAL NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
gender VARCHAR(7) NOT NULL,
email VARCHAR(100) NOT NULL,
date_of_birth DATE NOT NULL,
country_of_birth VARCHAR(50) NOT NULL,
car_id BIGINT REFERENCES car(id),
UNIQUE(car_id)
);
insert into person (first_name, last_name, gender, email, date_of_birth, country_of_birth) values ('Fernanda', 'Beardon', 'Female', 'fernandabais.gd', '1953-10-28', 'Finland');
insert into person (first_name, last_name, gender, email, date_of_birth, country_of_birth) values ('Omar', 'Colomore', 'Male', null, '1921-04-03', 'Finland');
insert into person (first_name, last_name, gender, email, date_of_birth, country_of_birth) values ('Adriana', 'Matuschek', 'Female', 'amatuschek2@feedburner.com', null, null);
insert into car (make, model, price) values ('Land Rover', 'Sterling', '87665.38');
insert into car (make, model, price) values ('GMC', 'Acadia', '17662.69');
UPDATE person SET car_id = 1 WHERE id = 1;
It returns rows that have matching values in both tables.
SELECT * FROM person
JOIN car ON person.car_id = car.id;
If there are no matches in the right table, it returns null values.
Example:
SELECT * FROM person
LEFT JOIN car ON person.car_id = car.id;
Return only the people who don’t have a car:
SELECT * FROM person
LEFT JOIN car ON person.car_id = car.id
WHERE car.* IS NULL;
You must delete the child records first.
-- first option is to delete the records from the person table
DELETE FROM person WHERE id = 1;
-- second option is to remove car_id from the person table for that specific record
UPDATE person SET car_id = NULL WHERE id = 1;
In psql terminal, type:
\copy (SELECT * FROM person) LEFT JOIN car ON person.car_id = car.id TO 'person.csv' DELIMITER ',' CSV HEADER;
SELECT * FROM pg_available_extensions;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
SELECT uuid_generate_v4(); --random uuid
Instead of BIGSERIAL, we can use UUID, don’t forget to update foreign key types too.
create table car (
car_uuid UUID NOT NULL PRIMARY KEY,
make VARCHAR(100) NOT NULL,
model VARCHAR(100) NOT NULL,
price NUMERIC(19, 2) NOT NULL CHECK (price > 0)
);
create table person (
person_uuid UUID NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
gender VARCHAR(7) NOT NULL,
email VARCHAR(100),
date_of_birth DATE NOT NULL,
country_of_birth VARCHAR(50) NOT NULL,
car_uuid UUID REFERENCES car(car_uuid),
UNIQUE(car_uuid),
UNIQUE(email)
);
-- INSERT INTO PERSON
insert into person (person_uuid, first_name, last_name, gender, email, date_of_birth, country_of_birth)
values (uuid_generate_v4(), 'Fernanda', 'Beardon', 'Female', 'fernandab@is.gd', '1953-10-28', 'Comoros');
insert into person (person_uuid, first_name, last_name, gender, email, date_of_birth, country_of_birth)
values (uuid_generate_v4(), 'Omar', 'Colomore', 'Male', null, '1921-04-03', 'Finland');
insert into person (person_uuid, first_name, last_name, gender, email, date_of_birth, country_of_birth)
values (uuid_generate_v4(), 'Adriana', 'Matuschek', 'Female', 'amatuschek2@feedburner.com', '1965-02-28', 'Cameroon');
-- INSERT INTO CAR
insert into car (car_uuid, make, model, price) values (uuid_generate_v4(), 'Land Rover', 'Sterling', '87665.38');
insert into car (car_uuid, make, model, price) values (uuid_generate_v4(), 'GMC', 'Acadia', '17662.69');
SELECT * FROM person,
JOIN car USING (car_uuid);