当前位置:网站首页>MySQL exercises elementary 45 questions (Unified table)
MySQL exercises elementary 45 questions (Unified table)
2022-07-26 18:41:00 【Piglet vs Hengge】
(1) Create a database sql_exercise01
(2) establish 4 Data tables :
① Student list (Student)
② The curriculum (Course)
③ League tables (Score)
④ Teacher information sheet (Teacher)
The design is as follows. Complete the following information for the four tables :
Table structure :
# Student list
mysql> desc student;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Sno | varchar(20) | NO | PRI | NULL | |
| Sname | varchar(20) | NO | | NULL | |
| Ssex | varchar(20) | NO | | NULL | |
| Sbirthday | datetime | YES | | NULL | |
| Class | varchar(20) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
# Teachers list
mysql> desc teacher;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Tno | varchar(20) | NO | PRI | NULL | |
| Tname | varchar(20) | NO | | NULL | |
| Tsex | varchar(20) | NO | | NULL | |
| Tbirthday | datetime | YES | | NULL | |
| Prof | varchar(20) | YES | | NULL | |
| Depart | varchar(20) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> # The curriculum
mysql> desc course;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Cno | varchar(20) | NO | PRI | NULL | |
| Cname | varchar(20) | NO | | NULL | |
| Tno | varchar(20) | NO | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> # League tables
mysql> desc score;
+--------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| Sno | varchar(20) | NO | MUL | NULL | |
| Cno | varchar(20) | NO | MUL | NULL | |
| Degree | decimal(10,0) | YES | | NULL | |
+--------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)Table data
mysql> # Student list
mysql> select *from student;
+-----+--------+------+---------------------+-------+
| Sno | Sname | Ssex | Sbirthday | Class |
+-----+--------+------+---------------------+-------+
| 101 | Li Jun | male | 1976-02-20 00:00:00 | 95033 |
| 103 | Lu Jun | male | 1974-06-03 00:00:00 | 95031 |
| 105 | Kuang Ming | male | 1975-10-02 00:00:00 | 95031 |
| 107 | Wang Li | Woman | 1976-01-23 00:00:00 | 95033 |
| 108 | Zeng Hua | male | 1977-09-01 00:00:00 | 95033 |
| 109 | Wang Fang | Woman | 1975-02-10 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+
6 rows in set (0.00 sec)
mysql> # The curriculum
mysql> select *from course;
+-------+-----------------+-----+
| Cno | Cname | Tno |
+-------+-----------------+-----+
| 3-105 | Introduction to computer | 825 |
| 3-245 | operating system | 804 |
| 6-166 | digital circuit | 856 |
| 9-888 | Advanced mathematics | 831 |
+-------+-----------------+-----+
4 rows in set (0.00 sec)
mysql> # League tables
mysql> select *from score;
+-----+-------+--------+
| Sno | Cno | Degree |
+-----+-------+--------+
| 103 | 3-245 | 86 |
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
| 103 | 3-105 | 92 |
| 105 | 3-105 | 88 |
| 109 | 3-105 | 76 |
| 103 | 3-105 | 64 |
| 105 | 3-105 | 91 |
| 109 | 3-105 | 78 |
| 103 | 6-166 | 85 |
| 105 | 6-166 | 79 |
| 109 | 6-166 | 81 |
+-----+-------+--------+
12 rows in set (0.00 sec)
mysql> # Teachers list
mysql> select *from teacher;
+-----+--------+------+---------------------+-----------+-----------------+
| Tno | Tname | Tsex | Tbirthday | Prof | Depart |
+-----+--------+------+---------------------+-----------+-----------------+
| 804 | Li Cheng | male | 1958-12-02 00:00:00 | associate professor | Department of Computer Science |
| 825 | Wang Ping | Woman | 1972-05-05 00:00:00 | Assistant | Department of Computer Science |
| 831 | Liu Bing | Woman | 1977-08-14 00:00:00 | Assistant | Department of electronic engineering |
| 856 | Zhang Xu | male | 1969-03-12 00:00:00 | lecturer | Department of electronic engineering |
+-----+--------+------+---------------------+-----------+-----------------+
4 rows in set (0.00 sec)Complete according to the above requirements MySQL Statement writing
# Create database sql_exercise01
CREATE DATABASE sql_exercise01;
# Using a database sql_exercise02
USE sql_exercise01;
# Student list
CREATE TABLE student(
`Sno` VARCHAR(20) NOT NULL COMMENT ' Student student id ',
`Sname` VARCHAR(20) NOT NULL COMMENT ' The student's name ',
`Ssex` VARCHAR(20) NOT NULL COMMENT ' Student gender ',
`Sbirthday` DATETIME COMMENT ' Date of birth ',
`Class` VARCHAR(20) COMMENT ' Class number ',
PRIMARY KEY(`Sno`)
)
# Teachers list
CREATE TABLE teacher(
`Tno` VARCHAR(20) NOT NULL COMMENT ' Teacher number ',
`Tname` VARCHAR(20) NOT NULL COMMENT ' Teacher name ',
`Tsex` VARCHAR(20) NOT NULL COMMENT ' Teacher gender ',
`Tbirthday` DATETIME COMMENT ' Date of birth ',
`Prof` VARCHAR(20) COMMENT ' Teacher title ',
`Depart` VARCHAR(20) NOT NULL COMMENT ' achievement ',
PRIMARY KEY(`Tno`)
)
# The curriculum
CREATE TABLE course(
`Cno` VARCHAR(20) NOT NULL COMMENT ' Elective number ',
`Cname` VARCHAR(20) NOT NULL COMMENT ' Course name ',
`Tno` VARCHAR(20) NOT NULL COMMENT ' Teacher number ',
PRIMARY KEY(`Cno`)
)
# Will the Tno In the field key Set to MUL
ALTER TABLE course ADD INDEX(Tno);
# League tables
CREATE TABLE score(
`Sno` VARCHAR(20) NOT NULL COMMENT ' Student student id ',
`Cno` VARCHAR(20) NOT NULL COMMENT ' Elective number ',
`Degree` DECIMAL(10,0) COMMENT ' achievement '
)
# Will the Sno,Cno In the field key Set to MUL
ALTER TABLE score ADD INDEX(Sno);
ALTER TABLE score ADD INDEX(Cno);
# To the students student Table add data
# Use insert Statement to user Add data to the table
# grammar :insert into Table name ( Field 1, Field 2, Field 3, … )values(' value 1', ' value 2', ' value 3',…);
INSERT INTO student(sno,sname,ssex,sbirthday,class)VALUES(101,' Li Jun ',' male ','1976-02-20','95033');
INSERT INTO student VALUES(103,' Lu Jun ',' male ','1974-06-03','95031');
INSERT INTO student VALUES(105,' Kuang Ming ',' male ','1975-10-02','95031');
INSERT INTO student VALUES(107,' Wang Li ',' Woman ','1976-01-23','95033');
INSERT INTO student VALUES(108,' Zeng Hua ',' male ','1977-09-01','95033');
INSERT INTO student VALUES(109,' Wang Fang ',' Woman ','1975-02-20','95031');
# Information to teachers teacher Table add data
INSERT INTO teacher
VALUES
(804,' Li Cheng ',' male ','1958-12-02',' associate professor ',' Department of Computer Science '),
(856,' Zhang Xu ',' male ','1969-03-12',' lecturer ',' Department of electronic engineering '),
(825,' Wang Ping ',' Woman ','1972-05-05',' Assistant ',' Department of Computer Science '),
(831,' Liu Bing ',' Woman ','1977-08-14',' Assistant ',' Department of electronic engineering ');
# To the course course Table add data
INSERT INTO course
VALUES
('3-105',' Introduction to computer ',825),
('3-245',' operating system ',804),
('6-166',' digital circuit ',856),
('9-888',' Advanced mathematics ',831);
# To the result score Table add data
INSERT INTO score
VALUES
(103,'3-245',86),
(105,'3-245',75),
(109,'3-245',68),
(103,'3-105',92),
(105,'3-105',88),
(109,'3-105',76),
(101,'3-105',64),
(107,'3-105',91),
(108,'3-105',78),
(101,'6-166',85),
(107,'6-166',79),
(108,'6-166',81);
1. Inquire about Student All the records in the table Sname , Ssex and Class Column
SELECT Sname,Ssex,Class FROM Student;
--> final result
mysql> SELECT Sname,Ssex,Class FROM Student;
+-------+------+-------+
| Sname | Ssex | Class |
+-------+------+-------+
| Li Jun | male | 95033 |
| Lu Jun | male | 95031 |
| Kuang Ming | male | 95031 |
| Wang Li | Woman | 95033 |
| Zeng Hua | male | 95033 |
| Wang Fang | Woman | 95031 |
+-------+------+-------+2. Query all the units of the teacher is not repeated Depart Column
SELECT DISTINCT Depart FROM teacher;
--> final result :
mysql> SELECT DISTINCT Depart FROM teacher;
+------------+
| Depart |
+------------+
| Department of Computer Science |
| Department of electronic engineering |
+------------+3. Inquire about Student All the records of the table
SELECT * FROM Student;
--> final result :
mysql> SELECT * FROM Student;
+-----+-------+------+---------------------+-------+
| Sno | Sname | Ssex | Sbirthday | Class |
+-----+-------+------+---------------------+-------+
| 101 | Li Jun | male | 1976-02-20 00:00:00 | 95033 |
| 103 | Lu Jun | male | 1974-06-03 00:00:00 | 95031 |
| 105 | Kuang Ming | male | 1975-10-02 00:00:00 | 95031 |
| 107 | Wang Li | Woman | 1976-01-23 00:00:00 | 95033 |
| 108 | Zeng Hua | male | 1977-09-01 00:00:00 | 95033 |
| 109 | Wang Fang | Woman | 1975-02-20 00:00:00 | 95031 |
+-----+-------+------+---------------------+-------+4. Inquire about Score The score in the table is 60 To 80 All records between
边栏推荐
- Module 8 job message data MySQL table design
- PS_1_认识主界面_新建文档(分辨率)_打开保存(序列动画)
- Visual VM 定位OOM,FullGC使用方法
- 神经网络学习(2)前言介绍二
- Leetcode 50 day question brushing plan (day 2 - the longest substring without repeated characters 10.00-12.00)
- Sudden! Arm stops cooperating with Huawei! How big is the impact on Huawei?
- 详解 gRPC 客户端长连接机制实现
- Ren Zhengfei revealed for the first time: the story behind Huawei's nearly $10billion "sale" to Motorola!
- SSM整合-功能模块和接口测试
- MySQL 遇到过死锁问题吗,你是如何解决的?
猜你喜欢

