You are on page 1of 47

Oracle –SQL 10g

© Tech Mahindra Limited 2008 Tech Mahindra Limited confidential


Objectives
At the end of this session, you will be able to:

 Understand history of SQL

 Learn the features of SQL

 Understand what are SQL Commands

 Use DDL commands

 Understand how to Enable, disable constraints

 Learn to merge data in Oracle tables

 Learn to Create Sequence

 Use DML commands

 Use TCL Commands

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 2


Agenda
 History of SQL
 Features of SQL
 SQL Commands
 DDL commands
 Create
 Alter
 Drop
 Truncate
 How to Enable, disable constraints
 Merge
 Create Sequence
 DML commands
 Insert
 Update
 Delete
 TCL Commands
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 3
History of SQL
 SQL is an ANSI (American National Standards Institute)
standard computer language for accessing and manipulating
database systems

 SQL statements are used to retrieve and update data in a


database

 SQL works with database programs like MS Access, DB2,


Informix, MS SQL Server, Oracle, Sybase, etc.

 The first version of SQL was developed at IBM by Donald D.


Chamberlin and Raymond F. Boyce in the early 1970s

 This version, initially called SEQUEL, was designed to


manipulate and retrieve data

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 4


Features of SQL
 SQL stands for Structured Query Language

 Is a Non-procedural query language

 Sometimes referred to as a Database Gateway Language as


ANSI (American National Standards Institute) made it a
standard language for all DBMS

 Used for creating, managing, manipulating and querying the


database objects such as tables, views etc.

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 5


SQL Commands
 SQL commands are divided into following categories:

 Data Definition Language


 CREATE, ALTER, DROP,TRUNCATE

 Data Manipulation Language


 INSERT, UPDATE, DELETE

 Data Query Language


 SELECT

 Data Control Language


 GRANT, REVOKE

 Transaction control Language


 COMMIT, ROLLBACK

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 6


Data Definition Language (DDL)
 DDL commands are used to
 Create database objects such as tables, indexes, views etc. in
the database

 Alter/Modify the structure of existing database objects

 Drop the existing database objects from the database


 CREATE
 ALTER
 DROP
 TRUNCATE

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 7


Creating a Table

CREATE TABLE tablename


