Unit05: Views, sequences, indexes, constraints
View Database Object One view is the same as the table in the SQL statement, but it is not a real table, it corresponds to the result set of a query statement. Create a view that looks at employee information for department number 10th:CREATE VIEWv_emp_dept10 asSELECTEname,sal,job,deptno fromEMPWHEREDeptno=TenDESCv_emp_dept10SELECT * fromv_emp_dept10 Modify the view to replace the subquery that corresponds to the view. Using the Create OR Replace. If the view does not exist, it is created, and the presence is replaced. If there is an alias for the field of the subquery that corresponds to the view, the name of the field that corresponds to the view is the alias. If the subquery field contains a function or expression, the field must be aliased. CREATE OR REPLACE VIEWv_emp_dept10 asSELECTempno id,ename name, Sal Salary,job,deptno fromEMPWHEREDeptno=Tena DML operation on a view is the operation of the underlying table of the view data source. INSERT intov_emp_dept10 (Id,name,salary,job,deptno)VALUES(1001,'JACK', the,'Clerk',Ten)SELECT * fromv_emp_dept10SELECT * fromwhen EMP inserts data, the fields that the view does not see are inserted into the default values for the corresponding fields of the underlying table, so you cannot violate the constraints of the corresponding fields in the underlying table, especially the non-null constraints of the fields that are not visible, which may cause the insert to fail. UPDATEv_emp_dept10SETName='JACKSON'WHEREId=1001SELECT * fromv_emp_dept10SELECT * fromEMPDELETE fromv_emp_dept10WHEREId=1001DML operations on a view may result in "contamination" of the base table data, i.e., a data view that DML operations on the views is not visible to it, but the base table is modified for that data. INSERT intov_emp_dept10 (Id,name,salary,job,deptno)VALUES(1001,'JACK', the,'Clerk', -)SELECT * fromv_emp_dept10SELECT * fromEMPUPDATEv_emp_dept10SETDeptno= -Adding a check option to a view avoids contaminating the base table when DML operations are performed on the views. CREATE OR REPLACE VIEWv_emp_dept10 asSELECTempno id,ename name, Sal Salary,job,deptno fromEMPWHEREDeptno=Ten with CHECK OPTIONWhen you add a read-only option to a view, you cannot perform DML operations on the image. CREATE OR REPLACE VIEWv_emp_dept10 asSELECTempno id,ename name, Sal Salary,job,deptno fromEMPWHEREDeptno=Ten with READ onlySELECTtable_name from The User_tables view is divided into simple views and complex views, depending on the corresponding sub-query. This view is a complex view when a subquery contains functions, expressions, grouping, and correlation queries. A complex view cannot perform DML operations. Create a view that includes payroll for each departmentCREATE VIEWV_emp_sal asSELECT AVG(e.sal) avg_sal,SUM(e.sal) sum_sal,MIN(e.sal) min_sal,MAX(e.sal) max_sal, D.deptno,d.dname fromEMP e,dept DWHEREE.deptno=D.deptnoGROUP byD.deptno,d.dnameSELECT * fromV_emp_sal See which employee's salary is higher than their department's average wage?SELECTE.ename,e.sal,e.deptno fromEMP E,v_emp_sal vWHEREE.deptno=V.deptno andE.sal>v.avg_sal Deleting a viewDROP VIEWv_emp_dept10 Deleting data in a view deletes the corresponding data in the base table, but deleting the view itself does not affect the base table data. sequence sequences are also one of the database objects that generate a series of numbers. It is commonly used to provide values for the primary key fields of a table. CREATESEQUENCE Seq_emp_idstart with 1INCREMENT by 1 sequence supports two pseudo-columns: nextval: Gets the sequence of the next digit currval: Gets the sequence of the current number Nextval causes the sequence to step in, and the sequence cannot be rolled back. Currval is the number that gets the last generation of the sequence, and the newly created sequence can use Currval at least after it is called with this nextval. SELECTseq_emp_id. Currval fromDualSELECT * fromEMPINSERT intoEMP (EMPNO,ENAME,SAL,JOB,DEPTNO)VALUES(seq_emp_id. Nextval,'JACK', the, 'Clerk',Ten) How the UUID is generated:SELECTSys_guid () fromdual Deleting a sequenceDROPSEQUENCE seq_emp_idCREATE TABLEEmployees1 (Eid Number(6)UNIQUE, nameVARCHAR2( -), emailVARCHAR2( -), Salary Number(7,2), HireDate DATE,CONSTRAINTEmployees1_email_ukUNIQUE(email));INSERT intoemployees1 (Eid,name,email)VALUES(NULL,'JACK',NULL)SELECT * fromemployees1CREATE TABLEEmployees2 (Eid Number(6)PRIMARY KEY, nameVARCHAR2( -), emailVARCHAR2( -), Salary Number(7,2), HireDate DATE);INSERT intoemployees2 (eid,name)VALUES(1,'JACK')
java-unit05: Views, sequences, indexes, constraints