Hello World

分布式链路追踪Jaeger在Golang中的使用

Offer set (1)

Vector CANape - How to Send Receive CAN Message in CANape

How about the employment prospects of Russian translation? How to do a good job of Russian translation

Still using xshell? Recommend this more modern terminal connection tool

PS_2_图层

模块八作业 - 消息数据 MySQL 表设计

链表-反转链表

flex布局
随机推荐
开发winform中遇到的一些问题汇总(持续跟新)
Ren Zhengfei revealed for the first time: the story behind Huawei's nearly $10billion "sale" to Motorola!
接口测试方案(接口测试思路)
Download and configuration of irrklang audio library
常用功能的测试用例
突发!Arm停止与华为合作!对华为影响究竟有多大?
ALV screen input option learning
ssm练习第四天_获取用户名_用户退出_用户crud_密码加密_角色_权限
更安全、更健康、无续航焦虑,魏牌拿铁DHT-PHEV来了
ECS MySQL prompt error
Visual VM positioning oom, fullgc usage
网易游戏研发工程师实习生(客户端方向)一面
Real passwords do not match during synchronization
Test cases of common functions
数据安全知识体系
14.梯度检测、随机初始化、神经网络总结
PyQt5快速开发与实战 3.5 菜单栏与工具栏
Netease game R & D Engineer Intern (client side)
14. Gradient detection, random initialization, neural network Summary
What should we do after the PMP Exam is postponed on July 30?
https://www.jianshu.com/p/dffd40d06e3c?u_atoken=347d4392-252a-4b45-a678-bcec250f9e04&u_asession=01eMtP4JqegFoAlsURoakpOGrbcerjLJ_zN8f1aUZcmXP4RFYHr8mgHddXP2E4de3jX0KNBwm7Lovlpxjd_P_q4JsKWYrT3W_NKPr8w6oU7K_rPtya4yy2JhGPHuukmz9-nHmbkqVcEgdObpAroqY1_GBkFo3NEHBv0PZUm6pbxQU&u_asig=05C37JabxAuQ31mDFqjIByfyLzE6L1QI2GClS_4S2oH_CRohkzGM9lmVwfVnmUENav6UAlwIGd0n5-OXsYaCKG3INIU3CDihJTJ51P3S6kkIKOa1ngTAW4FxeLWO19IXEBBGQDh9HMF8GBnsGLJ80uwzZlCYPl5VPi0r_5fKCzBJb9JS7q8ZD7Xtz2Ly-b0kmuyAKRFSVJkkdwVUnyHAIJzZmc6KEtJ8Ac7iz7KRn3KyaF2ULn3BedZgqLkWUaPI706xbSxAaWh9ph0bRUFW-6vO3h9VXwMyh6PgyDIVSG1W_zuO1CFQeRvItTQZoN37h469EgyhIIU9G134I4HWFmMM2zxaRDUMsexOWGXS4DANDdkV_2vuXIqfq23xGu1PxmmWspDxyAEEo4kbsryBKb9Q&u_aref=XH4TVtlUH3sT%2FSBszLSI6AAx8uI%3D