(columnname1 data_type (size) [constraints],
(columnname2 data_type (size) [constraints],
…)

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 8


Defining Database Tables
 To create a table, you must specify:
 Table name
 Column names
 Column data types
 Column sizes
 Constraints: restrictions on the data values that a column can
store

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 9


Names and Properties: Conventions
 Series of rules Oracle Corporation established for naming
all database objects:
1. From 1 to 30 characters
2. Only alphanumeric characters, and special characters
($ , _, #)
3. Must begin with a letter and can not contain blank spaces or
hyphens

 Are the following names valid? Why?


1. customer order
2. customer-order
3. #order

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 1


0
Data Types
 Data type
 Specifies kind of data that column stores
 Provides means for error checking
 Enables DBMS to use storage space more efficiently by
internally storing different types of data in different ways
 Basic types
 Character
 Number
 Date/time
 Large object

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 11


Data Types
 Built-in
 provided by the system

 Library
 built by the software vendor or a third party

 User-defined
 built by users

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 1


2
Basic Built-in Data Types
 Character
 VARCHAR2
 CHAR
 NVARCHAR2 / NCHAR
 Numeric
 NUMBER
 DATE
 OTHERS
 CLOB, BLOB, LONG, RAW, LONG RAW

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 1


3
Constraints
 A rule that restricts the values that can be inserted into a
column
 A mechanism used to protect
 the relationship between data within an Oracle table, or
 the correspondence between data in two different tables

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 1


4
Types of Constraints
 Integrity constraints: define primary and foreign keys

 Value constraints: define specific data values or data ranges


that must be inserted into columns and whether values
must be unique or not NULL

 Table constraint: restricts the data value with respect to all


other values in the table

 Column constraint: limits the value that can be placed in a


specific column, irrespective of values that exist in other
table records

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 1


5
Integrity Constraints
 Define Primary Key columns

 Specify Foreign Keys and their corresponding table and


column references

 Specify Composite Keys

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 1


6
Default Clause
 Default: specifies a default value that is inserted
automatically

 Syntax:
CREATE TABLE <tablename>(column name datatype
DEFAULT <value>);

 Example:
CREATE TABLE employee
(empno NUMBER(6) PRIMARY KEY,
ename VARCHAR2(30) NOT NULL,
grade CHAR(3) DEFAULT 'TG0',
dob DATE, deptno number(2));

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 1


7
Primary Key Constraints
 Column-Level: the constraint will be given along with the
column definition. This is required when only one column is
to be made as a primary key
 Table-level: this type is used when a compound primary key
is to be created i.e. super key

 Syntax:
CREATE TABLE <table name>
(<Colname> <datatype>[(<size>)] [ [CONSTRAINT
constraint_name] PRIMARY KEY ],
<colname> <datatype>[(<size>)],
. . .);

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 1


8
Column Level Primary Key
 CREATE TABLE employee
 (empno NUMBER(6) constraint emp_empno_pk
PRIMARY KEY,
 ename VARCHAR2(30),
 grade_class CHAR(2),
 email varchar2(15),
 dob DATE);

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 1


9
Table Level Primary Key Constraint
CREATE TABLE employee
(empno NUMBER(6),
ename VARCHAR2(30),
grade CHAR(2),
dob DATE,
CONSTRAINT emp_id_pk PRIMARY KEY
(empno,ename));

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 2


0
Oracle Constraint Naming Convention

 tablename_ columnname_constraintID

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 2


1
Foreign Key Constraints
 Can only be defined after column is defined as a primary
key in the parent table

 Syntax:
 Column level
CREATE TABLE <tablename>(Column name datatype
CONSTRAINT constraint_name
REFERENCES primary_key_table_name
(column_name));

 Table level
CREATE TABLE table_name
(column1 datatype null/not null, column2
datatype null/not null, …,CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ...
column_n)
);

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 2


2
Foreign Key Constraints - Column Level
CREATE TABLE dept
(deptno number(2) constraint dept_deptno_pk PRIMARY KEY,
dname varchar2(20) NOT NULL,
loc varchar2(10));

CREATE TABLE employee


(empno NUMBER(6) constraint emp_empno_pk PRIMARY KEY,

ename VARCHAR2(30),
grade CHAR(2),
dob DATE,
deptno number(2) REFERENCES dept(deptno)
ON DELETE CASCADE);

 ON DELETE CASCADE option permits deletions of referenced key


values in the parent table and automatically deletes dependent
rows in the child table to maintain referential integrity.

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 2


3
Foreign Key Constraints - Table Level
CREATE TABLE dept
( deptno number(2) constraint dept_deptno_pk PRIMARY
KEY,
dname varchar2(20) NOT NULL,
loc varchar2(10));

CREATE TABLE employee


(empno NUMBER(6) constraint emp_empno_pk PRIMARY
KEY,
ename VARCHAR2(30),
grade CHAR(2),
dob DATE,
deptno number(2), FOREIGN KEY (deptno)
REFERENCES dept(deptno)
);

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 2


4
Types of Value Constraints
 Check condition: restricts to specific values
 Syntax:
CREATE TABLE <table name>(Column name datatype,
CHECK (<condition>);

 Example: column level

CREATE TABLE employee


(empno NUMBER(6) PRIMARY KEY
CHECK (empno > 2999), ename VARCHAR2(30),
grade CHAR(2),dob DATE, deptno number(2) REFERENCES
dept(deptno)
);

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 2


5
Check Constraint Example – Table Level
CREATE TABLE employee
(empno NUMBER(6) PRIMARY KEY,
ename VARCHAR2(30),
grade CHAR(2),
dob DATE,
deptno number(2) REFERENCES dept(deptno),
CHECK (empno > 2999 OR dob >= '01-JAN-1988' ))

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 2


6
Types of Value Constraints
 Not NULL: specifies that a column cannot be empty

 Syntax:
CREATE TABLE <tablename>(column name datatype NOT
NULL);

 Example:
CREATE TABLE employee
(empno NUMBER(6) PRIMARY KEY,
ename VARCHAR2(30) NOT NULL,
grade CHAR(2),
dob DATE, deptno number(2) REFERENCES
dept(deptno),CHECK (empno > 2999));

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 2


7
Types of Value Constraints
 Unique: specifies that a non-primary key column must have
a unique value

 Syntax:
CREATE TABLE <table name>(column name datatype
CONSTRAINT term_term_desc_uk UNIQUE (term_desc));

 Example:
CREATE TABLE employee
(empno NUMBER(6) PRIMARY KEY,
ename VARCHAR2(30) NOT NULL,
grade CHAR(3) DEFAULT 'TG0',
email varchar2(20) constraint u_em_email
UNIQUE,
dob DATE, deptno number(2) REFERENCES
dept(deptno), CHECK (empno > 2999));
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 2
8
Create Table from Another Table
 Syntax:
CREATE TABLE <tablename>
AS ( Select query);
Example:

CREATE TABLE empcopy as SELECT * FROM emp WHERE


empno>1010;

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 2


9
Modifying and Deleting Database Tables
 Modify existing database tables by
 Changing the name of a table
 Adding new columns
 Deleting columns that are no longer needed
 Changing the data type or maximum size of an existing column
 Unrestricted action: some specifications can always be
modified
 Restricted action: specifications modified only in certain
situations

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 3


0
Deleting and Truncating Tables
 To delete:
Drop table <tablename> [CASCADE CONSTRAINTS];
 Use with caution

 To delete foreign key constraints, add “cascade constraints”


DROP TABLE emp;

 Truncate Table <tablename> ;


 removes all rows from a table
 high-speed data deletion statement

 You cannot roll back a TRUNCATE statement


 Example
TRUNCATE TABLE dept;

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 3


1
Renaming and Adding Columns
 To change the name of a table use RENAME
 Syntax
Rename old_tablename to new_tablename;
 Example
RENAME employee TO EMP;

 To add a column
 Syntax
ALTER TABLE tablename ADD(<columnname>
data_declaration [constraints], <columnname>
data_declaration [constraints]…);
 Example
ALTER TABLE emp ADD (Hire_date DATE);

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 3


2
Modifying Existing Column Data Definitions
 Can only change data type to compatible data type (i.e.
varchar2 to char)
ALTER tablename MODIFY(columnname
new_data_declaration);
 Examples
 Changing the column width of grade from 2 to 4:
ALTER TABLE employee
MODIFY (grade CHAR(4));

ALTER TABLE employee RENAME COLUMN


email TO email_address;

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 3


3
Deleting a Column
 Can be used to remove a column from a table
ALTER TABLE tablename DROP COLUMN columnname;

 Examples
ALTER TABLE employee
DROP COLUMN grade;

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 3


4
Adding and Deleting Constraints
 Add a constraint Syntax
ALTER TABLE tablename ADD CONSTRAINT
constraint name constraint definition;

 Remove a constraint Syntax


ALTER TABLE tablename DROP CONSTRAINT
constraint name;

 Examples
ALTER TABLE Emp
ADD CONSTRAINT emp_ename_uk UNIQUE (ename);

ALTER TABLE emp


DROP CONSTRAINT emp_ename_uk;

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 3


5
Enabling and Disabling Constraints
 When modifying a database it can be useful to disable
constraints

 Constraints are enabled by default

 To disable a constraint: ALTER TABLE tablename DISABLE


CONSTRAINT constraint_name;

 To enable a constraint: ALTER TABLE tablename ENABLE


CONSTRAINT constraint_name;

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 3


6
Examples

ALTER TABLE emp


DISABLE CONSTRAINT emp_ename_uk;

ALTER TABLE emp


ENABLE CONSTRAINT emp_ename_uk;

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 3


7
Merging Data in Oracle Tables(9i)
 Syntax
MERGE INTO table1 USING table2 ON (join condition)
WHEN MATCHED
UPDATE SET COL1=VALUE
WHEN NOT MATCHED
INSERT (COLUMN LIST) VALUES (COLUMN_VALUIES)
 Example
CREATE TABLE EMP_MERGE
(EMPNO NUMBER, BONUS NUMBER DEFAULT 100);

INSERT INTO EMP_MERGE (EMPNO) (SELECT EMPNO FROM EMP);

MERGE INTO EMP_MERGE D


USING (SELECT empno, sal, deptno FROM emp
WHERE deptno = 10) S
ON (D. empno = S.empno)
WHEN MATCHED THEN UPDATE SET D.bonus = D.bonus +
S.sal*.01
WHEN NOT MATCHED THEN INSERT (D.empno, D.bonus)
VALUES (S.empno, S.sal *0.1);
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 3
8
Sequence
 It is a database object from which users may generate
unique integers
 It is used to automatically generate primary key values
 When a sequence number is generated, the sequence is
incremented, independent of the transaction committing or
rolling back
 Syntax
CREATE SEQUENCE sequence_name
START WITH integer
INCREMENT BY integer
MAXVALUE integer
MINVALUE integer
CYCLE|NOCYLE
CACHE|NOCACHE;

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 3


9
Sequence
 Example
CREATE SEQUENCE emp_seq
START WITH 10
INCREMENT BY 1
MAXVALUE 1000
NOCACHE
NOCYCLE;

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 4


0
DML - Data Manipulation Language
 DML commands work with the content of the tables

 Used to add, delete and modify the contents of the tables:


 Insert
 Update
 Delete

 DML commands are not committed as soon as they are


issued, an explicit commit is required

 Auto commit happens under following conditions:


 Issue of a DDL command
 Closing the session

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 4


1
DML Insert
 Examples:

INSERT INTO emp Values(101, 'SUNIL', 'CLERK',


'7902', '01-JAN-08',10000,100,10);

INSERT INTO emp (empno,ename,job,deptno) Values


(102, 'RAJESH', 'SALESMAN',10);

INSERT INTO emp VALUES (&empno, &ename, &job);

INSERT INTO empcopy (SELECT * FROM emp);

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 4


2
DML Update
 UPDATE emp SET ename = 'KUMAR' WHERE empno = 101;

 UPDATE emp SET job = 'PRESIDENT', ename = 'SUNIL'


 WHERE empno = 101;

 UPDATE emp SET sal=(SELECT sal FROM emp WHERE


empno=7788)
 WHERE empno = 7656;

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 4


3
DML Delete
 DELETE FROM emp;

 DELETE FROM emp WHERE deptno = 20;

 DELETE FROM emp WHERE empno = 101 AND ename LIKE 'S
%';

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 4


4
TCL: Transaction Control Statements
 COMMIT: to explicitly save the changes in the database.
Execution of DDL statements does commit the changes
automatically.
SQL> COMMIT;

 SAVEPOINT <savepoint name>: is a transaction marker.


SQL> SAVEPOINT A;

 ROLLBACK [TO savepoint name]: to undo the changes till


the last commit or till the last savepoint.
SQL> ROLLBACK;
SQL> ROLLBACK TO SAVEPOINT A;

CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 4


5
Summary
In this session, we covered:

 History of SQL

 Features of SQL

 SQL Commands

 DDL commands

 How to Enable & Disable constraints

 Merge

 Create Sequence

 DML commands

 TCL Commands
CONFIDENTIAL© Copyright 2008 Tech Mahindra Limited 4
6
Thank You

© Tech Mahindra Limited 2008 Tech Mahindra Limited confidential

You might also like