database systems: design, implementation, and management
DESCRIPTION
Database Systems: Design, Implementation, and Management. Chapter 7 Introduction to Structured Query Language (SQL). Data Definition Language (DDL) and Data Manipulation Language (DML). Data Definition Language (DDL) defines the structure of the database - PowerPoint PPT PresentationTRANSCRIPT
BTM 382 Database Management
Chapter 7Introduction to SQL(Structured Query Language)
Chitu OkoliAssociate Professor in Business Technology Management
John Molson School of Business, Concordia University, Montréal
Data Definition Language (DDL) andData Manipulation Language (DML)
Data Definition Language (DDL) defines the structure of the database E.g. tables, columns, keys, indexes, etc.
Data Manipulation Language (DML) manipulates the actual data contents E.g. adding, modifying and deleting data
Understanding the difference between these two types of SQL commands will help you master SQL
Four major kinds of database operations:Create, Read, Edit/Update, Delete (CRED/CRUD)Data Definition
Language (DDL):Change database structure
Data Manipulation Language (DML):Change data contents inside tables
Create
CREATE tables and other structures (also ADD)
INSERT data into tables
Read SELECT (system catalogue queries)
SELECT data from tables
Edit/ Update
ALTER table and other structures (also MODIFY)
UPDATE table
Delete DROP tables and other structures
DELETE from table
COMMIT and ROLLBACK in Oracle(DML commands) COMMIT
Saves all DML changes since the last COMMIT or DDL command Whenever you issue any DDL command, COMMIT is automatically
triggered In other words, whenever you issue a command that changes
the structure of the database, all data contents are automatically saved right away
If you do not COMMIT before closing the connection, all uncommitted DML commands (changes to data contents) will be lost
ROLLBACK Cancel all DML changes since the last COMMIT or DDL
command Microsoft Access automatically commits every command (you
cannot undo any changes)
Oracle character types: CHAR and VARCHAR2 CHAR(n)
Always a string of exact length of n (any unused characters are filled with blanks)
E.g. Hello in CHAR(10) is stored as “Hello ” (10 characters) VARCHAR2(n)
A string of maximum length of n (any unused characters are empty) E.g. Hello in VARCHAR2(10) is stored as “Hello” (5 characters)
Converts empty strings (‘’) to null VARCHAR(n)
Same as VARCHAR2, except that it distinguishes between null and empty string (‘’)
Not used; automatically converted to VARCHAR2 for now Might be used in the future, so just don’t ever use it
Best practice (recommendation, not requirement): Only use VARCHAR2 all the time; there is no good reason to ever use anything else
WHERE versus HAVING clauses
WHERE is used to restrict a subset of rows from a regular query result Usually a SELECT query, but also UPDATE and DELETE
HAVING is used to restrict a subset of rows when using a GROUP BY aggregation HAVING only works with GROUP BY
Mnemonics to help you not confuse the two: SELECT FROM WHERE? (WHERE is the normal clause for
a SELECT statement)SELECT column-listFROM tablesWHERE conditions
G-H (GROUP BY goes with HAVING)
Set date and number formats in Oracle
Some data (e.g. dates and numbers) assume that certain formats are being used; using different formats can give errors
ALTER SESSION is used to set environment variables, including date and number formats Changes will last until you end the session (e.g. close Oracle SQL
Developer); then they will revert to the default Date formats: If you have problems entering dates, execute this SQL
command before all your other commands: alter session set nls_date_format = 'dd-mm-yyyy';
You need to customize the date format to exactly what you want
You can also use the function: to_date( '20150301', 'YYYYMMDD' ) Language and number formats: These commands might help:
alter session set nls_language = English; alter session set nls_territory = Canada; alter session set nls_date_format = 'dd-mm-yyyy';