SlideShare a Scribd company logo
SQL Fundamentals
Oracle 11g
M U H A M M A D WA H E E D
O R A C L E D ATA B A S E D E VE L O P E R
E M A I L : m.w a h e e d 3 6 6 8 @ g ma i l . c om
.
1
Lecture#3
Lab Work Video Tutorial
•Video is embedded inside the slides.
•Go through each slide for conceptual knowledge then practical is
performed in the video that is provided in last slides.
2
COMMENTS
•Use the COMMENT statementto add to the data dictionary a comment
about a table or table column etc.
•We use following syntax for creating comments:
COMMENT ON TABLE <table_name> IS ‘<comment_statement>’;
or
COMMENT ON COLUMN <table_name>.<column_name>
IS ‘<comment_statement>';
•We use following syntax to remove/drop comments:
COMMENT ON TABLE <table_name> IS ' ';
or
COMMENT ON COLUMN <table_name>.<column_name>IS ' ';
3
View COMMENTS
•SELECT * FROM USER_COL_COMMENTS;
•SELECT * FROM USER_TAB_COMMENTS;
4
Data Types
Data Type Description
VARCHAR2( size ) Variable length character data with
specified size ( 1 to 4000)
INT Fixed length number data type with default
size 38.
CHAR [ size ] Fixed length character data of specified size
( 1 to 2000)
NUMBER [ p,s ] Number having precision p(1 to 38) and
scale s(-84 to 127)
Date It must be between January 1, 4712 and
December 31,9999
TIMESTAMP(s) It is date+time data type with scale
specified from user on the right of decimal
point in time. Scale is 6 by default.
5
Data Integrity Constraints
•These are used to enforce limitations on the data entered in tables.
•It have two levels:
column level(applied on specific column definition)
table level(applied at table definition)
6
Column Level Constraints
•Column level constraints are defined with following syntax:
CREATE TABLE <table_name>
(<column_name> <data_type> <constraint_definition>);
7
Table Level Constraints
•Table level constraints are defined with following syntanx:
CREATE TABLE <table_name>
(<column_name> <data_type>,
<column_name> <data_type>,
CONSTRAINT<constraint_name> <constraint_definition>);
8
Constraint Types
•There are following five constraints:
1- NULL/NOT NULL
2- UNIQUE
3- PRIMARYKEY
4- FOREIGN KEY
5- CHECK
9
NULL/NOT NULL Constraint
•It determines whether a column can be blank or not.
•It is only possible at column level.
•By default each column is set to NULL.
•Example (column level):
CREATE TABLE student
( std_id NUMBER(4) NOT NULL,
std_name VARCHAR2(30));
•Example(table level):
ALTER TABLE student
MODIFY std_name VARCHAR2(30)NOT NULL);
10
UNIQUE Constraint
•It is used for a field which needs to be unique but also can be
null/empty.
•Example (column level):
contact_no of student table can be empty but must be unique.
CREATE TABLE student
( std_contact_no NUMBER(4)CONSTRAINTcontact_uk UNIQUE,
);
• Example(table level):
CREATE TABLE student
(std_contact_no int,
CONSTRAINTcontact_uk UNIQUE(std_contact_no));
11
PRIMARY KEY Constraint
•It restricts the duplication of values and doesn’t allow null values.
•Example(columnlevel):
CREATE TABLE student
( std_id int PRIMARYKEY,
std_name varchar2(100));
•Example( table level):
CREATE TABLE student
(std_id int,
CONSTRAINTstdid_pk PRIMARY KEY(std_id));
12
FOREIGN KEY Constraint
•It is used to create the link between data of two tables.
•It is based on common field in two tables where parent table have
primary key and child table uses it as foreign key.
•One table can have multiple foreign keys.
•Example(columnlevel):
CREATE TABLE result
( std_id int REFERENCES student);
•Example(table level):
CREATE TABLE result
(std_id int,
CONSTRAINTstdid_fk FOREIGN KEY(std_id)REFERENCES
student(std_id));
13
CHECK Constraint
•Defines a condition that each row mustsatisfy.
•It is only possible at column level.
•Example(columnlevel):
CREATE TABLE student
( age int CONSTRAINTstd_age CHECK(age>18));
•Example(table level):
CREATE TABLE result
(age int,
CONSTRAINTstd_age CHECK (age>18));
14
Modify Constraints
•SQL provides the facility of modification of constraints on existing
schemas.
•There are four following facilities to be used on constraints :
Add
Drop
Enable
Disable
Cascade
15
ADD Constraint
•ADD constraint:
ALTER TABLE <table_name>
ADD CONSTRAINT<constraint_name><constraint_defination>;
16
DROP Constraint
•DROPconstraint:
ALTER TABLE <table_name>
DROPCONSTRAINT<constraint_name>;
•*Note: PRIMARYKEY as a special case uses “CASCADE” to disable its
foreign keys. For example:
ALTER TABLE student
DROPPRIMARYKEY CASCADE;
In above case foreign key constraint on result table will be deleted too.
17
DISABLE Constraint
•DROPconstraint:
ALTER TABLE <table_name>
DISABLECONSTRAINT<constraint_name>CASCADE;
•Apply the “CASCADE” option to disable its dependent integrity
constraints.
18
ENABLE Constraint
•It is used to ENABLE the DISABLED constraints:
ALTER TABLE <table_name>
ENABLE CONSTRAINT<constraint_name>;
19
CASCADE Constraint
•The CASCADE constraints clause is used along with the DROPCOLUMN
clause.
•The CASCADE clause drops all referential integrity constraints that refer
to the primary and unique keys defined on the dropped columns.
•The CASCADE clause also drops all multicolumn constraints defined on
the dropped columns.
•Example:
ALTER TABLE <table_name>
DROP(<constraint_name(s)>)CASCADECONSTRAINTS;
20
Your Suggestions/Feedback?
-Elbert Hubbard
21
Email: m.waheed3668@gmail.com

