0% found this document useful (0 votes)
40 views44 pages

Dbms File (22001003002) (AutoRecovered)

Uploaded by

sardanaadi88
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views44 pages

Dbms File (22001003002) (AutoRecovered)

Uploaded by

sardanaadi88
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

PRACTICAL FILE

OF
Database Management Systems
(CSE383C)

Submitted To: Submitted By:


Mrs. Priya Malik Aaditya
Assistant Professor 22001003002
Dept. of CSE ECE - A

Deenbandhu Chhotu Ram University of Science and Technology


Murthal, Sonipat, Haryana - India

A Haryana State Government University


INDEX
SR. NO. PROGRAM DATE SIGN
Study of MySQL interface, with
1. different types of installations,
Accounts and privileges in MySQL.
Study of various Data Types and
2. Data Objects in SQL.
Implementation of Data Definition
3. Language (DDL) Commands in SQL.
Implementation of Data Manipulation
4. Language (DML) Commands in SQL.
Implementation of various
5. aggregate functions in SQL with
group by and Having Clause.
Implementation of various String
6. functions in SQL.
Implementation of various Date
7. Functions in SQL.
Implementation of Data Control
8. Language (DCL) Commands in SQL.
Implantation of Data Integrity Constraints
9. in SQL.
10. Implementation of Different types of
Views in SQL.
11. Implementation Nested Queries (Simple
and Correlated) in SQL.
12. Implementation of JOINS (Natural,
Equi, Theta, Inner, Outer) in SQL.
13. Implementation of SET Operations
(UNION,INTER-SECTION, SET
DIFFERENCE ) in SQL.
14. Implementation of SQL Commands
related to Database recovery and
Concurrency Control in DBMS.
15. Implementation different types of Index
in SQL.
Experiment - 1
Aim: Study of MySQL interface, with different types of
installations, Accountsand privileges in MySQL.

What is MySQL ?

MySQL is one of the most popular relational database management software thatis widely
used in today's industry. It provides multi-user access support with various storage
engines. It is backed by Oracle Company.

How to Download MySQL


Step 1: Go to the official website of MySQL and download the community server edition
software. Here, you will see the option to choose the Operating System, such as Windows.

Step 2: Next, there are two options available to download the setup. Choose the version
number for the MySQL community server, which you want. If you have good internet
connectivity, then choose the mysql-installer-web-community. Otherwise, choose the other
one.

How to Install MySQL on Windows


Step 1: After downloading the setup, unzip it anywhere and double click the MSI
installer .exe file. It will give the following screen:

Aaditya 22001003002
Step 2: In the next wizard, choose the Setup Type. There are several types available, and you
need to choose the appropriate option to install MySQL product and features. Here, we are
going to select the Full option and click on the Next button.

This option will install the following things: MySQL Server, MySQL Shell, MySQL Router,
MySQL Workbench, MySQL Connectors, documentation, samples andexamples, and many
more.

Step 3: Once we click on the Next button, it may give information about some features that
may fail to install on your system due to a lack of requirements. We can resolve them by
clicking on the Execute button that will install all requirements automatically or can skip
them. Now, click on the Next button.

Step 4: In the next wizard, we will see a dialog box that asks for our confirmation of afew
products not getting installed. Here, we have to click on the Yes button.

Aaditya 22001003002
After clicking on the Yes button, we will see the list of the products which are going to be
installed. So, if we need all products, click on the Execute button.

Step 5: Once we click on the Execute button, it will download and install all the products.
After completing the installation, click on the Next button.

Step 6: In the next wizard, we need to configure the MySQL Server and Router. Here , I am
not going to configure the Router because there is no need to use it with MySQL. We are
going to show you how to configure the server only. Now, click on the Next button.

Aaditya 22001003002
Step 7: As soon as you will click on the Next button, you can see the screen below. Here, we
have to configure the MySQL Server. Now, choose the Standalone MySQL Server/Classic
MySQL Replication option and click on Next. Here, you can also choose the In no DB
Cluster based on your needs.

Step 8: In the next screen, the system will ask you to choose the Config Type and other
connectivity options. Here, we are going to select the Config Type as 'Development
Machine' and Connectivity as TCP/IP, and Port Number is 3306, then click on Next.

Step 9: Now, select the Authentication Method and click on Next. Here, I am going toselect
the first option.

Aaditya 22001003002
Step 10: The next screen will ask you to mention the MySQL Root Password. Afterfilling
the password details, click on the Next button.

