DBMS New
DBMS New
1. Lab Prerequisute
3. Outcome
4. Program List
6. Value Addition
7. Appendix
PROGRAM LIST
TYPES OF CONSTRAINTS:
1) Primary key : A PRIMARY KEY constraint is a unique identifier for a row within a
database table. Every table should have a primary key constraint to uniquely identify
each row and only one primary key constraint can be created for each table. The
primary key constraints are used to enforce entity integrity.
2) Foreign key/references : A FOREIGN KEY constraint prevents any actions that would
destroy link between tables with the corresponding data values. A foreign key in one table
points to a primary key in another table. Foreign keys prevent actions that would leave rows
with foreign key values when there are no primary keys with that value. The foreign key
constraints are used to enforce referential integrity.
3) Check : A CHECK constraint is used to limit the values that can be placed in a column.
The check constraints are used to enforce domain integrity.
4) Unique : A UNIQUE constraint enforces the uniqueness of the values in a set of columns,
so no duplicate values are entered. The unique key constraints are used to enforce entity
integrity as the primary key constraints.
5) Not null : A NOT NULL constraint enforces that the column will not accept null values.
The not null constraints are used to enforce domain integrity, as the check constraints.
SQL joins are used to query data from two or more tables, based on a relationship between
certain columns in these tables.
SQL COMMANDS
• Provides user security on each view - it depends on your data policy security.
• Using view to convert units - if you have a financial data in US currency, you can create
view to convert them into Euro for viewing in Euro currency.
SQL COMMANDS
UNION
It returns a union of two select statements. It is returning unique (distinct) values of them.
MINUS
MINUS (also known as EXCEPT) returns the difference between the first and second
SELECT statement. It is the one where we need to be careful which statement will be put
first, cause we will get only those results that are in the first SELECT statement and not in the
second.
INTERSECT
INTERSECT is opposite from MINUS as it returns us the results that are both to be found in
first and second SELECT statement.
An aggregate function is a function where the values of multiple rows are grouped together
as input on certain criteria to form a single value of more significant meaning or
measurement such as a set, a bag or a list.
Nested Query can have more than one level of nesting in one single query. A SQL nested
query is a SELECT query that is nested inside a SELECT, UPDATE, INSERT, or DELETE
SQL query.
SQL COMMANDS
declare
n number;
i number;
f number:=1;
begin
n:=&n;
for i in 1..n
loop
f:=f*i;
end loop;
dbms_output.put_line(n||'! = '||f);
end;
Output:
Enter value for n: 5
old 6: n:=&n;
new 6: n:=5;
5! = 120
PL/SQL procedure successfully completed.
11.Write a PL/SQL code block to implement
, . . . ,column_nameN)
[COMPUTE STATISTICS];
A collection is an ordered group of elements having the same data type. Each element is
identified by a unique subscript that represents its position in the collection.
Nested table
An index-by table (also called an associative array) is a set of key-value pairs. Each key
is unique and is used to locate the corresponding value. The key can be either an integer
or a string.
Trigger automatically associated with DML statement, when DML statement execute
trigger implicitly execute.
We can create trigger using the CREATE TRIGGER statement. If trigger activated,
implicitly fire DML statement and if trigger deactivated can't fire.
DECLARE
[declaration_section
variable
declarations;
constant
declarations;
]
BEGIN
[executable_section
PL/SQL execute/subprogram body
]
EXCEPTION
[exception_section
PL/SQL Exception block
]
VALUE ADDITION
IMPLEMENTATION OF CURSORS
SQL> create table bill(name varchar2(10), address varchar2(20), city varchar2(20), unit
number(10));
Table created.
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
3 b bill %ROWTYPE;
4 begin
5 open c;
7 loop
8 fetch c into b;
10 exit;
11 else
12 if(b.unit<=100) then
4);
20 else
5);
22 end if;
23 end if;
24 end loop;
25 close c;
26 end;
27 /
APPENDIX
QUERY: 01 Write a query to create a table employee with empno, ename, designation, and
salary.
QUERY: 02 Write a query to display the column name and datatype of the table employee.
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
D.B.M.S. PROGRAM
M.I.E.T.
QUERY: 03 Write a Query to Alter the column EMPNO NUMBER (4) TO EMPNO
NUMBER(6).
SQL > ALTER <TABLE NAME> MODIFY <COLUMN NAME> <DATATYPE> (SIZE);
EMPNO NUMBER(6)
ENAME VARCHAR2(10)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUERY: 04 Write a Query to Alter the table employee with multiple columns (EMPNO,
ENAME.)
SQL > ALTER <TABLE NAME> MODIFY <COLUMN NAME1> <DATATYPE> (SIZE),
MODIFY <COLUMN NAME2> <DATATYPE> (SIZE)
………………………………………….;
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2);
D.B.M.S. PROGRAM
M.I.E.T.
SQL> ALTER TABLE <TABLE NAME> ADD (<COLUMN NAME> <DATA TYPE>
<SIZE>);
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
SQL> ALTER TABLE <TABLE NAME> ADD (<COLUMN NAME1> <DATA TYPE>
<SIZE>,(<COLUMN NAME2> <DATA TYPE> <SIZE>,
………………………………………………………………);
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
DOB DATE
DOJ DATE
REMOVE / DROP
D.B.M.S. PROGRAM
M.I.E.T.
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
DOB DATE
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
REMOVE
D.B.M.S. PROGRAM
M.I.E.T.
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
D.B.M.S. PROGRAM
M.I.E.T.
DELETE
UPDATE
INSERT
SELECT
D.B.M.S. PROGRAM
M.I.E.T.
SQL :> INSERT INTO <TABLE NAME> VALUES< ‘&column name’, ‘&column
name 2’,…..);
SQL> /
Enter value for empno: 103
Enter value for ename: PANNERSELVAM
Enter value for designatin: ASST. PROF
Enter value for salary: 20000
old 1: INSERT INTO EMP
VALUES(&EMPNO,'&ENAME','&DESIGNATIN','&SALARY')
new 1: INSERT INTO EMP VALUES(103,'PANNERSELVAM','ASST.
PROF','20000')
1 row created.
SQL> /
D.B.M.S. PROGRAM
M.I.E.T.
UPDATE
D.B.M.S. PROGRAM
M.I.E.T.
DELETE
D.B.M.S. PROGRAM
M.I.E.T.
SAVEPOINT:
ROLL BACK
D.B.M.S. PROGRAM
M.I.E.T.
COMMIT
QUERY: 03 Write a query to implement the Rollback.
SQL> COMMIT;
SQL> COMMIT;
Commit complete.
Constraints are part of the table definition that limits and restriction on the value entered into
its columns.
TYPES OF CONSTRAINTS:
D.B.M.S. PROGRAM