You are on page 1of 79

Relational Terminology

Tuple (Row)
Attribute (Column)
Relation (Table)

Integrity Constraints
Primary Key
Alternate Key
Foreign Key
Introduction to Databases.

A database is a collection of Data (Information). Examples of databases, which we use


in our daily life, is an Attendance Register, Telephone Directory, Muster Rule.

Database Management System(DBMS): A database management system is a


collection of programs written to manage a database. That is, it acts as a interface
between user and database.

.
Introduction to RDBMS

A Database Management System based on Relational Data Model


is known as Relational Database Management System (RDBMS).

The father of Relational Data Model was Dr. E.F. CODD. He


developed the relational data model by taking the concept from
Relational Algebra in June-1970.

Relational Data Model is nothing but 12 Rules which are named


after Codd as Codd Rules. According to Codd a package can be
called as RDBMS only if it satisfies the Codd Rules

.
CODDS RULES

Information Rule: All information in a relational database including table names,


column names are represented by values in tables

Guaranteed Access Rule: Every piece of data in a relational database, can be


accessed by using combination of a table name, a primary key value that identifies the
row and column name which identified a cell

Systematic Treatment of Nulls Rule: The RDBMS handles records that have
unknown or inapplicable values in a pre-defined fashion

View Updating Rule: Any view that is theoretically updateable can be updated using
the RDBMS. Data consistency is ensured since the changes made in the view are
transmitted to the base table and vice-versa

High-Level Insert, Update and Delete: The RDBMS supports insertions, updation
and deletion at a table level. .
Physical Data Independence: The execution of adhoc requests and application programs is
not affected by changes in the physical data access and storage methods

Logical Data Independence: Logical changes in tables and views such adding/deleting
columns or changing fields lengths need not necessitate modifications in the programs or in
the format of adhoc requests.

Integrity Independence: Like table/view definition, integrity constraints are stored in the
on-line catalog and can therefore be changed without necessitating changes in the application
programs

Distribution Independence: Application programs and adhoc requests are not affected by
change in the distribution of physical data

No subversion Rule: If the RDBMS has a language that accesses the information of a record
at a time, this language should not be used to bypass the integrity constraints. This is
necessary for data integrity

.
Oracle Datatypes

Fixed-length character data of length size bytes or Fixed for every row in the table (with trailing
Char characters blanks); maximum size is 2000 bytes per row

VARCHAR2 Variable-length character data Variable for each row, up to 4000 bytes per row

NCHAR Fixed-length Unicode character 2000 bytes per row

NVARCHAR2 Variable-length Unicode character data The upper limit is 4000 bytes per row

CLOB Single-byte character data Up to 232 - 1 bytes, or 4 gigabytes.

NCLOB Unicode national character set (NCHAR) data. Up to 232 - 1 bytes, or 4 gigabytes.

LONG Variable-length character data 2 gigabytes

.
NUMBER (p, s) Variable-length numeric dataVariable-length The maximum space required for
numeric data. Maximum precision p and/or scale a given column is 21 bytes per
s is 38. row

DATE Fixed-length date and time data Fixed at 7 bytes for each row

INTERVAL YEAR A period of time, represented as years and Fixed at 5 bytes.


(precision) TO months.
MONTH

INTERVAL DAY A period of time, represented as days, hours, Fixed at 11 bytes.


(precision) TO minutes, and seconds.
SECOND (precision)
BLOB Unstructured binary data 4 gigabytes

BFILE Binary data stored in an external file 4 gigabytes

.
Database Objects

The Oracle database can contain multiple data structures.


Each structure should be outlined in the database design so
that it can be created during the build stage of database
development.

Table: Stores data


View: Subset of data from one or more tables
Sequence: Generates numeric values
Index: Improves the performance of some queries
Synonym: Gives alternative name to an object
Oracle Table Structures

Tables can be created at any time, even when users are


using the database.

You do not need to specify the size of a table. The size


is ultimately defined by the amount of space allocated to
the database as a whole. It is important, however, to
estimate how much space a table will use over time.

Table structure can be modified online.


Naming Rules
Must begin with a letter

Must be 130 characters long

Must contain only AZ, az, 09, _, $, and #

Must not duplicate the name of another object owned


by the same user

Must not be an Oracle serverreserved word


Data Definition Language

A Data Definition Language or Data Description Language (DDL) is a computer


language for defining data structures.

CREATE statements
A CREATE statement in SQL creates an object inside of a relational database management
system (RDBMS). Like table, index, or stored query