Step 11: The next screen will ask you to configure the Windows Service to start theserver.
Keep the default setup and click on the Next button.

Step 12: In the next wizard, the system will ask you to apply the Server Configuration.If you
agree with this configuration, click on the Execute button.

Aaditya 22001003002
Step 13: Once the configuration has completed, you will get the screen below. Now, click on
the Finish button to continue.

Step 14: In the next screen, you can see that the Product Configuration is completed.Keep the
default setting and click on the Next-> Finish button to complete the MySQL package
installation.

Step 15: In the next wizard, we can choose to configure the Router. So click on Next-
>Finish and then click the Next button.

Aaditya 22001003002
Step 16: In the next wizard, we will see the Connect to Server option. Here, we have to
mention the root password, which we had set in the previous steps.

In this screen, it is also required to check about the connection is successful or not byclicking
on the Check button. If the connection is successful, click on the Execute button. Now, the
configuration is complete, click on Next.

Step 17: In the next wizard, select the applied configurations and click on the Executebutton.

Step 18: After completing the above step, we will get the following screen. Here, clickon the
Finish button.

Aaditya 22001003002
Step 19: Now, the MySQL installation is complete. Click on the Finish button.

Verify MySQL installation


Once MySQL has been successfully installed, the base tables have been initialized, and the
server has been started, you can verify its working via some simple tests.

Open your MySQL Command Line Client; it should have appeared with a mysql> prompt.
If you have set any password, write your password here. Now, you are connected to the
MySQL server, and you can execute all the SQL command at mysql>prompt as follows:

For example: Check the already created databases with show databases command:

Aaditya 22001003002
Experiment - 2
Aim: Study of Various Data Types of and Data Objects in SQL.
In SQL (Structured Query Language), data types are essential components that define
the type of data stored in a column or variable within a database table. Here's a
detailed explanation of various data types commonly used in SQL:
Data Types:
1. Numeric Types:
 INT, INTEGER: Represents whole numbers within a specified range.
 SMALLINT: Similar to INT but with a smaller range.
 BIGINT: Allows storage of large integer values.
 DECIMAL(p, s), NUMERIC(p, s): Fixed-point numbers with
user- specified precision and scale.
 FLOAT(p): Floating-point numbers with user-specified precision.
 REAL: Single precision floating-point number.
 DOUBLE PRECISION: Double precision floating-point number.
2. Character String Types:
 CHAR(n): Fixed-length character string with a specified length.
 VARCHAR(n): Variable-length character string with a
maximum length.
 TEXT: Variable-length character string with no specified
maximum length.
 NCHAR(n), NVARCHAR(n), NTEXT: Same as above but
for Unicode characters (national character set).
3. Binary Data Types:
 BINARY(n): Fixed-length binary string with a specified length.
 VARBINARY(n): Variable-length binary string with a
maximum length.
 BLOB: Binary Large Object for storing large binary data.
 BIT: Stores bit values or binary data.

Aaditya 22001003002
4. Date and Time Types:
 DATE: Stores date values without a time component.
 TIME: Stores time values without a date component.
 DATETIME: Stores date and time values together.
 TIMESTAMP: Represents a point in time, typically used for
recording when data was last modified.
 INTERVAL: Represents a time span or duration.
5. Boolean Type:
 BOOLEAN, BOOL: Represents boolean values, typically TRUE or
FALSE.
6. Other Data Types:
 XML: Stores XML data.
 JSON: Stores JSON data.
 GEOMETRY: Stores geometric data.
 GEOGRAPHY: Stores geographic data.
 ARRAY: Stores an array of elements.
These descriptions provide an overview of each data type's purpose and usage in SQL
databases. Keep in mind that some data types might not be supported in all SQL
database management systems, and there could be variations in naming and
implementation across different systems.

Data Objects:
In SQL, object types refer to the various constructs used to define and manage
database objects. Here are some common object types in SQL:
1. Tables:
 Tables are the most fundamental database objects used to store
and organize data in rows and columns.
2. Indexes:
 Indexes are used to improve the performance of database queries
by providing faster access to data based on specific columns.
3. Views:

Aaditya 22001003002
 Views are virtual tables that are defined by a query and do not store
data themselves. They provide a way to simplify complex queries and
present data in a structured format.
4. Constraints:
 Constraints are rules enforced on data columns to maintain data
