当前位置:网站首页>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
边栏推荐
- MySQL 遇到过死锁问题吗,你是如何解决的?
- Neural network learning (2) introduction 2
- Redis持久化RDB/AOF
- Online stock trading, where to choose to open an account is safer?
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3 record
- SSM整合-功能模块和接口测试
- Ren Zhengfei revealed for the first time: the story behind Huawei's nearly $10billion "sale" to Motorola!
- Are you suitable for automated testing?
- ssm练习第三天_分页助手_安全框架
- Data security knowledge system
猜你喜欢

Day 4 of SSM practice_ Get user name_ User exit_ User CRUD_ Password encryption_ Roles_ jurisdiction

【一知半解】线程池

Distributed link tracking Jaeger's use in golang

链表-反转链表

Oracle day 2 (Views, indexes, PLSQL, cursors, stored procedures and stored functions, triggers, JDBC access stored procedures and stored functions)

Hello World

ALV屏幕输入选项学习

Linked list - the penultimate K nodes

Learn UML system modeling from me

Duplicate gallerycms character length limit short domain name bypass
随机推荐
Linear regression -- Taking an arithmetic sequence as an example
财政部:今明两年对集成电路设计企业和软件企业免征企业所得税!
SSM整合-异常处理器和项目异常处理方案
Arm China responded to the "disconnection of Huawei supply" incident! Ren Zhengfei said "no impact"!
The second set of 2020 American Asian individual match
Shader code of parallax map in OpenGL
此章节用于补充3
The step jumping expansion problem of sword finger offer
The second day of SSM practice_ Project split moudle_ Basic addition, deletion, modification and query_ Batch delete_ One to one cascading query
Arm中国回应“断供华为”事件!任正非表示“没有影响”!
Leetcode 50 day question brushing plan (day 2 - the longest substring without repeated characters 10.00-12.00)
Leetcode 0137. number II that appears only once
数据库索引的原理,为什么要用 B+树,为什么不用二叉树?
被控专利授权费过高!美法院判决高通违反反垄断法:高通股价大跌10.86%!
How about the employment prospects of Russian translation? How to do a good job of Russian translation
同步时现实密码不匹配
Pyqt5 rapid development and practice 3.5 menu bar and toolbar
How to design test cases well
Flask encapsulates seven cattle cloud
NFT数字藏品系统开发:同道大叔首推祈福系列数字藏品开售即罄
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