You are on page 1of 25

DBMS LABMANUAL 21IT444

BIRLA VISHVAKARMA MAHAVIDYALAYA


ENGINEERING COLLEGE
(AN AUTONOMOUS INSTITUTION)
INFORMATION TECHNOLOGY DEPARTMENT
AY: 2022-23, SEMESTER-1
z

Student Name: Mansi Vinod Solanki


Subject: Database Management System (2IT03)
Year: 2 (3 Semester)
nd rd

ID: 21IT444

PRACTICAL LIST

Sr.
No. Title Date Sign
Study of Oracle Data Types.
1 Creation of Employee Table using DDL command,
Insertion of data into the database tables.
 Command to view the data in the table :
 all rows and all columns
 Filtering Table Data for selected columns and selected
Rows, for all rows and selected columns, for all columns
2 and selected rows
 Elimination of duplicated rows when using select statement
 Sorting data in a table
 Creating a table from table
 Insert data into the table from another table.
Commands to perform delete operation:
 Removal of all rows, removal of specific row.
Commands to perform update the contents of table.
3 Commands to modify the structure of tables:
 Add new column, drop a column, modify existing columns,
restrictions on “alter table” command.
Commands to rename truncate and destroy tables
Data Constraints:
 Add primary key, foreign key, unique key, Check
constraint.
4  How these constraints can be add at Column level and table
level.
 Define Integrity constraints via Alter table command.
 Destroy Integrity constraints via Alter table command.

Oracle functions:
 Aggregate functions, Numeric Functions, String Functions,
5
Conversion Functions, Date conversion functions, Date
Functions

pg. 1
DBMS LABMANUAL 21IT444

6 Study of group by and having clauses.


Perform Join operator :
7
 Equi-join, inner join, outer join, cross join, self-join
Perform Sub-queries:
 using sub-query in from clause,
 using correlated sub-query,
8
 using EXIST operator,
 using Multi-column sub-query,sub-query in “orderby”
clause.
9 Using UNION,INTERSECT,MINUS clause
10 Create Simple PL/ SQL block.
11 Create functions, trigger, procedure in PL/SQL
12 Introduction to No SQL

pg. 2
DBMS LABMANUAL 21IT444

Practical-1
Study of oracle data types:
Data types in SQL defines a sort of value that a database table column can contain. In a
database table, each column is required to have a data type with a name.

Types of data types:


User-Defined Types: User-defined data types employ Oracle's built-in data types and few
other user-defined data types as the construction blocks for object types, which models the
configuration and actions of data in applications.

 CHAR (character)
 DEC (Decimal)
 NUMERIC
 INT (Integer)
 SMALLEST
 FLOAT
 REAL
 DOUBLE PRECISION

Oracle-Supplied Types: Oracle offers SQL-based interfaces for defining new types when
the built-in or ANSI-supported types are not satisfactory. The performance and working for
these types can be implemented in C/C++, Java, or PL/ SQL. Oracle Database repeatedly
allows the low-level infrastructure services needed for input-output, varied client-side access
for new data types, and optimizations for data transferring between the application and the
database. The Oracle-supplied types, along with cross-references to the documentation of
their implementation listed as following:

 Any Types
 XML Types
 Spatial Types
 Media Types

pg. 3
DBMS LABMANUAL 21IT444

Creation of Employee Table using DDL command,


Insertion of data into the database tables.
Syntax:

Creating table:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);

Inserting data:
Insertion of data into the database tables.
The SQL INSERT INTO Statement:
The INSERT INTO statement is used to insert new records in a table.

INSERT INTO Syntax


It is possible to write the INSERT INTO statement in two ways:
1. Specify both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
2 If you are adding values for all the columns of the table, you do not need to
specify the column names in the SQL query. However, make sure the order of
the values is in the same order as the columns in the table. Here, the INSERT
INTO syntax would be as follows:

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

Example:

pg. 4
DBMS LABMANUAL 21IT444

pg. 5
DBMS LABMANUAL 21IT444

Practical-2
Command to view the data in the table:
All rows and all columns-

Filtering Table Data for selected columns and selected Rows, for
all rows and selected columns, for all columns and selected rows-
Syntax:

SELECT column1, column2, ...


FROM table_name;

pg. 6
DBMS LABMANUAL 21IT444

Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Elimination of duplicated rows when using select statement-


Syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name;

pg. 7
DBMS LABMANUAL 21IT444

Sorting data in a table-


Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

pg. 8
DBMS LABMANUAL 21IT444

Creating a table from table-


Syntax:
CREATE TABLE new_table_name AS
SELECT column1, column2,...
FROM existing_table_name
WHERE ....;

Insert data into the table from another table-


Syntax:
INSERT INTO table2
SELECT * FROM table1
WHERE condition;

pg. 9
DBMS LABMANUAL 21IT444

Practical-3
Commands to perform delete operation:

Removal of all rows, removal of specific row-


Syntax-
DELETE FROM table_name WHERE condition;

Commands to perform update the contents of table:


Syntax-
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

pg. 10
DBMS LABMANUAL 21IT444

Commands to modify the structure of tables:


Add new column, drop a column, modify existing columns,
restrictions on “alter table” command.
Syntax-
ALTER TABLE table_name
ADD column_name datatype;

Syntax-

ALTER TABLE table_name


DROP COLUMN column_name;

pg. 11
DBMS LABMANUAL 21IT444

Restriction on “Alter table” command-


 Column can’t be deleted with alter command.
 Column can’t be renamed a column.
 Column can’t be added in between of the existing columns.
 When a column is added, it will be added at the end of the table.

Commands to rename truncate and destroy tables-


