SlideShare a Scribd company logo
1 of 29
Download to read offline
Copyright © 2009, Oracle. All rights reserved.
Using DDL Statements
to Create and Manage Tables
Copyright © 2009, Oracle. All rights reserved.
9 - 2
Objectives
After completing this lesson, you should be able to do the
following:
• Categorize the main database objects
• Review the table structure
• List the data types that are available for columns
• Create a simple table
• Explain how constraints are created at the time of table
creation
• Describe how schema objects work
Copyright © 2009, Oracle. All rights reserved.
9 - 3
Object Description
Table Basic unit of storage; composed of rows
View Logically represents subsets of data from one or
more tables
Sequence Generates numeric values
Index Improves the performance of some queries
Synonym Gives alternative names to objects
Database Objects
Copyright © 2009, Oracle. All rights reserved.
9 - 4
 A table is a database object used to store data.
 Data in a table is organized in rows and columns.
 Each row in a table represents a unique record and each
column represents an attribute of the record.
Managing Tables
As a database developer, you
need to create and manage
tables to store data.
Copyright © 2009, Oracle. All rights reserved.
9 - 5
• You must have:
– The CREATE TABLE privilege
– A storage area
• In the syntax:
Schema : Is the same as the owner’s name
table : Is the name of the table
DEFAULT expr: Specifies a default value if a value is
omitted in the INSERT statement
Column : Is the name of the column
datatype : Is the column’s data type and length
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr][, ...]);
CREATE TABLE Statement
Copyright © 2009, Oracle. All rights reserved.
9 - 6
Table names and column names:
• Must begin with a letter
• Must be 1–30 characters long
• Must contain only A–Z, a–z, 0–9, _, $, and #
• Must not duplicate the name of another object owned by
the same user
• Must not be an Oracle server–reserved word
Use descriptive names for tables and other database objects.
Naming Rules
Naming Guidelines
Note
Names are case-insensitive. For example, EMPLOYEES is treated
as the same name as eMPloyees or eMpLOYEES.
Copyright © 2009, Oracle. All rights reserved.
9 - 7
Data Type Description
VARCHAR2(size) Variable-length character data
CHAR(size) Fixed-length character data
NUMBER(p,s) Variable-length numeric data
DATE Date and time values
LONG Variable-length character data (up to 2 GB)
CLOB Character data (up to 4 GB)
RAW and LONG
RAW
Raw binary data
BLOB Binary data (up to 4 GB)
BFILE Binary data stored in an external file (up to 4 GB)
ROWID A base-64 number system representing the unique
address of a row in its table
Data Types
Copyright © 2009, Oracle. All rights reserved.
9 - 8
Datetime Data Types
You can use several datetime data types:
Data Type Description
TIMESTAMP Date with fractional seconds
INTERVAL YEAR TO
MONTH
Stored as an interval of years
and months
INTERVAL DAY TO
SECOND
Stored as an interval of days, hours, minutes,
and seconds
Copyright © 2009, Oracle. All rights reserved.
9 - 9
Datetime Data Types
• The TIMESTAMP data type is an extension of the DATE
data type.
• It stores the year, month, and day of the DATE data type
plus hour, minute, and second values as well as the
fractional second value.
• You can optionally specify the time zone.
TIMESTAMP[(fractional_seconds_precision)]
TIMESTAMP[(fractional_seconds_precision)]
WITH TIME ZONE
TIMESTAMP[(fractional_seconds_precision)]
WITH LOCAL TIME ZONE
Copyright © 2009, Oracle. All rights reserved.
9 - 10
Datetime Data Types
• The INTERVAL YEAR TO MONTH data type stores a
period of time using the YEAR and MONTH datetime fields:
• The INTERVAL DAY TO SECOND data type stores a
period of time in terms of days, hours, minutes, and
seconds:
INTERVAL YEAR [(year_precision)] TO MONTH
INTERVAL DAY [(day_precision)]
TO SECOND [(fractional_seconds_precision)]
Copyright © 2009, Oracle. All rights reserved.
9 - 13
Referencing Another User’s Tables
• Tables belonging to other users are not in the user’s schema.
• You should use the owner’s name as a prefix to those tables.
USERB
USERA
SELECT *
FROM userB.employees;
SELECT *
FROM userA.employees;
Copyright © 2009, Oracle. All rights reserved.
9 - 14
• Consider the following Student table.
Columns Data Type
Sno Number(5)
Sname Varchar2(12)
Course Varchar2(10)
Doj Date
Fees Number(8,2)
Creating a Table
Copyright © 2009, Oracle. All rights reserved.
9 - 15
• You can use the following statement to create the Student table:
The above create statement creates a table named Student where all the
columns can store null values.
Creating a Table (Contd.)
CREATE TABLE Student(
Sno Number(5),
Sname varchar2(12),
Course varchar2(10),
Doj Date,
Fees Number(8,2)
);
• Use the following command to view the structure of Student table:
DESC [RIBE] Student
Copyright © 2009, Oracle. All rights reserved.
9 - 16
• Specify a default value for a column during an insert.
• The default data type must match the column data type.
... JoiningDate DATE DEFAULT SYSDATE, ...
CREATE TABLE hire_dates(
id NUMBER(8),
hire_date DATE DEFAULT SYSDATE
);
CREATE TABLE succeeded.
DEFAULT Option
Copyright © 2009, Oracle. All rights reserved.
9 - 17
• Consider the following EmployeeLeave table.
Columns DataType Default
EmployeeID Number(5)
LeaveStartDate Date SYSDATE
LeaveEndDate Date
LeaveReason Varchar2(15)
LeaveType Char(2) CL
Creating a Table (Contd.)
Copyright © 2009, Oracle. All rights reserved.
9 - 18
Creating a Table (Contd.)
CREATE TABLE EmployeeLeave(
EmployeeID Number(5),
LeaveStartDate Date DEFAULT SYSDATE,
LeaveEndDate Date,
LeaveReason Varchar2(15)
LeaveType Char(2) DEFAULT ‘CL’
);
DESCRIBE EmployeeLeave
• Create the table.
• Confirm table creation.
Copyright © 2009, Oracle. All rights reserved.
9 - 19
• Create a table and insert rows by combining the
CREATE TABLE statement and the AS subquery option.
CREATE TABLE table_name
[(column, column...)]
AS subquery;
Creating a Table by Using a Subquery
• Match the number of specified columns to the number of
subquery columns.
• Define columns with column names and default values.
Copyright © 2009, Oracle. All rights reserved.
9 - 20
CREATE TABLE Dept30
AS
SELECT EmpNo, EmpName,
Sal*12 ANNSAL,
Hire_Date
FROM Emp
WHERE DeptNo = 30;
CREATE TABLE Succeeded.
DESCRIBE Dept30
Creating a Table by Using a Subquery
Copyright © 2009, Oracle. All rights reserved.
9 - 21
Use the ALTER TABLE statement to:
• Add a new column
• Modify an existing column
• Define a Default value for the new column
• Drop a column
ALTER TABLE Statement
After you create a table, you may need to change the table
structure for any of the following reasons:
 You omitted a column.
 Your column definition needs to be changed.
 You need to remove columns.
