DBMS
DBMS
Chandigarh
Practical File
Database Management System
Submitted to : - Submitted by : -
Prof. Chetan Kumar Name - Chirag
Gupta Sir Roll no. - 4243/19
Class - BCA 4th Semester
1|Page
Sr. No. Title Page no. Remark
1. Introduction of Data Base Management 4
System
2. Show Databases, Create Databases, 5
Use Statement
3. Create Table, Describe Statement, 6
Insertion Table
4. Select Query, Where Clause, 6-7
Comparison Operators
5. Order By Clause, Delete, Rename 8-9
Command
6. Drop, Truncate, Update Tables 9-10
Statement
7. Count, Avg, Sum Function 10-11
8. Arithmetic Operator Command 12-13
9. Logical Operator Command 13-14
10. Between and Is Operator 14-15
11 Alter, Drop, Modify Table Statement 15-16
12. Change Data Type, Alter Table – Add 16-17
Column
13. Group By Statement, Sum 17-18
2|Page
Sr. No. Title Page no. Remark
24. Greatest, Mod, Rand(Random No.), 31-32
Sqrt(Squar Root) & pow(Power)
3|Page
Data -: In simple words, data can be facts related to any object in consideration. For
example, your name, age, height, weight, etc. are some data related to you. A picture,
image, file, pdf, etc. can also be considered data.
Components
4|Page
SQL COMMANDS
✓ Show Databases Command -: The most common way to get a list of the
MySQL databases is by using the MySQL client to connect to the MySQL server
and run the SHOW DATABASES command.
✓ Create Databases -:
Example :-
✓ USE -: The USE statement tells MySQL to use the named database as the default
(current) database for subsequent statements. This statement requires some
privilege for the database or some object within it.
The named database remains the default until the end of the session or
another USE statement is issued:
Syntax -:
USE db_name;
Example :-
5|Page
✓ Create Table -: A table is basic unit of storage. It is composed of rows and
columns. To create a table we will name the table and the columns of the table.
It must begin with a letter and can be up to 30 characters long.
It must not be duplicate and not any reserved word.
Syntax:
DESCRIBE one;
OR
DESC one;
Example :
✓ The Insertion Of Data Into Table :- Once a table is created, the most
natural thing to do is load this with data to be manipulated later i.e. to insert the
rows in a table. The data in a table can be inserted in three ways.
Syntax:-
INSERT INTO <table name >(<columnname1>,<columnname2>) VALUES(<expression1>,<expression 2>);
OR
INSERT INTO <tablename>VALUES(<expression1 >,<expression2> );
OR
INSERT INTO <tablename> VALUES(‘<&columnname1>’ ,’<&columnname2>’);
Example :-
6|Page
✓ Select Query -: SELECT QUERY is used to fetch the data from the MySQL
database. Databases store data for later retrieval. The purpose of MySQL Select is
to return from the database tables, one or more rows that match a given criteria.
Select query can be used in scripting language like PHP, Ruby, or you can execute
it via the command prompt.
Syntax:-
Select colname1,colname2,colname3,……. From tablename;
Select * from tablename;
Example :-
✓ Where Clause -: We have seen the SQL SELECT command to fetch data from
a MySQL table. We can use a conditional clause called the WHERE Clause to
filter out the results. Using this WHERE clause, we can specify a selection criteria
to select the required records from a table.
Syntax:-
Select colname1,colname2,colname3,……. From tablename where condition;
Example :-
Comparison Operators
= Checks if the values of the two operands are equal or not, if yes, (A = B) is not true.
then the condition becomes true.
!= Checks if the values of the two operands are equal or not, if the (A != B) is true.
values are not equal then the condition becomes true.
> Checks if the value of the left operand is greater than the value of (A > B) is not true.
the right operand, if yes, then the condition becomes true.
< Checks if the value of the left operand is less than the value of the (A < B) is true.
7|Page
right operand, if yes then the condition becomes true.
>= Checks if the value of the left operand is greater than or equal to (A >= B) is not true.
the value of the right operand, if yes, then the condition becomes
true.
<= Checks if the value of the left operand is less than or equal to the (A <= B) is true.
value of the right operand, if yes, then the condition becomes true.
Example :-
Syntax:-
DELETE FROM table_name WHERE condition;
Example :-
Delete from student;
All data delete.
8|Page
Example :-
Example :
Syntax:-
Drop Table Name;
Example :-
Syntax:-
TRUNCATE TABLE table_name;
Example :-
9|Page
✓ Update Statement -: The UPDATE statement is used to modify the existing
records in a table.
Note: Be careful when updating records in a table! Notice the WHERE clause in the
UPDATE statement. The WHERE clause specifies which record(s) that should be
updated. If you omit the WHERE clause, all records in the table will be updated!
Syntax:-
UPDATE table_name SET column1 =new value1, column2 = newvalue2, ... WHERE
condition;
Example :-
✓ Count() Function -: The COUNT() function returns the number of rows that
matches a specified criterion.
10 | P a g e
Syntax:-
SELECT COUNT(column_name) FROM table_name WHERE condition;
Example :-
Example :-
✓ Avg() Function -: The AVG() function returns the average value of a numeric
column.
Syntax -:
SELECT AVG(column_name) FROM table_name WHERE condition;
Example :-
✓ SUM() function -: The SUM() function returns the total sum of a numeric
column.
Syntax -:
SELECT SUM(column_name) FROM table_name WHERE condition;
Example :-
11 | P a g e
Arithmetic operator command
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
% Modulo
Example -:
12 | P a g e
Logical Operators
Operator Description
ALL TRUE if all of the subquery values meet the condition
AND TRUE if all the conditions separated by AND is TRUE
ANY TRUE if any of the subquery values meet the condition
BETWEEN TRUE if the operand is within the range of comparisons
EXISTS TRUE if the subquery returns one or more records
IN TRUE if the operand is equal to one of a list of
expressions
Example -:
13 | P a g e
✓ Between Operator -: The BETWEEN operator selects values within a given
range. The values can be numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
Syntax-:
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN
value1 AND value2;
Example -:
14 | P a g e
✓ Is Operator –: In databases, NULL is unknown, not applicable or missing
information, therefore, you cannot use the comparison operators (=, >,<, etc.,) to
check whether a value is NULL or not.
Syntax-:
SELECT * FROM table_name WHERE column_name is (not) null;
Example -:
15 | P a g e
Drop Example -:
Modify Example -:
Example -:
Syntax-:
SELECT column_name(s) FROM table_name WHERE condition GROUP BY
column_name(s) ORDER BY column_name(s);
Example -:
17 | P a g e
✓ HAVING Clause -: The HAVING clause was added to SQL because the
WHERE keyword cannot be used with aggregate functions.
Syntax-:
SELECT column_name(s) FROM table_name WHERE condition GROUP BY
column_name(s) HAVING condition ORDER BY column_name(s);
Example -:
Syntax-:
• CREATE TABLE Persons (ID int NOT NULL, LastName varchar(255) NOT
NULL, FirstName varchar(255),Age int,PRIMARY KEY (ID));
18 | P a g e
• CREATE TABLE Persons (ID int NOT NULL,LastName varchar(255) NOT
NULL,FirstName varchar(255),Age int,CONSTRAINT PK_Persons PRIMARY
KEY (ID,LastName));
Example -:
Example -:
19 | P a g e
Drop Primary Key Example -:
Syntax-:
• CREATE TABLE Orders (OrderID int NOT NULL,OrderNumber int NOT
NULL, PersonID int,PRIMARY KEY (OrderID),FOREIGN KEY (PersonID)
REFERENCES Persons(PersonID));
20 | P a g e
✓ Foreign Key On Alter Table -: To create a FOREIGN KEY constraint on the
"PersonID" column when the "Orders" table is already created
Syntax-:
• ALTER TABLE Orders ADD FOREIGN KEY (PersonID) REFERENCES
Persons(PersonID);
Syntax-:
• ALTER TABLE Orders DROP FOREIGN KEY FK_PersonOrder;
• ALTER TABLE Orders DROP CONSTRAINT FK_PersonOrder;
Example -:
21 | P a g e
✓ Unique Constraint On Create Table -:
• 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.
• A PRIMARY KEY constraint automatically has a UNIQUE constraint.
• However, you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.
Syntax-:
• CREATE TABLE Persons(ID int NOT NULL UNIQUE,LastName varchar(255)
NOT NULL,FirstName varchar(255),Age int);
22 | P a g e
Syntax-:
• ALTER TABLE Persons ADD UNIQUE (ID);
Syntax-:
• ALTER TABLE Persons DROP INDEX UC_Person;
Syntax-:
CREATE TABLE Persons (ID int NOT NULL,LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,Age int);
23 | P a g e
Example -:
✓ Not Null On Alter Table -: To create a NOT NULL constraint on the "Age"
column when the "Persons" table is already created, use the following SQL:
Syntax-:
ALTER TABLE Persons MODIFY Age int NOT NULL;
Example -:
Syntax-:
CREATE TABLE Persons (ID int NOT NULL,LastName varchar(255) NOTNULL,
FirstName varchar(255),Age int,CHECK (Age>=18));
24 | P a g e
Example -:
Syntax-:
ALTER TABLE Persons ADD CHECK (Age>=18);
Example -:
Syntax-:
ALTER TABLE Persons DROP CONSTRAINT CHK_PersonAge;
25 | P a g e
Example -:
Syntax-:
CREATE TABLE Persons (ID int NOT NULL,LastName varchar(10) NOT NULL,
FirstName varchar(10),Age int,City varchar(255) DEFAULT 'Sandnes');
Example -:
Syntax-:
ALTER TABLE table_name ALTER col_name SET DEFAULT 'name';
26 | P a g e
Example -:
Syntax-:
CREATE TABLE Persons (Personid int NOT NULL AUTO_INCREMENT,LastName
varchar(255) NOT NULL,FirstName varchar(255),Age int,PRIMARY KEY (Personid));
Example -:
✓ Gender enum -:
Syntax-:
Create table st2 (gender enum(“male”,”fenale”));
27 | P a g e
Example -:
✓ Lower case –:
Syntax-:
Select lcase(col_name) from table_name;
Update -: update table_name set col_name=lcase(cal_name);
Example -:
✓ Dual table -:
Syntax-:
Select ucase(“hello”) from dual;
28 | P a g e
Example -:
✓ Ascii -:
Syntax-:
Select ascii(“v”) from dual;
Example -:
✓ Char_length -:
Syntax-:
Select char_length(‘vikash’) from dual;
Example -:
✓ Concat -:
Syntax-:
Select concat(‘vikash’,’sharma’) from dual;
Example -:
30 | P a g e
✓ Abs, ceil and floor -:
Syntax-:
Select abs(-4255) from dual;
Select ceil(12.5) from dual;
Select floor(12.5) from dual;
Example -:
31 | P a g e
✓ Round function -:Two decimal places round.
Syntax-:
Select round(25.374896) from dual;
Example -:
Example -:
✓ IsNull function -:
Syntax-:
Select isnull(class) from dual;
Example -:
32 | P a g e
✓ Date function -:
Syntax-:
Select now() from dual;
Select curdate() from dual;
Select curtime() from dual;
Select date(now()) from dual;
Example -:
➢ Extract function -:
Syntax-:
Select extract(day from now()) from dual;
33 | P a g e
Example -:
➢ Cast function -:
Syntax-:
Select cast(“2”, as decimal) from dual;
34 | P a g e
Example -:
➢ Convert function -:
Syntax-:
Select convert(“2” ,as decimal) from dual;
Example -:
➢ Distinct function -:
Syntax-:
Select distinct(class) from table_name;
Example -:
35 | P a g e
➢ JOIN -: A JOIN clause is used to combine rows from two or more tables, based
on a related column between them.
Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in
the "Customers" table. The relationship between the two tables above is the
"CustomerID" column.
INNER JOIN -:
Syntax-:
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM 1table_name
INNER JOIN 2table_name ON Orders.CustomerID=Customers.CustomerID;
36 | P a g e
Example -:
37 | P a g e
NATURAL JOIN -:
Syntax-:
SELECT * FROM 1table_name Natural JOIN 2table name;
Example -:
LEFT JOIN -: The LEFT JOIN keyword returns all records from the left table
(table1), and the matching records from the right table (table2). The result is 0 records
from the right side, if there is no match.
Syntax-:
SELECT column_name(s) FROM table1 LEFT JOIN table2 ON
table1.column_name = table2.column_name;
Example -:
RIGHT JOIN -: The RIGHT JOIN keyword returns all records from the right table
(table2), and the matching records from the left table (table1). The result is 0 records
from the left side, if there is no match.
Syntax-:
SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON
table1.column_name = table2.column_name;
Example -:
38 | P a g e
➢ UNION OPERATOR -: The UNION operator is used to combine the result-
set of two or more SELECT statements.
• Every SELECT statement within UNION must have the same number of columns
• The columns must also have similar data types
• The columns in every SELECT statement must also be in the same order
Syntax-:
SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM
table2;
Example -:
UNION ALL -: The UNION operator selects only distinct values by default. To allow
duplicate values, use UNION ALL:
Syntax-:
SELECT column_name(s) FROM table1 UNION ALL SELECT column_name(s)
FROM table2;
Example -:
39 | P a g e
FULL OUTER JOIN -: The FULL OUTER JOIN keyword returns all records when
there is a match in left (table1) or right (table2) table records.
Tip : FULL OUTER JOIN and FULL JOIN are the same.
Syntax-:
SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON
table1.column_name = table2.column_name WHERE condition;
Or
Example -:
40 | P a g e
CROSS JOIN -:
Example -:
• VIEWS -: Views Are Virtual Tables That Do Not Store Any Data Of Their Own
But Display Data Stored In Other Tables. In Other Words, Views Are Nothing But
Sql Queries. A View Can Contain All Or A Few Rows From A Table. A Mysql
View Can Show Data From One Table Or Many Tables.
Syntax-:
CREATE VIEW `view_name` AS SELECT statement;
Example -:
41 | P a g e
DROP VIEW -:
Example -:
42 | P a g e
desc information_schema.views;
select table_schema,table_name,definer from information_schema.views;
Example -:
NOT UPDATABLE -:
Example -:
43 | P a g e
SUBQUERY -:
Syntax-:
select name from student where rollno=(select rollno from fees where
fees=10000);
• Single row subquery (=, not in)
• Multirow subquery (in, not in, any[<, >, = in], all[<, >)
• Multicolumn subquery
• Correlated subquery
Example -:
44 | P a g e
Exists and not exists operators -:
➢ From Clause -:
Syntax-:
select max(rollno) from (select rollno from fees) as sample;
select max(rollno) from (select rollno from fees) as sample;
select (rollno) from (select rollno from fees) as sample;
Example -:
45 | P a g e
ROLLUP -: The ROLLUP in MySQL is a modifier used to produce the summary
output, including extra rows that represent super-aggregate (higher-level) summary
operations. It enables us to sum-up the output at multiple levels of analysis using a single
query.
Syntax-:
select city,gender,sum(salary) from emp group by city,gender with rollup;
select gender,sum(salary) from emp group by gender with rollup;
Example -:
46 | P a g e
PL/SQL Introduction
PL/SQL is a block structured language that enables developers to combine the power of
SQL with procedural statements.All the statements of a block are passed to oracle engine
all at once which increases processing speed and decreases the traffic.
Typically, each block performs a logical action in the program. A block has the following
structure:
• DECLARE
declaration statements;
• BEGIN
executable statements
• EXCEPTIONS
exception handling statements
• END;
• Execution section starts with BEGIN and ends with END keyword.This is a
mandatory section and here the program logic is written to perform any task like
loops and conditional statements. It supports all DML commands, DDL commands
and SQL*PLUS built-in functions as well.
47 | P a g e
➢ Variables -:
Syntax for declaration of variables:
variable_name datatype [NOT NULL := value ];
Example-:
➢ Assignment Operator
Example-:
➢ Constant Number
Example-:
48 | P a g e
➢ Existing Table Fetch Data In Pl Sql
Example-:
➢ %TYPE -:
Example-:
➢ %ROWTYPE -:
Example-:
49 | P a g e