Download ORACLE - Majmaah University

Document related concepts

Oracle Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Database wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

SQL wikipedia , lookup

Functional Database Model wikipedia , lookup

Clusterpoint wikipedia , lookup

PL/SQL wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
Chapter 2:
Creating and Modifying
Database Tables
IS421 DB Applications
Mr. Ashraf Yaseen
1
Objectives




Become acquainted with Structured
Query Language (SQL)
Learn about Oracle10g user schemas
Learn how to define Oracle10g
database tables
Create database tables using SQL*Plus
IS421 DB Applications
Mr. Ashraf Yaseen
2
Objectives (cont.)



Learn how to debug Oracle10g SQL
commands and use Oracle Corporation
online help resources
Learn how to view information about
your database tables using Oracle10g
data dictionary views
Modify and delete database tables
using SQL*Plus
IS421 DB Applications
Mr. Ashraf Yaseen
3
Database Objects and Queries


An Oracle database consists of multiple user
accounts
Each user account owns database objects



•
•

Tables
Views
Stored programs
Etc.
Query: command to perform operation on database
object
Structured Query Language (SQL)
 Industry standard query language for most of relational
databases
IS421 DB Applications
Mr. Ashraf Yaseen
4
Basic SQL Concepts and Commands
SQL (Structured Query Language) is used to
manipulate the database.
There are two basic types of SQL commands:
Data Definition Language (DDL)
Data Manipulation Language (DML)
DDL commands work with the structure of the objects
(tables, indexes, views) in the database.
DML commands work with the data in the database
(i.e.,manipulate the data).
IS421 DB Applications
Mr. Ashraf Yaseen
5
DDL Commands

Used to create and modify the structure of database
objects






CREATE
ALTER
DROP
GRANT
REVOKE
DDL commands execute as soon as they are
issued, and do not need to be explicitly saved
IS421 DB Applications
Mr. Ashraf Yaseen
6
DML Commands

Used to insert, view, and modify database data





INSERT
UPDATE
DELETE
SELECT
DML commands need to be explicitly saved or rolled
back



COMMIT
ROLLBACK
SAVEPOINT
IS421 DB Applications
Mr. Ashraf Yaseen
7
Security Granting Table Privileges


Security is the prevention of unauthorized access to
the database. Within an organization, the database
administrator determines the types of access various
users need for the database.
Some users might be able to retrieve and update
data in the database. Other users might be able to
retrieve any data from the database but not make any
changes to it. Still other users might be able to
access only a portion of the database.
IS421 DB Applications
Mr. Ashraf Yaseen
8
Oracle10g User Accounts



User account - identified by a unique
username and password
User schema - all of the objects that the user
creates and stores in the database
Object owner has privileges to perform all
possible actions on an object
IS421 DB Applications
Mr. Ashraf Yaseen
9
Break Time: SQL Plus


Oracle SQL command line utility
for issuing SQL commands
Starting SQL Plus
LOGON to
YOUR
Oracle
Account
IS421 DB Applications
Mr. Ashraf Yaseen
10
How to Access Your Oracle Account
1. Click the START button, point to Programs
2. Select Oracle –Oracle10g, then
3. Click Application Development, then
4. Select SQL PLUS
User Name:
Password:
Host string:
IS421 DB Applications
Mr. Ashraf Yaseen
11
Types of Database Privileges

System Privileges


Control the operations that the user can perform within
the database
 Create user accounts
 Connecting to the database, 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
IS421 DB Applications
Mr. Ashraf Yaseen
12
Creating New User Accounts


Done by DBA
Syntax:
CREATE username IDENTIFIED BY password;
IS421 DB Applications
Mr. Ashraf Yaseen
13
Defining Oracle10g Database Tables

To create a table, you must specify:





Table name
Field names
Field data types
Field sizes
Constraints

