• Mysql两道英文上机练习


    环境准备

     安装Mysql(XAMPP集成开发环境快速安装)

    xmapp是傻瓜式安装的,一路next即可

     

     

     

     

     

     

     

    mysql默认用户名是root ,默认密码为空 

    安装Navicat数据库可视化管理软件

     

     

     

     

     

     

     

     使用navicat测试连接xmapp启动的mysql数据库

     

    ’题目要求

     

    X1MySQL复杂查询
    4.简答题
    3.1(b)Write the following queries in SQL,using the university schema.
    Find the IDs of all students who were taug ht by an instructor named Einstein;make s ure there are no duplicates in the result.

    开始创作

    1创建数据库

    1. create table classroom
    2. (building varchar(15),
    3. room_number varchar(7),
    4. capacity numeric(4,0),
    5. primary key (building, room_number)
    6. );
    7. create table department
    8. (dept_name varchar(20),
    9. building varchar(15),
    10. budget numeric(12,2) check (budget > 0),
    11. primary key (dept_name)
    12. );
    13. create table course
    14. (course_id varchar(8),
    15. title varchar(50),
    16. dept_name varchar(20),
    17. credits numeric(2,0) check (credits > 0),
    18. primary key (course_id),
    19. foreign key (dept_name) references department (dept_name)
    20. on delete set null
    21. );
    22. create table instructor
    23. (ID varchar(5),
    24. name varchar(20) not null,
    25. dept_name varchar(20),
    26. salary numeric(8,2) check (salary > 29000),
    27. primary key (ID),
    28. foreign key (dept_name) references department (dept_name)
    29. on delete set null
    30. );
    31. create table section
    32. (course_id varchar(8),
    33. sec_id varchar(8),
    34. semester varchar(6)
    35. check (semester in ('Fall', 'Winter', 'Spring', 'Summer')),
    36. year numeric(4,0) check (year > 1701 and year < 2100),
    37. building varchar(15),
    38. room_number varchar(7),
    39. time_slot_id varchar(4),
    40. primary key (course_id, sec_id, semester, year),
    41. foreign key (course_id) references course (course_id)
    42. on delete cascade,
    43. foreign key (building, room_number) references classroom (building, room_number)
    44. on delete set null
    45. );
    46. create table teaches
    47. (ID varchar(5),
    48. course_id varchar(8),
    49. sec_id varchar(8),
    50. semester varchar(6),
    51. year numeric(4,0),
    52. primary key (ID, course_id, sec_id, semester, year),
    53. foreign key (course_id, sec_id, semester, year) references section (course_id, sec_id, semester, year)
    54. on delete cascade,
    55. foreign key (ID) references instructor (ID)
    56. on delete cascade
    57. );
    58. create table student
    59. (ID varchar(5),
    60. name varchar(20) not null,
    61. dept_name varchar(20),
    62. tot_cred numeric(3,0) check (tot_cred >= 0),
    63. primary key (ID),
    64. foreign key (dept_name) references department (dept_name)
    65. on delete set null
    66. );
    67. create table takes
    68. (ID varchar(5),
    69. course_id varchar(8),
    70. sec_id varchar(8),
    71. semester varchar(6),
    72. year numeric(4,0),
    73. grade varchar(2),
    74. primary key (ID, course_id, sec_id, semester, year),
    75. foreign key (course_id, sec_id, semester, year) references section (course_id, sec_id, semester, year)
    76. on delete cascade,
    77. foreign key (ID) references student (ID)
    78. on delete cascade
    79. );
    80. create table advisor
    81. (s_ID varchar(5),
    82. i_ID varchar(5),
    83. primary key (s_ID),
    84. foreign key (i_ID) references instructor (ID)
    85. on delete set null,
    86. foreign key (s_ID) references student (ID)
    87. on delete cascade
    88. );
    89. create table time_slot
    90. (time_slot_id varchar(4),
    91. day varchar(1),
    92. start_hr numeric(2) check (start_hr >= 0 and start_hr < 24),
    93. start_min numeric(2) check (start_min >= 0 and start_min < 60),
    94. end_hr numeric(2) check (end_hr >= 0 and end_hr < 24),
    95. end_min numeric(2) check (end_min >= 0 and end_min < 60),
    96. primary key (time_slot_id, day, start_hr, start_min)
    97. );
    98. create table prereq
    99. (course_id varchar(8),
    100. prereq_id varchar(8),
    101. primary key (course_id, prereq_id),
    102. foreign key (course_id) references course (course_id)
    103. on delete cascade,
    104. foreign key (prereq_id) references course (course_id)
    105. );

    插入数据

    放入知识星球,3万多行sql语句下载地址:https://t.zsxq.com/08ur0K1e7

    (里面的LargeRelationshipsInsertFile.sql和SamllRelationshipsInsertFile.sql)

     

     

    执行SQL语句

    1. SELECT DISTINCT ID from takes WHERE course_id =(
    2. SELECT course_id from course where dept_name =(SELECT dept_name from instructor WHERE name="Einstein" AND ID="22222"));

    题目要求

     

    〈X1MySQL复杂查询
    2.简答题
    4.16 Write an soL query using the universit y schema to find the iD of each student wh o has never taken a course at the universit y.(Do this using subqueries,or an outerj oin).

    SELECT ID FROM student where ID not IN (SELECT DISTINCT ID FROM takes); 

  • 相关阅读:
    当涉及到API接口数据分析时,主要可以从以下几个方面展开
    Vue3+elementplus搭建通用管理系统实例十:动态表单及详情页实现上
    虚拟主机什么时候适合更换成云主机?
    AUTOSAR汽车电子嵌入式编程精讲300篇-基于 CAN 总线的车辆数据采集与远程监控系统研发(中)
    小米机器狗二代来了,比上一代更瘦、更快、更强!NVIDIA主控+全志MR813+全志R329协处理器
    数学建模—灰色关联分析
    webmin远程命令执行漏洞
    字符串和内存函数(2)
    构建决策树
    iTOP-3568开发板Ubuntu下安装ADB工具
  • 原文地址:https://blog.csdn.net/qq_39154376/article/details/127822163