Student(xh,xm,xb,nl,szx) Course(kch,kcm,xxk,xf) SG(xh,kch,cj)
ÆäÖУºStudentÊÇѧÉú±í£¬xh±íʾѧºÅ¡¢xm±íʾÐÕÃû¡¢sb±íʾÐÔ±ð¡¢nl±íʾÄêÁä¡¢szx±íʾËùÔÚϵ¡£xhΪÖ÷Âë¡£
CourseÊÇ¿Î³Ì±í£¬kch±íʾ¿Î³ÌºÅ¡¢kcm±íʾ¿Î³ÌÃû¡¢xxk±íʾÏÈÐ޿κš¢xf±íʾѧ·Ö¡£kchΪÖ÷Âë¡£
SGÊÇÑ¡¿Î±í£¬xh±íʾѧºÅ£¬²ÎÕÕѧÉú±íµÄѧºÅxh¡¢kch±íʾ¿Î³ÌºÅ£¬²ÎÕտγ̱íµÄ¿Î³ÌºÅkch¡¢cj±íʾ³É¼¨¡£(xh,kch)ΪÖ÷Âë¡£ ÇëÓÃSQLÓïÑÔʵÏÖÏÂÁй¦ÄÜ£º
1.½¨Á¢Ñ¡¿Î±íSG£¬ÒªÇóʵÏÖÖ÷¼üÔ¼ÊøºÍÍâ¼üÔ¼Êø£¬Çҳɼ¨²»ÄÜΪ¿Õ¡£ create table SG( xh char(5), kch char(5), cj int
primary key(xh,kch),
foreign key(xh) references Student(xh), foreign key(kch) references Course(kch)) 2.²éѯѡÐ޿γ̵ijɼ¨Ð¡ÓÚ60·ÖµÄÈËÊý¡£ select count(*) from SG where cj<60
3.²éѯѡÐÞÁËC3ºÅ¿Î³ÌµÄѧÉúµÄѧºÅ¼°Æä³É¼¨£¬²éѯ½á¹û°´·ÖÊýµÄ½µÐòÅÅÁС£ÒªÇó²éѯ½á¹ûµÄ±êÌâÏÔʾΪºº×Ö¡£
select xh ѧºÅ,cj ³É¼¨ from SG where kch='C3' order by cj
4.²éѯѡÐÞÁË¡°Êý¾Ý¿âÓ¦Ó᱿γÌÇҳɼ¨ÔÚ90·ÖÒÔÉϵÄѧÉúµÄÐÕÃûºÍËùÔÚϵ¡£
select xm,szx from Student s,Course c,SG g
where s.xh=g.xh and c.kch=g.kch and kcm='Êý¾Ý¿âÓ¦ÓÃ' and cj>90 5.ÏòStudent±íÖвåÈëÒ»Ìõ¼Ç¼£¬Ñ§ºÅΪ¡°05020¡±£¬ÐÕÃûΪ¡°¶¡Àò¡±£¬ÐÔ±ðΪ¡°Å®¡±£¬ÄêÁäΪ¡°17¡±£¬ËùÔÚϵΪ¡°¼ÆËã»úϵ¡±¡£ insert into Student
values('05020','¶¡Àò'£¬¡®Å®¡¯£¬¡®17¡¯£¬¡®¼ÆËã»ú¡¯) 6.½«¼ÆËã»úϵȫÌåѧÉúµÄ³É¼¨ÖÃÁã¡£ update SG set cj=0
where '¼ÆËã»ú'=(select szx from Student where Student.xh=SG.xh )
7.´´½¨Ò»¸ö¡°Ñ§Éú³É¼¨¡±ÊÓͼ£¬°üÀ¨Ñ¡ÐÞÁ˿γ̵ÄѧÉúµÄѧºÅ¡¢ÐÕÃû¡¢Ñ¡Ð޿γ̵Ŀγ̺š¢¿Î³ÌÃûÒÔ¼°³É¼¨¡£ create view ѧÉú³É¼¨ as
select s.xh,xm,g.kch,kcm ,cj from Student s,Course c,SG g where s.xh=g.xh and c.kch=g.kch
8.ΪStudent±í½¨Á¢Ò»¸ö°´Ñ§ºÅÉýÐòÅÅÁеÄΨһË÷ÒýStusno_IDX¡£ create unique index Stusno_IDX on Student(xh) order by xh
9.Çó¸÷¿Î³ÌµÄÑ¡ÐÞÈËÊý¼°Æ½¾ù³É¼¨¡£
select count(xh),avg(cj) from SG group by kch
10.²éѯѡÐÞÁ˿γ̱àºÅΪ¡¯14001¡¯ºÍ¡¯14002¡¯¿Î³ÌµÄѧÉúµÄѧºÅºÍÐÕÃû¡£
select s.xh,xm from Student s,SG g from Student s,SG g
where kch='14001' or kch='14002'
£¨ËÄ£©ÉèÓÐÈçÏÂËùʾµÄÈý¸ö¹ØÏµÄ£Ê½£ºÉ̵êShop£¨bh£¬mc£¬cs£©£¬ÉÌÆ·Product£¨sph£¬spm£¬jg£©£¬É̵êËùÊÛÉÌÆ·SP£¨bh£¬sph£¬sl£©£¬ÆäÖдøÏ»®ÏßµÄ×Ö¶ÎΪÖ÷¼ü¡£¸÷ÊôÐÔº¬ÒåÈçÏ£ºbh (É̵ê±àºÅ)¡¢mc (É̵êÃû)¡¢cs (ËùÔÚ³ÇÊÐ)¡¢sph (ÉÌÆ·±àºÅ)¡¢spm(ÉÌÆ·Ãû³Æ)¡¢jg (¼Û¸ñ)¡¢sl (ÉÌÆ·ÊýÁ¿)¡£
ShopSP bh mc Cs ³¤É³ ±±¾© ±±¾© ³¤É³ ÉϺ£ 101 °Ù»õÉ̵ê 204 ³¤°²É̳¡ 256 Î÷µ¥É̳¡ 345 ÌúµÀÉ̵ê 620 ̫ƽÑó°Ù»õ bh 101 101 101
Product sph spm 1 2 3 4 ¸Ö±Ê ÓðëÇò ¸´¶Á»ú Êé°ü 101 204 jg 21 5 300 76 256 256 345 345 345 620 sph sl 1 2 3 4 3 1 2 1 2 4 4 105 42 25 104 61 241 91 141 18 74 125 ÊÔÓÃSQLÓïÑÔÍê³ÉÏÂÁвÙ×÷£º £¨1£©ÓÃCreateÓï¾ä´´½¨É̵ê±íShop£¬ÒªÇó´´½¨Ö÷¼ü£¬É̵êÃû²»ÔÊÐíΪ¿Õ£¬¸÷ÊôÐÔµÄÊý¾ÝÀàÐ͸ù¾Ý±íÖÐËù¸øÊý¾ÝÑ¡¶¨¡£
creat table Shop( bh char(4) primary key, mc char(10) not null, cs char(4))
£¨2£©¼ìË÷ËùÓÐÉ̵êµÄÉ̵êÃûºÍËùÔÚ³ÇÊС£ select mc,cs from Shop
£¨3£©¼ìË÷¼Û¸ñµÍÓÚ50ÔªµÄËùÓÐÉÌÆ·µÄÉÌÆ·ÃûºÍ¼Û¸ñ¡£ select spm,jg from Product where jg<50
£¨4£©¼ìË÷λÓÚ¡°±±¾©¡±µÄÉ̵êµÄÉ̵ê±àºÅ£¬É̵êÃû£¬½á¹û°´ÕÕÉ̵ê±àºÅ½µÐòÅÅÁС£
select bh,mc from Shop where cs='±±¾©' order by bh;
£¨5£©¼ìË÷¹©Ó¦¡°Êé°ü¡±µÄÉ̵êÃû³Æ¡£
select mc from Shop s,Product p,SP
where s.bh=SP.bh and p.sph=SP.sph and spm='Êé°ü'; £¨6£©¼ìË÷ËùÓÐÉ̳¡Öи÷ÖÖÉÌÆ·µÄƽ¾ùÊýÁ¿¡£ select avg(sl) from SP group by sph;
£¨7£©½«ÉÌÆ·¡°¸´¶Á»ú¡±µÄ¼Û¸ñÐÞ¸ÄΪ350¡£ update Product set jg=350 where spm='¸´¶Á»ú';
£¨8£©½«¡°°Ù»õÉ̵ꡱµÄÉ̵êÃûÐÞ¸ÄΪ¡°°Ù»õÉ̳¡¡±¡£ update Shop set mc='°Ù»õÉ̳¡' where mc='°Ù»õÉ̵ê'; £¨9£©´´½¨ÊÓͼ£º¡°ÌúµÀÉ̵ꡱËùÊÛÉÌÆ·µÄÉÌÆ·±àºÅ£¬ÉÌÆ·ÃûºÍÊýÁ¿¡£