integrity. Examples include primary key, foreign key, unique,
check, and not null constraints.
5. Stored Procedures:
 Stored procedures are precompiled SQL statements or routines stored in
the database server. They can accept parameters, execute SQL
statements, and return results.
6. Functions:
 Functions are similar to stored procedures but return a single value.
They can be user-defined or built-in functions provided by the
database system.
7. Triggers:
 Triggers are special types of stored procedures that are automatically
executed or fired in response to specific events, such as insert, update,
or delete operations on tables.
8. Sequences:
 Sequences are database objects used to generate unique numeric
values, typically used for primary keys.
9. Synonyms:
 Synonyms are aliases for database objects such as tables, views, or
stored procedures, providing an alternative name for easier reference.
10. Roles:
 Roles are named groups of privileges that can be assigned to users or
other roles, simplifying the management of permissions in a
database.
11. Users:
 Users are accounts that can access and interact with a database. They
are granted specific permissions to perform operations on database
objects.
12. Database:
 Database is the top-level container that holds all database
objects, including tables, views, indexes, and stored procedures.

Aaditya 22001003002
13. Schema:
 Schema is a logical container within a database that contains database
objects such as tables, views, and stored procedures. It provides a way
to organize and manage objects.
14. Extensions:
 Extensions are add-on modules that provide additional functionality to
a database system, such as new data types, functions, or procedural
languages.
15. Foreign Data Wrappers (FDW):
 FDWs are extensions that allow a database to access and interact
with external data sources, such as other databases or web services.
16. Foreign Servers:
 Foreign servers are configurations that define connections to
external data sources, enabling the database to communicate with
remote databases or servers.
17. Event Scheduler:
 Event scheduler is a component that allows scheduling and execution
of recurring tasks or events within the database server.
18. User-Defined Types:
 User-defined types allow users to define custom data types based
on their specific requirements, providing flexibility in data
modeling.
19. User-Defined Aggregates:
 User-defined aggregates allow users to define custom
aggregate functions for performing calculations on groups of
data.
20. User-Defined Operators:
 User-defined operators allow users to define custom operators for
performing operations on data types that are not supported by default
in the database system.
These object types are fundamental components of SQL databases and are used to
define, manage, and manipulate data and database structures. Depending on the
specific database management system (DBMS), additional object types or variations
may exist.

Aaditya 22001003002
Experiment - 3
Aim: Implementation of DDL Commands in SQL.

1. Create command:
a) Creating database:
Code :
Create database university;
Output:

b) Creating table:
Code :
Create table student(Student_id int not null, S_name varchar(10),Dept char(3));
Output:

2. Alter command:
a) to add column:
Code :
Alter table student add email varchar(100);

Aaditya 22001003002
Output:

b) to modify column:
Code :
Alter table student modify dept varchar(10) ;
Output:

c) to drop column:
Code :
Alter table student drop column email;
Output:

Aaditya 22001003002
3. Rename command:
Code :
Alter table student rename column student_id to s_id;
Output:

4. Truncate command:
Code :
Truncate table student;
Output:

5. Drop command:
Code :
Drop table student;
Output:

Aaditya 22001003002
Experiment - 4
AIM: Implementation of DML Commands in SQL.

1. Insert command:

Code :

insert into student values(101,'deepak','ece'),(102,'bhupender','ece'),


(103,'harry','ece');

Output:

2. Update command:

Code :

update student set s_name='ashok',dept='ece' where student_id=103;

Output:

Aaditya 22001003002
3. Select command:

a) to view all records of table:

Code :

select * from student;

Output:

b) using where condition:

Code :

select * from student where student_id<103;

Output:

4. Delete command:

Code :

delete from student where s_name='ashok';

Output:

Aaditya 22001003002
Experiment - 5
AIM: Implementation of various aggregate functions in SQL with
group by and Having Clause.

SQL Aggregate functions:

SQL Aggregate functions are functions where the values of multiple rows are
grouped as input on certain criteria to form a single value result of more
significant meaning.

Various types of aggregate functions are:

1. COUNT()

The COUNT() function returns the number of rows that matches a specified
criterion.

2. SUM()

The SUM() function returns the total sum of a numeric column

3. AVG()

The AVG() function returns the average value of a numeric column.

4. MIN()

The MIN() function returns the minimum value of a numeric column.

5. MAX()

The MAX() function returns the maximum value of a numeric column.

Implementation of various aggregate functions are as follow:

Using the following table:

Aaditya 22001003002
1. COUNT()
a) to count all records by marks

