Jed Rembold
Monday, September 8, 2025
All the following values could be added to a
NUMERIC(6,4) type column except for one,
which would result in an error. Which number would result in the
error?
TIMESTAMP field holds basically both a
date and a time (in that order)TIMESTAMP by itself is without a
timezone, as per SQL standardsTIMESTAMP WITH TIME ZONE is with a
timezone. Shorthand in Postgres is
TIMESTAMPTZ★There are plenty of instances where you may need to convert between types
Can use the CAST function to convert
between reasonable data types
Core syntax is
CAST(|||column name||| AS |||new data type|||)Postgres has a shorthand conversion syntax that uses double colons:
|||column name|||::|||new data type|||INSERT INTO is great for small tables or
adding a few rows, not so great for bulk populating of a tableCOPY
command
C:\Users\Public should be
universally accessible/tmp folder should be
universally accessible, or just choose a particular folder and give
Postgres permissions to read and write to itid,first_name,last_name,birthday
1,Michael,Scott,"Mar 15, 1964"
2,Dwight,Schrute,"Jan 20, 1970"
3,Pam,Beesly,"Mar 25, 1979"
4,Jim,Halpert,"Oct 1, 1978"
5,Kelly,Kapoor,"Feb 5, 1980"
The COPY command will copy information
into an existing table, it won’t create the table
COPY will be useful
Syntax of the COPY statement:
COPY |||tablename|||
FROM |||full path to file|||
WITH ( |||import options||| );C:\/FORMAT CSV sets the delimiter to a comma
and the default text qualifier to double quotesHEADER specifies that there is a header,
and so the first row of the file should be skippedDELIMITER 'x' sets
x to be the delimiter instead of the default
comma (or tab)QUOTE 'x' sets
x to be the new text qualifier, instead of
double quotesCOPY command
multiple times, it will just keep adding the CSV contents to the end of
the current tableSometimes an CSV might not have all the data you want in your SQL table
You can import all the information from the CSV into only a subset of the SQL table columns
Just requires that you specify the target SQL columns after specifying the table
COPY |||tablename||| (|||column₁|||, |||column₂|||, |||column₃|||)
FROM |||full path to file|||
WITH (FORMAT CSV, HEADER);CREATE TABLE purchases (
"date" DATE,
"store" VARCHAR(20),
"purchased" TEXT,
"tot_price" NUMERIC(10,2));
How to import the data to the right into the above table?
date:store name:purchased
Jan-5-22:Walmart:eggs,milk
Jan-10-22:Roths:cereal,steak
Jan-16-22:Roths:milk,butter
Jan-18-22:Winco:beans
COPY purchases
FROM 'C:\DATA\purchases.csv'
WITH (FORMAT CSV, HEADER);
COPY purchases (date, store, purchased)
FROM 'C:\DATA\purchases.csv'
WITH (FORMAT CSV, HEADER,
DELIMITER ':');
COPY purchases (date, store, purchased)
FROM 'C:\DATA\purchases.csv'
WITH (FORMAT CSV, DELIMITER ':');
COPY purchases (date, "store name")
FROM 'purchases.csv'
WITH (FORMAT CSV, HEADER,
DELIMITER ',');
Exporting data takes information from your SQL table and allows you to store it in a text file
Syntax-wise, it is almost exactly like copying into a SQL table,
except using TO instead of
FROM
COPY |||tablename|||
TO |||full path to file|||
WITH ( |||import options||| );This exports all columns from the specified table to the filename with the desired format
COPY |||tablename||| (|||column₁|||, |||column₂|||, |||column₃|||)
TO |||full path to file|||
WITH ( |||import options||| );COPY (
SELECT |||col₁|||, |||col₂||| FROM |||tablename|||
ORDER BY |||col₂|||
)
TO |||full path to file|||
WITH ( |||import options||| );--/* and
end with a */-- This is a comment
SELECT *
FROM |||table_name||| -- this is my table name
ORDER BY |||column|||;/* this
is all ignored */