SQL
Structured Query Language
C-DAC HYDERABAD www.cdachyd.in
Relational Database
Dr. E.F. Codd proposed the relational model for
database systems in 1970.
It is the basis for the relational database
management system (RDBMS).
The relational model consists of the following:
Collection of objects or relations
Set of operators to act on the relations Data integrity for accuracy and consistency
C-DAC Hyderabad
3.2
Definition of Relational DB
A relational database is a collection of relations or
two-dimensional tables.
C-DAC Hyderabad
3.3
Relational Concepts
A relational database is a collection of data where
logically related data is grouped into tables (files) within the database
Rows (records) appear horizontally in a report, and
contain one or more columns
Tables within a relational database hold columns
(fields) of data that appear vertically in a report
C-DAC Hyderabad
3.4
Data Model
C-DAC Hyderabad
3.5
Relational Database Properties
A Relational Data base
Can be accessed and modified by executing
structured query language (SQL)
Contains a collection of tables with no physical
pointers
Uses a set of operators
C-DAC Hyderabad
3.6
Relational Database Management System
C-DAC Hyderabad
3.7
Structured Query Language(SQL)
When a user wants to get some information from a
database file, he can issue a query
A query is a userrequest to retrieve data or
information with a certain condition.
SQL is the Standard Query Language used for
retrieving and modifying data in a relational database.
C-DAC Hyderabad
3.8
Communicating with RDBMS using SQL
C-DAC Hyderabad
3.9
SQL Statements
C-DAC Hyderabad
3.10
Summary
Oracle9i is based on the object relational database
management system.
Relational databases are composed of relations,
managed by relational operations, and governed by data integrity constraints.
With the Oracle Server, you can store and manage
information by using the SQL language and PL/SQL engine.
C-DAC Hyderabad
3.11
DATA DEFENITION LANGUAGE (DDL)
C-DAC HYDERABAD www.cdachyd.in
DDL Commands
For Creating and Managing Tables
CREATE ALTER DROP RENAME TRUNCATE
C-DAC Hyderabad
3.13
Naming Rules
Table Names and Column Names
Must begin with a letter Must be 130 characters long Must contain only AZ, az, 09, _, $, and # Must not be an Oracle server reserved word
C-DAC Hyderabad
3.14
The CREATE TABLE Statement
CREATE TABLE table name (column name datatype [DEFAULT expr][, ...]); You specify:
Table name
Column name, column data type, and column size
C-DAC Hyderabad
3.15
The DEFAULT Option
Specify a default value for a column during
an insert.
The default data type must match the column
data type.
Literal values, expressions, or SQL functions
are legal values.
C-DAC Hyderabad
3.16
DATA TYPES
C-DAC Hyderabad
3.17
Creating Tables
Example: Create table
CREATE TABLE dept (deptno NUMBER(2), dname VARCHAR2(14), loc VARCHAR2(13)); Table created.
Confirm table creation.
DESCRIBE dept
C-DAC Hyderabad
3.18
Tables In Oracle Database
User Tables:
Are a collection of tables created and maintained by the user. Contain user information
Data Dictionary :
Is a collection of tables created and maintained by the Oracle Server. Contain database information
C-DAC Hyderabad
3.19
Relating Multiple Tables
Primary key :
Each row of data in a table is uniquely identified by a primary key (PK).
Foreign key :
You can logically relate data from multiple tables using foreign keys (FK).
C-DAC Hyderabad
3.20
The ALTER TABLE Statement
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
C-DAC Hyderabad
3.21
The ALTER TABLE Statement
Use the ALTER TABLE statement to add, modify, or
drop columns.
C-DAC Hyderabad
3.22
Adding a Column
You use the ADD clause to add columns.
The new column becomes the last column
C-DAC Hyderabad
3.23
Modifying A Column
You can change a columns data type, size, and
default value.
Table altered. A change to the default value affects only subsequent
insertions to the table.
C-DAC Hyderabad
3.24
Dropping a Column
Use the DROP COLUMN clause to drop columns
C-DAC Hyderabad
3.25
Dropping a Table
All data and structure in the table is deleted.
Any pending transactions are committed. You cannot roll back the DROP TABLE statement.
C-DAC Hyderabad
3.26
Changing Name Of An Object
To change the name of a table, view, sequence, or
synonym, you execute the RENAME statement.
C-DAC Hyderabad
3.27
Truncating a Table
The TRUNCATE TABLE statement:
-Removes all rows from a table -Releases the storage space used by that table
You cannot roll back row removal when using TRUNCATE. DELETE statement.
Alternatively, you can remove rows by using the
C-DAC Hyderabad
3.28
Summary
C-DAC Hyderabad
3.29