Professional Documents
Culture Documents
1. It is a nonprocedural language.
2. It reduces the amount of time required for creating and maintaining
systems.
3. It is English like language.
Componentsof SQL
1. CHAR
2. VARCHAR (size) or VARChAR2 (size)
3. NUMBER
4. DATE
5. LONG.
QUERY
Example:
SQL> create table student(name varchar(23), roll_no number(12), class
varchar2(12), address varchar(23));
Table created.
2) THE INSERTION OF DATA INTO TABLE: -
Once a table is created, the most natural thing to do is load this with data to be
manipulated later i.e. to insert the rows in a table. The data in a table can be
inserted in three ways.
Syntax:-
INSERT INTO <table name >(<columnname1>,<columnname2>) VALUES (
<expression1>,<expression 2>);
OR
INSERT INTO <table name> VALUES(<expression1 >,<expression2> );
OR
INSERT INTO <table name>VALUES(‘<&columnname1>’, ‘<&columnname2>’);
Example :-
SQL> insert into student(name, roll_no, class, address) values ('Prabhat' ,06,
'BCA' ,’Hatlimore');
1 row created.
OR
SQL> insert into student values ('kishore',01,'BCA','Nagri');
1 row created.
OR
SQL> insert into student values( '&name', '&roll_no', '&class', '&address' );
Enter value for name: Amarjeet
Enter value for roll_no: 30
Enter value for class: BCA
Enter value for address: airwan
old 1: insert into studentvalues('&name','&roll_no','&class','&address');
new 1: insert into student values('Atinder','04','BCA','Sawanchack');
1 row created.
FOR inserting more values we use ‘/’ slash after SQL> as below but after above
syntax use:- SQL> /
Enter value for name: Vinay
Enter value for roll_no: 08
Enter value for class: BCA
Enter value for address: Barnoti
1 row created.
1 row created.
When we use the command SELECT* FRM TAB;
The output is displayed as:-
SQL> select * from tab;
6 rows selected
6 rows selected.
SQL> SELECT * FROM STUDEN ORDER BY roll_no desc;
6 rows selected.
7) RENAMING TABLES: -
Table renamed.
8) DESTROYING TABLES:-
1. DROP COMMAND: -
By using the DROP TABLE statement with the table name we can destroy a specific table .
Syntax: -
DROP TABLE <table name>;
Example:--
SQL> Drop table student;
Table dropped.
2. TRUNCATE COMMAND:-
The truncate command is much faster in comparison to delete statement but similar to the
drop command as to destroy a specific table.
Syntax:- TRUNCATE table <tablename>
Example:- SQL> truncate table employees;
Table truncated.
This command displays the columns names, the data types and the special attributes
connected to the table.
Example:- SQL> describe employees;
1) NOTNULL:-
The NOT NULL column constraint ensures that a table column cannot be left empty. When
a column is defined as not null, then that column becomes a mandatory column. It implies
that a value must be entered into the column if the record is to be accepted for storage in the
table.
Syntax:- <Column Name><data type>(<size>) NOT NULL ;
Example:- name varchar2(22) not null;
A primary is one or more column in a table used to identify each row in a table. None of the
fields that are part of the primary key can contain a null value. A table can have only one
primary.
Syntax:- <Column name><data type>(<size>) PRIMARY KEY
Example:-
SQL> create table student name varchar2(12), roll_no number(12) primary key,
class varchar2(21) NOT NULL, dob date);
or
Foreign key represent relationships between tables. A foreign key is a column (or a group of
columns) whose values are derived from the primary key or unique key of some other table.
The table in which the foreign key is defined is called a FOREIGN TABLE or DETAIL
TABLE. The table that defines the primary or unique key and is referenced by the foreign
key is called the PRIMARY KEY or MASTER KEY.
Syntax: -
Foreign key (<column name>) REFERENCES <table name>(column name);
Example:-
SQL>create table department(dept_no number(10)primarykey, dept_name
varchar2(25), dept_loc char(5,e_nonumber(11), foreign key(e_no) references
employee (e_no);
Table created
The unique key constraint permits multiple entries of NULL into the column. These NULL
values are clubbed at the top of the column in the order in which they were entered into the
table. This is the essential difference between the primary key and the unique constraints
when applied to table column(s).
A table can have more than one unique key which is not possible in primary
key.
Syntax:-
CREATE TABLE Table name (<columnName1><datatype>(<size>),<columnName2><
data type>(<size>), UNIQUE(<columnName1>, <columnName2>));
Example:-
SQL> create table student1(roll_no number(12) primary key, dob date, name
varchar2(20),class varchar2(2),e_mailvarchar2(20) constraint un_st unique);
Table created.
ORACLE FUNCTIONS
Oracle functions serve the purpose of manipulating data items and returning a result.
Functions are the programs that take zero or more arguments and return a single value.
Oracle has built a no. of functions into SQL. These functions can be called from SQL
statements.
1. COUNT (expr) function :- Returns the number of rows where expression is not null.
Example:-
EMP_ID NAME DEPT_ID SALARY
--------------------------------------------------------------------------------------------
1 sourabh 21 55000
2 sonu 22 55000
3 anku 4 55000
5 anku 21 55000
3 panku 22 75000
COUNT (DISTINCTNAME)
-------------------
4
SQL> select count (salary) from employees;
COUNT (SALARY)
----------
5
2. COUNT (*) function: - Returns the number of rows in the table, including duplicates
and those with nulls.
Syntax : - COUNT(*)
Example:-
SQL> select count(*) from employees;
COUNT (*)
--------------
5
SQL> select count (*)"salary" from employees;
Salary
----------
5
SUM(SALARY)
-----------
295000
Example:-
MAX(SALARY)
-----------
75000
Example:-
MIN(SALARY)
-----------
55000
Example :-
AVG(SALARY)
-----------
59000
7. LIKE OPREATOR :- The LIKE predicate allows comparison of one string value with
another string value, which is not identical. This is achieved by using wildcard
characters. Two wildcard characters that are available are:
% allows to match any string of any length(including zero length)
allows to match on a single character.
Example:-
SQL> select emp_id, name, dept_id, salary from employees where name like
'a%';
Example:-
SQL>select emp_id, name, dept_id, salary from employees where name like
'_n_u';
Example:-
SQL> select emp_id, name, dept_id, salary from employees where dept_id in
(20, 22);
Example:-
SQL> select emp_id, name, dept_id, salary from employees where dept_id not
in (20,22);
1 sourabh 21 55000
3 anku 4 55000
5 anku 21 55000
SQL> select emp_id, name, dept_id, salary from employees where dept_id
between 11 and 21;
STRING FUNCTIONS
Syntax : -UPPER(char)
Example : -
UPPER (NAME)
--------------------
SOURABH
SONU
ANKU
ANKU
PANKU
Syntax: - LOWER(char)
Example:-
LOWER (NAME)
--------------------
sourabh
sonu
anku
anku
panku
3. INITCAP function: - Returns a string with the first letter of each word in upper case.
Syntax :- INITCAP(char)
INITCAP(NAME)
--------------------
Sourabh
Sonu
Anku
Anku
Panku