Structured Query Language SQL: Htet Mon Win Banking Division ACE Data Systems
Structured Query Language SQL: Htet Mon Win Banking Division ACE Data Systems
SQL
Htet Mon Win
Banking Division
ACE Data Systems
Content
• Nature of SQL Commands
• Naming Database Objects
• Data Types of Oracle
• Components of SQL
1. Data Definition Language (DDL) – create, drop, alter
2. Data Manipulation Language (DML) – insert, update, delete
3. Data Control Language (DCL) – grant , revoke
4. Data Query Language (DQL) – select
5. Data Transaction Language/ Transaction Control Language (
DTL /TCL) – commit, rollback, savepoint
Nature of SQL Commands
• Common Practices:
Eg.
SELECT * FROM Customer_Table;
Naming Database Objects
• Max 30 characters
• Names are NOT case sensitive.
• A-Z, a-z, 0-9
• Must begin with an alphabet, not number ( It is compulsory)
• $, #, _ allowed (but $ and # will not work other DBMS)
• Quotation marks, - , “ and ” are not allowed in names.
• Some reserved words are not allowed (such as Table, Group, Insert,
Create, Order, Key, etc..)
Data Types of Oracle
• The term data types refer to the kinds of data that can be stored in
a particular column.
• There are four basic character string types in Oracle, namely CHAR,
VARCHAR2, NCHAR, and NVARCHAR2.
• LONG, LONG RAW, RAW data types
• Numeric data types in Oracle, namely NUMBER, FLOAT and
INTEGER
• DATE/TIME data type in Oracle, namely DATE
• Large Object (LOB) data types
Character String Data Types
CHAR
• Fixed length character string that will be blank padded with spaces to its maximum
length and allows any character
Free space
VARCHAR2
• The string value's length will be stored on disk with the value itself.
Varchar(5)
• it will not occupy any space.
a n t
• can store up to 4000 bytes of characters.
No free space
Character String Data Types ( Cont’d )
NCHAR
• The national character set (UTF8 or UTF16 ) is defined when the database is
created. The default is UTF16. Char(5)
a n t
• The maximum length of an NCHAR column is 2000 bytes.
Free space
NVARCHAR2
No free space
LONG, LONG RAW, RAW Data types
LONG
• store variable-length character strings containing up to 2 gigabytes (bigger
version of the VARCHAR2 datatype).
• A table can only have one LONG column.
• Do not create tables with LONG columns because it is supported only for
backward compatibility.
RAW
• is used for storing variable length binary data or byte strings.
• Maximum size is 2000 bytes
LONG RAW
• Is used for storing binary data of variable length up to 2 gigabytes.
• a table can only have one LONG RAW column.
• can be used to store graphics, sound, documents, or arrays of binary data.
NUMBER Data types
• Ranges of which is supported starting from 1.0x10 -130 to 9.9x10126
• Used to specify data:
– NUMBER, NUMBER (precision ) or NUMBER (precision, scale)
• If scale is negative then data is rounded to scale digits to the left of decimal points.
Thus, for specification NUMBER(10,-3) for number 123456, the storage is 123000.
• Oracle NUMBER types are very precise- much more so than normal FLOAT and
DOUBLE types found in many programming language.
• Integer NUMBER
– NUMBER ( p ) OR NUMBER ( p, 0 )
– a fixed-point number with precision p and scale 0.
• Floating-point NUMBER
– NUMBER
– Floating-point number with decimal precision 38
– The absence of precision and scale designators specifies the maximum
range and precision for an Oracle number.
NUMBER (cont’d)
BINARY_DOUBLE
• 64-bit, double-precision floating-point number datatype.
• Each BINARY_DOUBLE value requires 9 bytes, including a length
byte.
Float
• Oracle Database also supports the ANSI datatype FLOAT.
– FLOAT(n)
stores the year (including the century), the month, the day, the hours,
the minutes, and the seconds (after midnight).
• Purge : Normally, a table is moved into the recycle bin (as of Oracle 10g), if
it is dropped. However, if the purge modifier is specified as well, the table
is unrecoverable (entirely) dropped from the database.
• Cascade constraints : Deletes all foreign keys that reference the table to
be dropped, then drops the table
DDL: Retrieving a Dropped Table
• If you accidentally drop the hr.employees table and wish to retrieve it,
issue the following statement:
• If another employees table has been created in the hr schema, use the
RENAME TO clause to rename the retrieved table:
• OBJECT_NAME DROPTIME
• ------------------------------ -------------------
RB$$45703$TABLE$0 2003-06-03:15:26:39
RB$$45704$TABLE$0 2003-06-12:12:27:27
RB$$45705$TABLE$0 2003-07-08:09:28:01
Data Manipulation Language
(DML)
Data Manipulation Language (DML)
– Is the subset of SQL used to access and manipulate data contained
within the data structures previously defined via DDL.
UPDATE suppliers
SET supplier_name = (SELECT customers.customer_name FROM
customers WHERE customers.customer_id = suppliers.supplier_id)
WHERE EXISTS (SELECT customers.customer_name FROM customers
WHERE customers.customer_id = suppliers.supplier_id);
(Note: The Delete Statement does not remove the specified table from database; only data is removed.
The table remains in database. The DROP command is used to remove a table from a database.)
• The following statement revokes all object privileges for the dept
table that you originally granted to the human_resource role
REVOKE ALL ON dept FROM human_resources;
Data Control Language (DCL)
Setting Roles Examples
To enable the role dw_manager identified by the password warehouse
for your current session:
• SET ROLE dw_manager IDENTIFIED BY warehouse;