Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
104 views

Structured Query Language SQL: Htet Mon Win Banking Division ACE Data Systems

The document discusses Structured Query Language (SQL) and its components. It describes: 1. The five main components of SQL - Data Definition Language (DDL) to define the database structure, Data Manipulation Language (DML) to manipulate data, Data Control Language (DCL) to control access, Data Query Language (DQL) to query data, and Transaction Control Language (TCL) to manage transactions. 2. DDL commands like CREATE, ALTER, and DROP to create, modify, and remove database objects like tables. 3. Data types in Oracle like character strings, numbers, dates, and LOBs to define the types of data that can be stored in database

Uploaded by

Kelvin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Structured Query Language SQL: Htet Mon Win Banking Division ACE Data Systems

The document discusses Structured Query Language (SQL) and its components. It describes: 1. The five main components of SQL - Data Definition Language (DDL) to define the database structure, Data Manipulation Language (DML) to manipulate data, Data Control Language (DCL) to control access, Data Query Language (DQL) to query data, and Transaction Control Language (TCL) to manage transactions. 2. DDL commands like CREATE, ALTER, and DROP to create, modify, and remove database objects like tables. 3. Data types in Oracle like character strings, numbers, dates, and LOBs to define the types of data that can be stored in database

Uploaded by

Kelvin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Structured Query Language

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:

– UPPERCASE text for reserved words

– semi-colon to terminate end of statement

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

• is used to store fix length character strings.

• Fixed length character string that will be blank padded with spaces to its maximum
length and allows any character

• Max 2000 characters Char(5)


a n t
• One char takes one byte of storage

Free space
VARCHAR2

• is used to store variable length character strings.

• 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

• A fixed-length character string that contains UNICODE formatted data.

• 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

• a variable-length character string that uses the national character set.

• The maximum length of an NVARCHAR2 column is 4000 bytes.


Varchar(5)
a n t

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.

Note : precision = total number of digits


scale = number of digits to the right of the decimal point
NUMBER (cont’d)
• Fixed-point NUMBER
– NUMBER ( p, s )
– Precision ( p ) = length of the number in digits with precision of up to 38
– Scale ( s ) = places after the decimal point, or (for negative scale values)
significant places before the decimal point.
– The scale can range from -84 to 127.

• 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)

Input Data Specified As Stored As


7,456,123.89 NUMBER 7456123.89
7,456,123.89 NUMBER(*,1) 7456123.9
7,456,123.89 NUMBER(9) 7456124
7,456,123.89 NUMBER(9,2) 7456123.89
7,456,123.89 NUMBER(9,1) 7456123.9
7,456,123.89 NUMBER(6) (not accepted, exceeds
precision)
7,456,123.89 NUMBER(7,-2) 7456100
Floating-Point Numbers
BINARY_FLOAT
• 32-bit, single-precision floating-point number datatype.
• Each BINARY_FLOAT value requires 5 bytes, including a length byte.

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.

• Used to specify data:

– FLOAT //default is 126

– FLOAT(n)

• n = the number of bits of precision that the value can store.

• n can range from 1 to 126.


Date/ Time
 stores point-in-time values (dates and times) in a table.

 stores the year (including the century), the month, the day, the hours,
the minutes, and the seconds (after midnight).

 from January 1, 4712 BC to December 31, 9999 AD.

 Oracle Database stores time in 24-hour format—HH:MI:SS.

 By default, the time in a date field is 00:00:00 A.M. (midnight)


LOB datatypes
• BLOB, CLOB, NCLOB, and BFILE enable you to store and manipulate large blocks of
unstructured data (such as text, graphic images, video clips, and sound waveforms) in binary
or character format.
• Oracle recommends that you always use LOB datatypes over LONG datatypes.
BLOB
• stores unstructured binary data in the database.
• can store up to 128 terabytes of binary data.
CLOB and NCLOB
• store up to 128 terabytes of character data in the database.
• CLOBs store database character set data.
• NCLOBs store Unicode national character set data.
BFILE
• stores unstructured binary data in operating-system files outside the database.
• A BFILE column or attribute stores a file locator that points to an external file containing the
data.
• The amount of BFILE data that can be stored is limited by the operating system.
• BFILEs are read only.
Components of SQL
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
5th component of SQL ( not an ANSI standard)
5. Data Transaction Language/ Transaction Control Language (
DTL /TCL) – commit, rollback, savepoint
Data Definition Language (DDL)
Data Definition Language (DDL)
– Is the subset of SQL used to define and modify various data structures

– define/ change/ remove database objects

– access rights to objects

– used to define the database structure or schema


• CREATE - create objects in the database
• ALTER - alters the structure of the database
• DROP - delete objects from the database
DDL: Creating Table
• No limitation on number of rows per table
• Max 1000 columns per table
• Only 1 LONG column per table (either LONG or LONG RAW) because
it slows down the speed of processing
Syntax:
• CREATE TABLE <tbname> (
<colname> <type>,
<colname> <type>,
<colname> <type>, …);
DDL: Modifying Tables
Adding a new column to an existing table
ALTER TABLE table_name ADD (column_name column_type);

Adding new columns to an existing table


