Oracle-2
Oracle-2
CREATE TABLE:
The CREATE TABLE statement allows you to create and define a table.
Syntax:
CREATE TABLE table_name
(
Column 1 data type(size),
column 2 data type(size),
...
Column data type (size)
);
Example:
Create table student
(
studno number(2),
studnm varchar2(20),
sem number(1),
course varchar2(6),
join_dt date
);
Example:
Example:
INSERT INTO student VALUES
(&student_no,’&student_nm’,&semester,’&course’,’&Joining_date’);
Rules:
1. Variable name can be same as column name of the
table or it can be different.
2. Space is not allowed in variable name.
3. Variable that represents Varchar2, Char and Date type
of columns, must be written in single bracket (‘ ‘).
Exam
ple:
INSERT INTO student (studno,studnm,course)
VALUES (10,’Ravi’,’PGDCA’);
Example:
INSERT INTO student (studno,studnm,course)
VALUES (&student_no,;&student_name’,’&course’);
ONE WORD QUESTION ANSWER
Example:
Example:
A primary key is one or more column in a table used to identify each row
uniquely in table.
Once the column has defined Primary Key constraint, it gets NOT NULL and
UNIQUE constraints.
A single column primary key is called a Simple Key and Multicolumn primary
key is called Composite Primary Key.
Syntax:
Column name data type (size) PRIMARY KEY,
Example:
CREATE TABLE
emp (
empno varchar2(4) PRIMARY KEY,
empnm varchar2(15),
address1 varchar2 (20),
address2 varchar2 (20),
City varchar2 (15)
);
e Now, after creating table, empno column must not be NULL and value
entered in empno column must be UNIQUE means one cannot repeat
the value of empno column.
Example:
CREATE TABLE empdept
(
empno varchar2(4),
empnm varchar2(15),
deptno varchar2(3),
Salary number (10, 2),
PRIMARY KEY (empno, deptno)
);
Now, after creating table, empno & deptno must not be NULL and value
entered in empno & deptno column must be UNIQUE.
This record will not get added and error has been generated.
When composite primary key is defined, table will not accept the value if
the data of the entire composite column gets repeated.
It accepts the value if any one of the composite key column value gets
repeated.
Syntax:
Column name data type (size) REFERENCES table name [(column name)] [ON DELETE
CASCADE]
Example:
CREATE TABLE empdetail
(
empno varchar2(4) REFERENCES
emp, Salary number (10, 2),
Design varchar2 (4)
);
Example:
CREATE TABLE empdetail
(
empno varchar2(4),
Salary number (10, 2),
Design varchar2 (4),
FOREIGN KEY empno REFERENCES emp(empno)
);
Syntax:
Column name data type (size) UNIQUE,
Example:
CREATE TABLE
emp (
empno varchar2(4) UNIQUE,
empnm varchar2(15)
address1 varchar2 (20)
address2 varchar2 (20),
);
Syntax:
UNIQUE (columnname [, columnname , … ])
Example:
CREATE TABLE
emp (
empno varchar2(4),
empnm varchar2(15),
address1 varchar2 (20),
address2 varchar2 (20),
city varchar2(15),
UNIQUE (empno)
);
DETAILING:-
NOT NULL constraints:
A NULL value is different from space or zero.
A NULL value can be inserted into the column of any data type.
NOT NULL constraint at Column Level:
When a column is defined as not null, it becomes mandatory column.
It means that a value must be entered into the column.
Syntax:
Columnname datatype(size) NOT NULL,
Example:
CREATE TABLE
emp (
empno varchar2 (4) NOT NULL,
empnm varchar2 (15) NOT NULL,
address1 varchar2 (20),
city varchar2(15) NOT NULL
);
Note: NOT NULL constraint cannot be applied at table level; it can only be
applied at column level.
CHECK constraint
Syntax:
Column name data type (size) CHECK (logical expression)
Syntax:
CHECK (logical expression)
Example:
CREATE TABLE
emp (
empno varchar2(4),
empnm varchar2(15),
city varchar2(15),
CHECK(empno like ‘E%’),
CHECK(empnm=upper(empnm)),
CHECK(city IN (‘bombay’,’delhi’,’rajkot’,’pune’))
);
IT. DETAILING:-
UPDATE statement
The UPDATE command is used to change or modify data values in a table.
UPDATE statement can update all the rows from a table or selected rows
from the table.
Example:
UPDATE student
SET studnm=’Meeta’
WHERE studnm=’Mita’;
Example:
DELETE FROM student;
Example:
DELETE FROM student
WHERE studno>60;
TRUNCATE statement
Syntax:
TRUNCATE TABLE table-name;
Example:
TRUNCATE TABLE student;
Syntax:
SELECT * FROM <table name> WHERE <column name>=expression;
Above example will retrieve all the row of student table whose course is
PGDCA and semester is 2.
Above example will retrieve all the rows of EMP table whose city is either
Bombay or pune.
NOT Operator:
Operation will be TRUE when condition becomes FALSE.
Above example will display all the rows of EMP table that are NOT in
Bombay or pune.
BETWEEN Operator:
To select the data within range, BETWEEN operators is used.
The above statement will display all the records whose salary is between
2000 and 7000.
Above example will return all the rows of EMP table whose city is either
Rajkot or Bombay or pune.
Example: SELECT * FROM EMP WHERE city NOT IN (‘rajkot’, ’bombay’, ’pune’);
Above example will return all the rows of EMP table whose city is other than
Rajkot, Bombay and pune.
GROUP BY:
Syntax:
SELECT column1, column2, column, aggregate function (expression) FROM
table
HAVING CLAUSE:
· A HAVING clause restricts the results of a GROUP BY clause.
· It can be used in conjunction with Group By clause.
· HAVING is used to give condition on group by clause column(s).
· The HAVING clause is applied to each group of the grouped data.
Syntax:
SELECT column1, column2 ... column, aggregate function (expression)
FROM table GROUP BY column1, column2, ... column HAVING groupbycolumn=expression;
EXAMPLE:
Select deptno, sum (sal) from EMP group by deptno having deptno=10 or
In above example, the common rows in the deptno column are grouped
together and the total salaries for only the deptno specified in HAVING
clause are displayed.
ONE WORD QUESTION AND ANSWER
LIKE:
These examples give all the detail from employee table whose names are
started with C.
This example will give the records from EMP table whose name has only 3
characters and last 2 characters are c and a respectively.
ONE WORD QUESTION AND ANSWER
1) INNER JOIN
2) OUTER JOIN
a. LEFT OUTER JOIN
b. RIGHT
OUTER
JOIN
c. FULL
OUTER
JOIN
3) CROSS JOIN
1) INNER JOIN
INNER JOIN is also known as Equi join.
It is known as Equi join because the where statement generally
compares two columns from two tables with the equivalence
operator (=).
It returns all rows from both tables where there is match.
EXAMPLE:
Select empno, bno from emp, branch on emp.bno = branch.bno
2) OUTER JOIN
· Outer join is usually similar to inner join but
it gives a bit more flexibility.
· This type of join can be used in situations where it is
desired to select all rows from the table on the left
or right or both and other table has values in
common.
There are three types of outer join
1) left outer join
2) right outer join
3) full outer
join Left outer join:
· It gives all the data of and left hand side table
and only those of right hand side table which
are similar.
1) Concat:
Purpose: it is used to combine the two strings and return combination of both
string.
2) Initcap:
Purpose: it returns the string with the first letter of each word in upper case and
all other letters in lower case.
Syntax: initcap(string)
3) Lower:
Purpose: it returns the string with all letters in lower case.
Output: abc
4) Upper:
Output: ABC
Lpad:
Purpose: it returns the Expr1 left padded to the length or n character with the
sequence of characters in Expr2.
Output: * * * * * * * hns
Syntax: rpad(expr1,n,expr2)
Output: hns* * * * * * *
6) Ltrim:
Purpose: It removes from the left end of string all of the character contains in
set.
If set is not defined it will default as a blank.
Syntax: lrtrim(string,set)
Output: XxyHNS
7) Rtrim:
Purpose: It removes from the right end string all of the character contains in
set. If set is not defined it will default as blank.
Syntax: rtrim(string,set)
Output: HNS
ONE WORD QUESTION AND ANSWER
8) Replace
Purpose:
It returns characters with every occurrence of search string replace with
replacement string.
If replacement string is omitted then all occurrence of search string are removed.
9) Substr
Purpose: This function returns a part of string beginning at character position
define by m to n character long.
If m is positive then start from beginning. If m is negative then start from ending.
Output: ello
Output: 5
EXPLAIN DATE
FUNCTION. DETAILING:-
In oracle following functions are comes into date function
ADD_MONTHS LAST_DAY
MONTHS_BETWEEN NEXT_DAY SYSDATE
ADD_MONTHS:
Syntax: ADD_MONTHS (date,
Purpose:
· This function returns the date plus integer months.
· The date argument can be a date value or any values that can be
implicitly converted to DATE.
· The integer argument can be integer value or any values that can
be implicitly converted to integer.
· The return type of this function is date.
Output: 07-FEB-11
ONE WORD QUESTION AND ANSWER
SR.NO QUESTION ANSWER
1. ADD MONTHUSE FOR TO ADD MONTH
IN GIVEN DATE
LAST_DAY:
Syntax: LAST_DAY (d1)
Purpose:
· It returns the last date of specified month.
· The d1 argument can be any date value.
Output: 31-JAN-11
MONTHS_BETWEEN:
Purpose:
· It returns the how many months fall between two dates.
· The return type of this function is number value.
· Here user has to give two dates as argument.
Output: 1
SYSDATE:
Syntax: SYSDATE
Purpose:
· This function returns the current system date.
· The return type of this function is date.
Output: 06-MAR-11
Purpose:
This function returns the average value of specified column.
This statement returns the average value of sal from EMP table.
2) MIN:
Output: 1
3) MAX:
Purpose:
This function returns the maximum value of specified expression or column.
Output: 4
COUNT:
Purpose:
This function returns the number of rows where expr is not null.
4) SUM:
Purpose:
This function returns the sum of the specified column or n.
OUTPUT: 10
EXPALIN NUMERIC
FUNCTION. DETAILING:-
1) ABS
Purpose:
This function returns the absolute value of n.
OUTPUT: 3
2) POWER:
Syntax: Power (m,n)
Purpose:
This function returns the m raised to the nth power.
· N must be an integer, else an error is returned.
OUTPUT: 8
3) ROUND:
Purpose:
· Returns n, rounded to m places to the right of a decimal point.
· If m is omitted, n is rounded to 0 places.
· M can be negative to round of digits to the left of the decimal point,
m must be an integer.
4) SQRT:
OUTPUT: 3
ONE WORD QUESTION AND ANSWER
SR.NO QUESTION ANSWER
1. SQRT USE FOR RETURN
SQUARE VALUE
OF GIVEN
COLUMN
5) EXP:
Syntax: exp
Purpose:
Returns e raised to the nth power where e = 2.71828183
6) GREATEST:
Purpose:
Returns the greatest value in a list of expression.
OUTPUT: 6
7) LEAST
Purpose:
Returns the smallest value in a list of expression.
OUTPUT: 1
OUTPUT: 0
9) FLOOR
Syntax: FLOOR (n)
Purpose:
Returns the largest integer value that is equal to or less than a number.
OUTPUT: 24
ONE WORD QUESTION AND ANSWER
SR.NO QUESTION ANSWER
1. FLOOR USE FOR RETURN FLOOR
VALUE OF GIVEN
NUMBER
10) CEIL
Syntax: CEIL (n)
Purpose: Returns the smallest integer value that is equal to or greater than a
number.
OUTPUT: 25
ONE WORD QUESTION AND ANSWER
SR.NO QUESTION ANSWER
1. CEIL USE FOR RETURN HIGEST
INTEGER VALUE
OF GIVEN
NUMBER