Home Database Mysql Tutorial 数据库概论(实验四1) 数据查询

数据库概论(实验四1) 数据查询

Jun 07, 2016 pm 03:29 PM
experiment master database data query Inquire Target

[实验目标] 1、掌握单表查询的应用 2、熟练掌握连接查询的应用 3、掌握嵌套查询和集合查询 [实验内容] 一、根据实验二,建立 SC 数据库(实验条件),含有 student 、 course 和 sc 三个表 二、数据查询实验 1 、单表查询 1 )选择指定的列 【例】 查询全体

[实验目标]

 

1、掌握单表查询的应用

2、熟练掌握连接查询的应用

3、掌握嵌套查询和集合查询

 

[实验内容]

 

一、根据实验二,建立 SC 数据库(实验条件),含有 student course sc 三个表

 

二、数据查询实验

 

1 、单表查询

1 )选择指定的列

【例】 查询全体学生的学号和姓名

 

2 )查询全部列

【例】 查询全体学生的详细信息

 

3 )对查询后的指定列进行命名

【例】 查询全部学生的 姓名 及其 出生年 两列

 

4 )消除取值重复的行

【例】 查询选修了课程的学生学号

 

5 )选择表中若干元组(满足条件的)

    I)大小比较

   【例】 查询计算机系( IS )全体学生名单

   【例】 查询全体 20 岁以下的学生姓名和年龄

 

    II)确定范围

    【例】 查询所有在 20 23 岁(含 20 23 )的学生姓名、系别和年龄

 

    III in not in 确定集合

    【例】 查询 IS 系和 CS 系的全体学生姓名和性别

    【例】 查询既不属于 IS 系,也不属于 MA 系的学生姓名和年龄

 

    IV)字符匹配( like % _

    【例】 查询所有姓李的学生姓名和性别

    【例】 查询所有 “2002” 年入学的学生学号、姓名和系别

    【例】 查询所有不姓 的学生信息

    【例】 查询名称含有 数据 的课程号、课程名及学分

 

    V)涉及空值的查询( is null

    【例】 查询没有先修课的课程号和课程名。

    【例】 查询所有有成绩的学生学号、课程号及成绩

 

6 )查询结果排序( order by

【例】 查询选修了 3 号课程的学生学号和成绩,结果按成绩降序排列。

  注:

       排序是对查询后的结果排序。所以应该放在最后。

 

 

 

7 )聚集函数

count sum avg max min

【例】 查询学生总数

【例】 查询所有课程的总学分

【例】 查询全体学生平均年龄

【例】 查询 1 号课程的最高分

 

8 )分组统计( group by

【例】 查询男女学生各有多少人。

【例】 查询每个课程的课程号和平均分。

【例】查询选修了3门课程以上(含3门)的学生学号和选修课程数。

【例】查询选修了2门课程以上(含2门,但不含1号课程),学生学号和选修课程数。

 

【例】查询不及格门数2门以上的学生学号。

【例】查询有2名以上(含2名)学生选修了的课程号和选修人数。

 注:1.分组查询的目标列可以出现分组的列及含在聚集函数中的其他列;

        2.where条件用于分组前的选择条件;having子句,用于分组后选择的条件。

 

2 、连接查询

1 )等值与非等值连接查询

【例】 查询每个学生及其的选修课程情况

 

2 )自身连接

【例】 查询每个课程的间接选修课

 

3 )外连接

【例】 查询所有学生选修课程情况(含没选修课程的学生)

【例】查询所有学生选修课程情况(含没选修课程的学生,属性有学号,姓名,课程名和成绩)  

 

4 )符合条件连接

【例】 查询选修了 2 号课程且成绩在 90 分以上的所有学生学号和姓名  

【例】 查询每个学生的学号、姓名,选修课程名和成绩。

 

3 、嵌套查询

1)带有IN谓词的子查询( 属性 in (子查询的查询结果) )

【例】查询与王敏同学在同一个系的学生信息。

【例】查询与王敏同学不在同一个系的学生信息。

【例】查询选修了课程名为“信息系统”的学生学号和姓名。

【例】查询曾与刘晨一同上课的学生学号和姓名。(假设:一个课程只有一个上课班)


2)带有比较运算符的子查询(=,>=,或!=)

【例】查询与王敏同学在同一个系的所有学生信息  (=判断)

【例】查询每个学生超过该课程最低分的课程号。(同类课程不是最低分的)

【例】查询每个学生超过他选修课程平均成绩的课程号。

【例】查询每个学生超过该课程平均成绩的课程号。

 

3)带有ANY或ALL谓词的子查询

【例】查询其他系中比计算机系某一学生年龄小的学生姓名,性别、年龄和所在系。

【例】查询其他系中比计算机系所有年龄都小的学生姓名和年龄。


4 )带有Exists谓词的子查询

【例】查询所有选修了1号课程的学生姓名。

【例】查询选修了全部课程的学生姓名。

【例】查询至少选修了学生200215122选修的全部课程的学生学号。

 

4、集合查询

 

1)并UNION

【例】 查询计算机系的学生及年龄不大于19岁的学生详细信息。

【例】查询选修了1号课程的及年龄不大于19岁的学生详细信息。


2)交INTERSECT

【例】查询选修了1号课程的与年龄不大于19岁的 学生 详细信息 的交集。


 

3)差EXCEPT

【例】查询计算机科学系的学生与年龄不大于19岁的学生详细信息的差集。


[课后作业]

1、通过SPJ数据库完成课后针对SPJ的查询练习。


2、根据SPJ数据库用SQL语句完成以下查询。

1)查询“天津”的供应商详细信息。

2)查询不是“天津”的供应商代码。

3)查询供应 工程 J1零件的供应商代码。

4)查询供应商S1供应过的商品代码。

5)查询供应商S1供应工程J1的零件种类有几种。

6)查询供应商S1供应工程J1的各种零件的零件代码和其数量和。

7)查询J1工程使用的零件种类有几种。

8)查询J1工程使用的各种零件代码和其数量和。

9)查询J1工程使用的零件总数大于300的零件代码和数量。

10)查询J1工程使用的各种零件,其数量至少大于J3使用的S2供应的所有零件数量和的,零件代码和数量。

11)查询J1工程使用的各种零件,其数量至少大于J3使用的同类数量和的,零件代码和数量。

12)查询至少使用了J1工程所用的几类零件的工程的工程号。

13)查询至少使用了所有红色零件的工程号。

14)查询使用了全部红色零件的工程号。

15)查询工程所在地与供应其零件的供应商在同一个城市的工程号和供应商号。

16)查询没有使用天津供应商生产的红色零件的工程号JNO。

17)查询至少使用了供应商S1供应的全部零件的工程号JNO。

18)查询使用了全部上海产的零件的工程号JNO。

19)查询没有使用J2所使用的所有零件的工程号JNO。

 

3、根据SC数据库用SQL语句完成以下任务。

1)将不及格的每个学生成绩增加5分。

2)将低于该课程平均分的学生成绩,成绩提高10%。

3)把计算机科学系所有学生增加1岁。

4)把学分为3分以内(含3分)的课程成绩提高3分,学分为3分以上的课程成绩提高4分。

5)建立名字为“成绩单”的视图,属性有 学生号,学生姓名、课程号、课程名和成绩

6)删除1号课程。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

See all articles