0% found this document useful (0 votes)
85 views57 pages

Oracle: Data Definition Language (DDL)

The document discusses Oracle's Data Definition Language (DDL) which defines database structures. DDL includes SQL statements that create or modify database objects like tables. When creating a table, its name, column names, data types, and sizes must be specified. Common data types include VARCHAR2, CHAR, NUMBER, and DATE. Tables can define constraints like primary keys, foreign keys, unique constraints, and NOT NULL constraints to enforce data integrity. Constraints can be defined at the column or table level.

Uploaded by

Stalin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views57 pages

Oracle: Data Definition Language (DDL)

The document discusses Oracle's Data Definition Language (DDL) which defines database structures. DDL includes SQL statements that create or modify database objects like tables. When creating a table, its name, column names, data types, and sizes must be specified. Common data types include VARCHAR2, CHAR, NUMBER, and DATE. Tables can define constraints like primary keys, foreign keys, unique constraints, and NOT NULL constraints to enforce data integrity. Constraints can be defined at the column or table level.

Uploaded by

Stalin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 57

Oracle: Data Definition Language (DDL)

1
2
 Data Definition Language (DDL) is a standard for
commands that define the different structures in a
database. 

 DDL refers to "Data Definition Language", a subset


of SQL statements that change the structure of the
database schema.

 Various data types are used in defining columns in a


database table.

3
 A table is an object that can store data in an Oracle
database.

 When you create a table, you must specify the


following things
The table name,
The name of each column,
The data type of each column,
The size of each column.

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)

MAJOR_CODE MAJOR CODE


(Space)
X CREATE (reserved word)

PROJECT2000 PROJECT*2000( special char)

STUDENT#REG #STUDENTREG ( start with letter)

6
 When a table is created, each column in the table is
assigned a data type.

 Some important data types:


◦ Varchar2
◦ Char
◦ Number
◦ Date

7
 The VARCHAR2 type is a character data type to store
variable-length alphanumeric data in a column.

 The size is specified within parentheses. for example,


VARCHAR2(20).

 If the data are smaller than the specified size, only the data
value is stored, and trailing spaces are not added to the
value.

 VARCHAR2 is the most appropriate type for a column


whose values do not have a fixed length.
 

8
 The CHAR type is a character data type to store
fixed-length alphanumeric data in a column.

 The CHAR data type uses the storage more


efficiently and processes data faster than the
VARCHAR2 type.

9
• The NUMBER data type is used to store negative,
positive, integer, fixed-decimal, and floating-point
numbers.

• When a number type is used for a column, its


precision and scale can be specified.

• Precision is the total number of significant digits


in the number, both to the left and to the right of
the decimal point.
• Scale is the total number of digits to the right of
the decimal point.

10
 An integer is a whole number without any decimal
part.

  The data type for it would be defined as NUMBER(3),


where 3 represents the maximum number of digits.

11
 Decimal number has a specific number of digits to
the right of the decimal point.

 The PRICE column has values in dollars and cents,


which requires two decimal places –

for example, values like 2.95, 3.99, 24.99

  If it is defined as NUMBER(4,2), the first number


specifies the precision and the second number the
scale.

12
 A floating-point decimal number has a variable
number of decimal places

 To define such a column, do not specify the scale or


precision along with the NUMBER type.

 By defining a column as a floating-point number, a


value can be stored in it with very high precision

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

<table name>_<column name>_<constraint type>

◦ table name is the name of the table where the constraint is


being defined,
◦ column name is the name of the column to which the
constraint applies,
◦ constraint type is an abbreviation used to identify the
constraint’s type.

19
1) a constraint name
emp_ deptno_fk refers to:

• a constraint in table EMP on column DeptNo of type


foreign key. 

2)constraint name
dept_deptno_pk

is for a primary key constraint in table DEPT on column


DeptNo.

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. 

 There are two levels where a constraint is defined:

 Column level.
 Table level.

22
 A column-level constraint references a single column and is defined
along with the definition of the column. 

 Any constraint can be defined at the column level except for a


FOREIGN KEY and COMPOSITE primary key constraints.

Column datatype [CONSTRAINT constraint_name] constraint_type

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.

 Normally, it is written after all columns are defined.

 All constraints can be defined at the table level except for the
NOT NULL constraint.

