You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
264 lines
4.5 KiB
264 lines
4.5 KiB
# PostGres - Advanced
|
|
|
|
## Lesson Objectives - important
|
|
|
|
1. Linking Tables
|
|
1. Alias
|
|
1. Indexes
|
|
1. Default Values
|
|
1. Constraints
|
|
1. Distinct
|
|
|
|
## Lesson Objectives - good to know about
|
|
|
|
1. EER Diagrams
|
|
1. Unions
|
|
1. Truncate
|
|
1. Views
|
|
1. Functions
|
|
1. Stored Procedures
|
|
1. Triggers
|
|
1. Transactions
|
|
1. Locks
|
|
1. Privileges
|
|
1. Denormalization
|
|
1. Excel -> CSV -> SQL
|
|
|
|
## Important
|
|
|
|
### Linking Tables
|
|
|
|
1. [Some Nice Visuals](http://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561)
|
|
1. One to Many/Many to One Relationships
|
|
- customer has many orders
|
|
1. One to One Relationships
|
|
- each user has one address
|
|
- only one person at that address
|
|
1. Many to Many Relationships
|
|
- actors and movies
|
|
1. Self Referncing Relationships
|
|
- customer referral
|
|
|
|
### Alias
|
|
|
|
```sql
|
|
SELECT t1.column1 as col1, t2.column2 as col2
|
|
FROM table1 as t1
|
|
INNER JOIN table2 as t2
|
|
ON t1.common_filed = t2.common_field;
|
|
```
|
|
|
|
### Indexes
|
|
|
|
1. `CREATE INDEX index_name ON table_name (column_name);`
|
|
1. `CREATE INDEX index_name ON table_name (column1_name, column2_name);`
|
|
1. Primary Key
|
|
1. use `\d table_name` to view indexes
|
|
|
|
### Default Values
|
|
|
|
```sql
|
|
CREATE TABLE people (id SERIAL, name VARCHAR(16), age INT DEAFULT 0);
|
|
\d people
|
|
|
|
INSERT INTO people (name) VALUES ('matt');
|
|
SELECT * FROM people;
|
|
```
|
|
|
|
### Constraints
|
|
|
|
1. NOT NULL
|
|
1. Unique
|
|
1. Foreign Keys
|
|
|
|
```sql
|
|
CREATE TABLE companies(
|
|
id SERIAL,
|
|
name VARCHAR(16) NOT NULL,
|
|
city VARCHAR(16)
|
|
);
|
|
|
|
INSERT INTO companies ( city ) VALUES ('Palo Alto');
|
|
|
|
CREATE TABLE people(
|
|
id INT,
|
|
name VARCHAR(16),
|
|
email VARCHAR(32) UNIQUE,
|
|
company_id INT REFERENCES companies(id)
|
|
);
|
|
|
|
\d people
|
|
|
|
INSERT INTO people (name, email, company_id) VALUES ('bob', 'bob@bob.com', 999) -- bad company_id
|
|
INSERT INTO people (name, email, company_id) VALUES ('bob', 'bob@bob.com', 1) -- not unique email
|
|
```
|
|
|
|
### Distinct
|
|
|
|
```sql
|
|
SELECT DISTINCT city FROM people;
|
|
```
|
|
|
|
## Good to Know About
|
|
|
|
### EER Diagrams
|
|
|
|

|
|
|
|
### Unions
|
|
|
|
```sql
|
|
SELECT name FROM people UNION SELECT name FROM companies; -- show distinct values
|
|
SELECT name FROM people UNION ALL SELECT name FROM companies; -- show duplicates
|
|
```
|
|
|
|
### Truncate
|
|
|
|
```sql
|
|
TRUNCATE TABLE people; -- delete all data, but don't delete table itself
|
|
```
|
|
|
|
### Views
|
|
|
|
```sql
|
|
CREATE VIEW new_yorkers AS SELECT * FROM people WHERE city = 'NYC';
|
|
|
|
\dv
|
|
|
|
SELECT * FROM new_yorkers
|
|
```
|
|
|
|
### Functions
|
|
|
|
```sql
|
|
CREATE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$
|
|
BEGIN
|
|
RETURN a + b;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
\df
|
|
|
|
SELECT add_numbers(2,4);
|
|
```
|
|
|
|
### Stored Procedures
|
|
|
|
```sql
|
|
CREATE PROCEDURE add_person(new_name VARCHAR(16))
|
|
LANGUAGE plpgsql
|
|
AS $$
|
|
BEGIN
|
|
INSERT INTO people (name) VALUES (new_name);
|
|
END
|
|
$$;
|
|
|
|
\df
|
|
|
|
call add_person('matt');
|
|
```
|
|
|
|
### Triggers
|
|
|
|
```sql
|
|
CREATE TABLE backup_people (id INT, name VARCHAR(16), age INT);
|
|
|
|
CREATE FUNCTION moveDeleted() RETURNS trigger AS $$
|
|
BEGIN
|
|
INSERT INTO backup_people VALUES (OLD.id, OLD.name, OLD.age);
|
|
RETURN OLD;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
CREATE TRIGGER archive_person
|
|
BEFORE DELETE on people
|
|
FOR EACH ROW
|
|
EXECUTE PROCEDURE moveDeleted();
|
|
|
|
\df
|
|
|
|
DELETE FROM people WHERE id = 1;
|
|
```
|
|
|
|
### Transactions
|
|
|
|
```sql
|
|
BEGIN;
|
|
|
|
INSERT INTO people (name) VALUES ('matt');
|
|
|
|
SELECT * FROM people;
|
|
|
|
-- start a different session and run SELECT * FROM people;
|
|
-- Switch back to original session
|
|
|
|
COMMIT;
|
|
|
|
-- in other session run SELECT * FROM people;
|
|
```
|
|
|
|
OR
|
|
|
|
```sql
|
|
BEGIN;
|
|
|
|
INSERT INTO people (name) VALUES ('matt');
|
|
|
|
SELECT * FROM people;
|
|
|
|
asdfasdfasdfasdfasdfasdf;
|
|
|
|
ROLLBACK;
|
|
|
|
SELECT * FROM people;
|
|
```
|
|
|
|
### Locks
|
|
|
|
```sql
|
|
BEGIN;
|
|
LOCK TABLE people IN ROW EXCLUSIVE MODE;
|
|
SELECT * FROM people WHERE id = 12 FOR UPDATE;
|
|
|
|
-- start a new session and run UPDATE people SET name = 'Matt' WHERE id = 12;
|
|
-- switch back to original session
|
|
|
|
UPDATE people SET age = 43 WHERE id = 12;
|
|
SELECT * FROM people;
|
|
END;
|
|
```
|
|
|
|
### Privileges
|
|
|
|
```sql
|
|
CREATE USER youruser;
|
|
\du
|
|
|
|
-- new session
|
|
psql -U youruser -d supertest_lab
|
|
SELECT * FROM people;
|
|
|
|
-- original session
|
|
GRANT ALL ON people TO youruser;
|
|
|
|
-- switch sessions again
|
|
SELECT * FROM people;
|
|
|
|
```
|
|
|
|
### Denormalization
|
|
|
|
| id | name | age | company_id | company_name | company_address |
|
|
|----|------|-----|------------|--------------|-----------------|
|
|
| 1 | matt | 43 | 1 | google | SF |
|
|
|
|
### Excel -> CSV -> SQL
|
|
|
|
- Create sheet
|
|
- File -> Download -> .csv
|
|
- `COPY people (name, age, ancestry, city) FROM '/Users/matthuntington/Downloads/people.csv' DELIMITER ',' CSV;`
|
|
|
|
### SQL Injection
|
|
|
|
- input with value `Huntington'; DROP TABLE people;`
|