云萌主云应用官方论坛

标题: MySQL数据库的那点小总结 [打印本页]

作者: 骑单车的小女孩    时间: 2016-12-30 00:27
标题: MySQL数据库的那点小总结
本帖最后由 骑单车的小女孩 于 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




欢迎光临 云萌主云应用官方论坛 (https://yunmengzhu.com/) Powered by Discuz! X3.4