Copyright © 2009, Oracle. All rights reserved.
9 - 22
Adding New Columns
ALTER TABLE tablename
ADD (column datatype [DEFAULT expr] [, column datatype]...);
Modifying Columns
ALTER TABLE tablename
MODIFY (column datatype [DEFAULT expr] [, column datatype]...);
Dropping Columns
ALTER TABLE tablename
DROP (column_name); (or) DROP COLUMN column_name;
Copyright © 2009, Oracle. All rights reserved.
9 - 23
Adding a New Column
ALTER TABLE Student
ADD ( Address Varchar2(15) );
• If a table already contains rows when a column is added, then the
new column is initially null for all the rows.
DESC Student
• You can add or modify columns.
• You Cannot specify where the column is to appear. The new column becomes
the last column.
Guidelines
Note
Confirmation
Copyright © 2009, Oracle. All rights reserved.
9 - 24
ALTER TABLE Student
MODIFY( Address Varchar2(25) );
• You can increase the width or precision of a numeric / character column.
• You can decrease the width of a column only if the column contains only null
values or if the table has no rows.
• You can change the data type only if the column contains null values.
• A change to the default value of a column affects only subsequent insertions
to the table.
Modifying a Column
• You can change a column’s data type, size, and default value.
ALTER TABLE Student
MODIFY Doj Date DEFAULT SYSDATE;
Guidelines
Copyright © 2009, Oracle. All rights reserved.
9 - 25
Dropping a Column
ALTER TABLE Student
DROP COLUMN Address;
SELECT * FROM TAB;
• The column may or may not contain data.
• Only one column can be dropped at a time.
• The table must have at least one column remaining in it after it is altered.
• Once a column is dropped, it cannot be recovered.
Guidelines
Confirmation
ALTER TABLE Student
DROP ( Address );
Copyright © 2009, Oracle. All rights reserved.
9 - 26
RENAME Statement
• To change the name of a table, view, sequence, or synonym
RENAME old_name to new_name
Renaming a Table
RENAME Student to StudentInf;
Note
• You must be the owner of the object.
SELECT * FROM TAB;
Confirmation
Copyright © 2009, Oracle. All rights reserved.
9 - 27
TRUNCATE Statement
• Removes all rows from a table.
• Release the storage space used by that table.
TRUNCATE TABLE table_name;
Truncating a Table
TRUNCATE TABLE Student;
Note
• You cannot ROLLBACK the rows
SELECT * FROM Student;
Confirmation
Copyright © 2009, Oracle. All rights reserved.
9 - 28
DROP Statement
• To remove a table, view, sequence, or synonym
DROP TABLE table_name;
• The DROP TABLE statement removes the definition of an Oracle table.
Dropping a Table
DROP TABLE Student;
• When you drop a table, the database loses all the data in the table and
all the indexes associated with it.
• All data is delete from the table.
• Any views and synonyms remain but are invalid.
• Any pending transactions are committed.
Guidelines
Copyright © 2009, Oracle. All rights reserved.
9 - 29
Exercise
• Create StudentDetails Table.
Columns Data Type
Sno Number(5)
Sname Varchar2(12)
Addr Varchar2(15)
Mat Number(3)
Phy Number(3)
Total Number(3)
Avrg Number(3)
 Add columns