CREATE TABLE statement


CREATE [TEMPORARY] TABLE [table name] ( [column definitions] ) [table parameters].

Column definition: [column name] [data type] {NULL | NOT NULL} {column options}
Primary key definition: PRIMARY KEY ( [comma separated column list] )
CONSTRAINTS: {CONSTRAINT} [constraint definition]

CREATE TABLE employees ( id number(10,2) , first_name CHAR(50) NULL,


last_name CHAR(75) NOT NULL, dateofbirth DATE NULL );
.
DEFAULT Option

Specify a default value for a column during an insert.

Literal values, expressions, or SQL functions are legal values.

Another columns name or a pseudocolumn are illegal values.

The default data type must match the column data type.

CREATE TABLE hire_dates


(id NUMBER(8),
hire_date DATE DEFAULT SYSDATE);
Create the table
Constraints
The Oracle server uses constraints to prevent invalid data
entry into tables.

Constraints enforce rules at the table level.


Constraints prevent the deletion of a table if there
are dependencies.
The following constraint types are valid:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
Constraints
Constraint Guidelines
You can name a constraint, or the Oracle server generates a
name by using the SYS_Cn format.

Create a constraint at either of the following times:


At the same time as the creation of the table
After the creation of the table

Define a constraint at the column or table level.

View a constraint in the data dictionary.


Defining Constraints
UNIQUE Constraint
FOREIGN KEY Constraint
Foreign Key
CREATE TABLE employees
(...
department_id NUMBER(4) CONSTRAINT
emp_deptid_fk
REFERENCES departments(department_id),
...
)
FOREIGN KEY: Defines the column in the child table at the
table-constraint level

REFERENCES: Identifies the table and column in the parent


Table

ON DELETE CASCADE: Deletes the dependent rows in the


child table when a row in the parent table is deleted

ON DELETE SET NULL: Converts dependent foreign key


values to null
On delete cascade
ON DELETE SET NULL
CHECK Constraint
The CHECK constraint defines a condition that each row must
satisfy

CHECK constraints can be defined at the column level or table


level.
CREATE TABLE employees
(...
salary NUMBER(8,2) CONSTRAINT emp_salary_min
CHECK (salary > 0),
Create a Table using sub query

Create a table and insert rows by combining the CREATE


TABLE statement and the AS subquery option.

Match the number of specified columns to the number


of subquery columns.

Define columns with column names and default values.


Create a table using query
ALTER TABLE Statement
Use the ALTER TABLE statement to:

Add a new column

Modify an existing column definition

Define a default value for the new column

Drop a column

Rename a column

Change table to read-only status


Read only mode
ALTER TABLE Statement
ALTER TABLE table_name ADD column_name column-definition;

ALTER TABLE customers ADD customer_name varchar2(45);

Add Multiple colums to table

ALTER TABLE table_name ADD (column_1 column-definition,


column_2 column-definition, ... column_n column_definition);

ALTER TABLE customers ADD (customer_name varchar2(45),


city varchar2(40));
Modify column in table
ALTER TABLE customers
MODIFY (customer_name varchar2(100) not null,
city varchar2(75));

Drop column in table


ALTER TABLE table_name DROP COLUMN column_name;

Rename column in table

ALTER TABLE customers RENAME COLUMN


customer_name to cname;
Rename table
ALTER TABLE customers RENAME TO contacts;

SQL PRIMARY KEY on ALTER TABLE

ALTER TABLE Persons ADD PRIMARY KEY (ID);

ALTER TABLE Persons


ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName);

ALTER TABLE Persons DROP CONSTRAINT PK_Person;


DROP statements

A DROP statement in SQL removes an object from a relational database


management system (RDBMS).
Like tables,views,synanyms,indexes

DROP TABLE employees;

ALTER statements
Alter - To modify an existing database object

ALTER TABLE sink ADD bubbles INTEGER;

ALTER TABLE sink DROP COLUMN bubbles;

.
Renaming a table

ALTER TABLE table_name


RENAME TO new_table_name;

Modifying column(s) in a table

ALTER TABLE supplier MODIFY ( supplier_name varchar2(100) not


null, city varchar2(75))

Rename the colums

ALTER TABLE supplier


RENAME COLUMN supplier_name to sname;
.
Integrity Constraints

Data integrity allows to define certain data quality requirements that the data
in the database needs to meet

