view
one of the database objects
The role of the view in the SQL statement is the same as that of the table
But the view is not a table, it just corresponds to a SELECT statement query result set.
CREATE VIEW v_emp_b_10
AS
SELECT empno,ename,sal,deptno
FROM emp_brown
WHERE deptno = 10;
DESC v_emp_b_10;
View view data
SELECT * FROM v_emp_b_10;
The subquery corresponding to the view can add aliases to the fields,
Then the field corresponding to the view is named with an alias.
When a field of the corresponding subquery is a function or expression
Then the field must use an alias.
CREATE OR REPLACE VIEW v_emp_brown_10
AS
SELECT empno id,ename name,
sal salary,deptno
FROM emp_brown
WHERE deptno = 10;
desc v_emp_b_10;
drop VIEW v_emp_brown_10;
SELECT * FROM v_emp_brown_10;
SELECT * FROM emp_brown;
Views are divided into different types according to the corresponding query statements
Simple view: query a single table, and query fields
Does not include functions, expressions, deduplication, grouping, etc. to process data
Complex Views: The opposite of Simple Views are Complex Views
Connection view: The data queried comes from multiple tables, and the connection view is counted as a kind of complex view
DML operations can be performed on views, but only on simple views.
The DML operation on the view is performed on the underlying table of the view data source.
So it can't violate the constraints of the underlying table
INSERT INTO v_emp_brown_10
(id,name,salary,deptno)
VALUES
(1001,'JACK',5000,10);
After the DML operation on the view, the bank's data
If the view cannot see it, it will pollute the base table data!
UPDATE v_emp_brown_10
SET deptno=20;
After adding the check option to the view, you can avoid polluting the base table after DML operation on the view
CREATE OR REPLACE VIEW v_emp_brown_10
AS
SELECT empno id,ename name,
sal salary,deptno
FROM emp_brown
WHERE deptno = 10
WITH CHECK OPTION;
After adding the read-only option to the view, the view can only be queried and cannot perform any DML operations.
CREATE OR REPLACE VIEW v_emp_brown_10
AS
SELECT empno id,ename name,
sal salary,deptno
FROM emp_brown
WHERE deptno = 10
WITH READ ONLY;
Query all view names in the data dictionary USER_OBJECTS:
SELECT object_name FROM user_objects
WHERE object_type = 'VIEW';
Query the specified view in the data dictionary USER_VIEWS:
SELECT text FROM user_views
WHERE view_name = 'V_EMP_BROWN_10';
SELECT table_name
FROM user_tables;
complex view
Create a record for each department's department number, department name, average salary, total salary,
View of maximum salary, minimum salary v_emp_dept_sal_info
CREATE VIEW v_emp_dept_sal_info_brown
AS
SELECT d.deptno, d.dname, avg (e.sal) avg_sal, sum (e.sal) sum_sal,
max(e.sal) max_sal,min(e.sal) min_sal
FROM emp_brown e ,dept_brown d
WHERE e.deptno = d.deptno
GROUP BY d.deptno,d.dname;
SELECT * FROM v_emp_dept_sal_info_brown;
View higher than average salary in your department?
SELECT e.ename, e.sal, e.deptno
FROM emp_brown e,(SELECT AVG(sal) avg_sal,deptno
FROM emp
GROUP BY deptno) t
WHERE e.deptno=t.deptno
AND e.sal>avg_sal
Simple writing:
SELECT e.ename, e.sal, e.deptno
FROM emp_brown e JOIN v_emp_dept_sal_info_brown v
ON e.deptno=v.deptno
WHERE e.sal>v.avg_sal
Complex views do not allow DML operations and will report an error.
SELECT * FROM v_emp_dept_sal_info_brown
Delete view v_emp_10:
DROP VIEW v_emp_b_10;
Deleting a view does not affect the base table data, but deletes the data in the view (DML operation)
Affects base table data.
sequence
one of the database objects
A sequence is to generate a series of numbers according to a set rule
Usually the number generated by the sequence is used as the primary key of the table
Fields provide values to use. ,
CREATE SEQUENCE seq_emp_brown_id
START WITH 1
INCREMENT BY 1
Sequence provides two pseudocolumns:
NEXTVAL: Get the next number in the sequence
If the sequence is just created, start with START WITH,
Then each time it is acquired, it is calculated according to the step size.
NEXTVAL will cause the sequence to be stepped, and the sequence cannot be rolled back!
CURRVAL: Get the current value of the sequence (the number generated by the last use of NEXTVAL)
Newly created sequences cannot use CURRVAL until NEXTVAL has been called at least once
SELECT seq_emp_brown_id.NEXTVAL
FROM dual;
Provide value for EMP table primary key field
INSERT INTO emp_brown
(empno,ename,sal,job,deptno)
VALUES
(seq_emp_b.NEXTVAL,'JACK',3000,'CLARK',20)
INSERT INTO emp_brown
(empno,ename,sal,job,deptno)
VALUES
(seq_emp_brown_id.NEXTVAL,'ROSE',8000,'MANAGER',20)
select * from emp_brown
delete sequence
DROP SEQUENCE sql_emp_id;
index
one of the database objects
Indexes are used to improve query efficiency
The built-in work of the index is transparent to the user and is maintained by the database itself.
We just need to specify whether to add an index.
Indexes are added for fields in a table. When a field frequently appears in WHERE as a filter condition,
or ORDER BY or DISTINCT, you can add indexes to improve query efficiency
CREATE INDEX idx_emp_brown_ename
ON emp_brown(ename)
SELECT ename,job,deptno
FROM emp_brown
WHERE ename='SCOTT'
multi-column index
CREATE INDEX idx_emp_brown_job_sal ON emp(job, sal);
SELECT empno, ename, sal, job FROM emp_brown
ORDER BY job, sal;
If you need to perform a case-insensitive search on the ename column of the emp table, you can create an index based on the UPPER function on this column:
CREATE INDEX emp_ename_upper_idx
ON emp(UPPER(ename));
Modify and delete indexes
If you frequently perform DML operations on indexed columns, you need to rebuild the index periodically to improve the space utilization of the index. The syntax is as follows:
ALTER INDEX index_name REBUILD;
Rebuild index idx_emp_ename:
ALTER INDEX idx_emp_ename REBUILD;
When there is an unreasonable index on a table, the operation performance will be degraded. The syntax of deleting the index:
DROP INDEX index_name;
Drop index idx_emp_ename:
DROP INDEX idx_emp_ename;
Reasonable use of indexes to improve query efficiency
To improve query efficiency, the principles for creating and using indexes:
Create indexes for columns that appear frequently in the WHERE clause
Build indexes for fields that often appear after ORDER BY and DISTINCT. If a composite index is established, the order of the fields in the index should be consistent with the order of the fields following these keywords.
Create indexes on columns that are often used as table join conditions
Don't build indexes on tables that do DML operations frequently
Don't build indexes on small tables
Limit the number of indexes on the table, the more indexes the better
Remove rarely used, unreasonable indexes
constraint
NOT NULL
NOT NULL constraint is a column-level constraint, that is: for a field
Adding this constraint must be done while defining the field.
When adding a non-null constraint to a field after the table is created, it should also be done when the field is modified.
CREATE TABLE employees_brown(
eid NUMBER(6),
name VARCHAR2(30) NOT NULL,
salary NUMBER(7,2),
hiredate DATE CONSTRAINT employees_brown_hiredate_nn NOT NULL);
desc employees_brown;
Add not-null constraint when modifying table
ALTER TABLE employees_brown
MODIFY(eid NUMBER(6) NOT NULL);
Cancel the not-null constraint
If the business requires to cancel the non-null constraint of a column, you can rebuild the table or modify the table:
ALTER TABLE employees_brown
MODIFY (eid NUMBER(6) null);
uniqueness constraint
A unique constraint can require a field to have any
Values in a set cannot be the same, except for NULL values.
CREATE TABLE employees1_brown (
eid NUMBER(6) UNIQUE,
name VARCHAR2(30),
email VARCHAR2(50),
salary NUMBER(7, 2),
hired DATE,
CONSTRAINT employees_brown_email_uk UNIQUE(email)
);
desc employees1_brown;
INSERT INTO employees1_brown
(eid,name,email)
VALUES
(1,'JACK','[email protected]')
SELECT * FROM employees1_brown
delete FROM employees1_brown
ALTER TABLE employees1_brown
ADD CONSTRAINT employees1_brown_name_uk UNIQUE(name);
primary key constraint
Primary key constraints can only be established on a single column, and a table
There can only be one primary key constraint, and the primary key constraint can guarantee
The field is not empty and unique
CREATE TABLE employees2_brown (
eid NUMBER(6) PRIMARY KEY,
name VARCHAR2(30),
email VARCHAR2(50),
salary NUMBER(7, 2),
hired DATE
)
INSERT INTO employees2_brown
(eid,name)
VALUES
(01,'JACK');
SELECT * FROM employees2_brown;
ALTER TABLE employees3_brown
ADD CONSTRAINT employees1_brown_eid_pk PRIMARY KEY(eid);
ALTER TABLE employees2_brown
ADD CONSTRAINT employees2_brown_salary_check
CHECK(salary > 2000);
INSERT INTO employees2_brown(eid,name,salary)
VALUES(1236,'donna noble',2500);
UPDATE employees2_brown SET salary = 1500
WHERE eid = 1236;