Guest User

Untitled

a guest
Jan 23rd, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. Exercises:
  2.  
  3. Use the commands above to complete the following tasks, and submit the SQL statements. Real-world examples must be distinct from those used in the text.
  4.  
  5. 1. List the commands for adding, updating, and deleting data.
  6. Adding: INSERT INTO
  7. Updating: UPDATE
  8. Deleting: DELETE
  9. Alter Table: ALTER TABLE
  10.  
  11. 2. Explain the structure for each type of command.
  12. Adding: INSERT INTO (name of table) VALUES (values to be inserted)
  13. Updating: UPDATE (name of table) SET (the column and values to be updated) WHERE (what table row)
  14. Deleting: DELETE FROM (name of table) WHERE (where/what values should be removed)
  15. Alter Table: ALTER TABLE (name of table) DROP COLUMN (what column to drop)
  16. ALTER TABLE (name of table) ADD COLUMN (what column to add)
  17.  
  18. 3. What are some of the data types that can be used in tables? Give a real-world example of each type.
  19. An inventory list for a bar/restaurant that tracks the food items (text), how many of each item (integer), when they were delivered/how long they've been on the shelf (date/time) and how much they cost (number)
  20.  
  21. 4. Decide how to create a new table to hold a list of people invited to a wedding dinner. The table needs to have first and last names, whether they sent in their RSVP, the number of guests they are bringing, and the number of meals (1 for adults and 1/2 for children).
  22. a) Which data type would you use to store each of the following pieces of information?
  23. First and last name. => text
  24. Whether they sent in their RSVP. => text
  25. Number of guests. => integer
  26. Number of meals. => numeric
  27.  
  28. b) Write a command that creates the table to track the wedding dinner.
  29. CREATE TABLE dinner (
  30. name text,
  31. rsvp text,
  32. guests integer,
  33. meals numeric
  34. );
  35.  
  36. c) Write a command that adds a column to track whether the guest sent a thank you card.
  37. ALTER TABLE dinner ADD COLUMN thankyou_cards text;
  38.  
  39. d) You have decided to move the data about the meals to another table, so write a command to remove the column storing the number meals from the wedding table.
  40. ALTER TABLE dinner DROP COLUMN meals;
  41.  
  42. e) The guests will need a place to sit at the reception, so write a command that adds a column for table number.
  43. ALTER TABLE dinner ADD COLUMN table_number integer;
  44.  
  45. f) The wedding is over and we do not need to keep this information, so write a command that deletes the table numbers from the database.
  46. ALTER TABLE dinner DROP COLUMN table_number;
  47.  
  48. 5. Write a command to create a new table to hold the books in a library with the columns ISBN, title, author, genre, publishing date, number of copies, and available copies.
  49. CREATE TABLE books(
  50. ISBN bigint,
  51. title text,
  52. genre text,
  53. publish_date date,
  54. copies integer
  55. copies_available integer
  56. );
  57.  
  58. a) Find three books and add their information to the table.
  59. INSERT INTO books(ISBN, title, genre, publish_date, copies, copies_available) VALUES
  60. (9780465050659, 'The Design of Everyday Things', 'Non-Fiction', '2013-01-01', 5, 2),
  61. (9780141033570, 'Thinking, Fast and Slow', 'Non-Fiction', '2011-01-01' 6, 3),
  62. (9780385480017, 'Bird By Bird', 'Non-Fiction', '1994-01-01' 3, 3);
  63.  
  64. b) Someone has just checked out one of the books. Change the number of available copies to 1 fewer.
  65. UPDATE books SET copies_available=2 WHERE title='The Design of Everyday Things';
  66.  
  67. c) Now one of the books has been added to the banned books list. Remove it from the table.
  68. DELETE FROM books WHERE title='Thinking, Fast and Slow';
  69.  
  70. 6. Write a command to make a new table to hold spacecrafts. Information should include id, name, year launched, country of origin, a brief description of the mission, orbiting body, if it is currently operating, and its approximate miles from Earth. In addition to the table creation, provide commands that perform the following operations:
  71. CREATE TABLE spacecrafts(
  72. id integer,
  73. name text,
  74. launch_year integer,
  75. origin text,
  76. description text,
  77. orbiting_body text,
  78. operational text,
  79. miles float
  80. );
  81.  
  82. a) Add three non-Earth-orbiting satellites to the table.
  83. INSERT INTO spacecrafts VALUES
  84. (111,'Starship 1', 1991,'USA', 'Life Mission', 'Saturn', 'yes', 2000000000.55),
  85. (222,'Starship 2', 1992,'Canada', 'Life Mission', 'Pluto', 'no', 3330000000000.55),
  86. (333,'Starship 3', 1993,'Russia', 'Life Mission', 'Moon', 'yes', 4440000000.55);
  87.  
  88. b) Remove one of the satellites from the table since it has just crashed into the planet.
  89. DELETE FROM spacecrafts WHERE id=222;
  90.  
  91. c) Edit another satellite because it is no longer operating and change the value to reflect that.
  92. UPDATE spacecrafts SET value='no' WHERE id=111;
  93.  
  94. 7. Write a command to create a new table to hold the emails in your inbox. This table should include an id, the subject line, the sender, any additional recipients, the body of the email, the timestamp, whether or not you have read the email, and the id of the email chain it's in. Also provide commands that perform the following operations:
  95. CREATE TABLE inbox(
  96. id integer,
  97. subject text,
  98. sender text,
  99. recipients text,
  100. body text,
  101. timestamp timestamp,
  102. read text,
  103. id_email integer
  104. );
  105.  
  106. a) Add three new emails to the inbox.
  107. INSERT INTO inbox VALUES
  108. (111, 'Yo!', 'Bob', 'Diana', 'Hey What's Up?', '2019-01-17 15:30:00', 'yes', 1111),
  109. (222, 'Hey!', 'Mary', 'Diana', 'Group Project', '2019-01-20 09:15:00', 'yes', 2222),
  110. (333, 'Hi', 'Sam', 'Diana', 'Ok sounds good', '2019-01-22 14:00:00', 'no', 3333);
  111.  
  112. b) You deleted one of the emails, so write a command to remove the row from the inbox table.
  113. DELETE from inbox WHERE id=111;
  114.  
  115. c) You started reading an email but just heard a crash in another room. Mark the email as unread before investigating the crash, so you can come back and read it later.
  116. UPDATE inbox SET read='no' WHERE id=333;
Add Comment
Please, Sign In to add comment