Not Null
A column in a table can be specified not null. It's not possible to insert a null
in such a column.

create table ri_not_null ( a number not null, b number null, c number );


insert into ri_not_null values ( 1, null, null);

insert into ri_not_null values ( 2, 3, 4);

insert into ri_not_null values (null, 5, 6);

.
Unique Key

The unique constraint doesn't allow duplicate values in a


column.

create table ri_unique ( a number unique, b number );

insert into ri_unique values (4, 5);


insert into ri_unique values (4, 5);

create table ri_3 ( a number, b number, c number, constraint


uq_ri_3 unique (a) );

.
Composite Unique Key

A unique constraint can be extended over multiple columns:

create table ri_3 ( a number, b number, c number, constraint


uq_ri_3 unique (a,b) );

Adding the Adding unique constrain

alter table ri_unique add contraint uq_ri_b unique (b);

Doping the Unique constariaint

Alter table drop contraint ri_unique


.
Primary Key

On a technical level, a primary key combines a unique and


a not null constraint

a table can have at most one primary key

create table ri_primary_key ( a number primary key, b


number );

create table ri_primary_key_1 ( a number, b number, c


number, constraint pk_name primary key (a, b) );
.
Foreign Key

A foreign key constraint (also called referential integrity


constraint) on a column ensures that the value in that column
is found in the primary key of another table.

CREATE TABLE supplier ( supplier_id numeric(10) not null, supplier_name


varchar2(50) not null, contact_name varchar2(50), CONSTRAINT
supplier_pk PRIMARY KEY (supplier_id) );

CREATE TABLE products ( product_id numeric(10) not null, supplier_id


numeric(10) not null, CONSTRAINT fk_supplier FOREIGN KEY
(supplier_id) REFERENCES supplier(supplier_id) );

.
Foreign key with more than one field

CREATE TABLE supplier


( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id, supplier_name)
);

CREATE TABLE products


( product_id numeric(10) not null,
supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
CONSTRAINT fk_supplier_comp
FOREIGN KEY (supplier_id, supplier_name)
REFERENCES supplier(supplier_id, supplier_name) );
.
Check

A check constraint allows to state a minimum requirement


for the value in a column

create table ri_check_1 ( a number check (a between 0


and 100), b number );

create table ri_check_2 ( begin_ number, end_ number,


value_ number, check (begin_ < end_) );

.
Disabling Constraints

create table foo (bar number, baz number, unique (bar, baz));

alter table foo disable unique (bar, baz);

create table foo (bar number, baz number, constraint uq_foo


unique (bar, baz));

alter table foo disable constraint uq_foo;

.
TRUNCATE - remove all records from a table,
including all spaces allocated for the records are
removed

Truncate table employee;

.
Data Manipulation language (DML)

Data Manipulation Language (DML) statements are used for managing


data within schema objects. Some examples:

SELECT - retrieve data from the a database

INSERT - insert data into a table

UPDATE - updates existing data within a table

DELETE - deletes all records from a table, the space for the records remain

MERGE - UPSERT operation (insert or update)

CALL - call a PL/SQL or Java subprogram


.
Examples tables for DML

create table emp(empno number(4),


ename varchar2(30),
job char(10),
mgr number(4),
hiredate date,sal number(7,2),
deptno number(2));

.
Examples tables for DML

create table dept(deptno number(2),dname


varchar2(20),
loc varchar2(20));

create table salgrade(grade number(1),losal


number(4),hisal number(4));

.
Insert statement

insert into emp values( 7369, 'SMITH', 'CLERK', 7902 ,'17-DEC-80' ,800, 20);

insert into emp values(7499, 'ALLEN', 'SALESMAN', 7698, '20-FEB-81', 1600, 30);

insert into emp values(7521, 'WARD', 'SALESMAN', 7698, '22-FEB-81', 1250, 30);

insert into emp values(7698, 'BLAKE', 'MANAGER', '01-MAY-81', 3850, 30);

insert into emp values(7902, 'FORD', 'ANALYST', 7566, '03-DEC-81', 3000, 10);

.
dept

insert into dept values(10, 'STORE' ,'CHICAGO');

insert into dept values(20, 'RESEARCH' ,'DALLAS');

insert into dept values(30, 'SALES', 'NEW YORK');

insert into dept values(40, 'MARKETING', 'BOSTON');

.
SALGRADE

insert into SALGRADE values(1, 700, 1200);

insert into SALGRADE values(2, 1201, 1400);

