Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. UPDATE, DELETE, INSERT INTO
- // 2. INSERT INTO populates the table with values; with the table name following and then stating VALUES . AFter that, you would write out the values.
- INSERT INTO products (id, name, price)
- VALUES
- (11773, 'South Face Jacket', 174.99),
- (11774, 'Big Mountain 2-Person Tent', 219.99),
- (11775, 'King Cole Camp Stove', 34.99),
- (11776, 'Oregon Trail Mountain Bike', 579.99);
- UPDATE gets stated first with the table name following, then SET with the column value, then WHERE is stated and includes clause statements
- UPDATE products SET price=39.99, name='King Cole Camping Stove' WHERE id=11775;
- DELETE FROM is typed first and then you state the table name. Afterwards, you type WHERE clauses to state which records you want removed.
- DELETE FROM products WHERE id> 11774 AND price < 100;
- //3. Numbers can be used, like prices for mercahndise. Text can be used like descriptions of a purchase or name of a store. Time can be used like how long someone stays logged into an app.
- //4.
- 4a.
- First and last name. (Text)
- Whether they sent in their RSVP. (boolean)
- Number of guests. (number)
- Number of meals. (number)
- 4b.
- Richards-MacBook-Air-2:~ Richard$ createdb -U postgres -w wedding
- Richards-MacBook-Air-2:~ Richard$ psql wedding postgres
- wedding=# CREATE TABLE invites (first_name text, last_name text, RSVPd boolean, guests_joining numeric(3, 1), meals numeric(3, 1) );
- CREATE TABLE
- wedding=# SELECT * FROM invites;
- first_name | last_name | rsvpd | guests_joining | meals
- ------------+-----------+-------+----------------+-------
- 4c.
- wedding=# ALTER TABLE invites ADD COLUMN thx_card boolean;
- ALTER TABLE
- wedding=# SELECT * FROM invites;
- first_name | last_name | rsvpd | guests_joining | meals | thx_card
- ------------+-----------+-------+----------------+-------+----------
- 4d.
- wedding=# ALTER TABLE invites DROP COLUMN meals;
- ALTER TABLE
- wedding=# SELECT * FROM invites;
- first_name | last_name | rsvpd | guests_joining | thx_card
- ------------+-----------+-------+----------------+----------
- 4e.
- wedding=# ALTER TABLE invites ADD COLUMN tbl_number numeric(2, 0);
- ALTER TABLE
- wedding=# SELECT * FROM invites;
- first_name | last_name | rsvpd | guests_joining | thx_card | tbl_number
- ------------+-----------+-------+----------------+----------+------------
- 4f.
- DROP TABLE invites;
- //5
- 5a.
- Richards-MacBook-Air-2:~ Richard$ createdb -U postgres -w new_library;
- Richards-MacBook-Air-2:~ Richard$ psql new_library postgres
- psql (11.1)
- Type "help" for help.
- new_library=# CREATE TABLE books ( ISBN numeric(13, 0), title text, author text, genre text, pub_date date, copies numeric(2, 0), avail_copies numeric(20, 0) );
- CREATE TABLE
- new_library=# INSERT INTO books (isbn, title, author, genre, pub_date, copies, avail_copies) VALUES (9780060652920, 'Mere Christianity', 'C.S. Lewis', 'Apologetics', '2015-01-10', 20, 5), (9780141439563, 'Great Expectations', 'Charles Dickens', 'Classics', '2002-02-20', 10, 9), (9781250192455, 'A Higher Loyalty', 'James Comey', 'Auto-Bio', '2018-02-14', 5, 5);
- INSERT 0 3
- new_library=# SELECT * FROM books;
- isbn | title | author | genre | pub_date | copies | avail_copies
- ---------------+--------------------+-----------------+-------------+------------+--------+--------------
- 9780060652920 | Mere Christianity | C.S. Lewis | Apologetics | 2015-01-10 | 20 | 5
- 9780141439563 | Great Expectations | Charles Dickens | Classics | 2002-02-20 | 10 | 9
- 9781250192455 | A Higher Loyalty | James Comey | Auto-Bio | 2018-02-14 | 5 | 5
- 5b.
- new_library=# UPDATE books SET avail_copies=4 WHERE isbn=9780060652920;
- UPDATE 1
- new_library=# SELECT * FROM books;
- isbn | title | author | genre | pub_date | copies | avail_copies
- ---------------+--------------------+-----------------+-------------+------------+--------+--------------
- 9780141439563 | Great Expectations | Charles Dickens | Classics | 2002-02-20 | 10 | 9
- 9781250192455 | A Higher Loyalty | James Comey | Auto-Bio | 2018-02-14 | 5 | 5
- 9780060652920 | Mere Christianity | C.S. Lewis | Apologetics | 2015-01-10 | 20 | 4
- 5c.
- new_library=# DELETE FROM books WHERE isbn=9780141439563;
- DELETE 1
- new_library=# SELECT * FROM books;
- isbn | title | author | genre | pub_date | copies | avail_copies
- ---------------+-------------------+-------------+-------------+------------+--------+--------------
- 9781250192455 | A Higher Loyalty | James Comey | Auto-Bio | 2018-02-14 | 5 | 5
- 9780060652920 | Mere Christianity | C.S. Lewis | Apologetics | 2015-01-10 | 20 | 4
- (2 rows)
- //6
- 6a.
- Richards-MacBook-Air-2:~ Richard$ createdb -U postgres -w NASA
- Richards-MacBook-Air-2:~ Richard$ psql NASA postgres
- psql (11.1)
- Type "help" for help.
- NASA=# CREATE TABLE fleet (id numeric(4, 0), name text, inaug_year numeric(4, 0), nat_origin text, mission text, orbiting_body text, in_flight boolean, mi_from_earth numeric);
- CREATE TABLE
- NASA=# SELECT * FROM fleet;
- id | name | inaug_year | nat_origin | mission | orbiting_body | in_flight | mi_from_earth
- ----+------+------------+------------+---------+---------------+-----------+---------------
- (0 rows)
- NASA=# INSERT INTO fleet (id, name, inaug_year, nat_origin, mission, orbiting_body, in_flight, mi_from_earth) VALUES (3456, 'ExoMars', 2016, 'Russia', 'atmospheric gas analysis', 'Mars', true, 452034985), (7896, 'OSIRIS', 2018, 'USA', 'obtain asteroid sample', 'asteroid 101955 Bennu', true, 20983475), (3451, 'MESSENGER', 2011, 'USA', 'flyby of mercury', 'Mercury', false, 2938476546);
- INSERT 0 3
- NASA=# SELECT * FROM fleet;
- id | name | inaug_year | nat_origin | mission | orbiting_body | in_flight | mi_from_earth
- ------+-----------+------------+------------+--------------------------+-----------------------+-----------+---------------
- 3456 | ExoMars | 2016 | Russia | atmospheric gas analysis | Mars | t | 452034985
- 7896 | OSIRIS | 2018 | USA | obtain asteroid sample | asteroid 101955 Bennu | t | 20983475
- 3451 | MESSENGER | 2011 | USA | flyby of mercury | Mercury | f | 2938476546
- 6b.
- NASA=# DELETE FROM fleet WHERE id=3451;
- DELETE 1
- NASA=# SELECT * FROM fleet;
- id | name | inaug_year | nat_origin | mission | orbiting_body | in_flight | mi_from_earth
- ------+---------+------------+------------+--------------------------+-----------------------+-----------+---------------
- 3456 | ExoMars | 2016 | Russia | atmospheric gas analysis | Mars | t | 452034985
- 7896 | OSIRIS | 2018 | USA | obtain asteroid sample | asteroid 101955 Bennu | t | 20983475
- 6c.
- NASA=# UPDATE fleet SET in_flight=FALSE WHERE id=7896;
- UPDATE 1
- NASA=# SELECT * FROM fleet;
- id | name | inaug_year | nat_origin | mission | orbiting_body | in_flight | mi_from_earth
- ------+---------+------------+------------+--------------------------+-----------------------+-----------+---------------
- 3456 | ExoMars | 2016 | Russia | atmospheric gas analysis | Mars | t | 452034985
- 7896 | OSIRIS | 2018 | USA | obtain asteroid sample | asteroid 101955 Bennu | f | 20983475
- //7
- 7a.
- Richards-MacBook-Air-2:~ Richard$ createDB -U postgres -w email
- Richards-MacBook-Air-2:~ Richard$ psql email postgres
- psql (11.1)
- Type "help" for help.
- email=# CREATE TABLE inbox (id numeric, subject text, add_recipients text, body text, timestamp timestamp, unread boolean, id_email_chain text, sender text);
- CREATE TABLE
- email=# INSERT INTO inbox VALUES (1, 'vdoc', '--', 'hey richard, just wondering if when you are going to send the song lyrics to wendy?', '2004-10-19 10:23:54', TRUE, 'origional msg', 'john.cai@gpmail.org');
- INSERT 0 1
- email=# INSERT INTO inbox VALUES (1, 'payment', '--', 'Just a reminder to send in your payment', '2004-10-19 10:23:54', TRUE, 'origional msg', 'sweetwater.support@gmail.com');
- INSERT 0 1
- email=# UPDATE inbox SET id=2 WHERE subject='payment';
- UPDATE 1
- email=# INSERT INTO inbox VALUES (3, 'light phone', '--', 'Update for the latest build is coming soon! Hold tight!', '2004-10-19 10:23:54', TRUE, 'originonal msg', 'lightphone@seed.co');
- INSERT 0 1
- email=# SELECT * FROM inbox;
- id | subject | add_recipients | body | timestamp | unread | id_email_chain | sender
- ----+-------------+----------------+-------------------------------------------------------------------------------------+---------------------+--------+----------------+------------------------------
- 1 | vdoc | -- | hey richard, just wondering if when you are going to send the song lyrics to wendy? | 2004-10-19 10:23:54 | t | origional msg | john.cai@gpmail.org
- 2 | payment | -- | Just a reminder to send in your payment | 2004-10-19 10:23:54 | t | origional msg | sweetwater.support@gmail.com
- 3 | light phone | -- | Update for the latest build is coming soon! Hold tight! | 2004-10-19 10:23:54 | t | originonal msg | lightphone@seed.co
- 7b.
- email=# DELETE FROM inbox WHERE id=3;
- DELETE 1
- email=# SELECT * FROM inbox;
- id | subject | add_recipients | body | timestamp | unread | id_email_chain | sender
- ----+---------+----------------+-------------------------------------------------------------------------------------+---------------------+--------+----------------+------------------------------
- 1 | vdoc | -- | hey richard, just wondering if when you are going to send the song lyrics to wendy? | 2004-10-19 10:23:54 | t | origional msg | john.cai@gpmail.org
- 2 | payment | -- | Just a reminder to send in your payment | 2004-10-19 10:23:54 | t | origional msg | sweetwater.support@gmail.com
- 7c.
- email=# UPDATE inbox SET unread=FALSE WHERE id=2;
- UPDATE 1
- email=# SELECT * FROM inbox;
- id | subject | add_recipients | body | timestamp | unread | id_email_chain | sender
- ----+---------+----------------+-------------------------------------------------------------------------------------+---------------------+--------+----------------+------------------------------
- 1 | vdoc | -- | hey richard, just wondering if when you are going to send the song lyrics to wendy? | 2004-10-19 10:23:54 | t | origional msg | john.cai@gpmail.org
- 2 | payment | -- | Just a reminder to send in your payment | 2004-10-19 10:23:54 | f | origional msg | sweetwater.support@gmail.com
- (2 rows)
- email=#
- email=# UPDATE inbox SET unread=TRUE WHERE id=2;
- UPDATE 1
- email=# SELECT * FROM inbox;
- id | subject | add_recipients | body | timestamp | unread | id_email_chain | sender
- ----+---------+----------------+-------------------------------------------------------------------------------------+---------------------+--------+----------------+------------------------------
- 1 | vdoc | -- | hey richard, just wondering if when you are going to send the song lyrics to wendy? | 2004-10-19 10:23:54 | t | origional msg | john.cai@gpmail.org
- 2 | payment | -- | Just a reminder to send in your payment | 2004-10-19 10:23:54 | t | origional msg | sweetwater.support@gmail.com
- (2 rows)
- email=#
Add Comment
Please, Sign In to add comment