Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Database Objects
An Oracle database consists of multiple user accounts Each user account owns database
objects
Database Queries
Query: command to perform operation on database object
Create
Modify
View Delete
relational databases
Used to create and modify the structure of database objects Used to insert, update, delete, and view database data
DDL Commands
DDL commands execute as soon as they are issued, and do not need to be explicitly saved
Guide to Oracle 10g 5
DML Commands
COMMIT ROLLBACK
Guide to Oracle 10g 6
User Accounts
schema
Area in the database where the users database objects are stored Identified by a unique username and protected by a password
System Privileges
Control the operations that the user can perform within the database
Connecting to the database (Create Session), creating new tables, shutting down the database, etc.
Object Privileges
Granted on individual database objects Controls operations that a user can perform on a specific object (insert data, delete data, etc.) When you create an object in your user schema, you can then grant object privileges on that object to other database users
Guide to Oracle 10g 8
Oracle SQL command line utility for issuing SQL commands Starting SQL Plus
10
11
1 to 30 characters long Must begin with a character Can contain characters, numbers, and the symbols $, _, and #
12
Field sizes
Constraints
Guide to Oracle 10g 13
Creating a Table
CREATE TABLE tablename (fieldname1 data_type, (fieldname2 data_type, )
15
Uses
Error checking
Efficient use of storage space
Guide to Oracle 10g 16
columnname CHAR(max_size)
Fixed-length character data Max_size can be between 1 and 2000 characters Max_size is optional. Default is 1. Adds trailing blank spaces to pad width If more than max_size data is inserted, an error occurs. Example declaration: student_gender CHAR(2)
Guide to Oracle 10g 18
Character Subtypes
19
s_last CHAR(15);
SELECT s_last, s_first, s_address FROM student WHERE s_last = Smith;
What data type should be used if there is any chance that all column spaces will NOT be filled? Answer: VARCHAR2
Guide to Oracle 10g 20
Analogous to VARCHAR2 and CHAR but use Unicode rather than ASCII Used to hold character data in languages other than English (Japanese).
21
22
23
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)
Guide to Oracle 10g 24
259.99
25
26
Sample declaration:
s_dob DATE
Guide to Oracle 10g 27
28
29
Stores up to 4 GB of binary data Stores up to 4 GB of character data Stores a reference to a binary file maintained in the operating system Character LOB that supports 16-bit character code
Guide to Oracle 10g 32
BFILE
NCLOB
34
Example:
CREATE TABLE my_students ( s_id NUMBER(6), s_name VARCHAR2(30), s_dob DATE, s_class CHAR(2));
Guide to Oracle 10g 35
Constraints
Rules that restrict the values that can be inserted into a field Types of constraints
Integrity: define primary and foreign keys Value: specify values or ranges of values
that can be inserted
36
Constraint Levels
Table constraint
Restricts the value of a field with respect to all other table records Example: primary key value must be unique for each record Restricts values in a specific column Example: values in an S_GENDER field must be M or F
Guide to Oracle 10g 37
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 starts with SYS_Cn. n is a numeric value.
Guide to Oracle 10g 38
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
Constraint Names
10g too
40
Example:
s_id NUMBER(6) CONSTRAINT student_s_id_pk PRIMARY KEY
41
42
43
44
45
46
47
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
48
Unique
Table constraint Specifies that a non-primary key field must have a unique value
CONSTRAINT consultant_c_email_uk UNIQUE (c_email)
(Primary key constrain does not allow NULL, but this does)
Guide to Oracle 10g 50
SQL*Plus
Oracle SQL command line utility for issuing SQL commands Starting SQL*Plus
51
Using SQL*Plus
All commands must be terminated with a semicolon (;) Use a text editor and copy and paste commands Character data is case sensitive and must be in single quotes
M Sarah
Exiting SQL*Plus
Type exit at SQL> prompt
or
53
54
Create a Table
55
Using Notepad
Study
57
When creating tables with foreign key references, create referenced tables first
Always create tables without foreign keys before those with foreign keys
Guide to Oracle 10g 58
59
Contains tables that describe the database structure Is in the SYSTEM user schema Is automatically updated as users create and modify tables Cannot be updated directly Contains views that allow users to retrieve information about the database structure View: is a db object that the DBMS bases on an actual db
table and which enables the DBMS to present the table data in a different format based on user needs. It can serve to hide some table columns in which the user has no interest or doesnt have privileges to view.
Guide to Oracle 10g 60
Views present data in different formats depending on the privileges of the user
USER: shows all objects belonging to the current user ALL: shows all objects belonging to the current user, as well as objects current user has privileges to manipulate DBA: allows users with DBA privileges to view objects of all database users
Guide to Oracle 10g 61
62
General command: SELECT view_columnname1, view_columnname2, FROM prefix_object; view_columnname1, view_columnname2, are the name of the fields you want to retrieve from the view. prefix: either USER, ALL or DBA. object: the type of the DB object you are examining. See next slide for these objects. Ex: select table_name from user_tables;
Table_name is a field name in the table user_tables. User_tables is a table saves the names of all the table you (user) create. Guide to Oracle 10g
63
TABLES
INDEXES VIEWS
Database tables
Table indexes created to improve query performance Database views
SEQUENCES
USERS
CONSTRAINTS
CONS_COLUMNS IND_COLUMNS TAB_COLUMNS
Table constraints
Table columns that have constraints Indexed columns All table Guide tocolumns Oracle 10g
64
65
Modifying Tables
Unrestricted actions: some specifications
can always be modified. Renaming tables Adding new columns Increasing column sizes Dropping columns Dropping constraints
66
Modifying Tables
Restricted actions:specifications modified only in
certain situations 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 Guide to Oracle 10g constraint
67
To delete:
DROP TABLE [tablename] Use with caution. It is a restricted actions, can not be dropped if it
contains a foreign key. Delete the constraint and then drop the table or use cascade.
To rename:
RENAME old_tablename TO new_tablename DBMS automatically transfers to the new table integrity constraints, indexes, and privileges that referenced the old table. Views and stored program units that reference the old table name become Invalid.
Guide to Oracle 10g 68
Altering Tables
69
Altering Tables
Can
only change data type to compatible data type (i.e. varchar2 to char)
Guide to Oracle 10g 70
Altering Tables
Renaming a Column
To rename a field
Add a constraint:
ALTER TABLE tablename ADD CONSTRAINT constraint_name constraint_definition;
Remove a constraint:
ALTER TABLE tablename DROP CONSTRAINT constraint_name;
Examples: ALTER TABLE faculty ADD CONSTRAINT faculty_f_pin_uk UNIQUE (f_pin); ALTER TABLE faculty DROP CONSTRAINT faculty_f_pin_uk;
Guide to Oracle 10g 73
When modifying a database it can be useful to disable constraints Constraints are enabled by default To disable a constraint:
ALTER TABLE tablename DISABLE CONSTRAINT constraint_name;
To enable a constraint:
ALTER TABLE tablename ENABLE CONSTRAINT constraint_name;
1. ALTER TABLE faculty DISABLE CONSTRAINT faculty_loc_id_fk; 2. ALTER TABLE faculty ENABLE CONSTRAINT faculty_loc_id_fk;
Deleting Tables
Syntax to delete table and constraints if table contains fields that are referenced as foreign keys:
DROP TABLE tablename CASCADE CONSTRAINTS;
Guide to Oracle 10g 75
Summary
Data description language (DDL) commands: create, modify, Deleted database objects Data manipulation language (DML) commands: insert, update, delete, view database data specify the table name, the name of each data field, and the data type and size of each data field
To create a table:
Data types ensure correct data values Constraints restrict values of database fields SQL*Plus commands are not case sensitive
Guide to Oracle 10g 76
Summary
(cont.)
Errors include line number, position, error code Use DESCRIBE command to display a tables fieldnames and data types Tables can be modified or deleted but some changes are restricted
Guide to Oracle 10g 77
End Of Chapter 2
78