From a6dd557f341c27c1d0a0bcb680f61bf000add83e Mon Sep 17 00:00:00 2001 From: Matthew Huntington Date: Mon, 23 Oct 2023 20:18:07 -0500 Subject: [PATCH] finishing sql3 --- SQL/lessons/sql_3.md | 96 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 19 deletions(-) diff --git a/SQL/lessons/sql_3.md b/SQL/lessons/sql_3.md index 3d1e302..a28a173 100644 --- a/SQL/lessons/sql_3.md +++ b/SQL/lessons/sql_3.md @@ -76,13 +76,25 @@ JOIN people AS receivers ### 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 +Indexes can speed up your queries. Basically, they duplicate a table and sort it based on a particular column or columns that you specify. + +Create an index on a single column + +``` +CREATE INDEX index_name ON table_name (column_name); +``` + +You can use `\d table_name` to view indexes you've created on it. +Create an index on two columns: + +``` +CREATE INDEX index_name ON table_name (column1_name, column2_name); +``` ### Default Values +You can create default values for a column so that if you create a row with that column blank, it will fill in the default value + ```sql CREATE TABLE people (id SERIAL, name VARCHAR(16), age INT DEAFULT 0); \d people @@ -93,13 +105,14 @@ SELECT * FROM people; ### Constraints -1. NOT NULL -1. Unique -1. Foreign Keys +1. `NOT NULL` ensures that a column is never left blank. If so the user receives an error +1. `UNIQUE` ensures that each value in that column of the table are you unique (there are no duplicates) +1. `PRIMARY KEY` is a unique, not null constraint that ensures a unique way to identify each row. This is usually how a table is sorted by default so that retrieving, sorting, joining, etc based on that column is fast +1. Foreign Keys are a way to ensure that a value placed in one column of a table appears somewhere in a specific column of another table. Usually a join occurs `ON` these two columns, so it's important that the column that `REFERENCES` the other column doesn't have values that don't exist in the other table's column. For example, if we had a `company_id` column in the `people` table that references the `id` column of the `companies` table, there could be missing data if the `company_id` column contained values that didn't exist in the `id` column of the `companies` table. The `REFERENCES` constraint helps maintain data integrity. ```sql CREATE TABLE companies( - id SERIAL, + id SERIAL PRIMARY KEY, name VARCHAR(16) NOT NULL, city VARCHAR(16) ); @@ -107,7 +120,7 @@ CREATE TABLE companies( INSERT INTO companies ( city ) VALUES ('Palo Alto'); CREATE TABLE people( - id INT, + id SERIAL PRIMARY KEY, name VARCHAR(16), email VARCHAR(32) UNIQUE, company_id INT REFERENCES companies(id) @@ -121,31 +134,54 @@ INSERT INTO people (name, email, company_id) VALUES ('bob', 'bob@bob.com', 1) -- ### Distinct +`DISTINCT` is useful for quickly seeing all the different values that a column may contain. + ```sql SELECT DISTINCT city FROM people; ``` +If you have more than one column specified with distinct, it will display rows that have a distinct combination of those two columns. In other words, for a row to be considered a duplicate, and thus removed, all columns specified must match those of another row + +```sql +SELECT DISTINCT city, name FROM people; +``` + ## Good to Know About ### EER Diagrams -![](https://cdn.tutsplus.com/cdn-cgi/image/width=992/net/uploads/legacy/538_sql3/ss_6.png) +There are many apps out there that will help you visualize the relationships between your tables with Enhanced Entity-Relationship diagrams. You can also draw your own by hand. Some apps will even analyze your tables' Foreign Key constraints and generate one for you. Some allow you to draw the EER diagram, and it will create the tables for you! + +![good article on table relationships](https://cdn.tutsplus.com/cdn-cgi/image/width=992/net/uploads/legacy/538_sql3/ss_6.png) ### Unions +You can stack `SELECT` statements (usually on two different tables) on top of each other vertically. + +This will show distinct rows + ```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 +SELECT name FROM people UNION SELECT name FROM companies; +``` + +This show duplicate rows + +```sql +SELECT name FROM people UNION ALL SELECT name FROM companies; ``` ### Truncate +Use `TRUNCATE` to delete all rows from a table without deleting the table itself + ```sql -TRUNCATE TABLE people; -- delete all data, but don't delete table itself +TRUNCATE TABLE people; ``` ### Views +Using a `VIEW` allows you to alias a `SELECT` statement as something that's easier to remember/use later + ```sql CREATE VIEW new_yorkers AS SELECT * FROM people WHERE city = 'NYC'; @@ -156,6 +192,8 @@ SELECT * FROM new_yorkers ### Functions +Functions as a part of a query + ```sql CREATE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN @@ -170,6 +208,8 @@ SELECT add_numbers(2,4); ### Stored Procedures +If you have a set of commands that you run frequently, you can use a Stored Procedure to save these commands for later for easy execution + ```sql CREATE PROCEDURE add_person(new_name VARCHAR(16)) LANGUAGE plpgsql @@ -186,6 +226,8 @@ call add_person('matt'); ### Triggers +Triggers allow you to run a Function (really a Stored Procedure) in response to something happening elsewhere in your database. This can help maintain data integrity and allows for a small amount of automation + ```sql CREATE TABLE backup_people (id INT, name VARCHAR(16), age INT); @@ -208,6 +250,10 @@ DELETE FROM people WHERE id = 1; ### Transactions +If you have several steps that all need to be successful or else none of them are (e.g. transferring money between accounts), transactions are a great way to maintain data integrity + +Anything after `BEGIN` will not be saved in the database until the `COMMIT` command has been run. This will give you a chance to make sure things are as they should be before writing the changes to disk + ```sql BEGIN; @@ -223,7 +269,7 @@ COMMIT; -- in other session run SELECT * FROM people; ``` -OR +If something goes wrong during your various commands, you can always `ROLLBACK` the changes that were made, so that you don't have to manually undo everything. ```sql BEGIN; @@ -241,6 +287,8 @@ SELECT * FROM people; ### Locks +Locks are a great way to make sure nobody messes with your data until after all of your statements have been run. This makes sure that someone doesn't change your data in the middle of your script in a way that could potentially affect the outcome. + ```sql BEGIN; LOCK TABLE people IN ROW EXCLUSIVE MODE; @@ -256,6 +304,8 @@ END; ### Privileges +Privileges are a great way to make sure other users of the database don't do things they shouldn't do. + ```sql CREATE USER youruser; \du @@ -265,7 +315,7 @@ psql -U youruser -d supertest_lab SELECT * FROM people; -- original session -GRANT ALL ON people TO youruser; +GRANT SELECT ON people TO youruser; -- switch sessions again SELECT * FROM people; @@ -274,16 +324,24 @@ SELECT * FROM people; ### Denormalization +Joins can take a while when working on extremely large data sets. Sometimes it might be best to combine the tables directly in memory so that you don't have to do any joins. This can slow down your updates, deletes, and inserts though, and it introduces an area where errors could potentially occur. In the example below, if Google changed its address, you would have to update all rows that referenced it. This could be slow and error prone. + | 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;` +Sometimes you may need to move from Excel to SQL. To do this, use the following steps: + +1. Create a new sheet +1. In the menu bar, run File -> Download -> .csv +1. In `psql` run: + +``` +COPY people (name, age) FROM '/Users/matthuntington/Downloads/people.csv' CSV; +``` ### SQL Injection -- input with value `Huntington'; DROP TABLE people;` +People will often try to hack your database by entering SQL commands into your inputs on websites. Often they will enter something like `Huntington'; DROP TABLE people;` in the hopes that your application will be built poorly enough that they'll wreck something. This is easiest to fix at the application level, as opposed to the database level