insert into SALGRADE values(3, 1401, 2000);

insert into SALGRADE values(4, 2001, 3000);

insert into SALGRADE values(5, 3001, 9999);

.
Select Statement

select [distinct] <column(s)>


from <table>
[ where <condition> ]
[ order by <column(s) [asc|desc]> ]

.
Selecting Columns

select LOC, DEPTNO from DEPT;

select from EMP;

Arithmetic operators

select ENAME, DEPTNO, SAL 1.55 from EMP;

.
Sorting

select ENAME, DEPTNO, HIREDATE from EMP;


from EMP order by DEPTNO [asc];

select ENAME, DEPTNO, HIREDATE from EMP;


from EMP order by DEPTNO [asc], HIREDATE desc;

.
Filtering the Data

select JOB, SAL from EMP where MGR = 7698;

Operators
=, != or <>,<, >,<=, =>

select JOB, SAL from EMP where MGR <>7698;

select JOB, SAL from EMP where MGR >= 7698;

.
Set Conditions:

Set Conditions: <column> [not] in (<list of values>)

select from DEPT where DEPTNO in (20,30);

Null value: <column> is [not] null,

select from EMP where MGR is not null

Domain conditions: <column> [not] between <lower bound> and


<upper bound>

select EMPNO, ENAME, SAL from EMP where SAL between 1500
and 2500; .
select JOB, SAL from EMP
where (MGR = 7698 or MGR = 7566) and SAL > 1500;

Select * from emp where ename like M%

Seleect * from emp where enam lime %M

.
String functions

string || string Concatenates string values


INITCAP(string) Converts a string to initial capital letters
LENGTH(string) Returns the number of characters in a string
LOWER(string) Converts a string to all lowercase characters
SUBSTR(string,starting Extracts a portion of a string
value,number of
characters)
UPPER(string) Converts a string to all uppercase characters
LPAD Add characters to the left of a string until a fixed number
is reached
LTRIM LTRIM removed characters from the left of a string if they
are equal to the specified string. Example:
ltrim('aaaaaabc','a') = 'bc'
RTRIM RTRIM removed characters from the right of a string if
. equal to the specified string.
they are
Date Fuctions

ADD_MONTHS Adds the specified number of months to the date value


LAST_DAY(date) Returns the last day of the month that contains the date
MONTHS_ Returns the difference between two dates
BETWEEN(date1,date2)
NEXT_DAY(date,day Returns the date of the first day of the specified name
name) that is later than the date supplied
ROUND (datetime, Returns the date-time rounded to the unit specified by
format) the format,
SYSDATE Returns the current date-time from the server where the
database is located
TRUNC(datetime) Removes the time component from a date-time value

.
Conversion functions

TO_CHAR(date,format) Converts a date to a string in the specified format

TO_CHAR(number, Converts a number to a string in the specified format


format)

TO_DATE(string,format) Converts a string to a date using the specified format

TO_NUMBER Converts a string to a number using the optional


(string, format) format if specified

.
Group Functions

AVG(expression) Returns the average of the values in a set of rows

COUNT(expression) Returns the number of rows in the set

MAX(expression) Returns the largest value from a set of rows

MIN(expression) Returns the smallest value from a set of rows

SUM(expression) Adds the value for all rows in the query or for all rows
with the same values for columns listed in the GROUP BY
clause

.
Numeric functions

ABS(number) Removes the sign, if any, returning a positive value


GREATEST(value1, Returns the largest of the values in the list
value2, )

LEAST(value1, Returns the smallest of the values in the list


value2, )

ROUND(number, Rounds a value to the specified number of decimal


decimal places) places

TRUNC(number, Cuts off a value at the specified number of decimal


decimal places) places
TRUNC(123.456,2) returns 123.45
TRUNC(234567.00,-3) returns 234000
.
Updates

For modifying attribute values of (some) tuples in a table

update <table> set


<column i> = <expression i>, . . . , <column j> =
<expression j>
[where <condition>];

update EMP set


JOB = MANAGER, DEPTNO = 20, SAL = SAL +1000
where ENAME = JONES;

.
update EMP set SAL = SAL 1.15 where DEPTNO in (10,30);

update EMP set SAL = (select min(SAL) from EMP


where JOB = MANAGER)
where JOB = SALESMAN and DEPTNO = 20;

.
Deletions

delete from <table> [where <condition>];

delete from PROJECT where PEND < sysdate;

.
TCL command?

Commit

