Oracle: Data Definition Language (DDL)
Oracle: Data Definition Language (DDL)
1
2
Data Definition Language (DDL) is a standard for
commands that define the different structures in a
database.
3
A table is an object that can store data in an Oracle
database.
4
Naming rules & conventions
The name for a table must follow these standard rules:
The name must begin with a letter A-Z or a-z
Can contain numbers
A name can consist of any combination of letters (A to Z a
to z), decimal digits (0 to 9), $, #, @, or underscore (_)
Can be in UPPER OR LOWER case
Can be up to 30 characters in length
Cannot use the same name of another existing object in
your schema
Spaces & hyphens are not allowed in the table or column
name
Must not be a SQL reserved word
5
VALID INVALID
STUDENT SUTDENT_COURSE_REGISTRATION_TABLE
(more than 30 char)
6
When a table is created, each column in the table is
assigned a data type.
7
The VARCHAR2 type is a character data type to store
variable-length alphanumeric data in a column.
If the data are smaller than the specified size, only the data
value is stored, and trailing spaces are not added to the
value.
8
The CHAR type is a character data type to store
fixed-length alphanumeric data in a column.
9
• The NUMBER data type is used to store negative,
positive, integer, fixed-decimal, and floating-point
numbers.
10
An integer is a whole number without any decimal
part.
11
Decimal number has a specific number of digits to
the right of the decimal point.
12
A floating-point decimal number has a variable
number of decimal places
13
The date data type is used for storing date and time.
Format
◦ DD-MON-YY
◦ DD-MON-YYYY
◦ TO_DATE function (convert to date format)
◦ Hour format
HH:MM:SS A.M
14
15
It enforces rules on tables.
The constraints helps to make database one with
integrity.
16
Oracle provides with different constraints
◦ to specify a primary or a composite key for the table,
◦ to define a foreign key in a table that references a primary
key in another table,
◦ to set data validation rules for each column (Check)
◦ to specify whether a column allows NULL values
(Not NULL)
◦ and to specify if a column should have unique values only.
(Unique)
17
There are two types of constraints:
1. Integrity constraints:
Defined by creating both the primary key and the foreign
key with the table and primary key it references.
2. Value constraints:
Define if NULL values are disallowed,
if UNIQUE values are required, and
if only certain set of values are allowed in a column.
18
The general convention used for naming constraints is
19
1) a constraint name
emp_ deptno_fk refers to:
2)constraint name
dept_deptno_pk
20
Primary Key pk
Foreign Key fk
Unique uk
Check ck
Not Null nn
21
A constraint can be created at the same time the table is
created, or it can be added to the table afterward.
Column level.
Table level.
22
A column-level constraint references a single column and is defined
along with the definition of the column.
Example:
building VARCHAR2(7) CONSTRAINT location_building_nn NOT
NULL
23
A table-level constraint references one or more columns and is
defined separately from the definitions of the columns.
All constraints can be defined at the table level except for the
NOT NULL constraint.
24
a column level constraint has scope only to the column it
is defined on.
A table level constraint can see every column in the
table.
Any column level constraint (exception: not null) can be
expressed at the table level - but the opposite is not true.
25
The PRIMARY KEY constraint is also known as the
entity integrity constraint
26
At the column level, the constraints is defined by
DeptId NUMBER (2) CONSTRAINT dept_deptid_pk PRIMARY KEY,
27
• The FOREIGN KEY constraint is also known as the
referential integrity constraint.
28
To establish a foreign key in a table, the other
referenced table and its primary key must already
exist.
29
The NOT NULL constraint ensures that the column
has a value and the value is not a null value
A space or a numeric zero is not a null value.
This constraints cannot be entered at the table level.
At the column level ONLY, the constraint is defined
by:
30
The UNIQUE constraint requires that every value in a
column or set of columns be unique.
31
The PRIMARY Key and UNIQUE Key constraints, both are similar
and enforce uniqueness of the column on which they are defined.
Primary Key
Primary key cannot have a NULL value.
Each table can have only one primary key.
Primary key can be related to another tables as a Foreign Key.
We can generate ID automatically with the help of Auto Increment
field. Primary key supports Auto Increment value.
We can't delete primary key value from the parent table which is
used as a foreign key in child table. To delete we first need to delete
that primary key value from the child table.
32
Unique Key
Unique Constraint may have a NULL value.
Each table can have more than one Unique
Constraint.
Unique Constraint can not be related with another
value.
33
• The CHECK constraint defines a condition that every row
must satisfy
34
• It can be declared as CHECK constraints either at
column or table level.
• EX:
Name VARCHAR2(15) CONSTRAINT Faculty_name_ck
CHECK(Name IS NOT NULL),
35
36
• A user creates an oracle table in the SQL *Plus environment.
37
SQL>CREATE TABLE student
(Student ID CHAR (5),
Last VARCHAR2 (15) CONSTRAINT student_last_nn NOT NULL,
First VARCHAR2 (15) CONSTRAINT student_first_nn NOT NULL,
Street VARCHAR2 (25),
City VARCHAR2 (15),
State CHAR (2) DEFAULT 'NJ',
Zip CHAR (5),
StartTerm CHAR (4),
BirthDate DATE,
FacultyId NUMBER (3),
MajorId NUMBER (3),
Phone CHAR (10),
CONSTRAINT student_studentid_pk PRIMARY KEY (StudentID));
38
When a user creates a table or many tables in the
database, Oracle tracks them using its own data dictionary
39
Data dictionary views come in three forms
USER_ [View] – Eg USER_TABLES
ALL_[View] – Eg ALL_TABLES
DBA_[View] – Eg DBA_TABLES
40
USER* Views
USER_*: Views that start with USER_ list only the
41
To find the list of tables in current schema that have the
column “EMPLOYEE_NUMBER”
SELECT * FROM user_tab_columns
WHERE column_name = 'EMPLOYEE_NUMBER‘
42
To find the list of tables across schemas that have the
column “EMPLOYEE_NUMBER”
SELECT * FROM dba_tab_columns
WHERE column_name = 'EMPLOYEE_NUMBER'
43
• The SQL*Plus command to view a table’s structure is
DESCRIBE, which does not need a semicolon at the end
because it is not a SQL statement.
44
Name NULL? TYPE
STUDENTID NOT NULL CHAR(5)
LAST NOT NULL VARCHAR2(15)
FIRST NOT NULL VARCHAR2(15)
STREET VARCHAR2(25)
CITY VARCHAR2(15)
STATE VARCHAR2(20)
ZIP
STARTTERM
BIRTHDATE
FACULTYID
MAJORID
PHONE
45
46
The general syntax to add a column to an
existing table is
ALTER TABLE tablename ADD columnname
datatype;
SQL> ALTER TABLE student
2 ADD SocialSecurity CHAR(9);
Table altered.
SQL>
47
The general syntax to modify an existing column is
ALTER TABLE tablename
MODIFY columnname newdatatype;
where newdatatype is the new data type or the new
size for the column.
48
The general syntax to drop a column:
alter table table_name drop column col_name1; -- drop
one column
alter table table_name drop (col_name1, col_name2); --
drop many columns
Example:
alter table employee drop column emp_doj;
49
The following statement is used to rename a table
50
Syntax:
DELETE FROM table_name [WHERE condition];
NOTE:
The WHERE clause in the sql delete command is
51
To delete an employee with id 100 from the employee
table, the sql delete query would be like,
52
The SQL DROP command is used to remove an object
from the database.
53
The general syntax is
For example,
DROP TABLE sample;
Oracle displays a “Table dropped” message when a
table is successfully dropped.
If you add optional CASCADE CONSTRAINTS
clause, it removes foreign key references to the table
also.
54
The SQL TRUNCATE command is used to delete all
the rows from the table and free the space containing
the table.
Syntax :
TRUNCATE TABLE table_name;
Example
To delete all the rows from employee table, the query
would be like,
TRUNCATE TABLE employee;
55
• During a session user can redirect all statements
,queries ,commands and results to a file for later
review or printout.
are used
SQL>SPOOL filename
SQL>SPOOL OFF
56
• If oracle Error Help is installed on the system, then
follow the steps below
Start Oracle OraHome92.
• This help screen allows the user to find out the cause of
the error and the action required to rectify it
57