Pg 1 of 9 Practice Test 2
Total Page:16
File Type:pdf, Size:1020Kb
Pg 1 of 9 Practice Test 2: SQL and Relational Algebra Name____________________________________ CIS395 Fall 2017 Part A: Evaluation Write DDL (Data Definition Language) or DML (Data Manipulation Language) above each SQL statement or keyword group depending on which part of the language it belongs to: (12 pts) 1._______________ 7._______________ SELECT * GRANT SELECT, INSERT FROM Stars; ON Moons TO Bobby; 8. ______________ 2. _______________ CREATE TABLE Asteroids( UPDATE SolarSystems Name VarChar(80), SET StarName = 'Alpha Centauri' Mass Number(8,4) NOT NULL, WHERE SolarId = 4E6BF309; CONSTRAINT pk_asteroids PRIMARY KEY (Name) ); 3._______________ DROP TABLE Planets; 9._________________ PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, 4._______________ CHECK ALTER TABLE Moons ADD mass Number(12, 4); 10.________________ INSERT INTO Asteroids 5._______________ VALUES ('HAL42', 3000.24); DELETE FROM Galaxies; 11._________________ REVOKE ALL PRIVILEGES FROM krusty; 6.____________ CREATE VIEW RedGiants AS SELECT * 12.__________________ FROM Stars VARCHAR2(80), WHERE color='red' AND size='giant'; NUMBER(4,2), DATE Pg 2 of 9 Evaluate the Results of the following code fragments (what is the output): (14 pts) 13. ABS(-999) ___________________ 20.FLOOR(34.6) ______________ 14. TRIM(' green ') ___________________ 21. CEIL(34.2) ______________ 15. CAST(72 AS VARCHAR(80)) __________________ 22. UPPER('Harry Hood') ______________ 16. POSITION('pp' IN 'apple') ___________________ 23. SUBSTRING('salad', 3, 2) ______________ 17. POWER(4, 3) ___________________ 24. CHAR_LENGTH('satan himself') ______________ 18. LOWER('Banana') ___________________ 26. CHECK('wolf' IN ('canine', 'dog', 'wolf') ______________ 19. 3 + 4 = CAST('7' AS NUMBER) _________________ 27. 'greenland' LIKE 'green%' ______________ Use the following tables to answer the next set of questions: (always use the original tables for each problem) Cars Trucks LicensePlate Color LicensePlate Color VYN-192 red XP-3000 black XYZ-723 red MT-4222 red ABC-123 blue LV-2020 chrome LMN-666 chrome NY-3476 black Draw the resulting table from the following query. Consider each command to be operating on the original unaltered table written on the test: (16 pts) 28. SELECT * FROM Cars INTERSECT SELECT * FROM Trucks; (2 pts) 29. DELETE FROM Trucks; (2 pts) Pg 3 of 9 30. DELETE FROM Cars WHERE Color = 'red'; (2 pts) 31. INSERT INTO Trucks VALUES ('PQ', 'yellow'); (2pts) 32. UPDATE Cars SET Color = 'watermelon' WHERE Color = 'red'; (2 pts) 33. SELECT Color FROM Trucks MINUS SELECT Color FROM Cars; (2 pt) 34. SELECT LicensePlate FROM TRUCK UNION SELECT LicensePlate FROM Car; (4 pts) Pg 4 of 9 Part C. Use these tables to answer the next set of questions: States StateCode StateName Population MA Massachusetts 19,750,000 PA Pennsylvania 38,000,000 Cities CityName StateCode Springfield MA Springfield PA Philadelphia PA Boston MA Residents ResidentName CityName StateCode Homer Springfield MA Janis Philadelphia PA Bill Boston MA Marge Springfield MA Theodore Springfield PA Taniel Philadelphia PA CanadianProvinces ProvinceCode ProvinceName Population ON Ontario 4,000,000 QC Quebec 8,100,000 AB Alberta 13,400,000 Pg 5 of 9 Draw the resulting table returned by each relational algebra query: 1. population > 20,000,000(States) 2. States Cities 3. (ResidentName)(Residents) 4. States ⋈States.StateCode = Cities.StateCode Cities Pg 6 of 9 Part C: Generation Write SQL to achieve what is requested in the following problems: (18 pts) 1. Create a table called Motorcycles. Each motorcycle will have a unique license plate number that must be provided as well as a color that also must not be empty. Make the license plate number the primary key. (6 pts) 2. Add a column named TankSize to the already existing Cars table in Part A. The possible allowed values are 10-gallon, 15-gallon, and 20-gallon. (5 pts) 3. Please remove the Trucks table from the database. (2 pts) 4. Suppose there is a Paint table with the following schema: Paint(colorName, hex) where colorName is its primary key. Add a foreign key constraint to the Cars table so that the Cars' color column references the colorName column of the Paint table. (5 pts) Pg 7 of 9 Use the following tables to retrieve the data requested on the following pages. Please write your answers as SQL SELECT queries and also write a corresponding relational algebra query. Moons revolve around planets. Planets revolve around stars. Stars belong to solar systems. Solar systems belong to galaxies: (40 pts) Stars Solar Systems Name Color Size Mass (suns) ParentSolarSystem Name Distance (light years) Sun Yellow dwarf 1 Sol Sol 0 Alpha Centauri A Yellow dwarf 1.1 Alpha Alpha Centauri 4.365 Centauri Tau Ceti 11.887 Alpha Centauri B Orange dwarf 0.9 Alpha Centauri Ross 780 15.342 Proxima Centauri red dwarf 0.123 Alpha Planets Centauri Gliese 876 red dwarf 0.37 Ross 780 Name Mass (Earths) type ParentStar Tau Ceti Yellow dwarf 0.783 Tau Ceti Mercury 0.055 Rocky Sun Venus 0.85 Rocky Sun Moons Earth 1 Rocky Sun Name Diameter (km) ParentPlanet Mars 0.107 Rocky Sun Moon 3474 Earth Jupiter 317.8 Gaseous Sun Deimos 12.6 Mars Saturn 95.159 Gaseous Sun Phobos 22.2 Mars Uranus 14.536 Gaseous Sun Io 3660.0 Jupiter Neptune 17.147 gaseous Sun Europa 3121.6 Jupiter Proxima b 1.27 rocky Proxima Centauri Ganymede 5262.4 Jupiter Gliese d 6.83 rocky Gliese 876 Callisto 4820.6 Jupiter Gliese c 226.99 gaseous Gliese 876 Titan 5150 Saturn Gliese b 723.25 gaseous Gliese 876 Enceladus 504 Saturn Gliese e 14.6 gaseous Gliese 876 Mimas 396 Saturn Tau Ceti b 2 rocky Tau Ceti Tethys 1062 Saturn Tau Ceti c 3.1 rocky Tau Ceti Dione 1123 Saturn Tau Ceti d 3.6 rocky Tau Ceti Rhea 1527 Saturn Tau Ceti e 4.3 rocky Tau Ceti Iapetus 1470 Saturn Tau Ceti f 6.6 rocky Tau Ceti Galaxies Name DistanceFromUs (mly) Type Andromeda 2.54 Barred spiral Milky Way 0 Barred spiral Sunflower 27 spiral Pg 8 of 9 5. Write an SQL query and a relational algebra query that returns the entire Galaxies table. (10 pts) 6. Write an SQL query and a relational algebra query that returns a list of all of the names of the stars. (10 pts) 7. Write an SQL query and a relational algebra query that returns all of the stars that belong to the solar system named Alpha Centauri. Your query should return the entire row(s). (10 pts) 8. Write an SQL query and a relational algebra query that returns the name of all of Mars' moons. (10 pts) 9. Write an SQL query and a relational algebra query that returns the total number of planets orbiting the star named Gliese 876. (10 pts) Pg 9 of 9 10. Write an SQL query and a relational algebra query that returns the average diameter of each planet's moons (there should be an average provided for Jupiter's, Mars', Saturn's, and Earth's moons). (10 pts) 11. Write an SQL query and a relational algebra query that returns a list of planet names paired with the name of the solar system it belongs to. (10 pts) 12. Write an SQL query and a relational algebra query that returns a table with the names and masses of all suns and planets, where each row could be a sun or a planet. (10 pts).