Truncate table delete the data in the table, but not the table.
Syntax-
TRUNCATE TABLE table_name;

Syntax-
DROP TABLE table_name;
Note:- Be careful before deleting a table. Deleting a table, results in loss of all
data stored in the table.

pg. 12
DBMS LABMANUAL 21IT444

Practical-4
Data Constraints:
Add primary key, foreign key, unique key, Check constraint-

Syntax- Primary key

CREATE TABLE table_name (


column1 datatype PRIMARY KEY,
column2 datatype,
column3 datatype,
....
);

Syntax- Foreign Key

CREATE TABLE table_name2 (


column1 datatype PRIMARY KEY,
column2 datatype,
column3 datatype,
FOREIGN KEY (column3) REFERENCES table_name1(column3)
);

pg. 13
DBMS LABMANUAL 21IT444

Syntax- Unique Key

CREATE TABLE table_name (


column1 datatype UNIQUE,
column2 datatype,
column3 datatype UNIQUE,
....
);

Note: Unique and primary key are basically same. Both can’t have repeating
values. The difference is that there can be more than one unique key, while
there can be only one primary key.

pg. 14
DBMS LABMANUAL 21IT444

Practical-5
Oracle Functions:
1.Aggregate Function
SQL aggregation function is used to perform the calculations on multiple rows of a single
column of a table. It returns a single value.
It is also used to summarize the data.

1. COUNT FUNCTION
COUNT function is used to Count the number of rows in a database table. It can work on
both numeric and non-numeric data types.

COUNT function uses the COUNT(*) that returns the count of all the rows in a specified
table. COUNT(*) considers duplicate and Null.

Syntax

COUNT(*)
or
COUNT( [ALL|DISTINCT] expression )

pg. 15
DBMS LABMANUAL 21IT444

2. SUM Function

Sum function is used to calculate the sum of all selected columns. It works on numeric fields
only.

Syntax

1. SUM()
2. or
3. SUM( [ALL|DISTINCT] expression )

3. AVG function

The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.

Syntax

1. AVG()
2. or
3. AVG( [ALL|DISTINCT] expression )

pg. 16
DBMS LABMANUAL 21IT444

4. MAX Function

MAX function is used to find the maximum value of a certain column. This function determines
the largest value of all selected values of a column.

Syntax

1. MAX()
2. or
3. MAX( [ALL|DISTINCT] expression )

5. MIN Function

MIN function is used to find the minimum value of a certain column. This function determines
the smallest value of all selected values of a column.

Syntax

1. MIN()
2. or
3. MIN( [ALL|DISTINCT] expression )

pg. 17
DBMS LABMANUAL 21IT444

2.String Function
String functions are used to perform an operation on input string and return an output
string.

Following are the string functions defined in SQL:

1.ASCII(): This function is used to find the ASCII value of a character.

2.CHAR_LENGTH(): Doesn’t work for SQL Server. Use LEN() for SQL Server. This
function is used to find the length of a word.

3.LOWER(): This function is used to convert the upper case string into lower case.

pg. 18
DBMS LABMANUAL 21IT444

4.LPAD(): This function is used to make the given string of the given size by adding the
given symbol.

5.LTRIM(): This function is used to cut the given sub string from the original string.

6.RPAD(): This function is used to make the given string as long as the given size by
adding the given symbol on the right.

pg. 19
DBMS LABMANUAL 21IT444

7.RTRIM(): This function is used to cut the given sub string from the original string.

8.SUBSTR(): This function is used to find a sub string from the a string from the given
position

9.UPPER(): This function is used to convert the upper case string into upper case.

pg. 20
DBMS LABMANUAL 21IT444

10.AVG(): This function is use to find average.

pg. 21
DBMS LABMANUAL 21IT444

Practical-6
Study of group by and having clauses
Group by

The GROUP BY statement groups rows that have the same values into summary rows, like
"find the number of customers in each country".

The GROUP BY statement is often used with aggregate functions


(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.

GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s);
EX:

HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.

HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition

pg. 22
DBMS LABMANUAL 21IT444

GROUP BY column_name(s)
HAVING condition

ROLL UP GROUP BY
The ROLLUP is an extension of the GROUP BY clause. The ROLLUP option allows you to
include extra rows that represent the subtotals, which are commonly referred to as super-
aggregate rows, along with the grand total row. By using the ROLLUP option, you can use a
single query to generate multiple grouping sets.

SYNTAX:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY ROLLUP(C1,C2);

ROLL UP CUBE BY
The CUBE operator generates multiple grouping sets inside a GROUP BY.

CUBE generates subtotals across all column combinations specified in GROUP BY.

CUBE is similar to ROLLUP

SYNTAX:
SELECT column_name(s)
FROM table_name

pg. 23
DBMS LABMANUAL 21IT444

WHERE condition
GROUP BY CUBE(C1,C2);

SUB QUERIES
 A subquery is a SQL query nested inside a larger query.
 A subquery may occur in :
- A SELECT clause
- A FROM clause
- A WHERE clause
 The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE
statement or inside another subquery.
 A subquery is usually added within the WHERE Clause of another SQL SELECT
statement.
 You can use the comparison operators, such as >, <, or =. The comparison operator
can also be a multiple-row operator, such as IN, ANY, or ALL.
 A subquery is also called an inner query or inner select, while the statement
containing a subquery is also called an outer query or outer select.
 The inner query executes first before its parent query so that the results of an inner
query can be passed to the outer query.

Syntax :

SELECT column_name [, column_name ]


FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]

pg. 24
DBMS LABMANUAL 21IT444

FROM table1 [, table2 ]


[WHERE])

pg. 25

You might also like