restrictions on the data values that a field can store
IS421 DB Applications
Mr. Ashraf Yaseen
14
Creating a Table
CREATE TABLE tablename
(fieldname1 data_type,
(fieldname2 data_type,
…)
IS421 DB Applications
Mr. Ashraf Yaseen
15
Oracle Naming standers and Conventions
Naming standards are Series of rules Oracle Corporation
established for naming all database objects
• From 1 to 30 characters
• Only alphanumeric characters, and special
•
characters ($ , _, #)
Must begin with a letter and can not contain
blank spaces or hyphens

Are the following names valid? Why?
customer order
customer-order
#order
IS421 DB Applications
Mr. Ashraf Yaseen
16
Oracle10g Data Types

Data type: specifies the kind of data that a field
stores

Assigning a data type provides a means for error
checking

Data types enable the DBMS to use storage space
more efficiently by internally storing different types of
data in different ways
IS421 DB Applications
Mr. Ashraf Yaseen
17
Data Types
• Built-in

provided by the system
• Library

built by the software
vendor or a third party
• User-defined

built by users
IS421 DB Applications
Mr. Ashraf Yaseen
18
Oracle Data Types
• Data type:
specifies type of data stored in a
field

Date, character, number, etc.
• Uses/Purposes
 Error checking
 Efficient use of storage space
IS421 DB Applications
Mr. Ashraf Yaseen
L
19
Basic Built-In Data Types
•
Character


•
•
•

VARCHAR2
CHAR
NVARCHAR2 / NCHAR
Numeric

NUMBER
Date/Time
Others:

LONG, RAW, LONG RAW, BLOB
IS421 DB Applications
Mr. Ashraf Yaseen
20
Character Data Types

1. VARCHAR2
Stores variable-length character data up
to a maximum of 4,000 characters
 Values in different records can have a
different number of characters
 fieldname VARCHAR2(maximum_size)



(e.g.) emp_name VARCHAR2(20);
an instance: ‘Jason Chen’
IS421 DB Applications
Mr. Ashraf Yaseen
21
Character Data Types (cont.)

2. CHAR






Fixed-length character data (<= 2000 characters)
default is 1 if no field size is specified
Data values for different records all have the same
number of characters
DBMS adds trailing blank spaces to the end of the entry
to make the entry fill the maximum_size value
Data longer than maximum_size causes an error
fieldname CHAR[(maximum_size)]
[optional]
 pros: use data storage space more efficiently and
processed faster
 cons: causes inconsistent query results in other
Oracle applications


IS421 DB Applications

e.g. s_class CHAR(2);

‘SR’ ‘JR’ ‘SO’ ‘FR’ ‘GR’
s_state CHAR(2) DEFAULT ‘WI’;
Mr. Ashraf Yaseen
student_gender CHAR;
22
Character Subtypes
Examples:
VARCHAR2(5)
‘Smith’ or ‘Smi’
CHAR(5)
‘Smith’ or ‘Smi
IS421 DB Applications
Mr. Ashraf Yaseen
’
23
Question: Which query will possibly generate
student information?
s_last VARCHAR2(15);
s_last CHAR(15);
SELECT s_last, s_first,
s_address
FROM student
WHERE s_last = ‘Smith’;
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
IS421 DB Applications
Mr. Ashraf Yaseen
L
24

When use Query:
SELECT s_last, s_first, ssn, telephone
FROM student
WHERE s_last = ‘Smith’;

Case is sensitive within the single
quotation.
SQL Plus commands are NOT case
sensitive, but Query within the single
quotation are case sensitive.

IS421 DB Applications
Mr. Ashraf Yaseen
25
Character Data Types (cont.)

3. NVARCHAR2 and NCHAR
Analogous to VARCHAR2 and
CHAR but use Unicode rather than
ASCII
 Used to hold character data in
languages other than English

IS421 DB Applications
Mr. Ashraf Yaseen
26
4. Number Data Types

Stores negative, positive, fixed, and floating point
numbers between 10 -130 and10 +125 with precision up
to 38 decimal places

General Syntax:
fieldname NUMBER [([precision,] [scale])]

Integer: fieldname NUMBER(precision)
Fixed point: fieldname NUMBER[([precision],[scale])]
Floating point: fieldname NUMBER


IS421 DB Applications
Mr. Ashraf Yaseen
27
Number Data Types (examples)




a) Integer: Number (n)
 e.g.
s_id NUMBER(5)

12345
b) Fixed-point numbers

e.g. current_price NUMBER (5, 2);
 259.99
33.89
c) Fixed-point numbers (cont.)

e.g. total_mileage NUMBER (5, 1);
 259.9
33.8
d) Floating-point Number – with a variable number of decimal
places
 e.g.
s_gpa NUMBER;

3.89
2.7569
3.2
IS421 DB Applications
Mr. Ashraf Yaseen
28
5. Date and Time Data Types

DATE