Result -- varchar2(5)
Grade -- char(1)
 Modify columns
Avrg -- number(6,2)
 Drop column Addr
 Rename the table to StudentInf
 Drop the Table
Modifications:
Copyright © 2009, Oracle. All rights reserved.
9 - 30
Exercise
• Create ProductDetails Table.
Columns Data Type
ProdCode Number(5)
ProdName Varchar2(12)
Price Number(10,2)
Qty Number(4)
Total Number(10,2)
Copyright © 2009, Oracle. All rights reserved.
9 - 31
Exercise
• Create EmployeeDetails Table.
Columns Data Type Description
Empno Number(5) Employee ID
Ename Varchar2(12) Employee Name
Job Varchar2(10) Nature of Job
Bsal Number(8,2) Basic Salary
DA Number(8,2) Dearness Allowance
TA Number(8,2) Travelling Allowance
HRA Number(8,2) House Rent Allowance
PF Number(8,2) Provident Fund
Gsal Number(8,2) Gross Salary
Nsal Number(8,2) Net Salary

More Related Content

Similar to DDL. data defination language for creating database

Similar to DDL. data defination language for creating database (20)

Less08_Schema Advanced Databases and Management.pptx
Less08_Schema Advanced Databases and Management.pptxLess08_Schema Advanced Databases and Management.pptx
Less08_Schema Advanced Databases and Management.pptx
 
Les09
Les09Les09
Les09
 
Les09
Les09Les09
Les09
 
Les20[1]Working with Composite Datatypes
Les20[1]Working with Composite DatatypesLes20[1]Working with Composite Datatypes
Les20[1]Working with Composite Datatypes
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
Using ddl statements to create and manage tables
Using ddl statements to create and manage tablesUsing ddl statements to create and manage tables
Using ddl statements to create and manage tables
 
