SQL - Advanced Flashcards
(35 cards)
What must you start-up in order to work with postgres?
PgAmin 4 – it’s in your programs - you can start it from the search bar
Why are datatypes important?
data integrity - they ensure the right data is stored in each column
efficiency - process data quickly and store it with less space
when creating a new table you have to specify the datatype for each column and any relevant properties for that datatype.
What are the 8 datatypes Luke likes to use?
INT - integer, whole number
BOOLEAN - true/false or null
NUMERIC (precision, scale)- can include decimals etc.
DATE
TIMESTAMP - when data was saved, changed etc.
TIMESTAMP with TIME ZONE
VARCHAR - strings w/character limit
TEXT - strings - no character limit
how many datatypes are there?
Probably hundreds. You can view all of them in the Postgresql documentation
What are the properties of the NUMERIC datatype?
NUMERIC data type has two properties: precision & scale
- Precision is how many total digits the number has
- Scale is how many digits to the right of the decimal
Example: 623.859 - this number has a precision of 6 and a scale of 3
This would be written:
NUMERIC(6,3)
TEXT vs. VARCHAR
TEXT - variable length strings of unlimited length - often used for text that doesn’t fit within the VARCHAR constraints
VARCHAR - has a ‘character limit’ property. Can store strings of any length up to that character limit.
Looks like the character limit is defined by the user. The character limit is represented by N in the following example:
VARCHAR(N)
What can you tell me about the BOOLEAN datatype?
accepts the values of true, false or null – he was using 0 and 1 and said this is effectively true or false
DATE vs. TIMESTAMP vs. TIMESTAMP w/ TIMEZONE
DATE - date w/out time
format: YYYY-MM-DD
TIMESTAMP - date with time - can include seconds depending on precision
format: YYYY-MM-DD HH-MI-SS
TIMESTAMP w/TIMEZONE -
Adjusts stored time based on the timezone
FORMAT:YYYY-MM-DD HH-MI-SS+00:00
I think the 00:00 would be HH:MI
What statement allows you to create a table?
CREATE TABLE
What statement allows you to add data columns to your table
INSERT INTO
What statement allows you to delete a table
DROP TABLE
What are the 4 main commands you can use with a table?
CREATE TABLE,
INSERT INTO - add columns
ALTER TABLE add columns, rename column, change datatype of column, delete a column
DROP TABLE - delete table
What are the 4 main commands INSIDE the ALTER TABLE command?`
ADD - add a column
RENAME COLUMN
ALTER COLUMN - change the datatype of a column
DROP COLUMN - delete a column
After you’ve created the database shell and you’re ready to work with your tables, what do you do?
- Go into pgAdmin4 and make sure your connection to the desired database is up and running - just click on it and it will start up
- Go into VS Code and click on databases (cylinder on left) and click on the desired database and make sure it has the name of your desired database on the very bottom - in the footer of the window.
- Click on the database name in the left pane - and then click on the new sql file icon - which is has a plus sign with “SQL” next to it.
How do you initially create a table with columns?
CREATE TABLE table_name (
column_name datatype,
column_name datatype,
. . .
);
how do you insert data into a table?
INSERT INTO table_name (column_name1, column_name2, column_name3 . . . . )
VALUES (value1, value2, . . . )
How do you alter a table?
ALTER TABLE table_name
ADD column_name datatype;
RENAME COLUMN column_name TO new_name;
ALTER COLUMN column_name TYPE datatype;
DROP COLUMN column_name;
Not sure if you can do multiple of these separated by a comma, or if you can only do one action per ALTER TABLE command.
How do you update a table?
UPDATE table_name
SET column_name = ‘new_value’
WHERE condition;
in this instance you use WHERE to filter which rows to update based on a condition.
Example:
UPDATE job_applied
SET contact = ‘Prince Harry’
WHERE job_id = 3;
How do you rename a column?
ALTER TABLE table_name
RENAME COLUMN column_name TO new_name;
How do you change the datatype of a column?
ALTER TABLE table_name
ALTER COLUMN column_name TYPE new_data_type;
example:
-ALTER TABLE job_applied
–ALTER COLUMN contact_name TYPE TEXT;
How do you drop (delete) a column?
ALTER TABLE table_name
DROP COLUMN column_name
Always check and double check before you drop anything!!!
How do you drop a table?
DROP TABLE table_name
Always be extremely careful when you do this! Check and double check!
How do you create a database (3 steps)
- download CSV and SQL files
- create tables in the database
- load data into tables
Primary Key vs. Foreign Key
A primary key is a unique identifier for each row within a table. Every table in the database has a primary key.
A foreign key is a column in one table that references the primary key in another table. It creates a link between the two tables.
Example
Customer_ID could be the primary key in the Customer table. Order_ID could be the primary key in the Orders table.
A Customer_ID column in the orders table would link each order to a customer. Customer_ID would be a foreign key in the ORDERS table and Order_ID could be a foreign key in the Customers table.
in VS Code - left side bar - gold key is primary key and gray key signifies the Foreign Key.