You are on page 1of 31

VENKATESH

9014403515

ORACLE (Orchid Relational Arithmetic Calculating Logical Engine)


DATA BASE SQL SURMGR DBA PROGRAMMIN G PL/SQL PRO *C PRO *C++ PRO *COBOL DEVELOPE R FORMS REPORTS GRAPHICS MENUS EXPORT IMPORT LOAD RESTORE UTILITIES

SQL-8 (Structured Query Language) Uses


1. To maintain business data in tables. 2. Analysis for data 3. Reports for data(printing) 4. Calculations
Arithmeti c Abs Cos Sin Tan Log Sqrt Round Ceil Floor Power Upper Lower Initcap Ltrim Rtrim Concat Decede Substr Clr Ascii Sounde x Rpad Sysdate To_Date To_Char Next_Day Last_Day Add_Months Months_Betwee n String Date/Time Statistica l Max Min Sum Average Count Stddv Group To_Char To_Date To_Rownum To_Rowlabl e To_Rowid To_Number Ed @ Get @@ / Start Change Save Delete Exit Cut Copy Paste Host 1 Conversion Edit

VENKATESH PREDEFINED SQL COMMANDS

9014403515

SQL Operators
OPERATORS ARTHMATIC RELATIONAL LOGICAL SQL SYMBOLS +,-,*,/ <,>,<=,>=,= AND, OR, NOT IN,ANY,ALL,SOLM,BETWEEN

STEPS TO START SQL: START->PROGRAMS->ORACLE 95->SQL PLUS LOGIN USERNAME PASS WORD TIGER OK CANCEL SQL>SELECT 10*10*10 FROM DUAL; 1000 SQL> SELECT 10*10, 20*20 FROM DUAL; 100,400 ARITHMETIC FUNCTIONS SQRT: Command Displays Squire Root Of A Number SQL>SELECT SQRT(9) FROM DUAL; 3 SQL>SELECT SQRT(10*19) FROM DUAL; 13.7840488 ABS: A Function Converts Negative Number In To Positive SQL>SELECT ABS(-10) FROM DUAL; 10 SQL>SELECT ABS(10-30) FROM DUAL; 20 2

SCOTT SQL> SELECT 10+10 FROM DUAL; 20

VENKATESH POWER: Function Displays Xn Value . SQL>SELECT POWER(10,3) FROM DUAL; 1000 SQL>SELECT POWER(15,4) FROM DUAL; 50625 SQL> SELECT POWER(2+3,3) FROM DUAL; 25 (THIS MEANS (2+3)2 ) FLOOR: A Function Rounds The Number Decimal Places To Least Integer CEIL: A Function Rounds The Number Decimal Places To Largest Integer

9014403515

ROUND: A Function Rounds The Number Decimal Places To Specified Number Of Digits SQL> SELECT FLOOR (1000.30) FROM DUAL; 1000 SQL> SELECT CEIL (1000.30) FROM DUAL; 1001 SQL> SELECT ROUND (1000.33333,2) FROM DUAL; 1000.33 SQL> SELECT CEIL(SQRT (10)) FROM DUAL; 3.16227766 MOD: Displays Remainder Of Two Number. SQL>SELECT MOD (10,5) FROM DUAL; 0 SQL>SELECT 10/5 FROM DUAL; 2 SIN,COS,TAN,LOG: Trigonometric Functions Used To Find Out Respective Values SQL> SELECT LOG (1000,1) FROM DUAL; LOG(1000,1) ----------0 SQL> SELECT LOG(3000,2) FROM DUAL; .08657449 SQL> SELECT COS (10) FROM DUAL -.8390715 SQL> SELECT SIN(10) FROM DUAL; -.5440211 3

VENKATESH UPPER: Function Converts Text Into Capital Letters. LOWER: Function Converts Text Into Lower Letters. INITCAB: Function Coverts Into Initial Capital Letters. SQL> select upper('bits computers') from dual; BITS COMPUTERS SQL> select lower('BITS COMPUTERS') from dual; bits computers SQL> select initcap('sreeni vasula reddy') from dual; Sreeni Vasula Reddy LENGTH: A Command Displays Number Of Characters In A Word SQL >SELECT LENGTH (BITS COMPUTERS) FROM DUAL; 13 SQL>SELECT LENGTH(1000) FROM DUAL; 4 LTRIM: Function Eliminates Left Spaces RTRIM: Function Eliminates Right Spaces SQL>Select Ltrim ( Bits Computers) From Dual; BITS COMPUTERS SQL> select rtrim('bits computers SQL> select rtrim('bits computers DATE&TIME FUNCTIONS : SYSDATE: A Command Displays Current Date SQL>select sysdate from dual; 14-12-2004 SQL>select sysdate +45 from dual; 28-1-2005 SQL>select sysdate-45 from dual; 30-10-2004 ALTER SESSION: A Command Used To Change System Date Format SQL>ALTER SESSION SET NLS_DATE_FORMAT=DD/MM/YYYY SESSION ALTERED. ') from dual; ') from dual; BITS COMPUTERS BITS COMPUTERS

9014403515

VENKATESH SQL>select sysdate from dual; 14/12/2004 TO-DATE: A Function Used To Input User Dates SQL>select to_date(10-10-1982) from dual; 10/10/1982 SQL>select sysdate-to_date(15-08-1947) from dual; 20941.701 SQL>select sysdate-to_date (20-07-1984) from dual; 7452.70145 SQL>select (sysdate-to_date(20-07-1984))/365 from dual; 20.418361 TO-CHAR: A Function Used To Change Date Styles And Formats. YEAR STYLES: YYYY-Displays Year With Century. YYY-Displays Last Three Digits Of Year YY-Displays Last Two Digits Of Year Y-Displays Last Digit Of Year YEAR-Displays Year In Words

9014403515

MONTHS STYLES: MM-Displays Month In Number MON-Displays Month In Characters MONTH-Displays Full Month In Characters RM- Roman Numeric month DATE STYLES: DD-Displays Days Of The Month DDD-Displays Day Of The Year. DY-Displays Day Of The Week DAY-Displays Day Of The Week D-Displays Day Of The Week. TIME STYLES: HH-Displays Hours MI-Displays Minuits SS-Displays Seconds 5

VENKATESH HH24- Displays Hour In 24 Clock GENERAL STYLES: J-Julian Number SP-Spell Number EXAMPLES: SQL>Select To_Char(Sysdate,yyyy) From Dual; 2004 SQL>Select To_Char(Sysdate,yy) From Dual; 04 SQL>Select To_Char(Sysdate,Y) From Dual; 4 SQL>Select To _Char(Sysdate,Year)From Dual; TWO THOUSAND FOUR SQL>Select To_Char(Sysdate,Yearsp) From Dual; TWO THOUSAND FOUR ------------------SQL>select to_char(sysdate,mm) from dual; 12 SQL>select to_char(sysdate,mon) from dual; DEC SQL>select to_char(sysdate,month) from dual; DECEMBER SQL>select to_char(sysdate,mmsp) from dual; TWELVE SQL>select to_char(sysdate,mm-yyy-mon-year) from dual; ----------------------------------12-2004-DEC-TWO THOUSAND FOUR

9014403515

SQL>select to_char(sysdate,dd) from dual; 15 SQL>select to_char(sysdate,d) from dual; 4 SQL>select to_char(sysdate,ddd) from dual; 350 SQL>select to_char(sysdate,dy) from dual; WED 6

VENKATESH SQL>select to_char(to_date(10-7-1984),dy) from dual; TUE SQL>select to_char(sysdate,day) from dual; WEDNESDAY SQL>select to_char(sysdate,w) from dual; 3 (WHICH WEEK IN THIS MONTH) 50 4 (WHICH WEEK IN THIS YEAR) SQL>select to_char(sysdate,ww) from dual; SQL>select to_char(sysdate,q) from dual; (WHICH QUARTER) 04:30:56 SQL>select to_char(sysdate,hh24:mm) from dual; 16:30 SQL> select to_char(sysdate,j) from dual; 2453355 SQL>select to_date(2453355,j) from dual; 15-12-2004 SQL>select to_char(to_date(1999,j),jip) from dual; ONE THOUSAND NINE HUNDRED NINETY-NINE SQL>select to_char (to_date(4545,j),jip) from dual; FOUR THOUSAND AND FIVE HUNDRED FORTY-FIVE LAST-DAY: A Function Returns Last Day Of The Month For A Given Date SQL> select last_day(sysdate) from dual; 31/12/2004 SQL>select last_day(last_day(sysdate)+1) from dual; 31/1/2005 NEXT-DAY: A Function Returns Next Coming Day Of A Given Date In The Coming Week SQL>select next_day(sysdate,thu) from dual; 23/12/2004 SQL>select next_day(sysdate,mon) from dual; 20/12/2004 SQL>select to_char(sysdarte,hh-mi-ss) from dual;

9014403515

SQL> select next_day(next_day(last_day(sysdate),sat),sat) from dual; 7

VENKATESH 08-01-2005 SQL>select next_day(last_day(sysdate)sat) from dual; 01/01/2005 ADD-MONTHS:

9014403515

A Function Used To Add Or Reduce Specified Number Of Months For A Given Date SQL>select add_months(sysdate,5) from dual; 16-05-2005 SQL>select add_months(sysdate,-5) from dual; 16-7-2004 SQL>select add_months(sysdate,60) from dual; 16-12-2004 MONTHS-BETWEEN: A FUNCTION GIVES NUMBER OF MONTHS BETWEEN TWO GIVEN DATE; SQL>select months_between(sysdate,to_date(01-01-1998)) from dual; 83.506272 SQL>select months_between(to_date(01-01-2004),to_date(15-12-2004) from dual; SOUNDEX: A FUNCTION USED TO DISPLAY SOUNDEX CODE FOR A GIVEN STRING SQL>select soundex(color), soundex(colour) from dual; C460,C460 SQL>select soundex(reddi), soundex(reddy) from dual; R300,R300 ASCII: A FUNCTION RETURNS ASCII NUMBER FOR A CHARACTER SQL>select ascii(H) from dual; 72 CHR: A FUNCTION DISPLAYS CHARACTER FOR ASCII NUMBER SQL>select chr(72) from dual; H

Ed:
Ed commands opens a editor for typing your sql commands Syntax: ed file name Ex: ed bits

@:
A command used to execute the file 8

VENKATESH Ex: @ bits;

9014403515

C:
A command used to change mistakes in previously typed command. SQL>select sqt(10) from dual; Error at line 1; SQL>C\SRT\SQRT SET SQL PROMPT: A COMMAND USED TO CHANGE THE SQL PROMPT SQL>set sqlprompt BITS> BITS>

SQL DATABASE COMMANDS


SQL data base commands are classified into four types for data base maintenance and database handling. The fallowing chart list outs commands of SQL.

SQL DATABASE
DDL CREATE ALTER DROP TRUNC K DML SELECT UPDAT E INSERT DELETE TCL GRANT REVOKE CONNECT COMMIT ROLL BACK SAVE POINT UTILITIES EXPORT IMPORT SQL LOAD SPOOL SUGMGR DDL COMMANDS: Data definition language commands which provides the user to create,modify and delete of SQL. CREATE: A command used to create objects of SQL. Creation of user: Start->programs->oracle 95->SQL plus->then type username as system password as manager SQL>CREATE USER USERNAME IDENTIFIED BY PASS WORD;

VENKATESH EXAMPLE: SQL>CREATE USER LSVR IDENTIFIED BY SREENU; USER CREATE SQL> GRANT DBA TO LSVR; GRANT SUCCEEDED To change password: SQL> alter user sreenu identified byl.sreenu; USER ALTERED. SHOW USER A command used to display the username in which you are current working. SQL>show user Sreenu

9014403515

DATA TYPES
VARCHAR(SIZE): It is a variable character string the maximum size is 2000 bytes CHAR(SIZE) It is a fixed length character string maximum size is 255 bytes. Default size is 1 byte NUMBER(P,S): It is used to store fixed or floating point number ranging from 1.0*10-130 to 999*10-125 P precision ( total number of digits) S scale DATE: It is used to store date and time information The default format is DD-MON-YY Example: 07-jan-05 LONG RAW: It is used for byte oriented data mainly used for graphics images (or) digital sounds The range is 255 bytes to 2 GB. CREATION OF TABLE A table is a series of rows and columns used for storing the date. Tables are classified in to fort types. They are o Simple tables Integrity constraint table o Master-child tables 10 (the number of digits to right of decimal point) Example: number(5,2)=999.99

VENKATESH o Nested tables

9014403515

CREATING A SIMPLE TABLES


Create table <table name>( Column1 Column2 ---------------------------------------column n <data type><size>; data types are number, char, varchar, date, long row, LOB, BLOB EXAMPLE1: CREATE A TABLE FOR EMPLOYEE TO STORE A EMPLOYEE DETAILS. EMP_NO NAME SALARY DOB ADDRESS <data type><size>, <data type><size>,

QUERY: SQL>create table employee( emp_no number(3), name varchar(20), salary number(10,2), dob date ,address varchar(40)); EXAMPLE2: CREATE A TABLE FOR STUDENT TO STORE A STUDENT DETAILS. STD_NO NAME COURSE AGE DOB

QUERY: SQL> create table student( std_no number(4), Name varchar(15), Course varchar(10), Age number(2),

11

VENKATESH Dob date); TAB: A command displays list of tables SQL>select * from tab; It displays all the tables in the current user SQL>select * from tab where tname like s%; It displays the tables starting letter is s. INSERT: A command used to store records on to tables. Syntax: Insert into <table> values( col1,col2,col2 ..colN) [select];

9014403515

1. To insert character and date columns should enclosed with in single quotations. 2. Numbers can be directly stored. 3. To insert repeated values use & operator. 4. To eliminate any column from insertion use null command. 5. To copy records from one table to another table use select command. To insert records into employee table Insert into employee values(101,mohan,4000); Insert into emp (empno,name) values (&empno,&name); Insert into emp values(&empno,upper(&name),&salary); To insert records using input command: Insert into emp values (&empno,&empname,&salary);

ORACLE NAVIGATOR
Oracle navigator is a facility with graphical oriented options. using oracle navigator user can create,delete and modify objects of SQL. Steps to start oracle navigator 1. click start-programs-oracle95-oracle navigator 2. in navigator click personal oracle-local database 3. click user and from mouse right button select new 4. enter user name and pass word 5. click rolls&privillizes and select DBA permission your user. 6. click ok to create the user 7. from list of options select table and from mouse right click selectnew manual table 12

VENKATESH 8. enter the table name and select user for table 9. click design tab enter table columns and click ok to create your table 10. select your table and from mouse right click select open

9014403515

11. enter data, to insert a new record from mouse right click select insert after 12. click close button

PATTERN MATCH(LIKE):
The comparison operator compared one value exactly to one other value. The like allows for a comparison of one string value with another string value which is not identical. This is achieved by using wild card characters Examples: Display all the names starting with s Select * from emp where ename like s%; Display the employee details whose name containing a character s select * from emp where ename like %s%; Retrive the information suppliers where the second character of names are either r or n select * from emp where empname like -r% or -n%; Display employee names whose name ends with s select * from emp where ename like % s; Display empnames whose names starts with a and length of the ename is 3 characters select * from emp where ename likea_ _;

IS NULL:
This operator is used to compare null values Example: select * from emp where commission is null;

INTEGRITY CONSTRAINT TABLE


Integrity constraint are set of conditions for columns for eliminating invalid and improper data. Constraints are classified into two types. 1. Column constraints 2. Table constraints

13

VENKATESH for multiple columns are table constraints CONSTRAINT TABLE CHART: COLUMN PRIMARY KEY UNIQUE NOT NULL CHECK FOREIGN KEY REFERENCES DEFAULT PRIMARY KEY: TABLE PRIMARY KEY -----------CHECK FOREIGN KEY REFERENCES ON DATA CASCADE

9014403515

Containing attached for a single column is column constraints and constraints attached

A command used for eliminating insertion of duplicate values(unique+not null) Example Create a table for apsrtc to make reservation for customers with set of conditions Create table apsrtc( busno number(4), Dot date, Seatno number(3), Pname varchar(20), Fare number(10), primary key(busno,dot,seatno)); UNIQUE A columns constraint which eliminates insertion of duplicates but not null values. Example Create a table for college applications Condition: application should not be repeted, but applications number may be nulls Create table application( apno number(3) unique, Name varchar(30), Course varchar(20), Fee number(10,2)); NOT NULL: A constraint used for checking null values. Insertion of null values when this constraint is given then null values are not taken

14

VENKATESH

9014403515

CHECK: A constraint used for attaching user conditions to tables Example1 : Create given table with set of conditions apno Name sex dob doj course fees

1. application number should not be duplicate and application number should be between 1-100 2. name should not be nulls 3. sex should be male or female 4. doj shouldnot be in Sundays and second Saturdays 5. course should be ba, b.com, b.sc 6. fee should be 5-10 thousand TABLE CREATION: create table application( apno number(3) primary key constraint c1 check(apno between 1 and 100), name varchar(20) not null, sex varchar(1) constraint c2 check(sex=f or sex=m), dob date, doj date constraint c3 check(to_char(doj.dy) not in (sun) and to_char(doj,w_dy) not in(2_sat)), course varchar(10) constraint c4 check (course in(ba,bcom,bsc)), fees number(12,2) constraint c5 check(fees between 5000 and 10000)); Example2: Create a table for a bank to store customer information acno name doj type balance

1. Acno should not be duplicates and acno should be 1-100 2. name should not be nulls 3. doj should not be Sundays 4.type should be fixed, savings, current. 5. balance should be minimum 500/TABLE CREATION: 15

VENKATESH create table bank( acno number(3) primary key constraint b1 check(acno between 1 and 100), name varchar(20) not null, doj date constraint b2 check(to_char(doj,dy)not in (sun)), type varchar(10) constraint b3 check(type in (savings,current,fixed)), balance number(10) constraint b4 check(balance>=500));

9014403515

command to display list of conditions for your tables


select * from user_constraints where table_name=<TABLE NAME>; create student table with set commands: 1. student name should not be not duplicate 2. name should be capital letters 3. age should be 20-25 4. fees should be greater than 5000 table creation: create table studs( stdno number(10) primary key, name varcahr(20) constraint s1 check (name = upper(name)),

age number(3) constraint s2 check(age between 20 and 25), course varchar(20) not null, fees number(10) constraint s3 check(fees > 5000));

MASTER CHILD TABLES


Provides a facility to connect two or more tables with each other. The table which consting of recoards is master table on the table which refers for records is child table Syntax: Create table <master table>( Column1 Column3 <date type> (size) primary key, --------------------------------); Column2 ------------------

16

VENKATESH Create table <child table>( Colmn1 <date type> (size) references master table (column), Column2 -------------------------Column3 ------------------------);

9014403515

Example :create given tables and connect the tables with master child relation ship. college Stdno name library Stdno book dot dor course fees

Create table college( Stdno number(3) primary key, Name varchar(20), Course varchar(10), Fee number(10,2)); Create table library( Stdno number(3) references college(stdno), Book varchar(20), Dot date, Dor date); Insert into college values(1,sreenu,bcom,4000);

Insert into library values(1,windows,01-01-2005,10-01-2005); Note: to insert stdno value is same in the two tables.

RENAME: A COMMAND USED TO CHANGE TABLE NAME


Syntax: RENAME <old name> <new name>; Example: Rename stud to student; 17

VENKATESH

9014403515

Describe:
A command used to display strucrure of table Syntax: describe <table name>; Desc <table name>; Example: desc student;

Alter:
This command used to change the table structure. Syntax: Alter { table/user} <name> {add/modify/drop} {column/constraint} Examples: To add a column to the table Alter table employee add dept varchar(30); To modify the length of a column Alter table employee modify name varchar(40); To delete the column Alter table employee drop age; To add the constraint to the table Alter table employee add primary key (empno); Alter table employee add constraint c1 check(sal>5000); To delete the primary key to the table Alter table employee drop primary key; Alter table employee drop constraint c1;

To change the user name Alter user lsvr identified by sreenu;

DROP:
A command used to remove tables, users Syntax: Drop {user / table} Example: To delete all the records and structure of employee Drop table employee To delete the user Drop user sreenu; 18

VENKATESH

9014403515

Delete:
Command used to remove records from table; Syntax: Delete <table name> [where] Example: Delete employee record with empno=1. Delete employee where empno=1; Delete all records of employee Delete employ; Delete records salary<1000 Delete employee where sal< 1000;

Truncate:
This command is same as delete command Using delete command rows are deleted but structure remaining same If we will use delete command then using roll back the table will comes return Examples: Delete from emp; (performs roll back) truncate table emp; (not performs roll back)

UPDATE:
A command is used to modify records of a table Syntax: Update <table name> set <column> = value/calculations [where] Examples: Update employee set name = mohan ravi where empno=1; Update employee set sal = 2000 where empno =1; Update employee set sal = sal + 500; Update employee set name = upper ( name);

SEQUENCE:
Sequence is a object which stores a numeric incriminator 19

VENKATESH Syntax: Creation of sequence: Create sequence <sequence name > Start with <number> Increment by <number> Deletion of sequence: Drop sequence < name>

9014403515

Create table for exams to store students information . table should select numbers from a sequence Htno name date Course

Table creation: Create table exam( Htno number(10), Name varchar(30), Dot date, Course varchar(30)); Sequence creation: Create sequence htno Start with 99990122 Increment by 1; Insertion of values: Insert in to exam values (Htno nextval, &name,&dot,&course);

SELECT:
A command used to display records of table and perform various analysis for table recoards. The following are various activities that can be performed using select command. 1. plays the records of table. 2. performs various statistical analysis on table. provides dictionary facility to analyze on the table Syntax: Select [*/distinct/all][columns/alias] [caluculations]from <table1> <tabele2> alias -------------------[where][order by][group by] [having][joins][intersect ][minus] [select][sub query][nested query][create]

20

VENKATESH [insert][delete][update][copy][spool] [link][dict][all-users][tab] [indexes][statistical functions ];

9014403515

Create Given Tables To Practice The Options Of Select Command In Each Table To Store At Least 10 Records 1. Empn o 2. Empn o 3. Empn o y Nam e Do b EMP_STUDY Universit Yea r Cours e Ran k Fees EMP Qualificatio n Salar y Commission

EMP_PERSONAL Fnam e Addres s Phonen o Email

*:
A symbol used to select all the columns of table. 1. display all records of emp table. Select * from emp; 2. display records from emp and study tables. Select emp.*,emp_study.* from emp,emp_study; 3. display records emp and study tables using alias. Select a.* ,b.* from emp a,emp_study b;

DISTINCT:
Option used to displays common values from a select column. 1. display different categories of sex. Select distinct sex from employee; display different categories of universities. Select distinct university from emp_study.

COLUMNS, FUNCTIONS, CALCULATIONS:


21

VENKATESH

9014403515

Columns option displays selected columns from a table, functions provides a facility to implement functions of SQL with data and calculations provides a facility to process calculations for data. 1. Display empno,name and salary from employee table Select empno,name,salary from employee; 2. display name and salary from employee table and course and rank from emp_study table. Select name,salary,course,rank from employee,emp_study; 3. display no and name in capital letters. Select empno,upper(name) from emp; 4. display empno, name and age in years select empno, name, (sysdate-dob)/365 from emp; 5. display empno, name, salary, one year salary, one month salary, and one day salary. Select empno, name, salary, salary*12, salary/30, (salary/30)/8 from employee;

WHERE:
A option of select command to display condition records. 1. display female records select * from employee where sex=female; 2. display empno 101record select * from emp where where empno=101; 3. display empsalary is greater than 1000. Select * from emp where salary>1000; 4. display s.v.university records. Select * from emp_study where university =s.v.university; 5. display female and salary >1000 select * from employee where sex=female and salary>1000; 6. display ename and age>25. Select name from emp where(sysdate-dob)/365 >=25; 7. display emp for empno 1or 2. Select * from emp where empno=1 or empno=2; (Or) select * from emp where empno in (1,2); 8. select upper and lower case letters of university is in s.v.university 22

VENKATESH select * from emp_study where upper(university)=s.v.university;

9014403515

ORDER BY:
Option of select command displays the records in ascending or descending order. 1. display employee records in ascending order of empno. Select * from employee order by empno; 2. display employee records order on name wise select * from employee order by name; 3. display employee records on descending order of empno. Select * from employee order by empno desc; 4. display employee records order on sex and name wise. Select * from employee order by sex, name; 5. display employee records order on age wise. Select * from employee order by (sysdate-dob)/365;

STATISTICAL FUNCTIONS:
MAX: Display maximum value of column MIN: Display minimum value of column SUM: Display sum of a column COUNT: Display number of records

AVERAGE: Display average value of a column GROUP BY: Display statistical analysis for a column.
Examples:

1. Select max(salary) from employee; 2. Select min(salary) from employee; 3. Select sum(salary) from employee; 4. Select avg(salary) from employee; 5. Select count(*) from employee; 6. Select sum(salary),count(*), average(salary) from employee; 7. Select sex,count(*) from employee group by sex;

23

VENKATESH 8. Select sex, max(salary) from employee group by sex;

9014403515

COMBINATION COMMANDS
1. CREATE WITH SELECT: Combination commands used to copy records from one table to another new table. SYNTAX: CREATE TABLE <TABLE NAME> AS SELECT .. EXAMPLES 1. Copy employee table records to another table employee1. Create table employee1 as select * from employee; 2. Copy male records to employee2 table Create table employee2 as select * from employee where sex=male; 3. Copy structure of emp table to employee3. Create table employee3 as select * from employee where 1=2; (any wrong condition) 3. COPY one user to another user create table system.xxx as select * from employee;

2. INSERT WITH SELECT:


A Command used to copy records form one table to another table Syntax: Insert into <table> select * from <table>; Examples: 1. copy records from ksrm to all_college insert into all_college select * from ksrm; 2. copy records from madina to all_college insert into all_college select * from madina; 3. to display all records select * from all_college;

3. UPDATE WITH SELECT


A Command used to check a condition in where and update the records for satisfied condition Syntax: update <table> set column= value 24

VENKATESH where ( select............................................................); Examples: increase 500 salary from max salary update employee set sal= sal + 500 where sal=(select max(sal) from employee); increase 1000 rupees salary for senior most

9014403515

update employee set sal= sal + 1000 where hiredate= (select min(hiredate) from employee);

3. DELETE WITH SELECT:


A command use to remove records for selected condition Syntax: Delete <table> where (select ); Example: 1. to remove lowest salary record delete employ where sal=(select min(sal) from employee);

JOINS
joins is a process of displaying the records from two tables. They are three types Equi join Outer join Inner join

EQUI JOIN:
Displays records from multiple tables for records matching in all tables Syntax: Select columns From <table1>,<table2> Where <table1>.column =<table>. Column Example: Select name,course,book, dot form office,library where office.stdno = library. Stdno;

OUTER JOIN:
Displays records from two or more tables for records matching in both tables as well as un mach recoards Example: Select name, course, book, dot, from office,library where offece.stdno = library .stdno(+);

25

VENKATESH

9014403515

ALL _TAB_ COLUMNS:


This is a table which contains all columns names of all the tables Examples: To see all the columns names of all tables Select * from all_tab_columns; Display all the columns of emp Select * from all _tab_columns where tab_name=emp

SQL UTILITIES:
EXPORT:
A command used to exports tables hard disk to floppy disk Syntax: Exp username/ password tables=( table1, table2,.) Steps: Click start programs ms dos Exp scott/tiger tables =(emp)

IMPORT:
A command used to import tables from floppy to hard disk Syntax: Imp user/password file=(export file) full=y Example: Imp scott/tiger file expdat full = y

LOAD:
A command used to copy data from one language to another language Syntax: Load data infile * Into table < table name> Fields terminated by , Optionally enclosed , (columns..) begin data 26

VENKATESH steps: create table in sql prompt sql> create table emp( empno number(3), name varchar(15), salary number(12,2)); switch to dos prompt then open editor c:\ edit sreenu.txt

9014403515

type program and data load data infile * into table emp fields terminated by , (empno, name, salary) begin data 101, ravi ,4000 102,Krishna,3000 103,mohan, 2000 type given command in dos prompt to execute the program sqlldr scott/tiger control = sreenu.txt

TRANSACTION CONTROL LANGUAGE(TCL)


Command used to control the transaction of DML( insert, delete, update) 1.Commit 2. roll back 3. save point 4. grant 5. revoke 6. connect 7. set auto commit on/off

COMMIT:
Command used to save your transaction

ROLL BACK:
Cancels the transaction Examples: Insert records and saves records 27

VENKATESH Insert into emp values (1,xxx, 2000); Commit: Insert records and cancels the records Insert into emp values (2,yyy,8000); Roll back; Delete records and recover the records Delete emp; Roll back; Delete records permanently with out recover Delete emp; Commit;

9014403515

SPOOL:
A Command Used To Copy The Result Of Sql From Oracle To Dos Text File Ex: Transfer The Our Put Of Select Command To A File. SQL> Spool C:\Sreenu.Txt SQL> Select * From Emp . SQL> Spool Off;

To See The Result: Click Start --- Run Type C:\ Sreenu.Txt

VIEWS
View Is A Stored Object Of Sql. Which Stores Select Command When View Is Executed Then Select Command Is Executed Syntax: Create View <Name> Select ......................................... ......................................................... Example: Create View Salary As Select Max(Sal) Maximum, Min(Sal) Minimum, Sum(Sal) Sum, From Emp; 28

VENKATESH One Day From Emp; Example To Use View: Select * From Salary; Select * From Salary1; Example To Drop View: Drop View Salary; Drop View Salary1;

9014403515

Create View Salary1 As Select Empno, Ename, Sal, Sal * 12 One Year, Sal /30

SET COMMANDS
1). Set Line Size <Num> Specifies Number Of Characters For Line Example: Set Linesize 100; Select * From Emp; 2). Set Pagesize <Number> Specifies Number Of Lines Per Page Example: Set Pagesize 50 Select * From Emp; 3). Set Sqlprompt <String> Changes The Sql Prompt Example: Set Sqlprompt Bits> Set Sqlprompt Sql> 4). Set Heading Off/ On A Command Used To Switch Off Headings Of Results

Example: Set Heading Off; Select * From Emp; 5). Set Numwidth <Number> Specified Numerical Width Size Example: 29

VENKATESH Set Numwidth 4 Select * From Emp; 6). Set Autocommit On/Off Saves Your Records And Transactions Automatically Example: Set Autocommit On Insert ............................. ................................. ............................. 7). Set Colsep <Char> Used To Separate Of Columns With Character Example: Set Colsep * (Any Character) Select * From Emp;

9014403515

DESIGN THE GIVEN EMPLOYEE REPORT:


PAGE NUMBER EMPLOYEE REPORT ALLWYN COMPANY EMPNO ENAME HIREDATE SAL

OPEN NOTE PAD AND TYPE THE GIVEN COMMANDS Set pagesize 30 Set linesize 100 Set colsep | Title center EMPLOYEE REPORT skip 1 center ALLWYN COMPANY skip 1 right page number Btitle center end of report Column salary format 9,99,999.99 Column hiredate format dd-mm-yyyy Select empno,name,salary,hiredate from emp;

30

VENKATESH

9014403515

DESIGN GIVEN DEPT REPORT


ALLWYN INDUSTRIES DEPT DEPT10 ........... .............. DEPT20 ........... .............. NAME ............. .................. .................. ................. ............. .............. SAL ................ .............. .............. ................ ............... .............

Set pagesize 30 Set linesize 100 Set colsep | Title center ALLWYN INDUSTRIES skip 1 right page no Btitle center end of report Column sal format 9,99,999.99 Break on deptno Select deptno, ename,salary from emp orderby deptno;

31

You might also like