Dream database experiment 2: DMDBMS table query operation

Experiment 2 DMDBMS table query operation

1. The purpose of the experiment:

1. Master the application of operators in query;
2. 2. Master the dismantling and analysis of query requirements for complex queries
; 3. Master SQL language to query database information and user information
; Understand and master the SQL language: Be able to use distinct, AS some_name (alias), join, group by, order by, having, limit, like related commands to perform complex queries on table data.
5. Complete complex query on sample data, including query on table data, deduplication, grouping, data restriction, join query, etc.

2. Experimental requirements:

1. Master the dismantling and analysis of query requirements for complex queries
2 . 3. Master the application of operators in query
. Familiar with the use method and application scenarios of each parameter in DQL language
4. Understand and master the SQL language: Be able to use distinct, AS some_name (alias), join, group by, order by, having, limit, like related commands to perform complex queries on table data.
5. Familiar with example query ideas.

3. Key points and difficulties of the experiment:

Experiment focus: use of aliases, conditional query, join query, subquery, data grouping, data sorting
Experiment difficulty: join query, subquery, group query, use of functions

4. Experiment content:

  1. Database information, user information query
  2. Table data query
    2.1. Deduplication query
    2.2. Selection of query columns
    2.3. Use of aliases
    2.4. Conditional query
    2.5. Join query
    2.6. Subquery
    2.7. Data grouping
    2.8. Data sorting
    2.9. Data restriction

5. Experimental steps and results:

Description: For each part of the experimental content, please describe the experimental process and match the screenshots of the experimental results

1. Database information, user information query

1.1

//查看数据库名及创建时间
select name,create_time from v$database;

1.2

//查看数据库里所有使用用户
select username,user_id,default_tablespace,profile from dba_users;

1.3

//查看当前登录用户
SELECT USER

1.4

	//当前用户拥有的表
select table_name from user_tables;

1.5

//所有用户的表
select table_name from all_tables;

1.6

//切换模式
set SCHEMA SYSDBA;

1.7

//查看当前模式
select * from  v$sessions;

2. Table data query

//Screenshot of SQL commands and input results

2.1. deduplicate query

Example: The following table shows the employee information (sections) of a department. Use the distinct keyword to query the employee's position and bonus, and remove the records with the same position and bonus.

select distinct JOB,BONUS from SYSDBA."sections";

2.2. Selection of query columns

View employee numbers, positions and bonuses in the sections table

select STAFF_ID,JOB,BONUS from SYSDBA."sections";

2.3. Use of Aliases

Example: The table name qualifies the column name to query the math and English scores of the student whose student ID is 10.
math score sheet

English Score SheetEnglish

select SCORE FROM SYSDBA."math" WHERE SID = 10;
select SCORE FROM SYSDBA."English" WHERE SID = 10

2.4. Conditional query

Example: Query the information of employees whose job title is developer and whose bonus is >8000 from the bonuses_depa table above.
Example: Query the information of employees with the last name wang and bonuses between 8500 and 9500 from the bonuses_depa table above.

select STAFF_ID,NAME FROM SYSDBA."bonuses_depa" WHERE JOB = 'developer' AND "bonuses_depa".BONUS>8000;
select STAFF_ID,NAME FROM SYSDBA."bonuses_depa" WHERE  NAME LIKE 'wang%' AND BONUS BETWEEN 8500 AND 9500;
 

2.5. Connection query

Data preparation:
– Create table training.

CREATE TABLE training(staff_id INT NOT NULL,course_name CHAR(50),exam_date DATETIME,score INT);
--向表training中插入记录。
INSERT INTO training(staff_id,course_name,exam_date,score) VALUES(10,'SQL majorization','2017-06-25 12:00:00',90);
INSERT INTO training(staff_id,course_name,exam_date,score) VALUES(11,'BIG DATA','2018-06-25 12:00:00',92);
INSERT INTO training(staff_id,course_name,exam_date,score) VALUES(12,'Performance Turning','2018-06-29 12:00:00',95);

– Create table education.

CREATE TABLE education(staff_id INT, higest_degree CHAR(8), graduate_school VARCHAR(64), education_note VARCHAR(70));

– Insert records into table education

