JATIN Dbms (1) 1
JATIN Dbms (1) 1
22SCSE1012203
INDEX
Exp.no Programs Date Signature
1. Design Er Diagrams for various or based on
given projects.
2. Implement DDL Statements and DML
statements.
3. Execute the SELECT command with
different Clauses.
4. Implement GROUP functions(avg, count,
max, min, sum).
5. Execute the concept of Data Control
Language (DCL).
6. Implement Transaction Control Language
(TCL).
7. Execute various types of Integrity
Constraints on database.
8. Analysis and design of the normalized
tables.
9. Write a PL/SQL block to satisfy some
conditions by accepting input from the user.
10. Implement the concept of grouping of Data
and sub- queries.
11. Write a PL/SQL block to implementation of
factorial using function.
12. Write a PL/SQL Procedure for GCD
Numbers.
13. Write a PL/SQL block for greatest of three
numbers using IF AND ELSE IF.
14. Write a PL/SQL block for summation of
odd numbers using for LOOP.
Jatin Kumar Sharma
22SCSE1012203
DDL or Data Definition Language actually consists of the SQL commands that can
be used to define the database schema.
The SQL DDL allows specification of not only a set of relations but also information
about each relation, including-
Schema for each relation
The domain of values associated with each attribute.
The integrity constraints.
The set of indices to be maintained for each relation.
The security and authorization information for each relation.
The physical storage structure of each relation on disk.
Syntax:-
CREATE TABLE
Emp1 (EID int, EName Char, Edept char, EDOB date,Salary int)
Create Table Emp1(EID int, EName varchar(20), Edept varchar(10), EDOB Date,
Salary int);
CREATE TABLE TABLENAME (COLUMN_NAME1
DATA_TYPE1(SIZE1),……. COLUMN_NAMEN DATA_TYPEN(SIZEN));
Output:-
ALTER TABLE:
Jatin Kumar Sharma
22SCSE1012203
Syntax:-
Output:-
DROP Command:
This command is used to remove the definition of a table. When you drop a table
database loses all the data in the table and all indexes associated with it.
Syntax:-
DROP object object_name.
Output:-
Jatin Kumar Sharma
22SCSE1012203
RENAME Command:
It is used to rename table.
Syntax:
Rename old name to new name;
Output:-
TRUNCATE Command
This command is used to remove all rows from the table and to release the storage
space used by that table.
Syntax:
Truncate table <tablename>;
Output:-
Jatin Kumar Sharma
22SCSE1012203
DML Commands:-
The SQL commands that deals with the manipulation of data present in the
database belong to DML or Data Manipulation Language and this includes most of
the SQL statements. It is the component of the SQL statement that controls access
to data and to the database. Basically, DCL statements are grouped with DML
statements.
SELECT Command:
This command is used to show table data.
Syntax:
Select * from <tablename>;
Output:-
INSERT Command:
Jatin Kumar Sharma
22SCSE1012203
Records can be added to table using this command. Only one row can be inserted at
a time.
Syntax:
insert into<tablename>(col1,col2…coln)values(List of values)
Output:-
UPDATE Command:-
The UPDATE statement is used to update the data of an existing table in database.
Syntax:-
UPDATE table_name SET column1 = value1, column2 = value2,... WHERE
condition;
OUTPUT:-
DELETE Command:
Jatin Kumar Sharma
22SCSE1012203
It is used to remove rows from a table. The entire row can be deleted using this
command.
Syntax:
Delete from<table name> where condition;
OUTPUT:-
Jatin Kumar Sharma
22SCSE1012203
WHERE CLAUSE:-
The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.
Syntax:-
Output:-
Jatin Kumar Sharma
22SCSE1012203
GROUP BY CLAUSE:-
A GROUP BY clause, part of a Select Expression, groups a result into subsets that
have matching values for one or more columns. In each group, no two rows have
the same value for the grouping column or columns. NULLs are considered
equivalent for grouping purposes.
Syntax:-
Output:-
HAVING CLAUSE:-
A HAVING clause restricts the results of a GROUP BY in a Select
Expression. The HAVING clause is applied to each group of the grouped table,
much as a WHERE clause is applied to a select list.
Syntax:-
Output:-
Jatin Kumar Sharma
22SCSE1012203
ORDER BY CLAUSE:-
In Oracle, ORDER BY Clause is used to sort or re-arrange the records in the result
set. The ORDER BY clause is only used with SELECT statement.
Syntax:-
SELECT expressions FROM tables ORDER BY expression;\
Output:-
Jatin Kumar Sharma
22SCSE1012203
I. Avg functions:-
The Oracle AVG() function accepts a list of values and returns the average.
Syntax:-
Output:-
Syntax:-
COUNT( [ALL | DISTINCT | * ] expression)
Output:-
Jatin Kumar Sharma
22SCSE1012203
Syntax:
MAX ( [ DISTINCT | ALL ] Expression )
Output:-
Syntax:
MIN ([ALL | DISTINCT] expression )
Output:-
Jatin Kumar Sharma
22SCSE1012203
V. Sum functions:-
The Oracle SUM() function is an aggregate function that returns the sum of all or
distinct values in a set of values.
Syntax:-
SUM( [ALL | DISTINCT] expression)
Output:-
Jatin Kumar Sharma
22SCSE1012203
Syntax:-
GRANT privileges_names ON object TO user;
(ii) REVOKE:-
This command withdraws the user’s access privileges given by using the GRANT
command.
Syntax :
revoke privilege_name on object_name from {user_name | public | role_name}
Jatin Kumar Sharma
22SCSE1012203
Hence, the following TCL commands are used to control the execution of a
transaction:
COMMIT: Commits a Transaction.
ROLLBACK: Rollbacks a transaction in case of any error occurs.
SAVEPOINT: Sets a save point within a transaction.
(i) COMMIT:-
If everything is in order with all statements within a single transaction, all changes
are recorded together in the database is called committed. The COMMIT command
saves all the transactions to the database since the last COMMIT or ROLLBACK
command.
Syntax:
COMMIT;
Output:-
Jatin Kumar Sharma
22SCSE1012203
(ii) SAVEPOINT:-
creates points within the groups of transactions in which to ROLLBACK.
A SAVEPOINT is a point in a transaction in which you can roll the transaction
back to a certain point without rolling back the entire transaction.
Syntax:-
SAVEPOINT SAVEPOINT_NAME;
(iii) ROLLBACK:-
If any error occurs with any of the SQL grouped statements, all changes need to be
aborted. The process of reversing changes is called rollback. This command can
only be used to undo transactions since the last COMMIT or ROLLBACK
command was issued.
Syntax:
ROLLBACK;
OUTPUT:-
Jatin Kumar Sharma
22SCSE1012203
The NOT NULL is a constraint in SQL which does not allow you to insert NULL
values in the specified column.
If any column is defined as a NOT NULL constraint in the table, we cannot insert a
new record in the table without adding the value to the column.
Output:-
(ii) UNIQUE:-
The UNIQUE constraint ensures that all values in a column are different. Both the
UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for
a column or set of columns.
Output:-
Output:-
Jatin Kumar Sharma
22SCSE1012203
(v) CHECK:-
The CHECK constraint is used to limit the value range that can be placed in a
column.
If you define a CHECK constraint on a column it will allow only certain values for
this column.
Output:-
(vi) DEFAULT:-
The DEFAULT Constraint is used to fill a column with a default and fixed value.
The value will be added to all new records when no other value is provided.
Output:-
Jatin Kumar Sharma
22SCSE1012203
Example:
Relation EMPLOYEE is not in 1NF because of multi-valued attribute
EMP_PHONE.
In the second normal form, all non-key attributes are fully functional dependent on
the primary key
Example:
Let's assume, a school can store the data of teachers and the subjects they teach. In
a school, a teacher can teach more than one subject.
A relation is in third normal form if it holds atleast one of the following conditions
for every non-trivial function dependency X → Y.
Jatin Kumar Sharma
22SCSE1012203
X is a super key.
Y is a prime attribute, i.e., each element of Y is part of some candidate key.
Example:-
Employee details tables
Example:
Let's assume there is a company where employees work in more than one
department.
Jatin Kumar Sharma
22SCSE1012203
Jatin Kumar Sharma
22SCSE1012203
a number;
b number;
c number;
begin
a: =&a;
b: =&b;
c: =a+b;
end;
Output:-
Jatin Kumar Sharma
22SCSE1012203
A subquery is used to return data that will be used in the main query as a condition
to further restrict the data to be retrieved.
Rules:-
There are a few rules that subqueries must follow −
Subqueries must be enclosed within parentheses.
A subquery can have only one column in the SELECT clause, unless
multiple columns are in the main query for the subquery to compare its
selected columns.
An ORDER BY command cannot be used in a subquery, although the main
query can use an ORDER BY. The GROUP BY command can be used to
perform the same function as the ORDER BY in a subquery.
Subqueries that return more than one row can only be used with multiple
value operators such as the IN operator.
The SELECT list cannot include any references to values that evaluate to a
BLOB, ARRAY, CLOB, or NCLOB.
A subquery cannot be immediately enclosed in a set function.
Subqueries are most frequently used with the SELECT statement. The basic syntax
is as follows
SYNTAX:-
SELECT column_name FROM table WHERE column_name OPERATOR
(SELECT column_name FROM table , WHERE);
Example:-
Jatin Kumar Sharma
22SCSE1012203
Syntax:-
SET column_name = new_value WHERE OPERATOR VALUE (SELECT
COLUMN_NAME FROM TABLE_NAME condition);
Example:-
Syntax:-
DELETE FROM TABLE_NAME WHERE OPERATOR VALUE (SELECT
COLUMN_NAME FROM TABLE_NAME condition);
Jatin Kumar Sharma
22SCSE1012203
Example:-
CODE:-
DECLARE
fact number := 1;
n number := 7;
BEGIN
fact:=n*fact;
n:=n-1;
End loop;
dbms_output.put_line(fact);
Jatin Kumar Sharma
22SCSE1012203
END;
Output:-
DECLARE
num1 INTEGER;
num2 INTEGER;
t INTEGER;
BEGIN
num1 := 8;
num2 := 56;
Output-
Jatin Kumar Sharma
22SCSE1012203
Output:-
Code:-
declare
n number;
endvalue number;
begin
endvalue:=500;
n:=1;
sum1:=sum1+n;
end loop;
Jatin Kumar Sharma
22SCSE1012203
dbms_output.put_line('sum ='||sum1);
end;
Output:-