SQL
SQLSQL
SQL
 
SQL DDL
SQL DDLSQL DDL
SQL DDL
 
SQL WORKSHOP::Lecture 10
SQL WORKSHOP::Lecture 10SQL WORKSHOP::Lecture 10
SQL WORKSHOP::Lecture 10
 
Less07 schema
Less07 schemaLess07 schema
Less07 schema
 
Les10
Les10Les10
Les10
 
Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
 
Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
 
Les09
Les09Les09
Les09
 
Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
 
Lab
LabLab
Lab
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
 
e computer notes - Creating and managing tables
e computer notes -  Creating and managing tablese computer notes -  Creating and managing tables
e computer notes - Creating and managing tables
 
Manage schema object.ppt
Manage schema object.pptManage schema object.ppt
Manage schema object.ppt
 

Recently uploaded

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Recently uploaded (20)

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 

DDL. data defination language for creating database

  • 1. Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables
  • 2. Copyright © 2009, Oracle. All rights reserved. 9 - 2 Objectives After completing this lesson, you should be able to do the following: • Categorize the main database objects • Review the table structure • List the data types that are available for columns • Create a simple table • Explain how constraints are created at the time of table creation • Describe how schema objects work
  • 3. Copyright © 2009, Oracle. All rights reserved. 9 - 3 Object Description Table Basic unit of storage; composed of rows View Logically represents subsets of data from one or more tables Sequence Generates numeric values Index Improves the performance of some queries Synonym Gives alternative names to objects Database Objects
  • 4. Copyright © 2009, Oracle. All rights reserved. 9 - 4  A table is a database object used to store data.  Data in a table is organized in rows and columns.  Each row in a table represents a unique record and each column represents an attribute of the record. Managing Tables As a database developer, you need to create and manage tables to store data.
  • 5. Copyright © 2009, Oracle. All rights reserved. 9 - 5 • You must have: – The CREATE TABLE privilege – A storage area • In the syntax: Schema : Is the same as the owner’s name table : Is the name of the table DEFAULT expr: Specifies a default value if a value is omitted in the INSERT statement Column : Is the name of the column datatype : Is the column’s data type and length CREATE TABLE [schema.]table (column datatype [DEFAULT expr][, ...]); CREATE TABLE Statement
  • 6. Copyright © 2009, Oracle. All rights reserved. 9 - 6 Table names and column names: • Must begin with a letter • Must be 1–30 characters long • Must contain only A–Z, a–z, 0–9, _, $, and # • Must not duplicate the name of another object owned by the same user • Must not be an Oracle server–reserved word Use descriptive names for tables and other database objects. Naming Rules Naming Guidelines Note Names are case-insensitive. For example, EMPLOYEES is treated as the same name as eMPloyees or eMpLOYEES.
  • 7. Copyright © 2009, Oracle. All rights reserved. 9 - 7 Data Type Description VARCHAR2(size) Variable-length character data CHAR(size) Fixed-length character data NUMBER(p,s) Variable-length numeric data DATE Date and time values LONG Variable-length character data (up to 2 GB) CLOB Character data (up to 4 GB) RAW and LONG RAW Raw binary data BLOB Binary data (up to 4 GB) BFILE Binary data stored in an external file (up to 4 GB) ROWID A base-64 number system representing the unique address of a row in its table Data Types
  • 8. Copyright © 2009, Oracle. All rights reserved. 9 - 8 Datetime Data Types You can use several datetime data types: Data Type Description TIMESTAMP Date with fractional seconds INTERVAL YEAR TO MONTH Stored as an interval of years and months INTERVAL DAY TO SECOND Stored as an interval of days, hours, minutes, and seconds
  • 9. Copyright © 2009, Oracle. All rights reserved. 9 - 9 Datetime Data Types • The TIMESTAMP data type is an extension of the DATE data type. • It stores the year, month, and day of the DATE data type plus hour, minute, and second values as well as the fractional second value. • You can optionally specify the time zone. TIMESTAMP[(fractional_seconds_precision)] TIMESTAMP[(fractional_seconds_precision)] WITH TIME ZONE TIMESTAMP[(fractional_seconds_precision)] WITH LOCAL TIME ZONE
  • 10. Copyright © 2009, Oracle. All rights reserved. 9 - 10 Datetime Data Types • The INTERVAL YEAR TO MONTH data type stores a period of time using the YEAR and MONTH datetime fields: • The INTERVAL DAY TO SECOND data type stores a period of time in terms of days, hours, minutes, and seconds: INTERVAL YEAR [(year_precision)] TO MONTH INTERVAL DAY [(day_precision)] TO SECOND [(fractional_seconds_precision)]
  • 11. Copyright © 2009, Oracle. All rights reserved. 9 - 13 Referencing Another User’s Tables • Tables belonging to other users are not in the user’s schema. • You should use the owner’s name as a prefix to those tables. USERB USERA SELECT * FROM userB.employees; SELECT * FROM userA.employees;
  • 12. Copyright © 2009, Oracle. All rights reserved. 9 - 14 • Consider the following Student table. Columns Data Type Sno Number(5) Sname Varchar2(12) Course Varchar2(10) Doj Date Fees Number(8,2) Creating a Table
  • 13. Copyright © 2009, Oracle. All rights reserved. 9 - 15 • You can use the following statement to create the Student table: The above create statement creates a table named Student where all the columns can store null values. Creating a Table (Contd.) CREATE TABLE Student( Sno Number(5), Sname varchar2(12), Course varchar2(10), Doj Date, Fees Number(8,2) ); • Use the following command to view the structure of Student table: DESC [RIBE] Student
  • 14. Copyright © 2009, Oracle. All rights reserved. 9 - 16 • Specify a default value for a column during an insert. • The default data type must match the column data type. ... JoiningDate DATE DEFAULT SYSDATE, ... CREATE TABLE hire_dates( id NUMBER(8), hire_date DATE DEFAULT SYSDATE ); CREATE TABLE succeeded. DEFAULT Option
  • 15. Copyright © 2009, Oracle. All rights reserved. 9 - 17 • Consider the following EmployeeLeave table. Columns DataType Default EmployeeID Number(5) LeaveStartDate Date SYSDATE LeaveEndDate Date LeaveReason Varchar2(15) LeaveType Char(2) CL Creating a Table (Contd.)
  • 16. Copyright © 2009, Oracle. All rights reserved. 9 - 18 Creating a Table (Contd.) CREATE TABLE EmployeeLeave( EmployeeID Number(5), LeaveStartDate Date DEFAULT SYSDATE, LeaveEndDate Date, LeaveReason Varchar2(15) LeaveType Char(2) DEFAULT ‘CL’ ); DESCRIBE EmployeeLeave • Create the table. • Confirm table creation.
  • 17. Copyright © 2009, Oracle. All rights reserved. 9 - 19 • Create a table and insert rows by combining the CREATE TABLE statement and the AS subquery option. CREATE TABLE table_name [(column, column...)] AS subquery; Creating a Table by Using a Subquery • Match the number of specified columns to the number of subquery columns. • Define columns with column names and default values.
  • 18. Copyright © 2009, Oracle. All rights reserved. 9 - 20 CREATE TABLE Dept30 AS SELECT EmpNo, EmpName, Sal*12 ANNSAL, Hire_Date FROM Emp WHERE DeptNo = 30; CREATE TABLE Succeeded. DESCRIBE Dept30 Creating a Table by Using a Subquery
  • 19. Copyright © 2009, Oracle. All rights reserved. 9 - 21 Use the ALTER TABLE statement to: • Add a new column • Modify an existing column • Define a Default value for the new column • Drop a column ALTER TABLE Statement After you create a table, you may need to change the table structure for any of the following reasons:  You omitted a column.  Your column definition needs to be changed.  You need to remove columns.
  • 20. Copyright © 2009, Oracle. All rights reserved. 9 - 22 Adding New Columns ALTER TABLE tablename ADD (column datatype [DEFAULT expr] [, column datatype]...); Modifying Columns ALTER TABLE tablename MODIFY (column datatype [DEFAULT expr] [, column datatype]...); Dropping Columns ALTER TABLE tablename DROP (column_name); (or) DROP COLUMN column_name;
  • 21. Copyright © 2009, Oracle. All rights reserved. 9 - 23 Adding a New Column ALTER TABLE Student ADD ( Address Varchar2(15) ); • If a table already contains rows when a column is added, then the new column is initially null for all the rows. DESC Student • You can add or modify columns. • You Cannot specify where the column is to appear. The new column becomes the last column. Guidelines Note Confirmation
  • 22. Copyright © 2009, Oracle. All rights reserved. 9 - 24 ALTER TABLE Student MODIFY( Address Varchar2(25) ); • You can increase the width or precision of a numeric / character column. • You can decrease the width of a column only if the column contains only null values or if the table has no rows. • You can change the data type only if the column contains null values. • A change to the default value of a column affects only subsequent insertions to the table. Modifying a Column • You can change a column’s data type, size, and default value. ALTER TABLE Student MODIFY Doj Date DEFAULT SYSDATE; Guidelines
  • 23. Copyright © 2009, Oracle. All rights reserved. 9 - 25 Dropping a Column ALTER TABLE Student DROP COLUMN Address; SELECT * FROM TAB; • The column may or may not contain data. • Only one column can be dropped at a time. • The table must have at least one column remaining in it after it is altered. • Once a column is dropped, it cannot be recovered. Guidelines Confirmation ALTER TABLE Student DROP ( Address );
  • 24. Copyright © 2009, Oracle. All rights reserved. 9 - 26 RENAME Statement • To change the name of a table, view, sequence, or synonym RENAME old_name to new_name Renaming a Table RENAME Student to StudentInf; Note • You must be the owner of the object. SELECT * FROM TAB; Confirmation
  • 25. Copyright © 2009, Oracle. All rights reserved. 9 - 27 TRUNCATE Statement • Removes all rows from a table. • Release the storage space used by that table. TRUNCATE TABLE table_name; Truncating a Table TRUNCATE TABLE Student; Note • You cannot ROLLBACK the rows SELECT * FROM Student; Confirmation
  • 26. Copyright © 2009, Oracle. All rights reserved. 9 - 28 DROP Statement • To remove a table, view, sequence, or synonym DROP TABLE table_name; • The DROP TABLE statement removes the definition of an Oracle table. Dropping a Table DROP TABLE Student; • When you drop a table, the database loses all the data in the table and all the indexes associated with it. • All data is delete from the table. • Any views and synonyms remain but are invalid. • Any pending transactions are committed. Guidelines
  • 27. Copyright © 2009, Oracle. All rights reserved. 9 - 29 Exercise • Create StudentDetails Table. Columns Data Type Sno Number(5) Sname Varchar2(12) Addr Varchar2(15) Mat Number(3) Phy Number(3) Total Number(3) Avrg Number(3)  Add columns Result -- varchar2(5) Grade -- char(1)  Modify columns Avrg -- number(6,2)  Drop column Addr  Rename the table to StudentInf  Drop the Table Modifications:
  • 28. Copyright © 2009, Oracle. All rights reserved. 9 - 30 Exercise • Create ProductDetails Table. Columns Data Type ProdCode Number(5) ProdName Varchar2(12) Price Number(10,2) Qty Number(4) Total Number(10,2)
  • 29. Copyright © 2009, Oracle. All rights reserved. 9 - 31 Exercise • Create EmployeeDetails Table. Columns Data Type Description Empno Number(5) Employee ID Ename Varchar2(12) Employee Name Job Varchar2(10) Nature of Job Bsal Number(8,2) Basic Salary DA Number(8,2) Dearness Allowance TA Number(8,2) Travelling Allowance HRA Number(8,2) House Rent Allowance PF Number(8,2) Provident Fund Gsal Number(8,2) Gross Salary Nsal Number(8,2) Net Salary