Sie sind auf Seite 1von 3

Kerry Heineck e Homework #2 CIS-317-DL

Prov ide the SQL for the following queries: 1. List all d etails of the k nown planets. SELECT * FROM PLA NET ; 2. List th e name, crew size, and sh ip class for ev ery starship. SELECT NA ME, CREWSI ZE, SHIPCLA SS FROM STA RSHIP; 3. What is the name of star ship number 501? SELECT NA ME, STA RSHIPID FROM STA RSHIP WHERE STA RSHIPID=501; 4. Show all Star ships launched after stardate 21500, sorted by shipClass and n ame. SELECT * FROM STA RSHIP WHERE LA UNCHSTA RDA TE > 21500 ORDER BY SHIPCLA SS, NA ME; 5. Show all Star ships exc ept those in th e 'Argon' class, sorted by launchStardate. SELECT * FROM STA RSHIP WHERE SHIPCLA SS != 'A rgon' ORDER BY LA UNCH STA RDATE; 6. Show all plan ets with a r adius r anging from 2000 to 4000 k ilo meter s, sorted from largest to smallest. SELECT * FROM PLA NET WHERE RA DIUS >=2000 A ND RA DIUS <=4000 ORDER BY RA DIUS; 7. Show all Plan etV isits where the length of th e v isit >= 4 stardates. SELECT * FROM PLA NETVISIT WHERE DEPA RTURESTA RDATE-A RRIVA LSTA RDATE >=4; 8. List th e planet, star ship, and length of stay for each planet v isit.

SELECT PLA NETID STA RSHIPID DEPA RTURESTA RDA TE-A RRIVA LSTA RDATE A S LENGTHOFSTA Y FROM PLA NETVISIT ; 9. Show all Plan etV isits to planets with planetIDs 1, 2, and 5, sorted by planetID and starshipID. SELECT * FROM PLA NETVISIT WHERE PLA NETID I N (1, 2, 5) ORDER BY PLA NETID, STA RSHIPID;

10. Show the av erage crew size for all starship s in each shipClass. Th e result table should hav e two columns: 'shipClass' and 'Av erage Crew'. SELECT SHIPCLA SS, A VG (CREWSIZE) A S A VERA GEC REW FROM STA RSHIP GRO UP BY SHIPCLA SS; 11. For each Planet, show the number of v isits made by all star ships. Your result table should hav e two columns: 'planetID ' and 'Nu m V isits'. SELECT PLA NETID, COUNT (STA RSHIPID) A S NUMVI SITS FROM PLA NETVISIT GRO UP BY PLA NETID ORDER BY PLA NETID; 12. Show all Star ships whose n ame has an 'a' as the 2nd character (For example, 'Nav igator'). SELECT * FROM STA RSHIP WHERE NA ME LIK E '_a % '; 13. List th e details of each plan et v isit along with the crewsize of the starship mak ing the v isit. SELECT P.PLA NETID, P.STA RSHIPID, P.A RRIVA LSTA RDA TE, P.DEPA RTURESTA RDATE, S.CREWSI ZE FROM PLA NETVISIT P, STA RSHIP S WHERE P.STA RSHIPID=S.STA RSHIPID ORDER BY PLA NETID; 14. Create a table Moon with the following attributes: MoonID, name, P lanetID, rad ius. CREA TE TA BLE MOON ( MoonID numeric not null primary key, Na me char (50), planetID numeric not null, Radius numeric) ; 15. Add a constraint to the Moon tab le mak ing plan etId a foreign k ey to the Plan et table. First, I a dde d the f ollow ing to the table plane t:

insert into planet value s (301,'MoonA ', 3500, 'None ' ) ; insert into planet value s (305,'MoonB ', 2500, 'None ' ) ; insert into planet value s (307,'MoonC ', 10000, 'None ' ); insert into planet value s (303,'MoonD ', 4 300, 'None ' ) ; insert into planet value s (309,'MoonE', 5750, 'None ' ); Then I created the following constraint: A lter table MOO N A dd constraint fk1_planet FO REI GN K EY (plane tID) References planet ( planetID) ;

16. Insert 5 rows of d ata in to the Moon table. Insert into MOO N va lue s ( 101, MoonA , 301, 3500); Insert into MOO N va lue s ( 102, MoonB, 305, 2500); Insert into MOO N va lue s ( 103, MoonC, 307, 10000); Insert into MOO N va lue s ( 104, MoonD, 303, 4300); Insert into MOO N va lue s ( 105, MoonE, 309, 5750);

17. Delete the Moon table. Drop ta ble MOO N; 18. List th e name of ev ery starship in th e Admiral class or the name of ev ery planet with a r adius > 3000. SELECT NA ME FROM STA RSHIP WHERE SHIPCLA SS = 'A dmiral' UNIO N SELECT NA ME FROM PLA NET WHERE RA DIUS > 3000; 19. Find the name, crewsize, and shipclass of ev ery starsh ip whose crew size is larger then the crewsize of ev ery star ship of shipclass L ightCruiser. SELECT NA ME, CREWSI ZE, SHIPCLA SS FROM STA RSHIP WHERE C REWSI ZE > A LL (SELECT CREWSIZE FROM STA RSHIP WHERE SHIPCLA SS = 'LightCruiser') ; 20. Change the name of star ship 354 to Atlas. UPDA TE STA RSHIP SET NA ME = 'A tlas' WHERE STA RSHIPID = '354';

Das könnte Ihnen auch gefallen