Vous êtes sur la page 1sur 21

ORACLE SQL

Session 4
Objectives
• Oracle Integrity Constraints
• Managing Oracle Views
• Managing Oracle Sequences
Integrity Constraints
• To enforce rules at the table level.
• Used to prevent the entry of invalid information into tables.
• Five Integrity Constraints Available in Oracle
 Not Null
 Primary Key
 Foreign Key
 Unique
 Check
Not Null
• To ensure that a column must always have a value
• Is defined at the column level.
• E.g. :
CREATE TABLE branch(…
bname CHAR(15) NOT NULL
)
Or
CREATE TABLE branch(
bname CHAR(15) ,
Constraint bname_nn NOT NULL
)
Primary Key
• A column or set of column uniquely identifies each row in a
table
• Ensures that no duplicate rows exist
• Eg:
CREATE TABLE branch(
bname CHAR(15) PRIMARY KEY,
bcity CHAR(20),
assets INT);
or
CREATE TABLE depositor(
cname CHAR(15),
acct_no CHAR(5),
PRIMARY KEY(cname, acct_no));
Foreign Key
• A non key attribute refers to primary key column of other
table.
• Table which has Primary Key is known as PARENT table and
the table which has Foreign key is known as CHILD table.

Here EMPNO in attendance table is a foreign key referring to


EMPNO of EMP table.
Cont…
Syntax:
CONSTRAINT constraint_name
REFERENCES primary_key_table_name (field_name)
• E.g:
CREATE TABLE branch(
bname CHAR(15) PRIMARY KEY
....)

CREATE TABLE loan (


.........
FOREIGN KEY bname REFERENCES
branch);
Unique Key
• A column or set of column uniquely identifies each row
in a table
• Some of the fields can contain null values as long as the
combination of values is unique
• Syntax:
• CREATE TABLE table_name
(column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT constraint_name UNIQUE (column1,
column2, . column_n)
);
Cont…
• Eg:
CREATE TABLE supplier (
supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_unique UNIQUE (supplier_id)
);
Check
• Allows you to specify a condition on each row in a table
• Syntax:
CREATE TABLE table_name
(column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT constraint_name CHECK
(column_name condition) [DISABLE]
);
• The condition will not be enforced. Using DISABLE
keyword
Cont…
• Eg:
CREATE TABLE suppliers (
supplier_id numeric(4),
supplier_name varchar2(50),
CONSTRAINT check_supplier_id CHECK (supplier_id
BETWEEN 100 and 9999) );
View
• A view is a virtual table that represent the data of one of
more tables.
• Syntax:
CREATE VIEW view_name AS
SELECT columns
FROM table
WHERE predicates ;

• Eg:
CREATE VIEW sup_orders AS
SELECT suppliers.supplier_id, orders.quantity,
orders.price
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id
and suppliers.supplier_name = 'IBM';
Cont…
• How a query on a view is written:
SELECT * FROM sup_orders;

• Updating a VIEW:
CREATE or REPLACE VIEW sup_orders AS
SELECT suppliers.supplier_id, orders.quantity, orders.price
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id
and suppliers.supplier_name = 'Microsoft';

• Dropping a VIEW:
DROP VIEW sup_orders;
Classification of Views
View

Simple View Complex View

– Derives data from only one table – Derives data from many tables
– Contains no functions or groups of – Contains functions or groups of
data data
– Can perform DML operations – Does not always allow DML
through the view operations through the view
– Eg: – Eg:
CREATE VIEW salvu50 CREATE VIEW dept_sum_vu (name,
AS SELECT employee_id minsal, maxsal, avgsal) AS SELECT
ID_NUMBER, last_name NAME, d.department_name,MIN(e.salary),MA
salary*12 ANN_SALARY X(e.salary),AVG(e.salary) FROM
FROM employees employees e, departments d WHERE
WHERE department_id = 50; e.department_id = d.department_id
GROUP BY d.department_name;
With Check Option
• Ensure that DML operations performed on the view stay
within the domain of the view by
using the WITH CHECK OPTION clause.
Eg:
CREATE OR REPLACE VIEW empvu20
AS SELECT *
FROM employees
WHERE department_id = 20
WITH CHECK OPTION CONSTRAINT ;
• Any attempt to change the department number for any row in
the view fails because it violates the WITH CHECK OPTION
constraint
With Read Only
• Ensure that no DML operations occur
• Eg:
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;
Sequence
• Automatically generates unique numbers
• Is a sharable object
• Is typically used to create a primary key value
• Syntax:
CREATE SEQUENCE sequence
[INCREMENT BY n]
[START WITH n]
[{MAXVALUE n | NOMAXVALUE}]
[{MINVALUE n | NOMINVALUE}]
[{CYCLE | NOCYCLE}]
[{CACHE n | NOCACHE}];
Cont…
• Eg:
CREATE SEQUENCE dept_deptid_seq
INCREMENT BY 10
START WITH 120
MAXVALUE 9999
NOCACHE
NOCYCLE;
• Confirming Sequences:
SELECT sequence_name, min_value, max_value,
increment_by, last_number
FROM user_sequences;
NEXTVAL and CURRVAL
Pseudocolumns
• NEXTVAL returns the next available sequence value.
• CURRVAL obtains the current sequence value.
• Using Sequence:
INSERT INTO departments(department_id,
department_name, location_id)VALUES
(dept_deptid_seq.NEXTVAL,'Support', 2500);
• View current value Sequence :
SELECT dept_deptid_seq.CURRVAL
FROM dual;
Modifying a Sequence
• Change the increment value, maximum value,minimum value,
cycle option, or cache option.
• Eg:
ALTER SEQUENCE dept_deptid_seq
INCREMENT BY 20
MAXVALUE 999999
NOCACHE
NOCYCLE;
• To Drop a Sequence:
DROP SEQUENCE dept_deptid_seq;

Vous aimerez peut-être aussi