- 1.1 What are the constraints?
- 1.2, table level and column-level constraint?
- 1.3, NOT NULL constraints
- 1.4, UNIQUE constraints
- 1.5, PRIMARY KEY constraint
- 1.6, FOREIGN KEY constraint
- 1.7, adding constraints
- 1.8, delete constraints
- 1.9, constraint invalidation
- 1.10, the activation constraint
- 1.11, query constraints
- 2.1, the concept of view
- 2.2 Why view
- 2.3, simple and complex view of view
- 2.4, create, query, modify, delete, view
- 2.5, using a predetermined view of DML
- 2.6 shield DML operation
- 2.7, Top-N analysis
- 4.1, create an index
- 4.2, when you need to create an index
- 4.3, when you do not create an index
- 4.4, query index
- 4.5, delete the index
- 4.6, synonyms -synonym
- 4.7, create and delete synonyms
constraint
- Table-level constraints are mandatory for the
- There are five constraints:
the NOT NULL
UNIQUE
PRIMARY KEY
a FOREIGN KEY
CHECK - If you do not specify a constraint name, Oracle server automatically assigns the constraint name in accordance with the format SYS_Cn
-
Scope:
①, column-level constraints can only affect a column
②, table-level constraints may act on a plurality of columns (table-level constraints may of course also act in a column) -
Defined method: column constraint must, with table constraint is not defined in the column behind the column, but is defined independently.
-
Non-empty (not null) constraint can only be defined in column
CREATE TABLE employees(
employee_id NUMBER(6),
first_name VARCHAR2(20) NOT NULL,//系统命名
job_id CONSTRAINT ji_nn NOT NULL VARCHAR2(10),//用户命名
CONSTRAINT emp_emp_id_pk PRIMARY KEY (EMPLOYEE_ID)
);
CREATE TABLE employees(
employee_id NUMBER(6),
last_name VARCHAR2(25) UNIQUE,--系统命名
email VARCHAR2(25),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE NOT NULL,
CONSTRAINT emp_email_uk UNIQUE(email) --用户命名
);
CREATE TABLE departments(
department_id NUMBER(4),
department_name VARCHAR2(30) CONSTRAINT dept_name_nn NOT NULL,
manager_id NUMBER(6),
location_id NUMBER(4),
CONSTRAINT dept_id_pk PRIMARY KEY(department_id));
CREATE TABLE employees(
employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25) UNIQUE,
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE NOT NULL,
department_id NUMBER(4),
CONSTRAINT emp_dept_fk FOREIGN KEY (department_id)
REFERENCES departments(department_id) on delete cascade
);
- FOREIGN KEY: table columns are designated at the table level
- REFERENCES: indicated in the parent table columns
- ON DELETE CASCADE (cascade delete): When the parent table column is deleted, the child table corresponding to the deleted columns are also
- ON DELETE SET NULL (blank cascade): sub-table corresponding column blanking
ALTER TABLE employees
ADD CONSTRAINT emp_manager_fk
FOREIGN KEY(manager_id) REFERENCES employees(employee_id);
ALTER TABLE employees DROP CONSTRAINT emp_manager_fk;
- Use the DISABLE clause to invalidate constraints in ALTER TABLE statement.
ALTER TABLE employees DISABLE CONSTRAINT emp_manager_fk;
- ENABLE clause can be activated currently invalid constraints
ALTER TABLE employees ENABLE CONSTRAINT emp_manager_fk;
- When you define or activate UNIQUE or PRIMARY KEY constraint will be created automatically UNIQUE or PRIMARY KEY index
- Query the data dictionary view USER_CONSTRAINTS
SELECT constraint_name, constraint_type, search_condition
FROM user_constraints
WHERE table_name = 'EMPLOYEES';
view
- View is a virtual table.
- View of the existing table based on the table view is called a base table upon which to build.
- Provide data to view the content of the statement is a SELECT statement, the view can be understood as a SELECT statement stored up.
- Another manifestation of providing a base view table data to the user
- Control data access
- Simplify the query
- To avoid repeated access to the same data
- Create a view
create or replace view empview
as
select employee_id emp_id,last_name name,department_name
from employees e,departments d
Where e.department_id = d.department_id
- Query View
SELECT * FROM empview;
- Modifying the view (using CREATE OR REPLACE VIEW clause modified view)
CREATE OR REPLACE VIEW empview
(emp_id, name, department_id)
AS SELECT employee_id, first_name || ' ' || last_name,
department_id
FROM employees
WHERE department_id = 80;
- Delete View (delete view just delete the definition of the view, and does not delete the data base table)
drop view empview
- DML operations may be performed in a simple view,
- When the view definition comprises one element can not use the delete:
Group functions
GROUP BY clause
DISTINCT keyword
ROWNUM pseudo-column - E.g:
create or replace view sal_view
as select
avg(salary) avg_sal from employees
group by department_id
-
When the view definition comprises one element can not use the update:
Group functions
GROUP BY clause
DISTINCT keyword
ROWNUM pseudo-column
column is defined as the expression -
When the view definition comprises one of the following elements can not insert:
Group functions
GROUP BY clause
DISTINCT keyword
ROWNUM pseudo-column
is defined as an expression column
in Table non-null columns not included in the view definition
- Options may be used WITH READ ONLY DML operations on the view screen
- Any DML operation will return an Oracle server error
CREATE OR REPLACE VIEW empvu10
(employee_number, employee_name, job_title)
AS SELECT employee_id, last_name, job_id
FROM employees
WHERE department_id = 10
WITH READ ONLY;
- Discover some of the largest value of Top-N analysis:
SELECT ROWNUM, e.empno, e.ename
FROM emp e
WHERE ROWNUM <= 5
ORDER BY e.empno;
- Note:
for ROWNUM (pseudo-column) using only <or <=, and a =,>,> = will not return any data.
sequence
- Sequence:
for a plurality of users for generating database objects unique value
automatically provides unique value
shared object
primarily used to provide primary key
sequence value into memory access efficiency can be improved - Create a sequence of CREATE SEQUENCE statement
CREATE SEQUENCE sequence
[INCREMENT BY n] --每次增长的数值
[START WITH n] --从哪个值开始
[{MAXVALUE n | NOMAXVALUE}]
[{MINVALUE n | NOMINVALUE}]
[{CYCLE | NOCYCLE}] --是否需要循环
[{CACHE n | NOCACHE}]; --是否缓存登录
- Create a sequence EMP_SEQ provide the primary key for the table emp
CREATE SEQUENCE EMP_SEQ
INCREMENT BY 10
START WITH 120
MAXVALUE 9999
NOCACHE
NOCYCLE;
- Query the data dictionary view USER_SEQUENCES obtain sequence information is defined
SELECT sequence_name, min_value, max_value,
increment_by, last_number
FROM user_sequences;
- Display sequence if the next valid value NOCACHE option is specified, the column LAST_NUMBER
- And dummy column NEXTVAL CURRVAL
- NEXTVAL return sequence next valid value, any user can reference
- The current value is stored in the sequence CURRVAL
- NEXTVAL should be specified before CURRVAL, otherwise it will report an error CURRVAL not yet defined in this session.
Insert into emp(empno,ename) values(seq.nextval,’c’);
- DROP SEQUENCE statement to remove the use of sequence
- After deletion, the sequence can not be referenced again
DROP SEQUENCE emp_seq;
index
- Mode that is independent of the object table, the table may be stored in a different table or disk space
- The index is deleted or damaged, will not have an impact on the table, its impact is only on the speed
- Index Once established, Oracle management system will be maintained automatically, and decide when to use Oracle Management index. Users do not have to specify which index to use in the query
- When you delete a table, all will be automatically deleted based index of the table
- By pointer accelerate query speed Oracle server
- By the method of rapid positioning data, reduce disk I / O
- Automatically created: the system automatically creates a unique index on the corresponding column in the definition of PRIMARY KEY constraints or UNIQUE
- Created manually: Users can create nonunique indexes on other columns to speed up queries
- Create an index on LAST_NAME column of the EMP table
CREATE INDEX emp_last_name_idx
ON emp(last_name);
- You can create the following indexes:
the value of the distribution range is very wide data columns
often appear in the WHERE clause or join condition columns
table is accessed frequently and large amount of data, data access would account for about 2% to 4% of the total data
-
Do not create the following indexes:
the table is small
columns do not often occur as the join condition in the WHERE clause or
data queries than 2% to 4%
for the regular updates -
Index do not need, just say when we query with a name, the speed will be faster. Of course, check the faster, the speed will slow insertion. Because while inserting data, but also need to maintain an index.
- Information can use the data dictionary views USER_INDEXES and USER_IND_COLUMNS View index
SELECT ic.index_name, ic.column_name,
ic.column_position col_pos,ix.uniqueness
FROM user_indexes ix, user_ind_columns ic
WHERE ic.index_name = ix.index_name
AND ic.table_name = 'EMP';
- Use DROP INDEX command to delete the index
DROP INDEX emp_last_name_idx;
- Only the owner of the index or have DROP ANY INDEX user privileges can delete the index, the delete operation can not be rolled back
- Access the same object using synonyms:
easy access to other user objects
shortening the length of the name of the object
CREATE SYNONYM e FOR emp;
select * from e;
- Create a synonym for the view DEPT_SUM_VU
CREATE SYNONYM d_sum
FOR dept_sum_vu;
- Delete Synonym
DROP SYNONYM d_sum;