Rollback

Savepoint

.
Joining Relations

Comparisons in the where clause are used to combine rows


from the tables listed in the from clause.

select ENAME, E.DEPTNO, DNAME from EMP E, DEPT D


where E.DEPTNO = D.DEPTNO and JOB = SALESMAN;

select ENAME, DNAME, PNAME from EMP E, DEPT D,


PROJECT Pwhere E.EMPNO = P.MGR
and D.DEPTNO = E.DEPTNO;

.
Self Join

List the names of all employees together with the name of


their manager

select E1.ENAME, E2.ENAME


from EMP E1, EMP E2
where E1.MGR = E2.EMPNO;

.
Sub Query

List the name and salary of employees of the department


20 who are leading a project that started before December
31, 1990:

select ENAME, SAL from EMP


where EMPNO in
(select PMGR from PROJECT
where PSTART < 31-DEC-90)
and DEPTNO =20;

.
List all employees who are working in a department located
in BOSTON:

select from EMP where DEPTNO in (select DEPTNO from


DEPT where LOC = BOSTON);

Retrieve all employees who are working in department 10


and who earn at least as much as any (i.e., at least one)
employee working in department 30:

select from EMP where SAL >= any(select SAL from EMP
where DEPTNO = 30) and DEPTNO = 10;
.
List all departments that have no employees:

select from DEPT


where not exists
(select from EMP
where DEPTNO = DEPT.DEPTNO);

.
Set operators

union [all] returns a table consisting of all rows either


appearing in the result of <query1> or in the result of
<query 2>. Duplicates are automatically eliminated unless
the clause all is used.

intersect returns all rows that appear in both results <query


1> and <query 2>.

minus returns those rows that appear in the result of


<query 1> but not in the result of <query 2>.

.
Views

create [or replace] view <view-name> [(<column(s)>)] as


<select-statement>

The following view contains the name, job title and the
annual salary of employeesworking in the department 20:
create view DEPT20 as

select ENAME, JOB, SAL12 ANNUAL SALARY from EMP


where DEPTNO = 20;

.
DECODE

Select deptno,decode(deptno, 10,accounts,


20,marketing,
30,hra,stores)
as dept_name from dept

.
Nvl function

The NVL and NVL2 functions allow you to test an


expression to see whether it is NULL

Select nvl(ename,no name) as name from emp;

.
CASE

A searched CASE expression evaluates a number of conditions


and returns a result determined by which condition is true

CASE
WHEN C1 THEN R1
WHEN C2 THEN R2
...
WHEN CN THEN RN
ELSE RD
END

.
SELECT TO_CHAR(order_dt, 'Q') sales_quarter,
SUM(sale_price) tot_sales FROM cust_order
WHERE order_dt >= TO_DATE('01-JAN-2001','DD-MON-
YYYY)AND order_dt < TO_DATE('01-JAN-2002','DD-MON-
YYYY') GROUP BY TO_CHAR(order_dt, 'Q)ORDER BY 1;

S TOT_SALES
- ----------
1 9739328
2 10379833
3 9703114
4 9772633
.
SELECT
SUM(DECODE(TO_CHAR(order_dt, 'Q'), '1', sale_price, 0)) Q_1,
SUM(DECODE(TO_CHAR (order_dt, 'Q'), '2', sale_price, 0)) Q_2,
SUM(DECODE(TO_CHAR (order_dt, 'Q'), '3', sale_price, 0)) Q_3,
SUM(DECODE(TO_CHAR (order_dt, 'Q'), '4', sale_price, 0)) Q_4
FROM cust_order
WHERE order_dt >= TO_DATE('01-JAN-2001','DD-MON-YYYY')
AND order_dt < TO_DATE('01-JAN-2002','DD-MON-YYYY');

Q_1 Q_2 Q_3 Q_4


---------- ---------- ---------- ----------
9739328 103798 33 970311 4 9772633

.
SELECT LNAME, EMP_ID, MANAGER_EMP_ID FROM EMPLOYEE
START WITH MANAGER_EMP_ID IS NULL CONNECT BY PRIOR
EMP_ID = MANAGER_EMP_ID;

LNAME EMP_ID MANAGER_EMP_ID


-------------------- ---------- --------------
KING 7839
JONES 7566 7839
SCOTT 7788 7566
ADAMS 7876 7788
FORD 7902 7566
SMITH 7369 7902
BLAKE 7698 7839
ALLEN 7499 7698
.

You might also like