Dates from December 31, 4712 BC to December 31,
4712 AD
Stores the century, year, month, day, hour, minute and
second.
Default format DD-MON-YY
Default time format HH:MI:SS A.M.

fieldname DATE



Sample declaration:
s_dob DATE;
Use one of the following format masks:



IS421 DB Applications
’, ‘MM/DD/YY’)
‘, ‘DD-MON-YYYY’)
‘, ‘HH:MI AM’)
TO_DATE (‘
TO_DATE (‘
TO_DATE (‘
Mr. Ashraf Yaseen
29
Date and Time Data Types (cont.)

TIMESTAMP

Similar to DATE but stores fractional seconds
fieldname TIMESTAMP (fractional_seconds_precision)


If you omit the fractional_seconds_precision
specification, the default value is 6 decimal
places.
E.g: s1_date_received TIMESTAMP(2);
IS421 DB Applications
Mr. Ashraf Yaseen
30
Date and Time Data Types (cont.)


INTERVAL YEAR TO MONTH
 Time interval, expressed in years and months
 +02-11 specifies a positive time interval of 2 years and 11 months
 fieldname INTERVAL YEAR[(year_precision)] TO MONTH
 e.g., Software Expert database (p.48)
elapsed_time INTERVAL YEAR(2) TO MONTH;
INTERVAL DAY TO SECOND
 Time interval, expressed in days, hours, minutes, and seconds
 -04 03:20:32.00: 4 days, 3 hours, 20 minutes, and 32 seconds
 fieldname INTERVAL DAY[(leading_precision)] TO
SECOND[(fractional_seconds_precision)]
 e.g.
 DDL:


elapsed_time INTERVAL DAY(6) TO SECOND,
DML:

TO_DSINTERVAL('53 00:00:00.00')
IS421 DB Applications
Mr. Ashraf Yaseen
31
6. Large Object (LOB) Data Types
IS421 DB Applications
Mr. Ashraf Yaseen
32
What is a Constraint?


A rule that restricts the values that can be
inserted into a field
A mechanism used to protect


the relationship between data within an Oracle
table, or
the correspondence between data in two different
tables.

For example, the state entered must be one of the 50
states in the U.S.
IS421 DB Applications
Mr. Ashraf Yaseen
33
Types of Constraints




Integrity constraints: define primary and foreign
keys
Value constraints: define specific data values or
data ranges that must be inserted into columns
and whether values must be unique or not NULL
Table constraint: restricts the data value with
respect to all other values in the table
Field constraint: limits the value that can be
placed in a specific field, irrespective of values
that exist in other table records
IS421 DB Applications
Mr. Ashraf Yaseen
34
Creating a Table
CREATE TABLE tablename
(fieldname1 data_type [CONSTRAINT constraint_name]
CONSTRAINT_TYPE,
fieldname2 data_type [CONSTRAINT constraint_name]
CONSTRAINT_TYPE,,…)
CREATE TABLE tablename
(fieldname1 data_type,
fieldname2 data_type,
[CONSTRAINT constraint_name]
CONSTRAINT_TYPE(coulmn_name));
IS421 DB Applications
Mr. Ashraf Yaseen
35
I. Naming conventions for constraints
<tablename>_<fieldname>_<constraint id>
Where <constraint id> is:
•
•
pk
fk
PRIMARY KEY
REFERENCES <tablename> (pk)
•
•
cc
CHECK <condition to be checked>
(note that cc stands for CHECKCONDITION)
•
•
nn
uk
NOT NULL
UNIQUE
e.g.,
s_id NUMBER (6) CONSTRAINT student_s_id_pk PRIMARY KEY;
IS421 DB Applications
Mr. Ashraf Yaseen
36
Oracle Constraint Naming Convention

tablename_ fieldname_constraintID
IS421 DB Applications
Mr. Ashraf Yaseen
37
Integrity Constraints

Define primary key fields

Specify foreign keys and their
corresponding table and column
references

Specify composite keys
IS421 DB Applications
Mr. Ashraf Yaseen
38
Primary Key Constraints

Table-level
Can be defined when field is declared

Syntax:

CONSTRAINT constraint_name PRIMARY KEY
SQL> CREATE TABLE my_students
2
(s_id NUMBER(6) CONSTRAINT my_students_s_id_pk PRIMARY KEY,
3
s_name VARCHAR2(30),
4
s_class CHAR(2),
5
s_dob DATE);
IS421 DB Applications
Mr. Ashraf Yaseen
39
Primary Key Constraints (cont.)

Can also be defined after all table field definitions are
completed
SQL> CREATE TABLE my_students
2 (s_id NUMBER(6) CONSTRAINT my_students_s_id_pk PRIMARY KEY,
3
s_name VARCHAR2(30),
4
s_class CHAR(2),
5
s_dob DATE);
SQL> CREATE TABLE my_students
2
(s_id NUMBER(6),
3
s_name VARCHAR2(30),
4
s_class CHAR(2),
5
s_dob DATE),
6
CONSTRAINT student_s_id_pk PRIMARY KEY (s_id));
IS421 DB Applications
Mr. Ashraf Yaseen
40
Primary Key Constraints (cont.)
You will learn how to create the following SQL (DDL)
command:
CREATE TABLE location
(loc_id NUMBER(6),
bldg_code VARCHAR2(10),
room VARCHAR2(6),
capacity NUMBER(5),
CONSTRAINT location_loc_id_pk PRIMARY KEY (loc_id));
IS421 DB Applications
Mr. Ashraf Yaseen
41
Foreign Key Constraints



Table-level
Can only be defined after field is defined as a
primary key in another table
Syntax:
CONSTRAINT constraint_name
REFERENCES primary_key_table_name (field_name)
IS421 DB Applications
Mr. Ashraf Yaseen
42
Foreign Key Constraints

Can be defined when field is declared
CREATE TABLE faculty
(f_id NUMBER(6) CONSTRAINT faculty_f_id_pk PRIMARY KEY(f_id),
f_last VARCHAR2(30),
f_first VARCHAR2(30),
f_mi CHAR(1),
loc_id NUMBER(5) CONSTRAINT faculty_loc_id_fk REFERENCES location(loc_id)
f_phone VARCHAR2(10),
f_rank VARCHAR2(8),
f_pin NUMBER(4),
f_image BLOB,
);
SQL> SELECT TABLE_NAME FROM USER_TABLES;
N
IS421 DB Applications
Mr. Ashraf Yaseen
43
Foreign Key Constraints (cont.)

Can also be defined after all table field
definitions are completed
CREATE TABLE faculty
(f_id NUMBER(6),
f_last VARCHAR2(30),
f_first VARCHAR2(30),
f_mi CHAR(1),
loc_id NUMBER(5),
f_phone VARCHAR2(10),
f_rank VARCHAR2(8),
f_pin NUMBER(4),
f_image BLOB,
CONSTRAINT faculty_f_id_pk PRIMARY KEY(f_id),
CONSTRAINT faculty_loc_id_fk FOREIGN KEY (loc_id) REFERENCES location(loc_id));
SQL> SELECT TABLE_NAME FROM USER_TABLES;
IS421 DB Applications
Mr. Ashraf Yaseen
44
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
IS421 DB Applications
Mr. Ashraf Yaseen
45
Types of Value Constraints

Check condition: restricts to specific values



CONSRAINT student_s_class_cc CHECK
((s_class='FR') OR (s_class='SO') OR (s_class='JR')
OR (s_class=’SR'));
CONSTRAINT course_credits_cc CHECK
((credits>0) AND (credits <=18));
s_gender (M or F)
CONSTRAINT my_students_s_gender_cc CHECK (s_gender =
‘M’) OR (s_gender = ‘F’);

Not NULL: specifies that a field cannot be NULL

Must be created in column declaration.

s_last VARCHAR2(30) CONSTRAINT
student_s_last_nn NOT NULL;

CONSTRAINT my_students_s_dob_nn NOT NULL;
IS421 DB Applications
Mr. Ashraf Yaseen
46
Types of Value Constraints (cont.)

Unique




Table constraint
Specifies that a non-primary key field must have a unique
value
CONSTRAINT term_term_desc_uk UNIQUE (term_desc);
Default: specifies a default value that is inserted
automatically


Must be created in the column declaration
s_state CHAR(2) DEFAULT ‘WI’;
IS421 DB Applications
Mr. Ashraf Yaseen
47
Summary on Value Constraints (cont.)




Check conditions: field value must be a specific
value or fall within a range of values
NOT NULL constraints: specify whether a field
value can be NULL
Default constraints: specify that a field has a
default value that the DBMS automatically inserts
for every record, unless the user specifies an
alternate value
Unique constraints: specify that a field must have
a unique value for every table record
IS421 DB Applications
Mr. Ashraf Yaseen
48
Creating Database Tables Using
SQL*Plus

Type SQL commands at the SQL prompt

End each command with a semicolon (;)

Not case sensitive
IS421 DB Applications
Mr. Ashraf Yaseen
49
Log On to SQL*Plus
Oraclelab
IS421 DB Applications
Mr. Ashraf Yaseen
50
Create a Table
IS421 DB Applications
Mr. Ashraf Yaseen
51
Using Notepad

Useful to use Notepad to edit sql
commands
Commands can be edited without
retyping
 Commands can be saved
 Saving multiple sql commands in a
file creates a script

IS421 DB Applications
Mr. Ashraf Yaseen
52
Study …
Can you create TABLE student now?
IS421 DB Applications
Mr. Ashraf Yaseen
53
Table Creation Sequence

When creating tables with foreign key
references, create referenced tables
first

Always create tables without foreign
keys before those with foreign keys
IS421 DB Applications
Mr. Ashraf Yaseen
54
Errors - Oracle Help Resources

When an error occurs error information is
displayed:





Line number
Position on line
Error code
Description of error
Error codes



3 letter prefix (I.e. ORA)
5 digit code
More information on errors can be found at
http://otn.oracle.com
IS421 DB Applications
Mr. Ashraf Yaseen
55
Composite Primary Keys

Syntax:
CONSTRAINT constraint_name
PRIMARY KEY (field1, field2)

Must be defined after fields that composite key are
defined
CREATE TABLE ENROLLMENT
(s_id NUMBER(6),
c_sec_id NUMBER(6),
grade CHAR(1),
CONSTRAINT enrollment_pk PRIMARY KEY (s_id, c_sec_id),
CONSTRAINT enrollment_sid_fk FOREIGN KEY (s_id)
REFERENCES student(s_id),
CONSTRAINT enrollment_csecid_fk FOREIGN KEY (c_sec_id)
REFERENCES course_section (c_sec_id));
IS421 DB Applications
Mr. Ashraf Yaseen
56
Integrity and Value Constraints: Other
Examples (cont.)
CREATE TABLE faculty
(f_id NUMBER(6) CONSTRAINT faculty_f_id_pk PRIMARY KEY,
f_last VARCHAR2(30) CONSTRAINT faculty_f_last_nn NOT NULL,
f_first VARCHAR2(30) CONSTRAINT faculty_f_first_nn NOT NULL,
f_mi CHAR(1),
loc_id NUMBER(5) CONSTRAINT faculty_loc_id_fk
REFERENCES location(loc_id),
f_phone VARCHAR2(10),
f_rank VARCHAR2(8) CONSTRAINT faculty_f_rank_cc
CHECK ((f_rank = ’INST') OR (f_rank = ’ASST')
OR (f_rank = 'ASSO') OR (f_rank = ’FULL')),
f_pin NUMBER(4) CONSTRAINT faculty_f_pin_uk UNIQUE,
f_image BLOB);
IS421 DB Applications
Mr. Ashraf Yaseen
57
Exiting SQL*Plus

Three ways to exit SQL*Plus:




Type exit at the SQL prompt
Click File on the menu bar, and then click Exit
Click the Close button on the program window title
bar
Database session ends when SQL*Plus exits
IS421 DB Applications
Mr. Ashraf Yaseen
58
Viewing Information About Tables

describe tablename: displays short description about
tablename; column names, NOT NULL columns and data types.

Data dictionary: tables that contain information about the
structure of the database.
 When the DBA creates a new DB, the system creates the data
dictionary in a user schema named SYS.
 Oracle10g DBMS automatically updates the data dictionary
tables as users create, update, or delete database objects.
 Users do not directly interact with data dictionary tables,
 They interact with the data dictionary views.

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 doesn’t have privileges
to view.
IS421 DB Applications
Mr. Ashraf Yaseen
59
Viewing Information About Tables

Categories of Data Dictionary Views:




USER: shows the objects in the current user’s schema
ALL: shows both objects in the current user’s schema
and objects that the user has privileges to manipulate
DBA: allows users who are database administrators to
view information about all database objects
General command:
SELECT view_columnname1, view_columnname2, …
FROM prefix_object;
IS421 DB Applications
Mr. Ashraf Yaseen
60
Database Objects with Data Dictionary View
Object Name
Object Type
OBJECTS
All database objects
TABLES
Database tables
INDEXES
Table indexes created to improve query
performance
VIEWS
Database views
SEQUENCES
Sequences created to automatically
generate surrogate key values
USERS
Database users
CONSTRAINTS
Table constraints
CONS_COLUMNS
Table columns that have constraints
IND_COLUMNS
Indexed columns
TAB_COLUMNS
All table columns
IS421 DB Applications
Mr. Ashraf Yaseen
61
Viewing Tables in the Database
IS421 DB Applications
Mr. Ashraf Yaseen
62
Viewing Constraints on One Table
IS421 DB Applications
Mr. Ashraf Yaseen
63
Modifying and Deleting Database
Tables

Modify existing database tables by:






Changing the name of a table
Adding new columns
Deleting columns that are no longer needed
Changing the data type or maximum size of an existing
column
Unrestricted action: some specifications can always
be modified
Restricted action: specifications modified only in
certain situations
IS421 DB Applications
Mr. Ashraf Yaseen
64
Deleting and Renaming Tables

To delete:




DROP TABLE [tablename]
Use with caution
To delete foreign key constraints, add
“CASCADE CONSTRAINTS”
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.
IS421 DB Applications
Mr. Ashraf Yaseen
65
Adding Columns to Existing Tables

To add a field:
ALTER TABLE tablename
ADD(fieldname data_declaration constraints);
Example:
ALTER TABLE faculty
ADD (start_date DATE);
IS421 DB Applications
Mr. Ashraf Yaseen
66
Modifying Existing Column Data
Definitions

Can only change datatype to compatible data
type (i.e. varchar2 to char)
ALTER tablename
MODIFY(fieldname new_data_declaration);
Example:
ALTER TABLE faculty
MODIFY (f_rank VARCHAR2 (4));
IS421 DB Applications
Mr. Ashraf Yaseen
67
Deleting a Column

To delete a field
ALTER TABLE tablename
DROP COLUMN fieldname;
Examples:
ALTER TABLE faculty
DROP COLUMN f_rank;
ALTER TABLE faculty
ADD (faculty_rank VARCHAR2(4));
IS421 DB Applications
Mr. Ashraf Yaseen
68
Renaming a Column

To rename a field
ALTER TABLE tablename
RENAME COLUMN old_fieldname TO new_fieldname;
IS421 DB Applications
Mr. Ashraf Yaseen
69
Adding and Deleting Constraints

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;
IS421 DB Applications
Mr. Ashraf Yaseen
70
Enabling and Disabling Constraints



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;
ALTER TABLE faculty
DISABLE CONSTRAINT faculty_loc_id_fk;
ALTER TABLE faculty
ENABLE CONSTRAINT faculty_loc_id_fk;
IS421 DB Applications
Mr. Ashraf Yaseen
DROP TABLE faculty
CASCADE CONSTRAINTS;
EXIT;
71
SQL Commands



CREATE TABLE <tablename> … ;
DROP TABLE <tablename>;
RENAME <tablename>TO<newtablename>;
Use the following commands to check your tables
 SELECT table_name FROM user_tables;
 DESCRIBE <tablename>;
IS421 DB Applications
Mr. Ashraf Yaseen
72
SQL Commands


SELECT constraint_name
FROM user_constraints;
SELECT constraint_name
FROM user_constraints
WHERE TABLE_NAME =‘<tablename>’;
‘case sensitive’ within the quotation
IS421 DB Applications
Mr. Ashraf Yaseen
73
SQL Commands (cont.)
ALTER TABLE <tablename>
ADD <fieldname> <data declaration>;
 ALTER TABLE <tablename>
ADD/MODIFY <fieldname> <data declaration>
CONSTRAINT <integrity constraints>
CONSTRAINT <value constraints>;


EXIT; or QUIT;
IS421 DB Applications
Mr. Ashraf Yaseen
74
Summary

SQL commands include:



To create a table:




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
Data types ensure correct data values
Constraints restrict values of database fields
SQL*Plus commands are not case sensitive
IS421 DB Applications
Mr. Ashraf Yaseen
75
Summary (cont.)



Errors include line number, position,
error code
Use DESCRIBE command to display a
table’s fieldnames and data types
Tables can be modified or deleted but
some changes are restricted
IS421 DB Applications
Mr. Ashraf Yaseen
76