Guest User

Untitled

a guest
Oct 15th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. 1. List the commands for adding, updating, and deleting data.
  2.  
  3. INSERT INTO…VALUES…, UPDATE...SET..., DELETE FROM...WHERE
  4.  
  5.  
  6. 2. Explain the structure for each type of command.
  7.  
  8. INSERT INTO populates the table with values
  9. UPDATE updates the values in the table, both SET and WHERE can take multiple clauses.
  10. DELETE FROM removes data from the table, this action is irreversible.
  11.  
  12.  
  13. 3. What are some of the data types that can be used in tables? Give a real-world example of each type.
  14.  
  15. There are several of data types used in tables for example numbers (decimal, numeric, float, money, date...), text (character, text, char...), date & time (date, timestamp...)
  16.  
  17.  
  18. 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).
  19.  
  20. - Which data type would you use to store each of the following pieces of information?
  21. - First and last name. - text
  22. - Whether they sent in their RSVP. - boolean
  23. - Number of guests. - numeric
  24. - Number of meals. - numeric
  25.  
  26. - Write a command that creates the table to track the wedding dinner.
  27.  
  28. CREATE TABLE dinner (
  29. name text,
  30. rsvp boolean,
  31. guests_num int,
  32. meals_num int
  33. );
  34.  
  35. - Write a command that adds a column to track whether the guest sent a thank you card.
  36.  
  37. ALTER TABLE dinner ADD COLUMN thank_card boolean SET DEFAULT ‘no’;
  38.  
  39. - 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.  
  41. ALTER TABLE dinner DROP COLUMN meals_num;
  42.  
  43. - The guests will need a place to sit at the reception, so write a command that adds a column for table number.
  44.  
  45. ALTER TABLE dinner ADD COLUMN table_num int;
  46.  
  47. - 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.
  48.  
  49. ALTER TABLE dinner DROP COLUMN table_num;
  50.  
  51.  
  52. 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.
  53.  
  54. CREATE TABLE library_inventory (
  55. isbn int,
  56. title text,
  57. author text,
  58. genre text,
  59. publishing_date date,
  60. book_quantity int,
  61. book_available int,
  62. );
  63.  
  64. - Find three books and add their information to the table.
  65.  
  66. INSERT INTO library_inventory (isbn, title, author, genre, publishing_date, book_quantity, book_available) VALUES
  67. (15196, ‘Harry Potter’, ‘J. K. Rowling’, 2010/06/10, 120, 5),
  68. (29849, ‘Where The Crawdads Sing’, ‘Delia Owens’, 2006/08/11, 13, 10),
  69. (38726, ‘Alaskan Holiday’, ‘Debbie Macomber’, 2002/01/20, 10, 2);
  70.  
  71. - Someone has just checked out one of the books. Change the number of available copies to 1 fewer.
  72.  
  73. UPDATE library_inventory SET book_available=9 WHERE isbn=29849;
  74.  
  75. - Now one of the books has been added to the banned books list. Remove it from the table.
  76.  
  77. DELETE FROM library_inventory WHERE isbn=38726;
  78.  
  79.  
  80. 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:
  81.  
  82. CREATE TABLE spacecraft (
  83. id int,
  84. name text,
  85. year date,
  86. country text,
  87. mission text,
  88. orbiting_body text,
  89. is_operating boolean,
  90. distance_earth float
  91. );
  92.  
  93. - Add three non-Earth-orbiting satellites to the table.
  94.  
  95. INSERT INTO spacecraft (id, name, year, country, mission, orbiting_body, is_operating, distance_earth) VALUES
  96. (256,'Apollo 1', 1991,'USA', 'Satellite communication mission', 'Saturn', 'yes', 2000000000.55),
  97. (398,'Apollo 2', 1992,'Canada', 'Life research mission', 'Uranus', 'no', 3330000000.55),
  98. (593,'Apollo 3', 1993,'Russia', 'Water research mission', 'Moon', 'yes', 4440000000.55);
  99.  
  100. - Remove one of the satellites from the table since it has just crashed into the planet.
  101.  
  102. DELETE FROM spacecraft WHERE id=398;
  103.  
  104. - Edit another satellite because it is no longer operating and change the value to reflect that.
  105.  
  106. UPDATE spacecraft SET is_operating=’no’ WHERE id=256
  107.  
  108.  
  109. 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:
  110.  
  111. CREATE TABLE inbox (
  112. id int,
  113. subject text,
  114. sender text,
  115. recipients text,
  116. body text,
  117. timestamp timestamp,
  118. is_read boolean,
  119. chain_id int
  120. );
  121.  
  122. - Add three new emails to the inbox.
  123.  
  124. INSERT INTO inbox (id, subject, sender, recipients, body, timestamp, is_read, chain_id) VALUES
  125. (100, 'Hello', 'Matt','Valery', 'Hello Evgheni!', '2018-10-03 09:20:05', 'no',1000),
  126. (101, 'Hey', 'Chris','Brad', 'Hello Evgheni!', '2018-10-04 11:30:13', 'no',200),
  127. (102, 'What’s up', 'Sarah','Kevin', 'Hello Evgheni!', '2018-10-05 01:48:06', 'yes',658);
  128.  
  129. - You deleted one of the emails, so write a command to remove the row from the inbox table.
  130.  
  131. DELETE FROM inbox WHERE id=100;
  132.  
  133. - 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.
  134.  
  135. UPDATE inbox SET is_read=’no’ WHERE id=102;
Add Comment
Please, Sign In to add comment