ALTER TABLE table_name ADD (column_name column_type, column_name
column_type,…);

Modifying a column’s datatype or size


ALTER TABLE table_name MODIFY column_name column_type;

Droping a column from existing table


ALTER TABLE table_name DROP COLUMN column_name;

Renaming table name


ALTER TABLE oldtable_name RENAME TO newtable_name;

Renaming a column’s name


ALTER TABLE table_name RENAME COLUMN oldcolumn_name TO newcolumn_name;
DDL: Removing Tables from the
Database
Deleting table (Not Permanently)
DROP TABLE table_name;
 show RECYCLEBIN; (OR) select * from RECYCLEBIN; (OR) select * from USER_
RECYCLEBIN;
 PURGE RECYCLEBIN;

Deleting table (Permanently)


if you want to drop the table permanently , without needing a flashback feature
DROP TABLE table_name PURGE;

Deleting table that contains “Foreign Key” (Permanently)


DROP TABLE table_name CASCADE CONSTRAINTS;
DROP TABLE table_name PURGE;
DDL: Removing Tables from the
• Database
Drop : The drop table command moves a table into the recycle bin unless
purge was also specified.

• 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:

FLASHBACK TABLE employees TO BEFORE DROP;

• If another employees table has been created in the hr schema, use the
RENAME TO clause to rename the retrieved table:

FLASHBACK TABLE employees TO BEFORE DROP RENAME TO


employees_old;
DDL: Retrieving a Dropped Table
• If you know that the employees table has been dropped multiple times,
and you wish to retrieve the oldest version, query the USER_RECYLEBIN
table to determine the system-generated name, and then use that name
in the FLASHBACK TABLE statement. (System-generated names in your
database will differ from those shown here.)
SELECT object_name;

DROPTIME FROM USER_RECYCLEBIN


WHERE ORIGINAL_NAME = 'employees';

• 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.

– maintain and query a data

– used to manipulate the data in the database objects.


• 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
DML : Adding Rows to a table
(INSERT)
Syntax:
1. INSERT INTO table_name
VALUES (value_1, value_2, ….., value_n);

2. INSERT INTO table_name (column_1, column_2,…., column_n)


VALUES (value_1, value_2,….., value_n);

(Inserting NULL values)


3. INSERT INTO table_name
VALUES (value_1, value_2, NULL, NULL, NULL);

4. INSERT INTO table_name


VALUES ( data1, data2, data3,’’); //no space within single quote
DML: Modifying Existing Data
(UPDATE)
1. Updating value of single column
UPDATE table_name SET column_name=value WHERE condition;

2. Updating values of multiple columns


UPDATE table_name SET column_name=value, coulumn_name=value, …..
WHERE condition;

3. Updating column with Multiple Conditions


UPDATE table_name SET column_name=value
WHERE condition1 AND/OR condition2;
DML: Modifying Existing Data
(UPDATE)
4. Updating value of column with computed field
UPDATE table_name SET coulumn_name=expression WHERE condition;
Example:
UPDATE customers
SET c_details = (SELECT contract_date FROM suppliers WHERE
suppliers.supplier_name = customers.customer_name)
WHERE customer_id < 1000;

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);

5. Updating value of column for ALL rows


UPDATE table_name SET column_name=value;
DML: Removing Data
1. Deleting Selected Rows from the table
DELETE FROM table_name WHERE condition;
OR
DELETE FROM table_name
WHERE condition1 AND/OR condition2;

2. Deleting ALL Rows from the table


DELETE FROM table_name;

(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.)

DROP TABLE table_name;


Data Control Language (DCL)
Data Control Language (DCL)
– are commands to control which users can access specific data (tables,
views, and so on).
– includes the GRANT and REVOKE commands, which you use to grant and
revoke privileges, as well as the SET ROLE command, which enables or
disables roles in your current session.

DCL Commands: Data Control Language (DCL) statements


• GRANT – gives user’s access privileges to database
• REVOKE – withdraw access privileges given with the GRANT command
• SET ROLE – To enable the role (e.g. dw_manager) identified by the password
(e.g. warehouse) for your current session
Data Control Language (DCL)
GRANT
• The following statement grants the SELECT, INSERT, and DELETE object
privileges for all columns of the emp table to the users jfee and tsmith:

GRANT SELECT, INSERT, DELETE ON emp TO jfee, tsmith;

• The following statement grants INSERT privilege on the acct_no column of


the accounts table to scott:
• GRANT INSERT (acct_no) ON accounts TO scott;
Data Control Language (DCL)
REVOKE
• The following statement revokes the CREATE TABLE system privilege and
the accts_rec role from tsmith:
REVOKE CREATE TABLE, accts_rec FROM tsmith;

• The following statement revokes the SELECT and INSERT privileges


on the emp table from the users jfee and tsmith:
REVOKE SELECT, insert ON emp FROM jfee, tsmith;

• 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;

To enable all roles granted to you for the current session:


• SET ROLE ALL;

To enable all roles granted to you except dw_manager:


• SET ROLE ALL EXCEPT dw_manager;

To disable all roles granted to you for the current session:


• SET ROLE NONE;
Thank You!

You might also like