SQL語句PART3

constraint Example:
1. grammer:
 create table [schema.]table
      (column datatype [DEFAULT expr]
     [column_constraint], ...
     [table_constraint] [,......]);
2. example of a column_level constraint:
create table employees(employee_id number(6) constraint emp_emp_id_pk primiary key, first_name varchar2(20), ...);
3. example of a table-level constraint:
create table employees(employee_id number(6), first_name varchar2(20), ... job_id varchar2(20) not null, constraint emp_emp_id_pk primary key(employee_id));
4. UNIQUE example
create table employees(employee_id number(6), last_name varchar2(20) not null,..., constraint emp_email_uk UNIQUE(email));
5. FOREIGN KEY constraint
create table employees(employee_id number(6), last_name varchar2(20),... constraint emp_dept_fk FOREIGN KEY (department_id) REFERENCES departments(department_id), constraint emp_email_uk UNIQUE(email));
** foreign key constaint:
** on delete cascade: deletes the dependent rows in the child table when a row in the parent table is deleted.
** on delete set null: converts dependent foreign key values to null.
6. CHECK constraint
* not allowed: 1) references to CURRVAL, NEXTVAL, LEVEL, and ROWNUM pseudocolumns 2)calls to SYSDATE, UID, USER and USERENV functions. 3) queries that refer to other values in other rows
FINAL example on constraints:
CREATE TABLE employees
( employee_id NUMBER(6) CONSTRAINT emp_employee_id PRIMARY KEY
, first_name VARCHAR2(20)
, last_name VARCHAR2(25) CONSTRAINT emp_last_name_nn NOT NULL
, email VARCHAR2(25) CONSTRAINT emp_email_nn NOT NULL CONSTRAINT emp_email_uk UNIQUE
, phone_number VARCHAR2(20)
, hire_date DATE CONSTRAINT emp_hire_date_nn NOT NULL
, job_id VARCHAR2(10) CONSTRAINT emp_job_nn NOT NULL
, salary NUMBER(8,2) CONSTRAINT emp_salary_ck CHECK (salary>0)
, commission_pct NUMBER(2,2)
, manager_id NUMBER(6) CONSTRAINT emp_manager_fk REFERENCES employees (employee_id)
, department_id NUMBER(4) CONSTRAINT emp_dept_fk REFERENCES departments (department_id));


create table using subquery
1. grammer:
CREATE TABLE table
[(column, column...)]
AS subquery;
2. examples:
CREATE TABLE dept80
AS
SELECT employee_id, last_name,
salary*12 ANNSAL,
hire_date
FROM employees
WHERE department_id = 80;


Read-Only tables:
grammer:
alter table t1 read only;
alter table t1 read write;
 


Create or Update view grammer:
CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view
[(alias[, alias]...)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY [CONSTRAINT constraint]];
e.g: simple view:
CREATE OR REPLACE VIEW empvu80
(id_number, name, sal, department_id)
AS SELECT employee_id, first_name || ' '
|| last_name, salary, department_id
FROM employees
WHERE department_id = 80;
e.g: complex view:
CREATE OR REPLACE VIEW dept_sum_vu
(name, minsal, maxsal, avgsal)
AS SELECT d.department_name, MIN(e.salary),
MAX(e.salary),AVG(e.salary)
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
GROUP BY d.department_name;
** can perform DML operations on simple view
** cannot remove a row  if the view contains: Group functions, a GROUP BY clause, the DISTINCT keywork, the pseudocolumn ROWNUM keyword
** cannot modify data in a view if it contains: group functions, a GROUP BY clause, the DISTINCT keyword, the pseudocolumn ROWNUM keyword, Columns defined by expressions.
** cannot add data through a view if the view includes: GROUP functions, a GROUP BY clause, the DISTINCT keyword, the pseudocolumn ROWNUM keyword, columns defined by expressions, NOT NULL columns in the base tables that are not selected by the view.
e.g.: with check option
create or replace view v_1 as select * from employees where department_id=20 with check option constraint emp20_ck;
** any atempts to insert a row with a department_id other than 20, or to updae the department number for any row in the view fails because it violates the with check option constraint.
e.g.: with read only option
create or replace view v_1 as select * from employees where department_id=10 with read only;

