Oracle Lecture 3 - DDL
Oracle Lecture 3 - DDL
Naming Rules
• Table names and column names:
– Must begin with a letter
– Must be 1–30 characters long
– Must contain only A–Z, a–z, 0–9, _, $, and #
– Must not duplicate the name of another object
owned by the same user
– Must not be an Oracle server reserved word
Data Types
Data Type Description
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
Constraints...
– Column-level constraint:
column [CONSTRAINT constraint_name] constraint_type,
– Table-level constraint:
column,...
[CONSTRAINT constraint_name] constraint_type
(column, ...),
Defining Constraints
– Column-level constraint:
CREATE TABLE employees(
employee_id NUMBER(6)
CONSTRAINT emp_emp_id_pk PRIMARY KEY, 1
first_name VARCHAR2(20),
...);
– Table-level constraint:
CREATE TABLE employees(
employee_id NUMBER(6),
first_name VARCHAR2(20),
... 2
job_id VARCHAR2(10) NOT NULL,
CONSTRAINT emp_emp_id_pk
PRIMARY KEY (EMPLOYEE_ID));
The NOT NULL Constraint
• The NOT NULL constraint ensures that the column
has a value and the value is not a null value
PRIMARY
KEY
…
EMPLOYEES
FOREIGN
KEY
… Not allowed
INSERT INTO (9 does not
exist)
Allowed
FOREIGN KEY Constraint
• Defined at table level only:
CREATE TABLE employees(
employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25),
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),
CONSTRAINT emp_email_uk UNIQUE(email));
FOREIGN KEY Constraint:
Keywords
– FOREIGN KEY: Defines the column in the child
table at the table-constraint level
– REFERENCES: Identifies the table and column in
the parent table
CHECK Constraint
UPDATE employees
SET department_id = 55
WHERE department_id = 110;
UPDATE employees
*
ERROR at line 1:
ORA-02291: integrity constraint (HR.EMP_DEPT_FK)
violated - parent key not found
DESCRIBE dept80
ALTER TABLE Statement
• Use the ALTER TABLE statement to:
– Add a new column
– Modify an existing column
– Define a default value for the new column
– Drop a column
Adding a New Column to an
Existing Table
• The general syntax to add a column to an
existing table is
where newdatatype is the new data type or the new size for the
column.
• For example,
DROP TABLE sample;
– Usually the first entry is assigned 1, the next 2, and so on, but Oracle
lets you change this
Sequences
• In Oracle we use a Sequence
– A sequence is a source of numbers
– We can declare several sequences, giving each a
name, a start point, and a step size
– We can then generate unique numbers by asking
for the next element from a sequence
Sequences in Oracle
• To declare a sequence:
CREATE SEQUENCE <name>
[START WITH <value>]
[INCREMENT BY <value>]
– If no START WITH or INCREMENT BY values
are given they default to 1
• To get the next value from a sequence
<sequence name>.nextVal
Sequence Example
• Creating a sequence
CREATE SEQUENCE mySeq START WITH 1
• Using a sequence
SELECT mySeq.nextVal FROM DUAL;
INSERT INTO Student
(stuID, stuName, stuAddress)
VALUES
(mySeq.nextVal, 'Steve Mills',
'13 Elm Street')
Oracle Data Dictionary
• To find out what tables and sequences you
have defined use
SELECT table_name
FROM user_tables
– The user_tables table is maintained by Oracle
– It has lots of columns, so don’t use
SELECT * FROM user_tables
Oracle Data Dictionary
• To find the details of a table use
DESCRIBE <table name>
• Example:
SQL> DESCRIBE Student;
Name Null? Type
------------ -------- ----------
STUID NOT NULL NUMBER(38)
STUNAME NOT NULL VARCHAR2(50)
STUADDRESS VARCHAR2(50)
STUYEAR NUMBER(38)
DCL: Data Control Language
• Controlling Access to database objects such as tables
and views
• Example : Granting “Mary” the access to Table “student” (for inserting,
updating and deleting)
– GRANT INSERT, UPDATE, DELETE ON Emp TO Mary
– GRANT <privileges> ON <object name>
TO <grantee> [ <comma> <grantee> ... ]
[ WITH GRANT OPTION ]
– WITH GRANT OPTION: allows the grantee to further grant privileges
– Can be limited to a column of a table, Ex: GRANT UPDATE(name) ON emp TO
Mary
– To revoke privileges : REVOKE