More Related Content

What's hot (19)

PDF
SQL Functions - Oracle SQL Fundamentals
MuhammadWaheed44
 
PPT
Sql basics and DDL statements
Mohd Tousif
 
PPTX
STRUCTURE OF SQL QUERIES
VENNILAV6
 
ODP
Sql commands
Balakumaran Arunachalam
 
PPTX
Database Management - Lecture 2 - SQL select, insert, update and delete
Al-Mamun Sarkar
 
PPTX
SQL: Structured Query Language
Rohit Bisht
 
PPTX
SQL(DDL & DML)
Sharad Dubey
 
PPTX
SQL - DML and DDL Commands
Shrija Madhu
 
PDF
Sql delete, truncate, drop statements
Vivek Singh
 
PDF
Sql commands
Mohd Tousif
 
PPTX
Creating database using sql commands
Belle Wx
 
PPTX
SQL-Alter Table, SELECT DISTINCT & WHERE
I L0V3 CODING DR
 
PPTX
SQL commands
GirdharRatne
 
PPTX
SQL Basics
Hammad Rasheed
 
PPT
Sql DML
Vikas Gupta
 
PPTX
Database constraints
Khadija Parween
 
PPT
SQL Tutorial - Basic Commands
1keydata
 
SQL Functions - Oracle SQL Fundamentals
MuhammadWaheed44
 
Sql basics and DDL statements
Mohd Tousif
 
STRUCTURE OF SQL QUERIES
VENNILAV6
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Al-Mamun Sarkar
 
SQL: Structured Query Language
Rohit Bisht
 
SQL(DDL & DML)
Sharad Dubey
 
SQL - DML and DDL Commands
Shrija Madhu
 
Sql delete, truncate, drop statements
Vivek Singh
 
Sql commands
Mohd Tousif
 
Creating database using sql commands
Belle Wx
 
SQL-Alter Table, SELECT DISTINCT & WHERE
I L0V3 CODING DR
 
SQL commands
GirdharRatne
 
SQL Basics
Hammad Rasheed
 
Sql DML
Vikas Gupta
 
Database constraints
Khadija Parween
 
SQL Tutorial - Basic Commands
1keydata
 

Similar to Oracle SQL Fundamentals - Lecture 3 (20)

PPTX
Sql commands
Pooja Dixit
 
PPT
Introduction to sql
VARSHAKUMARI49
 
PPTX
Oracle basic queries
PRAKHAR JHA
 
