Lab Commands
Lab Commands
Create
Alter
Drop
Rename
Truncate
NCS, APS / SITE 2
Data Manipulation Language
Insert
Select
Update
Delete
Example
SQL> insert into emp values
(3,6000,‘siva','01-jan-75','vellore');
1 row created.
1 row created.
NCS, APS / SITE 9
SQL> /
Enter value for id: 2
Enter value for salary: 2000
Enter value for name: blue
Enter value for dob: 24-oct-1987
Enter value for addr: chennai
old 2: &id,&salary,'&name','&dob','&addr')
new 2: 2,2000,'blue','24-oct-1987','chennai')
1 row created.
Example:
SQL> select * from emp;
ID SALARY NAME DOB ADDR
---------- ---------- ----------------------- ------------- --------------------
1 2000 red 23-OCT-86 vellore
2 2000 blue 24-OCT-87 chennai
3 6000 siva 01-jan-75 vellore
NCS, APS / SITE 11
Select (Particular column)
Syntax:
Select CN1,CN2 from tablename;
Example:
ALTER
Drop column
Syntax:
Example:
Table altered.
Syntax:
ALTER TABLE tablename
MODIFY column name newdatatype;
Example:
SQL> alter table emp modify age varchar2(2);
Table altered.
SQL> alter table emp modify age number(2);
Table altered.
ERROR at line 1:
Syntax:
Example:
Table altered.
Syntax:
Example:
SQL> alter table emp11
rename column eno to emp_no;
Table altered.
ENO AGE
---------- ----------
1 25
3 rows updated.
1 row updated.
NCS, APS / SITE 34
UPDATE (Using Null)
Syntax:
UPDATE tablename
SET col1 = new value, col2 = new value
WHERE cn IS NULL;
Example:
SQL> update emp11 set age=25
where ename is null;
1 row updated.
SQL> update emp11 set age=27
where ename is not null;
2 rows updated.
Syntax:
Example:
3 rows deleted.
Example
SQL> delete from emp11
where ename='z';
1 row deleted.
Arithmetic operator: +, -, *, /
Comparison Operator –
=, !=, <, <=, >, >=,
between, not between,
in, not in, like, not like,
is null, is not null
1 row updated.
SQL> /
Enter value for regno: 2
old 2: where regno=®no
new 2: where regno=2
1 row updated.
REGNO C1 C2 TOTAL
---------- ---------- ---------- ----------
1 27 34 61
2 25 43 68
REGNO C1 C2 TOTAL
---------- ---------- ---------- ----------
1 27 34 61
2 25 43 68
REGNO C1 C2 TOTAL
---------- ---------- ---------- ----------
1 27 34 61
2 25 43 68
EMPNO HIREDATE
----------- --------------
7698 01-MAY-81
7934 23-JAN-82
NCS, APS / SITE 49
SQL> select * from emp11;
ENO ENAME AGE
-------- ----------- ----------
1 sahil 25
2 anu
3 vijay 26
Comparison Operators:
Syntax for LIKE, NOT LIKE:
SELECT * FROM tn
WHERE cn com.op (pattern);
Foreign Key
Example:
SQL> create table emp2
(eno number(2) primary key,
ename varchar2(10) not null,
address varchar2(20) unique,
salary number(4) check (salary >=5000),
age number(2) check (age between 18 and 60));
Table created.
NCS, APS / SITE 55
Syntax 2 (constraint name along with cn):
CREATE TABLE tn
(cn1 datatype CONSTRAINT con_name con_type,
cn2 datatype CONSTRAINT con_name con_type,
cn3 datatype CONSTRAINT con_name con_type,
. . . );
Example:
SQL> create table dept2
(dno number(2) constraint dept_dno_pk primary key,
dname varchar(7) constraint dept_dn_nn not null,
dloc varchar(7) constraint dept_dl_uni unique,
no_of_emp number(3) constraint dept_emp_chk check (no_of_emp>10));
Table created.
NCS, APS / SITE 56
Syntax 3(with constraint name):
CREATE TABLE tn
(cn1 datatype,
CONSTRAINT con_name con_type (cn1), . . . );
Example:
SQL> create table c1 ( a number(3), b varchar2(5), c varchar2(7),
constraint c1_a_pk primary key (a),
constraint c1_b_uni unique (b),
constraint c1_c_chk check (c like ('s%')));
Table created.;
CREATE TABLE tn
(cn1 datatype REFERENCES parent_tn (pk cn) );
Example:
Table created.
CREATE TABLE tn
(cn1 datatype CONSTRAINT con_name REFERENCES
parent_tn (pk cn) );
Example
Table created.
NCS, APS / SITE 59
Syntax 3(with constraint name):
CREATE TABLE tn
(cn1 datatype,
CONSTRAINT con_name FOREIGN KEY (cn1)
REFERENCES parent_tn (pk cn) );
Example:
SQL> create table c4
(s number(3),
constraint c3_s_fk foreign key(s) references c1 (a));
Table created.
NCS, APS / SITE 60
SQL> create table works
(emp_no number(4),
dnum number(3),
constraint works_eno_fk foreign key(emp_no) references
emp2(eno),
constraint works_dnum_fk foreign key(dnum) references
dept2(dno),
constraint works_pk primary key(emp_no,dnum));
Table created.
Example:
SQL> alter table emp11 add constraint pk_eno_emp11
primary key(eno);
Table altered.
Table altered.
Table altered.
MODIFY cn datatype
Example:
SQL> alter table emp11 modify ename varchar2(5)
Table altered.
ALTER TABLE tn
Example:
SQL> alter table emp11 add constraint fk_dno_emp11
Table altered.
Syntax:
ALTER TABLE tn
Example:
SQL> alter table emp11
Table altered.
Group functions:
Result based on group of rows.
Syntax:
SELECT gf (cn) FROM tn;
Example:
SQL> select min(sal) from emp;
MIN(SAL)
---------------
800
NCS, APS / SITE 68
SQL> select max(sal) from emp;
MAX(SAL)
---------------
5000
COUNT(SAL)
-----------------
14
COUNT(DISTINCTSAL)
-------------------------------
12
Numeric functions:
Syntax:
SELECT n.f(CN) from TN ;
SELECT n.f(value) from DUAL;
Conversion functions:
Syntax:
SELECT con.f(CN) from TN ;
SELECT con.f(value) from DUAL;
NCS, APS / SITE 90
SQL> select to_char(hiredate,'DDth month yyyy') from emp;
TO_CHAR(HIREDATE,'D
-------------------
17TH december 1980
02ND april 1981
SQL> select to_char(hiredate,'DDth fmmonth yyyy') from emp;
TO CHAR(HIREDATE,'D
-------------------
17TH december 1980
02ND april 1981
SQL> select to_char(hiredate,'DDth Month yyyy') from emp;
TO_CHAR(HIREDATE,'D
-------------------
17TH December 1980
02ND April 1981
6 rows selected.
NCS, APS / SITE 95
SET OPERATORS:
Union
Union all
Intersect
Minus
Syntax:
SELECT cn1, cn2 FROM TN1
set operator
SELECT cn3, cn4 FROM TN2;
Equi Join:
SQL> select eno, name, a.dno, dname
2 from fj1 a, fj2 b
3 where a.dno = b.dno;
Example:
SQL> select eno, name, dno, dname
2 from fj1 natural join fj2 ;
6 rows selected.
Example:
Local declarations;
BEGIN
executable statements;
EXCEPTION
exception handlers;
END;
Syntax:
variablename tablename.columnname<attribute>
Example:
r student.rollno%type;
y student%rowtype;
- if then
- if then else
- if then elsif
Iterative control
- Simple loop
- For loop
- While loop
statements;
ELSIF
statements;
END IF;
declare
a number:=100;
begin
loop
a:=a+25;
exit when a=250;
end loop;
dbms_output.put_line(a);
end;
NCS, APS / SITE 122
WHILE loop
WHILE condition
LOOP
statements;
END LOOP;
declare
i number:=0;
j number:=0;
begin
while i<100 loop
j:=j+1;
i:=i+2;
dbms_output.put_line(i||','||j);
end loop;
end;
/
NCS, APS / SITE 123
FOR loop
FOR variable IN [REVERSE] initial value . . final value
LOOP
statements;
END LOOP;
declare
i number:=0;
j number:=0;
begin
for i in 1..10
loop
j:=j+1;
dbms_output.put_line(i+2||','||j);
end loop;
end;
NCS, APS / SITE 124
SQL> select * from student;
6 rows selected.
NCS, APS / SITE 125
SQL> declare
2 r student.rollno%type;
3 n student.name%type;
4 begin
5 select rollno,name into r,n from student
6 where mark=35;
7 dbms_output.put_line(r);
8 dbms_output.put_line(n);
9 exception
10 when no_data_found then
11 dbms_output.put_line('such an item not available');
12 end;
13 /
1
arya
Cursor Processing:
Declare the cursor
CURSOR cursor_name IS select statement;
Open the cursor for a query
OPEN cursor_name;
Fetch the results into PL/SQL variables
FETCH cursor_name INTO list_of_variables;
Close the cursor
CLOSE cursor_name;
SUBPROGRAMS
Subprograms are PL/SQL block that can be created and invoked
whenever required
* Procedure
* Function
Syntax:
CREATE OR REPLACE PROCEDURE proc_name(parameters) IS
local declarations;
BEGIN
executable statements;
EXCEPTION
exception handlers;
END;
To Execute
exec proc_name (parameter values);
SQL> print v;
V
--------------------------------
fail
RETURN datatype IS
local declarations;
BEGIN
executable statements;
EXCEPTION
exception handlers;
END;
DECLARE
local decl.;
BEGIN
executable statements;
END;
NCS, APS / SITE 148
Variable names used in triggers
*two are used :old and :new
- old specifies the row value before change
- new specifies the value after change
Example:
create or replace trigger tot
before insert on stu
for each row
begin
:new.total := :new.c1 + :new.c2;
end;