Week1 Creating Database Objects Part1
Week1 Creating Database Objects Part1
Database Objects
objects
Tables
Views
Stored programs
Etc.
2
Database Queries
Query: command to perform
operation on database object
Create
Read
Update
Delete
relational databases
SQL example
select id_type,substr(idno,1,4),count(*) from(
select a.polno,b.idno,substr(b.idno,1,4),b.id_type
from pol_info a,client_info b
where a.applicant=b.CLIENTNO
and a.undwrt_date>=to_date('20021202','yyyymmdd)
and substr(b.idno,1,4)<>'6501
union
select a.polno,b.idno,substr(b.idno,1,4),b.id_type
from h_pol_info a,client_info b
where a.applicant=b.CLIENTNO
and a.undwrt_date>=to_date('20021202','yyyymmdd)
and substr(b.idno,1,4)<>'6501)
)
group by id_type,substr(idno,1,4)
order by id_type,substr(idno,1,4)
User Accounts
schema
System Privileges
Object Privileges
11
1 to 30 characters long
Must begin with a character
Can contain characters, numbers, and the
symbols $, _, and #
12
Done by DBA
Syntax:
13
Level Purpose
CREATE SESSION
User
Connecting to database
CREATE TABLE
User
DROP TABLE
User
UNLIMITED TABLESPACE
User
CREATE USER
DBA
DBA
DBA
DBA
Done by DBA
Syntax:
GRANT privilege1, privilege2, TO username;
Example:
15
Database Roles
16
Creating Roles
Syntax:
CREATE ROLE role_name;
Example:
17
Syntax:
18
Example:
GRANT student to alice;
19
Syntax:
20
21
Field sizes
Constraints
22
Uses
Error checking
24
Example declaration:
student_gender CHAR(1)
26
27
28
Integer Numbers
Whole number with no digits to
right of decimal point
Precision is maximum width
Scale is omitted
Sample declaration:
s_age NUMBER (2)
30
32
Sample declaration:
s_dob DATE
33
34
BFILE
NCLOB
Examples:
item_image BLOB
item_image BFILE
36
Example:
CREATE TABLE my_students
( s_id NUMBER(6),
s_name VARCHAR2(30),
s_dob DATE,
s_class CHAR(2));
37
Constraints
38
Constraint Levels
Table constraint
Column constraint
Constraint Names
Internal name used by DBMS to identify the
constraint
Each constraint name in a user schema
must be unique
If you do not name a constraint, the system
will automatically generate an unintuitive
name
40
Constraint Names
Constraint naming convention:
tablename_fieldname_constraintID
Constraint ID values:
Primary key: pk
Foreign key: fk
Check condition: cc
Not NULL: nn
Unique: uk
Example:
s_id NUMBER(6)
CONSTRAINT student_s_id_pk PRIMARY KEY
42
45
46
47
48
Foreign Key
49
Value Constraints
Column-level
Restricts data values that can be inserted
in a field
In general, avoid value constraints
because they make the database very
inflexible
50
Unique
Table constraint
Specifies that a non-primary key field must have a
unique value
CONSTRAINT consultant_c_email_uk UNIQUE (c_email)
52
53
Syntax:
56
Summary of Oracle
Data Dictionary Views
OBJECTS
TABLES
Database tables
INDEXES
VIEWS
Database views
SEQUENCES
USERS
Database users
CONSTRAINTS
Table constraints
CONS_CONSTRAINTS
IND_COLUMNS
Indexed columns
TAB_COLUMNS
Modifying Tables
Unrestricted actions
Renaming tables
Adding new columns
Increasing column sizes
Dropping columns
Dropping constraints
58
Modifying Tables
Restricted actions
Dropping tables
Only allowed if table does not contain any fields that
are referenced as foreign keys, or if foreign key
constraints are dropped
Adding constraints
Only allowed if existing data meets requirements of
new constraint
59
Renaming Tables
60
Altering Tables
61
Altering Tables
62
Altering Tables
63
Altering Tables
Renaming a field
ALTER TABLE tablename RENAME
COLUMN field1 TO field2;
Deleting Tables