SQL语句常见DDL/DML/DCL操作部分试题(一)

时间:2024-07-09 13:36:56

创建三个空表emp1,emp2,emp3,结构参考emp表

CREATE TABLE EMP1 AS SELECT * FROM EMP WHERE 1=2;

CREATE TABLE EMP2 AS SELECT * FROM EMP WHERE 1=2;

CREATE TABLE EMP3 AS SELECT * FROM EMP WHERE 1=2;

用一条INSERT语句将emp表中部门号为10的插入到emp1,20的插入到emp2,30的插入到emp3

INSERT ALL

WHEN DEPTNO=10 THEN

INTO EMP1

WHEN DEPTNO=20 THEN

INTO EMP2

WHEN DEPTNO=30 THEN

INTO EMP3

SELECT * FROM EMP;

创建一个表t35,x列为任意时间戳类型,插入当前时间,并查询

CREATE TABLE T35(X TIMESTAMP WITH LOCAL TIME ZONE);

INSERT INTO T35 VALUES(SYSDATE);

SELECT * FROM T35;

查询emp表所有员工信息,谁的薪金超过其部门的平均工资

SELECT ENAME,SAL,DEPTNO FROM EMP OUTER_TABLE

WHERE SAL>(SELECT AVG(SAL) FROM EMP INNER_TABLE WHERE INNER_TABLE.DEPTNO=OUTER_TABLE.DEPTNO);

查询emp中不是领导的几层员工信息

SELECT * FROM EMP OUTER_TABLE WHERE NOT EXISTS (SELECT 'X' FROM EMP INNER_TABLE WHERE INNER_TABLE.MGR = OUTER_TABLE.EMPNO);

查找dept表中没有任何员工的部门

SELECT DEPTNO,DNAME FROM DEPT WHERE NOT EXISTS(SELECT 'X' FROM EMP WHERE EMP.DEPTNO=DEPT.DEPTNO);

在hr用户授权scott可以查询employees,departments表的权限

GRANT SELECT ON DEPARTMENTS TO SCOTT;

GRANT SELECT ON EMPLOYEES TO SCOTT;

在scott用户创建同名employees,departments表并复制数据

CREATE TABLE EMPLOYEES AS SELECT * FROM HR.EMPLOYEES;

CREATE TABLE DEPARTMENTS AS SELECT * FROM HR.DEPARTMENTS;

在scott用户给employees表增加一列,部门名称

ALTER TABLE EMPLOYEES ADD(DNAME VARCHAR2(20));

在scott用户根据departments表的部门名称更新employees表的部门名称

UPDATE EMPLOYEES SET DNAME=(SELECT DEPARTMENT_NAME FROM DEPARTMENTS WHERE DEPARTMENTS.DEPARTMENT_ID=EMPLOYEES.DEPARTMENT_ID);

SELECT * FROM DEPARTMENTS;

SELECT * FROM EMPLOYEES;

在scott用户employees中删除在location_id是1700地区的员工信息

DELETE FROM EMPLOYEES WHERE DEPARTMENT_ID IN(SELECT DEPARTMENT_ID FROM DEPARTMENTS WHERE LOCATION_ID=1700);