0% found this document useful (0 votes)
25 views20 pages

07 Creating Managing Tables

Chapter 07 covers the creation and management of tables in Oracle SQL, detailing the Create Table statement, naming rules, data types, and constraints. It also explains how to alter tables, including adding, dropping, and renaming columns, as well as managing tables through renaming, truncating, and dropping. The chapter concludes with practical SQL exercises for users to apply their knowledge.

Uploaded by

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

07 Creating Managing Tables

Chapter 07 covers the creation and management of tables in Oracle SQL, detailing the Create Table statement, naming rules, data types, and constraints. It also explains how to alter tables, including adding, dropping, and renaming columns, as well as managing tables through renaming, truncating, and dropping. The chapter concludes with practical SQL exercises for users to apply their knowledge.

Uploaded by

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

Oracle SQL

07.Creating and Managing Tables


Eng:- Ahmed Ramadan , Eng:- Marwan Elkordey
Chapter 07 : Creating and Managing Tables

Chapter Content :

- Create Table Statement


- Naming Rules
- Data types
- Constraints
- Alter Table Statement
- Managing Tables
Rename table
Truncate table
Drop table
Chapter 07 : Creating and Managing Tables

Create Table
What is Table ? Columns
Two Dimensional Structure consists of rows and columns used to store data

Y ( rows ) Employee
Employee Number Employee Name Employee Job Salary
7788 SCOTT Analyst 200
Rows 7789 alen Sales 100

X ( Columns )
Chapter 07 : Creating and Managing Tables

Create Table Statement

Create table table_name


(
Column1_name Data_type [ Constraint ] ,
Column2_name Data_type [ Constraint ] ,
Column3_name Data_type [ Constraint ] ,
………

)
Chapter 07 : Creating and Managing Tables

Create Table Statement:

Naming Rules
1. Maximum number of Character = 30 ( length = 30 )
2. A-Z , a –z , 0-9 ( it must start with character )
3. # $ _
4. Can not be Oracle key word
Chapter 07 : Creating and Managing Tables

Create Table Statement:

Data Type
Number : Employee Number , id number
Salary , Quantity , Commission , Size , Weight , Height
Number ( n ) : Number (2) , Number ( 4 )  Number ( 38 ) = Integer , Number = Number (38 )
Number (m,n) : Number ( 7 , 2 )  9999.99
Characters : Name , Address , Job , Description , Location
Varchar2(n) : Varchar2(30)  varchar2(32000)
Char (m) : Char(30)  char(2000)

Date : Hiredate , Registration date , birthdate


date - ( dd/mm/yyyy)
Timestamp dd/mm/yyyy hh24:mi:ss LOB: Large Object Family
Timestamp with time zone dd/mm/yyyy hh24:mi:ss TZH:TZM -------------------------------------
CLOB : Character Large Object 4GB text
Image : employee_photo , product photo , …. BLOB : Binary Large Object 4GB images
Long Raw ( 2GB) Raw ( 1G) Bfile : Binary File 4GB Video
Chapter 07 : Creating and Managing Tables

Create Table Statement:


Constraints Constraint Constraint_name Constraint_Type

Constraint Types :
not null : Required ( Must be entered ) Ex., Constraint emp_name_nn not null
Check : Condition or Rule on your column Ex. Constraint emp_sal_ck check ( sal>0 )
unique : No Duplication in values Ex. Constraint emp_id_UQ Unique ( ID)
Foreign Key :
Primary Key :
Chapter 07 : Creating and Managing Tables

Create Table Statement:


Examples
create table dept
( deptno number(2) constraint dept_deptno_pk primary key ,
dname varchar2(100) constraint dept_dname_nn not null ,
loc varchar2(50)
);

create table emp2


( empno number(4) constraint emp_empno_pk primary key ,
ename varchar2(100) constraint emp_ename_nn not null ,
sal number(9,2) constraint emp_sal_ck check ( sal > 0) ,
job varchar2(200) ,
hiredate date ,
idno number(15) constraint emp_idno_uq unique ,
deptno number(2) constraint emp_dept_fk references dept(deptno)
);
Chapter 07 : Creating and Managing Tables

Create Table Statement:


Examples
Chapter 07 : Creating and Managing Tables

Create Table Statement:


Examples
Chapter 07 : Creating and Managing Tables

Create Table Statement:


column level and table level constraint definition
Chapter 07 : Creating and Managing Tables

Alter Table Statement:


Examples
Alter table table_name add column_name data_type  alter table emp add address varchar2(100) ;
Alter table table_name drop column column_name  alter table emp drop column address ;
Alter table table_name rename column old to new  alter table emp rename column ename to empname ;

alter table emp add constraint emp_sal_ck check ( sal > 1000) ;
alter table emp disable constraint emp_sal_ck ;
alter table emp enable constraint emp_sal_ck ;
alter table emp rename constraint emp_sal_ck to emp_sal_check ;
alter table emp drop constraint emp_sal_check ;

alter table dept modify (dname varchar2(100) );


alter table dept modify (dname varchar2(100) constraint dept_dname_nn not null) ;
alter table dept modify (loc varchar2(50) default 'Alexandria');
Chapter 07 : Creating and Managing Tables

Alter Table Statement:


Examples
Chapter 07 : Creating and Managing Tables

Alter Table Statement:


Examples
Chapter 07 : Creating and Managing Tables

Managing Tables

Rename :
Rename old_name to new_name  rename emp2 to emp3

Truncate :
data removed permanent
Storage area removed permanent
Keep Table definition

Truncate table table_name  Truncate table emp3

Drop :
data removed permanent
Storage area removed permanent
Definition Removed from database
Drop table table_name  Drop Table dept2
Chapter 07 : Creating and Managing Tables

SQL Practice

A : text varchar2
“*”: not null constraint
O : Optional
# : Primary key
Chapter 07 : Creating and Managing Tables

SQL Practice
Chapter 07 : Creating and Managing Tables

SQL Practice
Chapter 07 : Creating and Managing Tables

SQL Practice
Chapter 07 : Creating and Managing Tables

SQL Practice

1. Create user with your name and a password


2. Grant the new user the needed privileges
3. Create the tables in the previous slide
4. Insert at least two rows in each table
5. Grant scott permission to be able to select data from your tables
6. Create View to display project ( code , name , desc and department_name )
7. Create View to display project employee ( code , name , employee_name )
8. Create View to display project name , number of employees in each project

You might also like