系统设计与数据库系统 作业一 Basic SQL command LAB

1. Purpose of Experiment purpose and Requirements

Please show all work for these problems.
Just writing down the answer will not get full credit.
Note: create a Table EMP_your_StudentID and DEPT_your_studentID with all required constraints both Integrity and Referential Integrity Constraint.
EMP Table:

EMPNOPRIMARY KEY NOT NULL IF PRIMARY KEY MODIFED THEN CHILD MUST BE UPDATEDNUMBER(4)
ENAMENOT NULLVARCHAR2(10)
JOBNOT NULLVARCHAR2(9)
MGRREFERENCES EMP(EMPNO)NUMBER(4)
HIREDATENOT NULL CANNOT BE GREATER THAN TODAYS DATEDATE
SALNOT NULL AND MORE THAN 5000NUMBER(7,2)
COMMNUMBER(7,2)
DEPTNOREFERENCES DEPT TABLE DEFAULT 10NUMBER(2)

DEPT TABLE:

DEPTNOPRIMARY KEY NOT NULL DON’T ALLOW PRIMARY KEY TO BE MODIFIED IF CHILD RECORD EXISTNUMBER(2)
DNAMECHAR(10)
LOCCHAR(10)

INSERT TUPLES FOR EMP TABLE:

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7369SMITHCLERK790217-DEC-9013750NULL20
7499ALLENSALESMAN769820-FEB-8919000640030
7521WARDSALESMAN76981993-02-2218500425030
7566JONESMANAGER78391989-04-0226850NULL20
7654MARTINSALESMAN76981997-09-2815675350030
7698BLAKEMANAGER78391990-05-0124000NULL30
7782CLARKMANAGER78391988-06-0927500NULL10
7788SCOTTANALYST75661987-04-1919500NULL20
7839KINGPRESIDENT1983-11-1782500NULL10
7844TURNERSALESMAN76981992-09-0818500625030
7876ADAMSCLERK77881996-05-2311900NULL20
7900JAMESCLERK76981995-12-0312500NULL30
7902FORDANALYST75661991-12-0321500NULL20
7934MILLERCLERK77821995-01-2313250NULL10
3258GREENSALESMAN442224-JUL-9518500275050
4422STEVENSMANAGER783914-JAN-942475050
6548BARNESCLERK442216-JAN-951195050
7500CAMPBELLANALYST756630-OCT-9224500040

INSERT TUPLES FOR DEPT TABLE:

DEPTNODNAMELOC
10ACCOUNTINGLONDON
30SALESLIVERPOOL
40OPERATIONSSTAFFORD
50MARKETINGLUTON
20RESEARCHPRESTON

2. Methods and steps

FIRST: CREATE THE DATABASE

We can create the database by the following code:

CREATE DATABASE `dong2019284073` /*!40100 DEFAULT CHARACTER SET latin1 */

在这里插入图片描述

SECOND: CREAT THE TABLES

We can create the tables and then initialize each column by the following code:
①dept2019284073