1
sequence
CREATE SEQUENCE sequence
[INCREMENT BY n]
[START WITH n]
[{MAXVALUE n | NOMAXVALUE}]
[{MINVALUE n | NOMINVALUE}]
[{CYCLE | NOCYCLE}]
[{CACHE n | NOCACHE}];
e.g.: CREATE SEQUENCE dept_deptid_seq INCREMENT BY 10 START WITH 120 MAXVALUE 9999 NOCACHE NOCYCLE;
ALTER SEQUENCE dept_deptid_seq
INCREMENT BY 20
MAXVALUE 999999
NOCACHE
NOCYCLE;


Index.
** automatically created when set UNIQUE, PRIMARY KEY.
** manually crreate index.
grammer:
CREATE [UNIQUE][BITMAP]INDEX index
ON table (column[, column]...);
examples:
CREATE INDEX emp_last_name_idx
ON employees(last_name);
******** create an index when *****
1. a column contains a wide range of values
2. a column contains a large number of null values
3. one or more columns are frequently used together in a WHERE clause or a join condition.
4. the table is large and most queries are expected to retrieve less than 2% to 4% of the rows in the table.
*****  do not create an index when ****
1. the columns are not often used as a condition in the query.
2. the table is small or most queries are expected to retrieve more than 2% to 4% of the rows in the table.
3. the tale is updated frequently
4. the indexed columns are referenced as part of an expression.
DROP INDEX index;


Synonym.
CREATE [PUBLIC] SYNONYM synonym FOR object;
e.g.: CREATE SYNONYM d_sum FOR dept_sum_vu;
DROP SYNONYM d_sum;


Types of Oracle - Proprietary Joins: Equijoin, NonEquijoin, Outer join, Self-join.
e.g.: Equijoins:
select e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id from employee e, departments d where e.department_id=d.department_id;
e.g.: Nonequijons:
select e.last_name, e.salary, j.grade_level from employees e, job_grades j where e.salary between j.lowest_sal and j.highest_sal;
** outer join syntax: to see rows that do not meet the join conditions
select table1.column, table2.column from table1, table2 where table1.column(+)=table.column;
or
select table1.column, table2.column from table1, table2 where table1.column = table2.column(+)
e.g.:
select g.*, f.*
from
     wf_guidang_time g, wf_doc_gw_fawen f
where
     g.ndocid=f.ndocid(+)    --count: 7, displaying 1) results in g and f (count 3) and 2) results in g but not in f (count 3)
     and g.ndocid>440000
order by g.ndocid desc;
select g.*, f.*
from
     wf_guidang_time g, wf_doc_gw_fawen f
where
     g.ndocid(+)=f.ndocid    --count: 30, displaying 1) results in g and f (count 3) and 2) results in f but not in g (count 27)
     and f.ndocid>440000
order by g.ndocid desc;
select g.*, f.*
from
     wf_guidang_time g, wf_doc_gw_fawen f
where
     g.ndocid=f.ndocid    --count:3, displaying 1) results in g and f (count 3)
     and g.ndocid>440000
order by g.ndocid desc;


Creating a user:
grammer: create user user1 identified by pwd1;
grant privilege [, privilege...] to user [, user | role, PUBLIC...];
** system privileges: create session, create table, create sequence, create view, create procedure...
e.g.:
create user demo1 identified by demo1;
grant create session, create table, create view... to demo1;
grammer: alter...
alter user demo1 identified by demopwd1;


Creating a Role
 grammer: create role role1;
e.g.:
create role role1;
grant create session, create table to role1;
grant role1 to user1;


Object privileges
grammer: grant object_priv [(columns)] on object to {user|role|public} with grant option;
e.g.: grant select on employees to demo // grant query privileges on the employees table.
e.g. grant update(department_name, location_id) on departments to demo, manager; // grant privileges to update specific columns to users and roles.
e.g.: grant select, insert on departments to demo with grant option; // give a user authority to pass along privileges;
e.g.: grant select on alice.department to public;// allow all users on the system to query data from Alice's department table.

發佈了67 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章