Code :

select count(*) marks from student;

Output:

b) to count records having distinct marks

Code :

select count(DISTINCT marks) from student;

Output:

c) using group by and having clause

Code :

select s_name from student group by s_name having count(student_id)=1;

Aaditya 22001003002
Output :

2. SUM()
a) sum of all records marks

Code :
select sum(marks) from student;

Output :

b) sum of distinct marks

Code :
select sum(DISTINCT marks) from student;

Output :

Aaditya 22001003002
c) using group by and having clause

Code :
select s_name from student group by s_name having sum(marks)>95;

Output :

3. AVG()
a) average of all records marks

Code :
select avg(marks) from student;

Output :

b) using group by and having clause

Code :
select s_name from student group by s_name having avg(marks)>93;

Aaditya 22001003002
Output :

4. MAX()
a) maximum value from marks

Code :
select max(marks) from student;

Output :

b) using group by and having clause

Code :
select s_name from student group by s_name having max(marks)>94;

Output :

Aaditya 22001003002
5. MIN()
a) minimum value from marks
Code :
select min(marks) from student;

Output :

b) using group by and having clause

Code :
select s_name from student group by s_name having min(marks)<94;

Output :

Aaditya 22001003002
Experiment - 6
AIM: Implementation of various String functions in SQL.

String functions

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. a) CHAR_LENGTH(): This function is used to find the length of a word.

Code :

select char_length('Deepak');

Output :

b) CHARACTER_LENGTH(): This function is used to find the length of a line.

Code :

select character_length('geeks for geeks');

Output :

Aaditya 22001003002
2. a) CONCAT(): This function is used to add two words or strings.

Code :

select concat('coding','ninjas');

Output :

b) CONCAT_WS(): This function is used to add two words or strings with a


symbol as concatenating symbol.

Code :

select concat_ws('_','coding','ninjas');

Output :

3. FORMAT(): This function is used to display a number in the given format.

Code :

select format("0.67","percent");

Output :

Aaditya 22001003002
4. a) LCASE(): This function is used to convert the given string into lower case.

Code :

select lcase('Deenbandhu Chhotu Ram UNIVERSITY');

Output :

b) UCASE(): This function is used to make the string in upper case.

Code :

select ucase('Deenbandhu Chhotu Ram University');

Output :

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

Code :

select ltrim(' GEEKS FOR GEEKS');

Output :

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

Code :

select rtrim('GEEKS FOR GEEKS ');

Output :

6. MID(): This function is to find a word from the given position and of the
given size.

Code :

select mid("geeksforgeeks",9,5);

Output :

7. POSITION(): This function is used to find position of the first occurrence of


the given alphabet.

Code :

select position("o"in"geeksforgeeks");

Output :

Aaditya 22001003002
8. REVERSE(): This function is used to reverse a string.

Code :

select reverse("geeksforgeeks");

Output :

9. REPEAT(): This function is used to write the given string again and again till
the number of times mentioned.

Code :

select repeat('geeks','2');

Output :

10. LOCATE(): This function is used to find the nth position of the given word
in a string.

Code :

select locate('geeks','geeksforgeeks','2');

Output :

Aaditya 22001003002
Experiment - 7
AIM: Implementation of various DateFunctions in SQL.

Date Functions
In SQL, dates are complicated for newbies, since while working with a database,
the format of the date in the table must be matched with the input date in order to
insert. In various scenarios instead of date, datetime (time is also involved with
date) is used.

Following are the date functions defined in SQL:

1. a) CURDATE(): It returns the current date.

Code :

SELECT CURDATE();

Output :

b) CURTIME(): It returns the current time.

Code :

SELECT CURTIME();

Output :

Aaditya 22001003002
2. a) DATEDIFF(): It returns the difference in days between two date values.

Code :

SELECT DATEDIFF("2017-06-25", "2017-06-15");

Output :

b) TIMEDIFF(): It returns the difference between two time/datetime values.

Code :

SELECT TIMEDIFF("09:16:10", "09:16:04");

Output :

3. a) DATE_ADD(): It returns a date after a certain time/date interval has been


added.

Code :

SELECT DATE_ADD("2018-07-16", INTERVAL 10 DAY);

Output :

Aaditya 22001003002
b) DATE_SUB(): It returns a date after a certain time/date interval has been
subtracted.

Code :

SELECT DATE_SUB("2017-06-15", INTERVAL 10 DAY);

Output :

