PostgreSQL Flashcards
What steps do you want for cleaning string data?
Restrict capitalization in column names
Remove extra divider spaces in column names
make column names uniform
INITCAP()
SELECT INITCAPT(column)
Output: Hello Friend!
Fixes capitalization
REPLACE()
SELECT REPLACE(column, string_to_replace, replacement)
SELECT REPLACE(streets, ‘Main Street’, ‘St’);
Output: Main St
LPAD(input_string, length, fill_value)
Left-Pad a Column
SELECT LPAD(‘column’, 7, ‘X’);
OUTPUT: XXXX123
Prepending text values to a string. The fourth value has to be a string.
SELECT LPAD(‘123’, 5, ‘0’); – Output: ‘00123’
Like
Like is used for pattern matching using wildcards such as % or _
SELECT *
FROM employees
WHERE firstname LIKE ‘Ali%’ or firstname LIKE ‘_li’
%
Matches 0 or more characters
_
Matches exactly 1 character
Can use multiple _ to represent more than 1 character
REGEXP_REPLACE()
REGEXP_REPLACE(column, ‘regrex’, replacement, ‘g’)
‘g’ = global
DIFFERENCE()
DIFFERENCE is used to compare the SOUNDEX values of two strings and returns a score between 0 and 4:
DIFFERENCE(vehicle_color, ‘GRAY’) = 4;
SOUNDEX()
SOUNDEX is a phonetic algorithm used to match words or strings that sound similar in English. It converts a word to a code based on its pronunciation.
Example:
SOUNDEX(‘GRAY’) → G600
SOUNDEX(‘GREY’) → G600
EXPLAIN ANALYZE
can be used to provide insight into the execution plan of a SQL query. actually runs the query. use with caution.
EXPLAIN ANALYZE
SELECT * FROM patient WHERE age = 60;
EXPLAIN
the EXPLAIN command alone can be used to review the planned execution path without the associated overhead of query execution.
EXPLAIN
SELECT * FROM patient WHERE age = 60;
INDEX
CREATE INDEX index_title
ON table(column);
example:
CREATE INDEX idx_patient_age_cholesterol_restingbp
ON patient(age,cholesterol,restingbp);
Visualize how to query to confirm index exists
SELECT * FROM pg_indexes
WHERE tablename = ‘patient’
AND indexname = ‘idx_patient_age’;
Visualize how to extract a row data from a column, and create a new table, inserting the column data from an old table.
SELECT DISTINCT(chestpaintype) FROM patient;
CREATE TABLE IF NOT EXISTS chestpain (
id serial PRIMARY KEY,
type TEXT
);
INSERT INTO chestpain(type)
SELECT DISTINCT(chestpaintype) FROM patient;
Visualize how to alter a table with a foreign key
ALTER TABLE patient
ADD CONSTRAINT patient_chestpain_fk
FOREIGN KEY (chestpaintypeid) REFERENCES chestpain(id);
Visualize how to use INFORMATION_SCHEMA
is used to retrieve metadata about the columns of a table from the database.
Example:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = ‘public’ #this is a default
AND table_name = ‘table’;
Visualize how to check for columns using INFORMATION_SCHEMA
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = ‘integer’;
Database transactions
A transaction is like a package of operations that are grouped together. Either all operations within the transaction succeed (committed) or none of them take effect (rolled back). It ensures ACID (Atomicity, Consistency, Isolation, Durability).
How do you create a database transaction?
Use BEGIN and COMMIT
BEGIN starts the transaction, and all statements are part until transaction is ended with COMMIT.
What is ACID?
Atomicity, Consistency, Isolation, Durability
ROLLBACK
If an error occurs or if you decide not to save the changes, you can use ROLLBACK instead of COMMIT.
ROLLBACK undoes all changes made during the transaction.
SERIAL
Create surrogate keys
CREATE TABLE table_name(
column_number SERIAL PRIMARY KEY
)
How to designate a foregin key
CREATE TABLE table_name(
column_name INT REFERENCES table(column) ON DELETE CASCADE)
When the column is deleted in the reference table, it automatically deletes when using ON DELETE CASCADE.