Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. 1) Get all the restaurants.
  2. Write a query that returns all of the restaurants, with all of the fields.
  3.  
  4. knex.select('id', 'name', 'borough', 'cuisine')
  5. .from('restaurants')
  6. .then(results => console.log(JSON.stringify(results, null, 4)));
  7.  
  8. 2) Get Italian restaurants
  9. Write a query that returns all of the Italian restaurants, with all of the fields
  10.  
  11.  
  12.  
  13. 3) Get 10 Italian restaurants, subset of fields
  14. Write a query that gets 10 Italian restaurants, returning only the id and name fields.
  15.  
  16. SELECT id, name FROM restaurants WHERE cuisine = 'Italian' LIMIT 10;
  17.  
  18. 4) Count of Thai restaurants
  19. Write a query that returns the number of Thai restaurants.
  20. SELECT count(*) FROM restaurants WHERE cuisine = 'Thai';
  21.  
  22. 5) Count of restaurants
  23. Write a query that returns the total number of restaurants.
  24.  
  25. SELECT count(*) FROM restaurants;
  26.  
  27. 6) Count of Thai restaurants in zip code
  28. Write a query that returns the number of Thai restaurants in the 11372 zip code.
  29.  
  30. SELECT count(*) FROM restaurants WHERE cuisine = 'Thai' AND address_zipcode = '11372';
  31.  
  32. 7) Italian restaurants in one of several zip codes
  33. Write a query that returns the id and name of five Italian restaurants in the 10012, 10013, or 10014 zip codes. The initial results (before limiting to five) should be alphabetically sorted.
  34.  
  35. SELECT id, name as restaurant_name FROM restaurants WHERE cuisine = 'Italian' AND address_zipcode IN ('10013', '10012', '10014') ORDER BY name ASC LIMIT 5;
  36.  
  37. 8) Create a restaurant
  38. Create a restaurant with the following properties:
  39.  
  40. name: 'Byte Cafe',
  41. borough: 'Brooklyn',
  42. cuisine: 'coffee',
  43. address_building_number: '123',
  44. address_street: 'Atlantic Avenue',
  45. address_zipcode: '11231'
  46.  
  47. INSERT INTO restaurants ( name, borough, cuisine, address_building_number, address_street, address_zipcode) VALUES ('Byte Cafe', 'Brooklyn', 'coffee', '123', 'Atlantic Avenue', '11231');
  48.  
  49. 9) Create a restaurant and return id and name
  50. Create a restaurant with values of your choosing, and return the id and name.
  51.  
  52. INSERT INTO restaurants (name, borough, cuisine, address_building_number, address_street, address_zipcode) VALUES ('Frankys', 'Brooklyn', 'Italian', '111', 'Atlantic Avenue', '11231') RETURNING id, name;
  53.  
  54. 10) Create three restaurants and return id and name
  55. Create three restaurants using a single command, with values of your choosing, returning the id and name of each restaurant.
  56.  
  57. INSERT INTO restaurants (name, borough, cuisine, address_building_number, address_street, address_zipcode) VALUES
  58. ('Kritens', 'Brooklyn', 'Italian', '112', 'Atlantic Avenue', '11231'),
  59. ('Hopes', 'Brooklyn', 'Italian', '113', 'Atlantic Avenue', '11231'),
  60. ('Pops', 'Brooklyn', 'Italian', '114', 'Atlantic Avenue', '11231')
  61. RETURNING id, name;
  62.  
  63. 11.) Update a record
  64. Update the record whose value for nyc_restaurant_id is '30191841'. Change the name from 'Dj Reynolds Pub And Restaurant' to 'DJ Reynolds Pub and Restaurant'.
  65.  
  66. UPDATE restaurants SET name = 'DJ Reynolds Pub and Restaurant' WHERE nyc_restaurant_id = '30191841';
  67.  
  68. 12) Delete by id
  69. Delete the grade whose id is 10.
  70.  
  71. DELETE from grades WHERE id = 10;
  72.  
  73. 13) A blocked delete
  74. Try deleting the restaurant with id of 22. What error do you get?
  75.  
  76. DELETE from restaurants WHERE id = 22;
  77. ERROR: update or delete on table "restaurants" violates foreign key constraint "grades_restaurant_id_fkey" on table "grades"
  78. DETAIL: Key (id)=(22) is still referenced from table "grades".
  79.  
  80. Take two seconds and come up with your own theory about what this message means.
  81.  
  82. Guessing that it's referenced somewhere else in another table and therefor deleting here gives error because it needs
  83. to be deleted in both tables? Or the main table?
  84.  
  85. A:
  86. Postgres is stopping us from deleting the restaurant because of a foreign key constraint. A foreign key constraint
  87. prevents us from destroying the referential integrity of a relational database.
  88.  
  89. 14) Create a table
  90. Create a new table called inspectors with the following properties:
  91.  
  92. first_name: String of inspector's first name, required
  93. last_name: String of inspector's last name, required
  94. borough: The borough the inspector works in, not required, one of Bronx, Brooklyn, Manhattan, Queens, Staten Island.
  95. inspectors should also have a system generated primary key property, id.
  96.  
  97. Note that the borough property requires you to use an enumerated type, which is a list of set values you can use for a property. You can use an existing enumerated type that will already be in the table: borough_options.
  98.  
  99. CREATE TABLE inspectors (
  100. id serial PRIMARY KEY,
  101. first_name text NOT NULL,
  102. last_name text NOT NULL,
  103. borough borough_options
  104. );
  105.  
  106. 15) Update a table
  107. Add a notes field to the grades table. notes are not required, and are text.
  108.  
  109. ALTER TABLE grades ADD COLUMN notes text;
  110.  
  111. 16) Drop a table
  112. Drop the inspectors table from the database.
  113.  
  114. DROP TABLE inspectors;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement