Вы находитесь на странице: 1из 7

SQL>create table vendor (vencode varchar2(5) venname varchar2(10), venaddr varchar2(10) );

SQL>alter table vendor add(phonenum number(6));

SQL>truncate table vendor; /*deletes all the rows from the table but retains its structure

SQL>desc vendor ;/*command to view table's structure

SQL>drop table vendor ;/* command to drop table

SQL>insert into vendor values (&vencode,&venname,&venaddr, &phonenum); /*command to prompt the user to enter values for all columns in the table

SQL>insert into vendor (venname, phonenum) values(XXX,12345);/* To insert values only into two specified columns and skipping insertion into other columns SQL>create table order(order_no varchar2 (5),odae date,vencode varchar2(5),ostatus char(1),del_date date);

SQL>insert into order values(O001,12-DEC-2007,v002,c, 25-DEC-2007);

SQL>select distinct vencode from vendor; /*To prevent selection of duplicate rows we use distinct,here duplicate vencode values if any are excluded from being displayed

/* To arrange the displayed rows in a specific order (either ascending or descending) we use order by clause(default is ascending order.) SQL>select * from order where vencode=v004 order by del_date;

/* to retrieve information about the orders being handled by a vendor whose vencode is v004 so that preference can be given to those orders whose delivery date comes first SQL>select * from order where vencode=v004 order by del_date desc; /* To arrange the displayed rows in descending order of del_date

SQL>create table item(itemcode varchar2(5),itemdesc varchar2(10),p_category varchar2(10),qty_hand number(5),re_level number(5),max_level number(5),itemrate number(9,2)); SQL>insert into itemfile values(i1,nuts,spares,100,50,250,20,20); SQL>insert into itemfile values(i2,bolts,spares,95,125,300,16,50); SQL>insert into itemfile values(i3,covers,accessories,30,15,50,400); SQL>insert into itemfile values(i4,panels,accessories,75,30,150,4000);

SQL>select itemcode, itemdesc, p_category, qty_hand, re_level, max_level, from itemfile order by p_category, itemcode desc;

SELECT COMMAND TO CREATE TABLE Syntax:create table <newtablename> as select col_name from <existing tablename>; Ex: SQL>create table vend_details as select * from vendor; COPYING A PART OF EXISTING TABLE INTO A NEW TABLE Ex: SQL>create table ven(venid,name) as select vencode,venname from vendor; SQL>create table new_itm as select * from itemfile where p_category=spares; SQL>create table new_itm as select * from itemfile where 1=2;/* Since the condition 1=2 will never satisfy,a new table is created with only the structure of the itemfile table but not its records.

SELECT COMMAND TO INSERT RECORDS insert into <tablename>(select columnnames from <existing_tablename>); SQL>create table order_dup(orderno varchar2(5),odate date ,vencode varchar2(5),ostatus char(1), del_date date); SQL>insert into order _dup(select * from order); NOTE:In such a case, care should be taken to check that the two table structures are the same.(here order and order_dup structures must be same)

SELECT COMMAND FOR COLUMN ALIASES

select column_name<alias_name> from table_name; SQL>select vencode VID from vendor;

OPERATORS IN SQL*PLUS ARITHMETIC OPERATORS +,-,*, / Ex:SQL>select itemdesc, max_level-qty_hand available_limit from itemfile where p_category=spares;(To verify the quantity required to reach the maximum level in the category of spares COMPARISON OPERATORS Used to compare one expression to another. they are =,!=,>=,<=,<,>,BETWEEN .. AND (to check between any two values), in(to match with any of the values in the list),LIKE (to match a character pattern) and IS NULL(to check whether it is null).,NOT BETWEEN,NOT LIKE and so on.

Ex: SQL>select itemdesc where qty_hand between 20 and 40;(Lists the itemdecs of those items whose qty_hand lies between 20 and 40) SQL>select orderno from order where del_date in(06-OCT-2007,25-DEC2007);(Lists all the orderno.s from order table for which del_date lies in the given list.) SQL>select vencode,venname,phonenum from vendor where venname LIKE R%;( Selects the specified fields for those names whose name begins with R)

SQL>select vencode,venname from vendor where venname like R_ j;(Selects specified column s from vendor table whose names are three letters long starting with R and ending with j);

LOGICAL OPERATORS:Used to combine the results of two or more conditions to produce a single result. The logical operators are: OR, AND, NOT. Ex: SQL>select * from order where odate>10-MAY-2007 and del_date<26-MAY2007; Operator Precedence Arithmetic operators-Highest precedence Comparison operators NOT operator AND operator

OR operator----Lowest precedence The order of precedence can be altered using parenthesis.

SQL*PLUS FUNCTIOPNS SINGLE ROW FUNCTIONS : The single row function returns only one value for every row queried in the table. The can be included in a SELECT and also be included in a WHERE clause. DATE FUNCTIONS: i)ADD_MONTHS(d,n):returns a date after adding a specified date d, to a specified number of months n. Ex: SQL>select del_date ,add_months(del_date,2) from order ; ii)LAST_DAY(d):returns the date corresponding to the last day of the month specified by the date d. Ex: SQL>select sys_date,last_day(del_date) from order where odate=12-DEC-2007; iii)MONTHS_BETWEEN(d1,d2):to find the number of months between two dates d1 and d2. Ex: SQL>select months_ between (del_date, odate) from order; iv)Round: Returns the date which is rounded to the unit specified by the format model. Syntax :round(d,[fmt]) D is the date fmt- format required Ex: SQL>select del_date, round (del_ date, year) from order where vencode=v001;(the given date is rounded to nearest year) SQL>select del_date, round (del_ date, month) from order where vencode=v001; (the given date is rounded to nearest month) SQL>select del_date, round (del_ date, day) from order where vencode=v001; (the given date is rounded to nearest day) SQL> select del_date, round (del_ date) from order where vencode=v001;(here there is no fmt therefore it is rounded to the nearest day) v)NEXT_DAY(d,day): SQL>select next_day(sys_date,Tuesday) from dual; displays 25-DEC-2007 vi) GREATEST(d1,d2,):returns the latest date. Ex SQL>select sys_ date, odate greatest(sys_date, odate) from order where orderno=o001;

CHARACTER FUNCTIONS: Character functions accept a character input and return either character or number values. Some of them supported by Oracle are listed below

FUNCTION Initcap(char) Lower(char) Upper(char) Ltrim(char, set) Rtrim(char, set) translate(char,from,to) Replace(char, searchstring, [restring]) Substr(char,m,n)

INPUT SQL>select initcap(hello) from dual; SQL>select lower(FUN) from dual; SQL>select upper(sun) from dual; SQL>select ltrim(xyzhello,xyz)from dual; SQL>select rtrim(xyzhello,llo)from dual; SQL>select translate(jack,j,b) from dual; SQL>select replace(jack and jue, j, bl) from dual; SQL>select substr(abcdefg,3,2) from dual;

OUTPUT Hello Fun SUN hello xyzhe Back Black and blue cd

Soundex is a character function that compares words that are spelled differently but sounds alike. EX: SQL>select venname from vendor where soundex (venname) =soundex (Sumesh); SQL>select chr (67) from dual; Displays C- character equivalent of 67.

Lpad is a function that takes three arguments. The first argument is the character string which has to be displayed with the left padding. The second is the number which indicates the total length of the return value, the third is the string with which the left padding has to be done when required. Ex: SQL>select lpad (function, 15,*) Lpd from dual; Lpd *******function Ex: SQL>select rpad (function, 15,*) Rpd from dual; Rpd function*******

Length: returns the length of a string Ex: SQL>select length (VIT) from dual; (Displays 3).

Concatenation || operator: it is used to merge or more strings. Ex: SQL>select (The address of|| venname|| is|| venaddr) from vendor where vencode =v001;

NUMERIC FUNCTIONS: Numeric functions accept numeric input and returns numeric values as output. FUNCTION INPUT OUTPUT Abs( n) SQL>select abs(-15) from dual 15 Ceil(n) SQL>select ceil(48.778) from dual; 49 Cos(n) SQL>select cons(180) from dual; -0.59884601 Cost(n) SQL>select cushy(0) from dual; 1 Exp(n) SQL>select exp(4) from dual; 54.59815 Floor(n) SQL>select floor(4.678) from dual; 4 Power(m ,n) SQL>select power(5,2) from dual; 25 Mod(m ,n) SQL>select mod(11,2) from dual; 1 Round(m ,n) SQL>select round(112.257,2) from dual; 112.26 Trunc(m, n) SQL>select trunc(112.257,2) from dual; 112.25 Sqrt(n) SQL>Select sqrt(16) from dual; 4 SQL> select ln (2) from dual; (returns natural logarithm value of 2) SQL>select sign (-35) from dual; (output is -1)

CONVERSION FUNCTIONS: Convert a value from one data type to another. To_char ( ):To_ char (d [,fmt]) where d is the date fmt is the format model which specifies the format of the date. This function converts date to a value of varchar2 datatype in a form specified by date format fmt.if fmt is neglected then it converts date to varchar2 in the default date format. Ex: SQL>select to_ char (sys_ date, ddth of fmmonth yyyy) from dual; Output is 18th of December 2007. Fill mode (fm) format mask is used to avoid blank padding of characters and zero padding to numeric. To_ date ( ): The format is to_date (char [, fmt]).This converts char or varchar datatype to date datatype.Format model, fmt specifies the form of character. Ex: SQL>select to_date (December 18 2007,month-dd-yyyy) from dual; 18-DEC-07 is the output. SQL>select round (to_date (27-dec-98),year) from dual;

To_ Number( ): allows the conversion of string containing numbers into the number datatype on which arithmetic operations can be performed. Ex: SQL>select to_number (100) from dual;

Вам также может понравиться