INSERT INTO education(staff_id,higest_degree,graduate_school,education_note) VALUES(11,'master','Northwestern Polytechnical University','211&985');
INSERT INTO education(staff_id,higest_degree,graduate_school,education_note) VALUES(12,‘doctor','Peking University','211&985');
INSERT INTO education(staff_id,higest_degree,graduate_school,education_note) VALUES(13,'scholar','Peking University','211&985');
示例:查询员工ID、最高学历和考试分数。使用training和education两个相关的列(staff_id)做查询操作。
SELECT STAFF_ID,EDUCATION.HIGEST_DEGREE,TRAINING.SCORE
FROM SYSDBA.TRAINING JOIN SYSDBA.EDUCATION USING(STAFF_ID);

2.6. Subqueries

Data preparation: staffs
build tables

CREATE TABLE staffs
(
staff_id NUMBER(6) not null,
first_name VARCHAR2(20),
last_name VARCHAR2(25),
email VARCHAR2(25),
phone_number VARCHAR2(20),
hire_date DATE,
employment_id VARCHAR2(10),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
manager_id NUMBER(6),
section_id NUMBER(4),
graduated_name VARCHAR2(60)
);

Insert data:

insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date,employment_id, salary, commission_pct, manager_id, section_id)values (198, 'Donald', 'OConnell', 'DOCONNEL', '650.507.9833', to_date('21-06-1999', 'dd-mm-yyyy'), 'SH_CLERK', 2600.00, null, 124, 50);

insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date,employment_id, salary, commission_pct, manager_id, section_id)values (199, 'Douglas', 'Grant', 'DGRANT', '650.507.9844', to_date('13-01-2000', 'dd-mm-yyyy'), 'SH_CLERK', 2600.00, null, 124, 50);

insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date,employment_id, salary, commission_pct, manager_id, section_id)values (200, 'Jennifer', 'Whalen', 'JWHALEN', '515.123.4444', to_date('17-09-1987', 'dd-mm-yyyy'), 'AD_ASST', 4400.00, null, 101, 10);

insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date,employment_id, salary, commission_pct, manager_id, section_id)values (201, 'Michael', 'Hartstein', 'MHARTSTE', '515.123.5555', to_date('17-02-1996', 'dd-mm-yyyy'), 'MK_MAN', 13000.00, null, 100, 20);

insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date,employment_id, salary, commission_pct, manager_id, section_id)values (202, 'Pat', 'Fay', 'PFAY', '603.123.6666', to_date('17-08-1997', 'dd-mm-yyyy'),
'MK_REP', 6000.00, null, 201, 20);

insert into staffs (staff_id, first_name, last_name, email, phone_number, hire_date,employment_id, salary, commission_pct, manager_id, section_id)values (203, 'Susan', 'Mavris', 'SMAVRIS', '515.123.7777', to_date('07-06-1994', 'dd-mm-yyyy'), 'HR_REP', 6500.00, null, 101, 40);

Use a correlated subquery to find people in each department who have a higher salary than the department's average salary

select STAFFS.first_name,STAFFS.LAST_NAME  
from STAFFS,(select AVG(salary) aver,section_id from STAFFS group by section_id)av
where STAFFS.section_id=av.section_id and STAFFS.salary>av.aver;

2.7. Data Grouping

Data preparation: create a table (bonuses_depa) as shown and insert data

Example: The department is grouped by position and bonus, and the number of employees in each group is queried, and the results are sorted in ascending order.

SELECT job,COUNT(*) FROM SYSDBA."bonuses_depa" GROUP 
BY JOB ORDER BY COUNT(*);
SELECT BONUS,COUNT(*) FROM SYSDBA."bonuses_depa" GROUP 
BY BONUS ORDER BY COUNT(*);

2.8. Data sorting

Example: Query the bonus information of various job types in bonuses_depa in the following table. The query results are first sorted in ascending order of bonus, and then in descending order of name.

SELECT NAME,JOB,BONUS FROM SYSDBA."bonuses_depa"  ORDER BY BONUS;
SELECT NAME,JOB,BONUS FROM SYSDBA."bonuses_depa"  ORDER BY NAME DESC;
 

2.9. Data Restrictions

Example: Query employee information in bonuses_depa in the following table. By adding LIMIT 2 OFFSET 1 to limit the query to skip the first row, a total of 2 rows of data are queried.

SELECT *FROM SYSDBA."bonuses_depa" LIMIT 2 OFFSET 1;

Guess you like

Origin blog.csdn.net/david2000999/article/details/122504503