1. 程式人生 > >SQL語句常見DDL/DML/DCL操作部分試題(一)

SQL語句常見DDL/DML/DCL操作部分試題(一)

增加 數據 scott from 當前 領導 name 員工 emp

創建三個空表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);

SQL語句常見DDL/DML/DCL操作部分試題(一)