返回顶部

MySQL数据库的那点小总结

[复制链接]
骑单车的小女孩Lv.2 显示全部楼层 发表于 2016-12-30 00:27:20 |阅读模式 打印 上一主题 下一主题

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 骑单车的小女孩 于 2016-12-30 00:28 编辑
  1. <div class="blockcode"><blockquote>CREATE DATABASE  ku1;
  2. CREATE TABLE person(
  3.    id INT PRIMARY KEY,
  4.    username VARCHAR(20)
  5.    );  
  6.    
  7. CREATE TABLE car (
  8.    id INT PRIMARY KEY,
  9.    carname VARCHAR(20),
  10.    pid INT,
  11.    FOREIGN KEY(pid) REFERENCES person (id)   
  12. )   


  13. INSERT INTO car VALUES(1,'baoma',NULL)
  14. INSERT INTO person VALUES(1,'zhangsan');

  15. INSERT INTO car VALUES(2,'qq',1);

  16. DELETE FROM person

  17. /* 给张三买汽车 */
  18. INSERT INTO person VALUES(1,'zhangsan');
  19. INSERT INTO car VALUES (2,'hanma',1);
  20. INSERT INTO car VALUES (3,'luhu',1);

  21. /*李四买汽车*/
  22. INSERT INTO person VALUES(2,'lisi');
  23. INSERT INTO car  VALUES(4,'xiali',2);
  24. INSERT INTO car  VALUES(5,'qq',2)  

  25. /*莉莉没车*/
  26. INSERT INTO person VALUES(3,'lili');

  27. /*某人有某车*/

  28. SELECT person.username,car.carname FROM person,car;  

  29. SELECT p.username,c.carname FROM person p,car c WHERE p.id = c.pid;  

  30. SELECT p.username,c.carname FROM  person p INNER JOIN car c ON p.id = c.pid;




  31. SELECT p.username,c.carname FROM  person p RIGHT JOIN car c ON p.id = c.pid;

  32.   
  33.   
  34.   
  35. /*谁没有车*/  
  36. SELECT p.username,c.carname FROM  person p LEFT JOIN car c ON p.id = c.pid WHERE c.carname IS NULL;  
  37.   
  38. /*某人有悍马车*/  
  39. SELECT * FROM person WHERE id = 1   

  40. SELECT pid FROM car WHERE carname='hanma'   

  41. SELECT * FROM person WHERE id = ( SELECT pid FROM car WHERE carname='hanma' );

  42. /******/
  43.   
  44. CREATE TABLE idcard (
  45.    id INT PRIMARY KEY,
  46.    gov VARCHAR(30),
  47.    FOREIGN KEY (id) REFERENCES person (id)
  48. )
  49.   
  50. INSERT INTO idcard VALUES(1,'北京发证');
  51. INSERT INTO idcard VALUES(2,'上海发证');

  52. /*某人在某地发证*/

  53. SELECT username , gov FROM  person INNER JOIN  idcard ON person.id = idcard.id;

  54. SELECT username ,gov FROM person LEFT JOIN idcard ON person.id = idcard.id;


  55. SELECT username ,gov FROM person LEFT JOIN idcard ON person.id = idcard.id WHERE idcard.gov IS NULL;  

  56. SELECT username ,gov FROM person RIGHT JOIN idcard ON person.id = idcard.id

  57. /*  */  

  58. CREATE TABLE  student (

  59.   id INT PRIMARY KEY,
  60.   username VARCHAR(20)  

  61. )  


  62. CREATE TABLE course(

  63.    id INT PRIMARY KEY,
  64.    cname VARCHAR(20)
  65. )

  66. /*联合主键*/
  67. CREATE TABLE sc(

  68.     sid INT,
  69.     cid INT,
  70.     PRIMARY KEY(sid,cid),
  71.     FOREIGN KEY(sid) REFERENCES student (id),
  72.     FOREIGN KEY(cid) REFERENCES course (id)
  73. )



  74. INSERT INTO student VALUES (1,'zhangsan');
  75. INSERT INTO student VALUES (2,'lisi');



  76. INSERT INTO course VALUES(1,'java');
  77. INSERT INTO course VALUES(2,'php');
  78. INSERT INTO course VALUES(3,'ios');

  79. INSERT INTO sc VALUES (1,1);
  80. INSERT INTO sc VALUES (1,2);

  81. INSERT INTO sc VALUES (2,3);


  82. /*某人选择了某课*/

  83. SELECT username,cname FROM student INNER JOIN sc ON student.id = sc.sid
  84.                                    INNER JOIN course ON sc.cid = course.id


  85. SELECT username,cname FROM student LEFT JOIN sc ON student.id = sc.sid
  86.                                    INNER JOIN course ON sc.cid = course.id

  87. SELECT username,cname FROM student LEFT JOIN sc ON student.id = sc.sid
  88.                                    LEFT JOIN course ON sc.cid = course.id

  89. SELECT username,cname FROM student RIGHT JOIN sc ON student.id = sc.sid
  90.                                    RIGHT JOIN course ON sc.cid = course.id
复制代码


来源:http://www.open-open.com/code/view/1448983351264
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

云萌主 云萌主-BIGSAAS旗下,由北京合智互联信息技术有限公司在2018年创立,为广大云应用技术爱好者的平台。在云萌主论坛可以查看云应用技术文章、云产品产品最新资讯、技术问答、技术视频。在畅游云上技术的同时,学到最新的云应用产品和技术。
  • 微信公众号

  • Powered by Discuz! X3.5 | Licensed | Copyright © 2001-2022, Aliyun Cloud. | 星点互联设计
  • 京ICP备18052714号 | 营业执照 | |合智互联| QQ