4. a) MICROSECOND(): It returns the microsecond portion of a date value.

Code :

SELECT MICROSECOND("2018-07-18 09:12:00.000345");

Output :

b) SECOND(): It returns the second portion of a date value.

Code :

SELECT SECOND("09:14:23:00032");

Output :

Aaditya 22001003002
c) MINUTE(): It returns the minute portion of a date value.

Code :

SELECT MINUTE("2018-07-18 09:12:00");

Output :

d) HOUR(): It returns the hour portion of a date value.

Code :

SELECT HOUR("2018-07-16 09:34:00");

Output :

e) DAY(): It returns the day portion of a date value.

Code :

SELECT DAY(“2018-07-16”);

Output :

Aaditya 22001003002
f) WEEK(): It returns the week portion of a date value.

Code :

SELECT WEEK("2018-07-18");

Output :

g) MONTH(): It returns the month portion of a date value.

Code :

SELECT MONTH ('2018/07/18')AS MONTH;

Output :

h) YEAR(): It returns the year portion of a date value.

Code :

SELECT YEAR("2018-07-18");

Output :

Aaditya 22001003002
5. a) DAYNAME(): It returns the weekday name for a date.

Code :

SELECT DAYNAME('2008-05-15');

Output :

b) MONTHNAME(): It returns the full month name for a date.

Code :

SELECT MONTHNAME("2018/07/18");

Output :

6.a) DAYOFMONTH(): It returns the day portion of a date value.

Code :

SELECT DAYOFMONTH('2018-07-16');

Output :

Aaditya 22001003002
b) DAYOFYEAR(): It returns the day of the year for a date value.

Code :

SELECT DAYOFYEAR("2018-07-16");

Output :

c) WEEKOFYEAR(): It returns the week of the year for a date value.

Code :

SELECT WEEKOFYEAR("2018-07-18");

Output :

7. EXTRACT(): It extracts parts from a date.

Code :

SELECT EXTRACT(MONTH FROM "2018-07-16");

Output :

Aaditya 22001003002
8. LAST_DAY(): It returns the last day of the month for a given date.

Code :

SELECT LAST_DAY('2018-07-16');

Output :

9. TIME_TO_SEC(): It converts a time value into numeric seconds.

Code :

SELECT TIME_TO_SEC("09:16:10");

Output :

10. a) MAKEDATE(): It returns the date for a certain year and day-of-year value.

Code :

SELECT MAKEDATE(2009, 138);

Output :

Aaditya 22001003002
b) MAKETIME(): It returns the time for a certain hour, minute, second
combination.

Code :
SELECT MAKETIME(11, 35, 4);

Output :

Aaditya 22001003002
Experiment - 8
AIM: Implementation of Data Control Language (DCL) Commands
in SQL.

DCL (Data Control Language)

DCL includes commands such as GRANT and REVOKE which mainly deal with
the rights, permissions, and other controls of the database system.

List of DCL commands:

1. GRANT

2. REVOKE

Creating a user to grant and revoke permission for a existing table:

Code :

Create user Deepak identified by '@123';

Output :

Aaditya 22001003002
1. GRANT: This command gives users access privileges to the database.

Code :

Grant select, insert, update on student to Deepak;

Output :

2. REVOKE: This command withdraws the user’s access privileges given


by using the GRANT command.

Code :

Revoke select, insert, update on student from Deepak;

Output :

Aaditya 22001003002
Experiment - 9
AIM: Implantation of Data Integrity Constraintsin SQL.

SQL Constraints

SQL constraints are used to specify rules for the data in a table.

Constraints are used to limit the type of data that can go into a table. This ensures
the accuracy and reliability of the data in the table. If there is any violation between
the constraint and the data action, the action is aborted.

The following constraints are commonly used in SQL:

NOT NULL - Ensures that a column cannot have a NULL value

Code :

Create table staff(Staff_id int not null);

Output :

Aaditya 22001003002
UNIQUE - Ensures that all values in a column are different

Code :

alter table staff add s_name varchar(15) unique;

Output :

PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely


identifies each row in a table

Code :

alter table staff add s_mobile_num integer primary key;

Output :

Aaditya 22001003002
CHECK - Ensures that the values in a column satisfies a specific condition

Code :

alter table staff add column age int check (age>=18);

Output :

DEFAULT - Sets a default value for a column if no value is specified

Code :

alter table staff add column country varchar(15) default 'india';

Output :

Aaditya 22001003002

You might also like