Professional Documents
Culture Documents
Dbms Lab Syntax: Table Creation
Dbms Lab Syntax: Table Creation
Dbms Lab Syntax: Table Creation
Table creation:
ALTER COMMAND:
2. modify:
3. to remove column:
TRUNCATE:
DROP:
RENAME:
// to rename table.
DELETE:
UPDATE:
Update tablename
Set field1 = val1, field2 = val2 ……………….
[where condition];
SELECT:
DISTINCT/UNIQUE:
CONSTRAINTS:
1. NOT NULL:
// does not allow null values
Using create:
Using alter:
2. UNIQUE:
// does not allow duplicate values
Using create:
Using alter:
3. PRIMARY KEY:
Using create:
Using alter:
4. CHECK:
// allows the values which lie between given boundaries only
Using create:
Create table tablename
(
Field1 datetype not null;
……………
Using alter:
5. FOREIGN KEY:
// establishes relation between two tables
// values of foreign key are subset of primary key
Using create:
Using alter:
// can be used to modify the foreign key when primary key gets
updated or deleted
DROPPING A CONSTRAINT:
1. ORDER BY:
2. WHERE CLAUSE:
// used to filter the records based on given condition
1. SUM() :
// used to calculate sum of all the attribute values
2. AVG() :
// used to calculate average of all the attribute values
5. MIN():
// gives minimum value from given attribute values
NESTED QUERIES:
EXAMPLE:
GROUP BY CLAUSE:
Example:
1. UNION:
UNION
2. INTERSECT:
INTERSECT
3. MINUS:
MINUS
1.CROSS JOIN:
2.NATURAL JOIN:
// returns all matched records and all unmatched records of any one or
both tables based on left, right or full outer join.
EXAMPLE:
[ DECLARE
BEGIN
………
END;
/
DATATYPES:
Number(size)
Number(size,decimals)
Char(size)
Varchar2(size)
Boolean
%type
%rowtype
To print output:
//in program
Dbms_output.put_line();
Sql> ed d:/foldername/filename.sql
To execute:
Sql> @ d:/foldername/filename.sql
IF STATEMENT:
IF condition then
………..
[
ELSE
………
]
END IF;
LOOPS:
1.simple loop:
LOOP
……..
Exit when condition;
END LOOP;
2. while loop:
While condition
LOOP
……..
End loop;
3.for loop:
SELECT STATEMENT:
// used to create a table variable which can store all the attribute
values similar to a structure variable
Example:
Recvar student%rowtype;
CURSORS:
SYNTAX:
1. declare:
Cursor cursorname IS select statement;
NOTE:
// while using loop
LOOP
Fetch cursorname into <variables>;
EXIT WHEN cursorname%NOTFOUND; //(to exit when reached end)
End loop;
PROCEDURES:
SYNTAX:
BEGIN
……….
END;
/
// IN – constant
SYNTAX:
BEGIN
……….
Return x;
END;
/
EXAMPLE PROBLEMS ON PROCEDURES AND FUNCTIONS:
SOLUTION:
dcount number(3);
ecount number(3);
pname emp.ename%type;
dmax number(3);
dmin number(3);
begin
end;
/
EMPLOYEE DETAILS
------------------------------------------------------------------------------------------
Number of Employees: 14
Name of the President: KING
Name of the Employee (Highest Salary):
Name of the Employee (Lowest Salary):
Name of the Employee (Most Senior):
Name of the Employee (Most Junior):
____________________________________________________________
END
SOLUTION:
is
ecount number(3);
pname emp.ename%type;
maxsalname emp.ename%type;
minsalname emp.ename%type;
seniorname emp.ename%type;
juniorname emp.ename%type;
begin
end;
/
____________________________________________________________
END
SOLUTION:
is
dcount number(3);
deptnoo dept.deptno%type;
empcount number(2);
rowdept cdept%rowtype;
begin
open cdept;
open cemp;
loop
fetch cdept into rowdept;
exit when cdept%NOTFOUND;
dbms_output.put_line(rpad(rowdept.deptno,10,' ')||rowdept.dname);
end loop;
close cdept;
dbms_output.put_line(' ');
dbms_output.put_line('Number of Employees in each department: ');
dbms_output.put_line(' ');
dbms_output.put_line('Deptno'||' '||'No.Of Employees');
loop
fetch cemp into deptnoo,empcount;
exit when cemp%NOTFOUND;
dbms_output.put_line(rpad(deptnoo,15,' ')||empcount);
end loop;
close cemp;
dbms_output.put_line('----------------------------------------------
-----------------------------');
dbms_output.put_line('| '||' END
'||' |');
end;
/
____________________________________________________________
END
SOLUTION:
is
ecount number(3);
deptnoo dept.deptno%type;
ttsal number(6);
tsal number(6);
hsal emp%rowtype;
lsal emp%rowtype;
begin
open cdept;
loop
fetch cdept into deptnoo,ttsal;
if ttsal is null then
ttsal:=0;
end if;
exit when cdept%NOTFOUND;
dbms_output.put_line(rpad(deptnoo,15,' ')||ttsal);
end loop;
close cdept;
dbms_output.put_line(' ');
dbms_output.put_line('Highest salary details: ');
dbms_output.put_line('Employeename'||' '||'salary'||'
'||'Deptno');
dbms_output.put_line(hsal.ename||' '||hsal.sal||'
'||hsal.deptno);
dbms_output.put_line('Lowest salary details: ');
dbms_output.put_line('Employeename'||' '||'salary'||'
'||'Deptno');
dbms_output.put_line(lsal.ename||' '||lsal.sal||'
'||lsal.deptno);
dbms_output.put_line('----------------------------------------------
-----------------------------');
dbms_output.put_line('| '||' END
'||' |');
end;
/
NOTE:
// to execute procedures