SQL語句常見視圖操作部分試題(一)
創建一個名稱為EMPLOYEES_VU的視圖,它基於EMPLOYEES表中的雇員號、雇員名和部門號。將雇員名的列標題改為EMPLOYEE。
CREATE VIEW EMPLOYEES_VU AS SELECT EMPLOYEE_ID EMPLOYEE,FIRST_NAME,LAST_NAME,DEPARTMENT_ID FROM EMPLOYEES;
從USER_VIEWS數據字典視圖中選擇視圖名字和文本
SELECT * FROM USER_VIEWS;
使用EMPLOYEES_VU視圖,輸入一個查詢來顯示所有的雇員名和部門號
SELECT FIRST_NAME,LAST_NAME,DEPARTMENT_ID FROM EMPLOYEES_VU;
創建一個名為DEPT50視圖,其中包含部門50中的所有雇員的雇員號、雇員名和部門號,視圖的列標簽為EMPNO、EMPLOYEE和DEPTNO,不允許通過視圖將一個雇員重新分配到另一個部門
CREATE OR REPLACE VIEW DEPT50(EMPNO,EMPLOYEE,DEPTNO) AS SELECT EMPLOYEE_ID,LAST_NAME,DEPARTMENT_ID FROM EMPLOYEES WHERE DEPARTMENT_ID=50 WITH CHECK OPTION;
視圖重新指定Matos到部門80
UPDATE DEPT50 SET DEPTNO=80 WHERE EMPLOYEE=‘Matos‘;
ORA-01402: 視圖 WITH CHECK OPTION where 子句違規
創建一個名為SALARY_VU的視圖,該視圖基於所有雇員的名字、部門號、薪水和薪水級別。用EMPLOYEES、DEPARTMENTS和JOB_GRADES表,分別命名列標簽為Employy、Department、Salary和Grade。
GRANT SELECT ON SALGRADE TO HR;
CREATE TABLE SAL_LEVEL AS SELECT * FROM SCOTT.SALGRADE;
CREATE OR REPLACE VIEW SALARY_VU(Employee,Department,Salary,Grade)
AS
SELECT EMPLOYEES.LAST_NAME,DEPARTMENTS.DEPARTMENT_NAME,EMPLOYEES.SALARY,SAL_LEVEL.GRADE
FROM EMPLOYEES,DEPARTMENTS,SAL_LEVEL
WHERE EMPLOYEES.DEPARTMENT_ID=DEPARTMENTS.DEPARTMENT_ID AND (EMPLOYEES.SALARY BETWEEN SAL_LEVEL.LOSAL AND SAL_LEVEL.HISAL);
創建一個表DEPT_2復制到DEPT內容,並在創建表之後給department_id設置主鍵。
CREATE TABLE DEPT_2 AS SELECT * FROM DEPT;
ALTER TABLE DEPT_2 ADD CONSTRAINT PK_1 PRIMARY KEY(DEPTNO);
SQL語句常見視圖操作部分試題(一)