SQL-Basic queries
SQL-Basic queries
CONTENTS
What is SQL?
DML and DDL
Select Query
Data types in SQL
Operators allowed in the where clause of SQL
Alias names for tables and columns
Predefined functions in SQL
INSERT query
UPDATE query
DELETE query
COMMIT and ROLLBACK
APPENDIX A,B,C
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL is an ANSI standard(American National Standards Institute)
In oracle,the data is stored in RDBMS(Relational Database Management
System)
The data is stored in RDBMS in database objects called tables and it
consists of columns(fields) and rows(records).
EMPLOYEES TABLE
Desc employees;
DATA TYPES:
1. DATE
2. NUMBER(size,precision)
3. VARCHAR(SIZE)
DATE TYPES:
Day: dd (01-31),dy(mon-sun),day(Monday-Sunday)
Month: mo (01-12),mon(Jan-Dec), Month(January-December)
Year: YY(10,11 etc),YYYY(2011,1989 etc)
Note:
Character is case-sensitive and Date is format sensitive. Default date
format 'dd-mon-yy'
SELECT query
Example:
Select salary,salary+200 “increment” from departments;
Select salary*12 annual_Salary from departments;
Select (salary+200)*12 “incremented annual salary” from departments;
Concatenation Operator:
Select First_name || Last_name as “Full_Name” from employees;
Select First_name || ' ' || Last_name as “Full_Name” from employees;
Select First_name || 'is in' || City from employees;
Alternative Quote Operator:
Select First_name || q'[it's first name]' || Last_name from employees;
Delimitors can be [] , {} , <>
Syntax : q'delimitor <string> delimitor '
Operator Description
= Equal to
Write a query to change the Last_name of EMP_ID 501233 to Guru
Analyze what will happen if “where” keyword is not used in UPDATE
statement.
DELETE query
SYNTAX:
DELETE FROM table_name
WHERE some_column=some_value;
DELETE FROM table_name (or) DELETE * FROM table_name
Delete is used to delete a particular record(row) from a table
Delete is also used to delete all the contents(all rows) of table
However the table structure remains
COMMIT AND ROLLBACK
COMMIT -For DML statements,explicit commit is needed. DDL statements
are auto-commited.
ROLLBACK – DML statements can be rolled back,DDL statements cannot be
rolled back.
Write a query to change the 'Skills' of emp_id 333118 to Websphere and
analyze with two users
Findings:
Concept of mutiple user logging,locks & Redo-log buffer
The values will be reflecting only in current session. If some other user
logs-in to db and queries the table, the old value will be displayed
So we need give commit manually for other users to view the changes
Since it is a DML it can be rolled backed.. but once we give commit
command, it cant be changed. Rollback is nothing but an UNDO
APPENDIX-A:EMPLOYEES TABLE
Emp_ID First_nam Last_nam Address City Experie
e e nce