PPTX
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
deeptanshudas100
 
PDF
Introduction to SQL..pdf
mayurisonawane29
 
PDF
Relational database management system
Praveen Soni
 
PPTX
Sql server ___________session_15(data integrity)
Ehtisham Ali
 
PPTX
Ppt INFORMATIVE PRACTICES for class 11th chapter 14
prashant0000
 
PPTX
3. ddl create
Amrit Kaur
 
PPTX
Database Akjljljlkjlkjkljlkjldiministration.pptx
EliasPetros
 
PPT
Les09
Sudharsan S
 
PPTX
DDL(Data defination Language ) Using Oracle
Farhan Aslam
 
PPT
CSE311_IAH_Slide07_SQL Advanced Quries.ppt
noshinnawar31
 
PPT
Les09 (using ddl statements to create and manage tables)
Achmad Solichin
 
PPT
MY SQL
sundar
 
PPT
Constraints constraints of oracle data base management systems
SHAKIR325211
 
PPTX
Joins & constraints
VENNILAV6
 
PDF
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
PPTX
2. DBMS Experiment - Lab 2 Made in SQL Used
TheVerse1
 
Sql commands
Pooja Dixit
 
Introduction to sql
VARSHAKUMARI49
 
Oracle basic queries
PRAKHAR JHA
 
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
deeptanshudas100
 
Introduction to SQL..pdf
mayurisonawane29
 
Relational database management system
Praveen Soni
 
Sql server ___________session_15(data integrity)
Ehtisham Ali
 
Ppt INFORMATIVE PRACTICES for class 11th chapter 14
prashant0000
 
3. ddl create
Amrit Kaur
 
Database Akjljljlkjlkjkljlkjldiministration.pptx
EliasPetros
 
DDL(Data defination Language ) Using Oracle
Farhan Aslam
 
CSE311_IAH_Slide07_SQL Advanced Quries.ppt
noshinnawar31
 
Les09 (using ddl statements to create and manage tables)
Achmad Solichin
 
MY SQL
sundar
 
Constraints constraints of oracle data base management systems
SHAKIR325211
 
Joins & constraints
VENNILAV6
 
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
2. DBMS Experiment - Lab 2 Made in SQL Used
TheVerse1
 
Ad

Recently uploaded (20)

PPTX
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PPTX
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PDF
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
PDF
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PDF
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
PPTX
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
PDF
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PDF
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
How to Comply With Saudi Arabia’s National Cybersecurity Regulations.pdf
Bluechip Advanced Technologies
 
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
Ad

