Guest User

Untitled

a guest
Aug 19th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. Exercises
  2. Use the commands we have learned about to complete the following tasks and
  3. submit the SQL statements in the submission tab. Real world examples must be
  4. distinct from those used in the text.
  5.  
  6. 1. List the commands for adding, updating, and deleting data
  7. INSERT INTO will add data,
  8. UPDATE will update data and
  9. DELETE FROM will delete data
  10.  
  11. 2. Explain the structure for each type of command.
  12. The SQL INSERT INTO Statement is used to add new rows of data to a table
  13. in the database.
  14. syntax: INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
  15. VALUES (value1, value2, value3,...valueN);
  16.  
  17. The SQL UPDATE Query is used to modify the existing records in a table.
  18. You can use the WHERE clause with the UPDATE query to update the selected
  19. rows, otherwise all the rows would be affected.
  20. syntax: UPDATE table_name
  21. SET column1 = value1, column2 = value2...., columnN = valueN
  22. WHERE [condition];
  23.  
  24. The SQL DELETE Query is used to delete the existing records from a table.
  25. You can use the WHERE clause with a DELETE query to delete the selected
  26. rows, otherwise all the records would be deleted.
  27. syntax: DELETE FROM table_name
  28. WHERE [condition];
  29.  
  30. 3. What are some the data types that can be used in tables? Give a real
  31. world example of each.
  32. The item description on the Kroger receipt is a text data type.
  33. The date of my post last month on Facebook is a date and time data type.
  34. My medical record number on myChart is an integer data type.
  35.  
  36. 4. Think through how to create a new table to hold a list of people invited
  37. to a wedding. This table needs to have first and last name, whether they sent
  38. in their RSVP, the number of guests they are bringing, and the number of
  39. meals (1 for adults and 1/2 for children).
  40. (....thinking....)
  41.  
  42. Which data type would you use to store each of the following
  43. pieces of information?
  44. First and last name. [ text ]
  45. Whether they sent in their RSVP. [ bool ]
  46. Number of guests. [ integer ]
  47. Number of meals. [ numeric(p, s) ]
  48.  
  49. Write a command that makes the table to track the wedding.
  50. CREATE TABLE wedding (
  51. first_name text,
  52. last_name text,
  53. rsvp bool,
  54. guest integer,
  55. meals numeric(5, 2)
  56. );
  57.  
  58. Using the table we just created, write a command that adds a column to track
  59. whether they were sent a thank you card.
  60. ALTER TABLE wedding ADD COLUMN card bool;
  61.  
  62. You have decided to move the data about the meals to another table, so write
  63. a command to remove the column storing the number meals from the wedding table.
  64. ALTER TABLE wedding DROP COLUMN meals;
  65.  
  66. The guests are going to need a place to sit at the reception, so write
  67. a command that adds a column for table number.
  68. ALTER TABLE wedding ADD COLUMN table integer;
  69.  
  70. The wedding is over and we do not need to keep this information, so write
  71. a command that deletes the wedding table from the database.
  72. DROP TABLE wedding;
  73.  
  74. 5. Write a command to make a new table to hold the books in a library with
  75. the columns ISBN, title, author, genre, publishing date, number of copies,
  76. and available copies.
  77. CREATE TABLE library (
  78. isbn integer,
  79. title text,
  80. author text,
  81. genre text,
  82. publishing_date date,
  83. copies integer,
  84. available_copies integer
  85. );
  86.  
  87. Find three books and add their information to the table.
  88. INSERT INTO library (isbn,
  89. title,
  90. author,
  91. genre,
  92. publishing_date,
  93. copies,
  94. available_copies)
  95. VALUES
  96. (0446357170, 'Chances', 'Jackie Collins',
  97. 'contemporary fiction', 1991-08-01, 30870, 6845),
  98. (0671808826, 'The Lonely Lady', 'Harold Robbins',
  99. 'contemporary fiction', 1977-03-01, 308170, 45),
  100. (0312987013, 'Minion (Vampire Huntress Legends)', 'L. A. Banks',
  101. 'romance', 2004-04-29, 9215, 997);
  102.  
  103. Someone has just checked out one of the books. Change the available copies
  104. column to 1 fewer.
  105. UPDATE library SET available_copies=996
  106. WHERE isbn=0312987013;
  107.  
  108. Now one of the books has been added to the banned books list. Remove it
  109. from the table.
  110. DELETE FROM library where isbn=0671808826;
  111.  
  112. 6. Write a command to make a new table to hold spacecrafts. Information
  113. should include id, name, year launched, country of origin, a brief description
  114. of the mission, orbiting body, if it is currently operating, and approximate
  115. miles from Earth.
  116. CREATE TABLE spacecraft (
  117. id integer,
  118. name text,
  119. launch_year integer,
  120. country_launched text,
  121. mission text,
  122. orbiting_location text,
  123. mission_completed date,
  124. distance_from_earth float
  125. );
  126.  
  127. Add 3 non-Earth-orbiting satellites to the table.
  128. INSERT INTO spacecraft (id,
  129. name,
  130. launch_year,
  131. country_launched,
  132. mission,
  133. orbiting_location,
  134. mission_completed,
  135. distance_from_earth)
  136. VALUES
  137. (10, 'U S S Enterprise', 1966, 'United Federation',
  138. 'To boldly go where no man has gone before.', 'Sector 90210', 1969-01-09, 0),
  139. (13, 'Nostromo', 1979, 'East Wayland',
  140. 'Investigate potential distress during travel.', 'LV-426', 0000-00-00, 7.054e+13),
  141. (67, 'Max', 1986, 'Phaelon',
  142. 'Collect galatic specimens, analyze, return specimens.', 'Earth', 1986-08-01, 99999);
  143.  
  144. Remove one of the satellites from the table since it has just been crashed into the planet.
  145. DELETE FROM spacecrafts WHERE id=67;
  146.  
  147. Edit another satellite because it is no longer operating and change the value to reflect that.
  148. UPDATE spacecrafts SET mission_completed=2018-08-19 WHERE id-13;
  149.  
  150. 7. Write a command to make a new table to hold the emails in your inbox. This
  151. table should include an id, the subject line, the sender, any additional
  152. recipients, the body of the email, the timestamp, whether or not it’s been read,
  153. and the id of the email chain it’s in.
  154. CREATE TABLE inbox (
  155. id integer,
  156. subject text,
  157. from text,
  158. recipients text,
  159. email text,
  160. timestamp date,
  161. read bool,
  162. chain_id integer
  163. );
  164.  
  165. Add 3 new emails to the inbox.
  166. INSERT INTO inbox
  167. (id, subject, from ,recipients, email, timestamp, read, chain_id)
  168. VALUES
  169. (1, 'Prince', 'love4OneAnother@npg.com', 'michaeljackson@beatit.com','There are theives in the temple tonight', 2001-10-13, true, 17),
  170. (2, 'Aretha', 'angel@detroit.com', 'whitneyhouston@sing.com', 'A few stormy moments is all that we share', 2003-10-13, false, 27),
  171. (3, 'Gerald', 'lavert@ojay.com', 'marvingaye@prideandjoy.com', 'Baby hold on to me', 2006-10-13, true, 37);
  172.  
  173. You’ve just deleted one of the emails, so write a command to remove the row from the inbox table.
  174. DELETE FROM inbox where id=2;
  175.  
  176. You started reading an email but just heard a crash in another room. Mark
  177. the email as unread before investigating, so you can come back to it later.
  178. UPDATE inbox SET read=false WHERE id=1;
Add Comment
Please, Sign In to add comment