CREATE TABLE `dept2019284073` (
  `DEPTNO` int(2) NOT NULL,
  `DNAME` char(10) DEFAULT NULL,
  `LOC` char(10) DEFAULT NULL,
  PRIMARY KEY (`DEPTNO`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

②emp2019284073

CREATE TABLE `emp2019284073` (
  `EMPNO` decimal(4,0) NOT NULL,
  `ENAME` varchar(10) DEFAULT NULL,
  `JOB` varchar(9) DEFAULT NULL,
  `MGR` decimal(4,0) DEFAULT NULL,
  `HIREDATE` date NOT NULL,
  `SAL` decimal(7,2) NOT NULL,
  `COMM` decimal(7,2) DEFAULT NULL,
  `DEPTNO` decimal(2,0) DEFAULT '10',
  PRIMARY KEY (`EMPNO`),
  KEY `fk_EMP_DEPT` (`DEPTNO`),
  KEY `fk_EMP_EMP1` (`MGR`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

THIRD:INSERT VALUES INTO TABLES

We can use the following code to insert values into tables. In addition, we must keep the data we type in corresponding order of columns of the tables.

INSERT INTO dept2019284073   
VALUES   
(10 , "ACCOUNTING",  "LONDON" ),
(30  ,"SALES" , "LIVERPOOL" ), 
(40 , "OPERATIONS" , "STAFFORD" ), 
(50 , "MARKETING" , "LUTON" ), 
(20 , "RESEARCH"  ,"PRESTON")

The structure can be shown in the following two pictures.
在这里插入图片描述
在这里插入图片描述

FORTH: USE THE COMMAND TO QUERY AND EDIT THE TABLES

  1. List all information about the employees.
SELECT * FROM dong2019284073.emp2019284073;

在这里插入图片描述

  1. List all information about the departments
SELECT * FROM dong2019284073.dept2019284073;

在这里插入图片描述

  1. List only the following information from the EMP table ( Employee name, employee number, salary, department number)
SELECT ENAME,EMPNO,SAL,DEPTNO FROM emp2019284073;

在这里插入图片描述

  1. List details of employees in departments 10 and 30.
SELECT * FROM emp2019284073 WHERE DEPTNO = 10 OR DEPTNO = 30;

在这里插入图片描述

  1. List all the jobs in the EMP table eliminating duplicates.
SELECT DISTINCT JOB FROM emp2019284073;

在这里插入图片描述

  1. What are the names of the employees who earn less than £20,000?
SELECT ENAME FROM emp2019284073 WHERE SAL < 20000

在这里插入图片描述

  1. What is the name, job title and employee number of the person in department 20 who earns more than £25000?
SELECT ENAME,JOB,EMPNO FROM emp2019284073 WHERE DEPTNO=20 AND SAL > 25000;

在这里插入图片描述

  1. Find all employees whose job is either Clerk or Salesman.
SELECT * FROM emp2019284073 WHERE JOB = "CLERK" OR JOB = "SALESMAN"

在这里插入图片描述

  1. Find any Clerk who is not in department 10
SELECT * FROM emp2019284073 WHERE DEPTNO!=10

在这里插入图片描述

  1. Find everyone whose job is Salesman and all the Analysts in department 20
SELECT * FROM emp2019284073 WHERE JOB = 'SALESMAN' OR (JOB = 'ANALYST' AND DEPTNO = 20);

在这里插入图片描述

  1. Find all the employees who earn between £15,000 and £20,000. Show the employee name, department and salary.
SELECT ENAME,DEPTNO,SAL FROM emp2019284073 WHERE SAL BETWEEN 15000 AND 20000;

在这里插入图片描述

  1. Find the name of the President.
SELECT ENAME FROM emp2019284073 WHERE JOB = 'PRESIDENT';

在这里插入图片描述

  1. Find all the employees whose last names end with S
SELECT * FROM emp2019284073 WHERE ENAME LIKE '%S';

在这里插入图片描述

  1. List the employees whose names have TH or LL in them
SELECT * FROM emp2019284073 WHERE ENAME LIKE '%TH%' OR ENAME LIKE '%LL%';

在这里插入图片描述

  1. List only those employees who receive commission.
SELECT * FROM emp2019284073 WHERE COMM IS NOT NULL;

在这里插入图片描述

  1. Find the name, job, salary, hiredate, and department number of all employees by alphabetical order of name.
SELECT ENAME,JOB,SAL,HIREDATE,DEPTNO FROM emp2019284073 ORDER BY ENAME;

在这里插入图片描述

  1. Find the name, job, salary, hiredate and department number of all employees in ascending order by their salaries.
SELECT ENAME,JOB,SAL,HIREDATE,DEPTNO FROM emp2019284073 ORDER BY SAL DESC;

在这里插入图片描述

  1. List all salesmen in descending order by commission divided by their salary
SELECT * FROM emp2019284073 ORDER BY ifnull(COMM, 0)/SAL DESC;
  1. Order employees in department 30 who receive commision, in ascending order by commission
SELECT * FROM emp2019284073 WHERE DEPTNO=30 AND COMM IS NOT NULL ORDER BY COMM DESC;

在这里插入图片描述

  1. Find the names, jobs, salaries and commissions of all employees who do not have managers.
SELECT ENAME,JOB,SAL,COMM FROM emp2019284073 WHERE MGR IS NULL;

在这里插入图片描述

  1. Find all the salesmen in department 30 who have a salary greater than or equal to £18000.
SELECT * FROM emp2019284073 WHERE DEPTNO=30 AND JOB = 'SALESMAN' AND SAL >= 18000;

在这里插入图片描述

  1. Find the employees who were hired before 01-Jan-1998 and have salary above 5000 or below 1000.
SELECT * FROM emp2019284073 WHERE HIREDATE<'1998-01-01' AND (SAL > 5000 OR SAL<1000);

在这里插入图片描述

  1. What is the command to add primary key constraint to EMPNO
ALTER TABLE emp2019284073 ADD PRIMARY KEY(EMPNO);
  1. What is the command to add a new column EMP_family_name to existing EMP table
ALTER TABLE emp2019284073 ADD EMP_family_name VARCHAR(20);
  1. How to drop primary key constraint for EMPNO
ALTER TABLE emp2019284073 DROP PRIMARY KEY;
  1. rename EMP table to EMPLOYEE
RENAME TABLE emp2019284073 TO EMPLOYEE_2019284073;
  1. rename EMPLOYEE back to EMP
RENAME TABLE EMPLOYEE_2019284073 TO emp2019284073;
  1. What is the SQL command to remove column EMP_family_name from EMP table
ALTER TABLE emp2019284073 DROP COLUMN EMP_family_name;
  1. What is the SQL command to copy emp table to employee table
CREATE TABLE EMPLOYEE_2019284073 AS SELECT * FROM emp2019284073;
  1. What is the SQL command to drop employee table
DROP TABLE EMPLOYEE_2019284073;
  1. What is the SQL command to display name’s of employee entered interactively from user
SELECT * FROM emp2019284073 WHERE ENAME = '&ENTER';
  1. What is the SQL command to find the employee whose commission is NULL
SELECT * FROM emp2019284073 WHERE COMM IS NULL;

在这里插入图片描述

3. Experiment process and content

  1. From this experiment of MySQL, I learned how to build a database, how to initialize the database, how to insert data into tables and how to select and edit values by language of MySQL.
  2. MySQL has more convenient way to deal with huge data, which makes it fast to run. In addition, it offers enough operations to deal with this data.
  3. During this experiment, errors occur sometimes. With the help of my classmates and knowledge in Internet, I finally finished this experiment successfully.
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上山打老虎D

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值