Oracle SQL Fundamentals - Lecture 3

  • 1. SQL Fundamentals Oracle 11g M U H A M M A D WA H E E D O R A C L E D ATA B A S E D E VE L O P E R E M A I L : m.w a h e e d 3 6 6 8 @ g ma i l . c om . 1 Lecture#3
  • 2. Lab Work Video Tutorial •Video is embedded inside the slides. •Go through each slide for conceptual knowledge then practical is performed in the video that is provided in last slides. 2
  • 3. COMMENTS •Use the COMMENT statementto add to the data dictionary a comment about a table or table column etc. •We use following syntax for creating comments: COMMENT ON TABLE <table_name> IS ‘<comment_statement>’; or COMMENT ON COLUMN <table_name>.<column_name> IS ‘<comment_statement>'; •We use following syntax to remove/drop comments: COMMENT ON TABLE <table_name> IS ' '; or COMMENT ON COLUMN <table_name>.<column_name>IS ' '; 3
  • 4. View COMMENTS •SELECT * FROM USER_COL_COMMENTS; •SELECT * FROM USER_TAB_COMMENTS; 4
  • 5. Data Types Data Type Description VARCHAR2( size ) Variable length character data with specified size ( 1 to 4000) INT Fixed length number data type with default size 38. CHAR [ size ] Fixed length character data of specified size ( 1 to 2000) NUMBER [ p,s ] Number having precision p(1 to 38) and scale s(-84 to 127) Date It must be between January 1, 4712 and December 31,9999 TIMESTAMP(s) It is date+time data type with scale specified from user on the right of decimal point in time. Scale is 6 by default. 5
  • 6. Data Integrity Constraints •These are used to enforce limitations on the data entered in tables. •It have two levels: column level(applied on specific column definition) table level(applied at table definition) 6
  • 7. Column Level Constraints •Column level constraints are defined with following syntax: CREATE TABLE <table_name> (<column_name> <data_type> <constraint_definition>); 7
  • 8. Table Level Constraints •Table level constraints are defined with following syntanx: CREATE TABLE <table_name> (<column_name> <data_type>, <column_name> <data_type>, CONSTRAINT<constraint_name> <constraint_definition>); 8
  • 9. Constraint Types •There are following five constraints: 1- NULL/NOT NULL 2- UNIQUE 3- PRIMARYKEY 4- FOREIGN KEY 5- CHECK 9
  • 10. NULL/NOT NULL Constraint •It determines whether a column can be blank or not. •It is only possible at column level. •By default each column is set to NULL. •Example (column level): CREATE TABLE student ( std_id NUMBER(4) NOT NULL, std_name VARCHAR2(30)); •Example(table level): ALTER TABLE student MODIFY std_name VARCHAR2(30)NOT NULL); 10
  • 11. UNIQUE Constraint •It is used for a field which needs to be unique but also can be null/empty. •Example (column level): contact_no of student table can be empty but must be unique. CREATE TABLE student ( std_contact_no NUMBER(4)CONSTRAINTcontact_uk UNIQUE, ); • Example(table level): CREATE TABLE student (std_contact_no int, CONSTRAINTcontact_uk UNIQUE(std_contact_no)); 11
  • 12. PRIMARY KEY Constraint •It restricts the duplication of values and doesn’t allow null values. •Example(columnlevel): CREATE TABLE student ( std_id int PRIMARYKEY, std_name varchar2(100)); •Example( table level): CREATE TABLE student (std_id int, CONSTRAINTstdid_pk PRIMARY KEY(std_id)); 12
  • 13. FOREIGN KEY Constraint •It is used to create the link between data of two tables. •It is based on common field in two tables where parent table have primary key and child table uses it as foreign key. •One table can have multiple foreign keys. •Example(columnlevel): CREATE TABLE result ( std_id int REFERENCES student); •Example(table level): CREATE TABLE result (std_id int, CONSTRAINTstdid_fk FOREIGN KEY(std_id)REFERENCES student(std_id)); 13
  • 14. CHECK Constraint •Defines a condition that each row mustsatisfy. •It is only possible at column level. •Example(columnlevel): CREATE TABLE student ( age int CONSTRAINTstd_age CHECK(age>18)); •Example(table level): CREATE TABLE result (age int, CONSTRAINTstd_age CHECK (age>18)); 14
  • 15. Modify Constraints •SQL provides the facility of modification of constraints on existing schemas. •There are four following facilities to be used on constraints : Add Drop Enable Disable Cascade 15
  • 16. ADD Constraint •ADD constraint: ALTER TABLE <table_name> ADD CONSTRAINT<constraint_name><constraint_defination>; 16
  • 17. DROP Constraint •DROPconstraint: ALTER TABLE <table_name> DROPCONSTRAINT<constraint_name>; •*Note: PRIMARYKEY as a special case uses “CASCADE” to disable its foreign keys. For example: ALTER TABLE student DROPPRIMARYKEY CASCADE; In above case foreign key constraint on result table will be deleted too. 17
  • 18. DISABLE Constraint •DROPconstraint: ALTER TABLE <table_name> DISABLECONSTRAINT<constraint_name>CASCADE; •Apply the “CASCADE” option to disable its dependent integrity constraints. 18
  • 19. ENABLE Constraint •It is used to ENABLE the DISABLED constraints: ALTER TABLE <table_name> ENABLE CONSTRAINT<constraint_name>; 19
  • 20. CASCADE Constraint •The CASCADE constraints clause is used along with the DROPCOLUMN clause. •The CASCADE clause drops all referential integrity constraints that refer to the primary and unique keys defined on the dropped columns. •The CASCADE clause also drops all multicolumn constraints defined on the dropped columns. •Example: ALTER TABLE <table_name> DROP(<constraint_name(s)>)CASCADECONSTRAINTS; 20