[CONSTRAINT constraint_name] constraint_typ (Column, .


. .),
Example:
CONSTRAINT location_roomid_pk PRIMARY
KEY(Roomid)

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. 

A column level constraint is syntactically more clear - it


is OBVIOUS it applies to that single column.
 It is more meaningful therefore.

25
 The PRIMARY KEY constraint is also known as the
entity integrity constraint

 It creates a primary key for the table. A table can have


only one primary key constraint. 

 If a table uses more than one column as its primary key


(i.e., a composite key), the key can only be declared at the
table level. 

26
 At the column level, the constraints is defined by
DeptId NUMBER (2) CONSTRAINT dept_deptid_pk PRIMARY KEY,

 At the table level, the constraint is defined by


CONSTRAINT dept_deptid_pk PRIMARY KEY(DeptId),

27
• The FOREIGN KEY constraint is also known as the
referential integrity constraint.

• It uses a column or columns as a foreign key, and it


establishes a relationship with the primary key of the
same or another table. 

28
 To establish a foreign key in a table, the other
referenced table and its primary key must already
exist. 

 Foreign key and referenced primary key columns need


not have the same name, but a foreign key value must
match the value in the parent table’s primary key value
or be NULL

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:

fName VARCHAR2(15) CONSTRAINT faculty_name_nn


NOT NULL,

30
 The UNIQUE constraint requires that every value in a
column or set of columns be unique.

 At the column level, the constraint is defined by:


DeptName VARCHAR2(12) CONSTRAINT
dept_deptname_uk UNIQUE,

 At the table level, the constraint is defined by


CONSTRAINT dept_deptname_uk UNIQUE(DeptName),

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

table's as a Foreign Key.


 Unique Constraint doesn't supports Auto Increment

value.

33
• The CHECK constraint defines a condition that every row
must satisfy

• At the column level, the constraint is defined by


DeptId NUMBER(2) CONSTRAINT dept_deptid_cc
CHECK((DeptId >= 10) and (DeptId <= 99)),

• At the table level, the constraint is defined by:


CONSTRAINT dept_deptid_cc
CHECK((DeptId >= 10) and (DeptId <= 99)),

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.

• It is created from the SQL> prompt


 
The general syntax of CREATE TABLE statement is

CREATE TABLE [schema.] tablename

(column1 datatype [CONSTRAINT constraint_name]


constraint_type . . .,

(column2 datatype [CONSTRAINT constraint_name] constraint_type,

[CONSTRAINT constraint_name] constraint_type (column, . . . ), . . . );

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

Viewing a User’s Table Names


SELECT TABLE_NAME FROM USER_TABLES;

To display all information:


SELECT * FROM USER_TABLES;

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

objects owned by the currently logged in user


 ALL* Views
 ALL_*: Views that start with ALL_ list only the

objects the currently logged in user has permissions to


access
 DBA* Views
 DBA_*: Views that start with DBA_ list all objects

unless restricted by the WHERE clause

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‘

 To find the list of tables in schema accessible to the


current user that have the column
“EMPLOYEE_NUMBER”
 SELECT * FROM all_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.

SQL> DESCRIBE student

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.

SQL> ALTER TABLE student


2 MODIFY SocialSecurity VARCHAR2(11);
Table altered.
SQL>

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

RENAME oldtablename TO tablenewname

EX: RENAME dept TO Department;

50
 Syntax:
 DELETE FROM table_name [WHERE condition];

 NOTE:
  The WHERE clause in the sql delete command is

optional and it identifies the rows in the column that


gets deleted.
 If you do not include the WHERE clause all the rows

in the table is deleted,

51
 To delete an employee with id 100 from the employee
table, the sql delete query would be like,

 DELETE FROM employee WHERE id = 100;

 To delete all the rows from the employee table, the


query would be like,

 DELETE FROM employee;

52
 The SQL DROP command is used to remove an object
from the database.

 If you drop a table, all the rows in the table is deleted


and the table structure is removed from the database

53
 The general syntax is

DROP TABLE tablename [CASCADE CONSTRAINTS];

 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.

• The spooling method creates a text file of all actions


and their results.
• To start and stop spooling ,the following commands

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

• To use online help type


Http://otn.oracle.com/pls/db92/db92.error_search

57

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy