Dbms Lab Manula-1
Dbms Lab Manula-1
AND MANAGEMENT
GURUGRAM, HARYANA
PRACTICAL LAB
FILE CODE: - BCA
205
MIN
MAX
AVG
SUM
Like operator
Not Like operator
In and Not in operator
Between operator
Structured Query Language
SQL, or Structured Query Language, is a standard programming language
specifically designed for managing and manipulating databases. It enables users
to create, retrieve, update, and delete data within a relational database. SQL is
widely used in database management systems like MySQL, Oracle, SQL Server,
and PostgreSQL.
Advantages of SQL
SQL (Structured Query Language) is a powerful and widely-used language for
managing relational databases. Here are some of its key advantages:
Ease of Use:
Data Integrity:
High Performance:
Scalability:
Handles Large Datasets: SQL databases can handle large datasets and
complex data structures.
Scalable Architecture: Many SQL databases support horizontal scaling,
allowing you to distribute the workload across multiple servers.
Standardization:
Security:
Features of MYSQL
MySQL is an open-source relational database management system (RDBMS)
known for its efficiency, reliability, and ease of use. It’s widely used for web
applications, data warehousing, and enterprise-level applications. Here are some
of the main features that make MySQL a popular choice:
1. Open Source
2. High Performance
4. Scalability
5. Data Security
7. SQL Compatibility
8. Transaction Control
9. ACID Compliance
MySQL allows for data replication across multiple servers, helping with
load balancing, redundancy, and high availability. Replication can be
configured in master-slave or master-master setups to improve
performance and provide backups.
SQL Components
SQL (Structured Query Language) has several essential components, each
serving a distinct purpose in managing and manipulating databases. Here’s a
breakdown of the primary SQL components:
DDL commands define and modify the structure of database objects, such as
tables, indexes, and databases. These commands primarily focus on the schema
or structure of the database.
DML commands are used to manage data within tables. They allow for
retrieving, inserting, updating, and deleting data from the database.
Code: -
Explanation
Output of table
After executing the CREATE TABLE command, the table structure can be
viewed using:
DESCRIBE employees;
Altering the Table
Code
Explanation
Output
After executing the ALTER TABLE command, check the modified table
structure:
The salary column is now part of the employees table.
The DROP TABLE statement deletes an entire table, including all its data and
structure.
Code: -
DROP TABLE
employees; Explanation
Output
If you try to describe or access the employees table after running DROP
TABLE, you’ll receive an error stating that the table does not exist:
Aim: - Write a Query to Insert, Update, and Delete Data in a Table.
1. Overview and Purpose of Data Manipulation Commands
The INSERT, UPDATE, and DELETE commands are part of SQL's Data
Manipulation Language (DML), used to modify data within tables in a
relational database. These commands are essential for managing data over time,
allowing for flexible data operations.
2. INSERT Command
Definition:
The INSERT command adds one or more rows of data to a table. You can insert
data into all columns or specific columns.
Syntax:
Consider a table Employee with columns Employee ID, Employee Name, Age,
and Salary.
Explanation:
Output:
The table Employee now has two rows:
3. UPDATE Command
Definition:
The UPDATE statement modifies existing records in a table. It can update one
or more columns based on specified conditions. The UPDATE command
modifies existing rows in a table. It allows changing values in specific columns,
and you can specify conditions to select which rows to update.
Syntax:
WHERE clause specifies which rows to update; without it, all rows
are affected.
Example:
Explanation:
Output:
4. DELETE Command
Definition:
The DELETE statement removes records from a table. It can delete specific
records or all records if no condition is provided.
Syntax:
Example:
Explanation:
Example:
Or, in some databases, the alternative keyword SHOW COLUMNS FROM may
be used as follows:
3. Example
1. Field:
o Lists each column name in the table. This is essential to
identify column names accurately, especially for complex
queries.
2. Type:
o Shows the data type of each column. Data types are crucial for
understanding what type of values each column can hold, e.g.,
INT for integer values, VARCHAR for variable-length strings,
and DECIMAL for decimal numbers.
3. Null:
o Indicates whether each column can store NULL values. If NO,
the column is mandatory, meaning each row must have a value in
this column.
4. Key:
o Displays any key constraints, like PRI for primary key and UNI for
unique key. Primary keys uniquely identify each row, while unique
keys prevent duplicate values in the column.
5. Default:
o Shows any default values assigned to the column. If no value
is specified during data insertion, this default value will be
used.
6. Extra:
o Displays additional properties. For example, auto_increment
indicates that the value in this column automatically increments
with each new record, useful for unique identifiers.
Data Validation:
o Before inserting data, use DESCRIBE to confirm the data
types and constraints.
Schema Inspection:
o When working with a new table or unfamiliar database,
use DESCRIBE to understand the table structure.
Debugging:
o If a query returns an error related to data types or missing
columns, DESCRIBE helps identify the exact structure.
7. Additional Notes
Employee Name:
o Type: VARCHAR (100) - A variable character field, allowing
up to 100 characters.
o Null: YES - Can store NULL values, allowing optional data
entry for names.
Age:
o Type: INT - Stores integer values.
o Null: YES - Can store NULL values if the age is not provided.
Salary:
o Type: DECIMAL (10,2) - Stores decimal numbers with up to
10 digits and 2 decimal places.
o Null: YES - Can store NULL values, making it optional.
Department:
o Type: VARCHAR (50) - A variable character field, allowing up
to 50 characters.
o Null: YES - Can store NULL values, making it optional.
Summary:
The DESCRIBE command is a valuable tool in SQL for viewing the structure
of any table. It provides details on column names, data types, constraints, and
additional properties that define how data is stored and managed within a
table. This command helps database administrators, developers, and analysts
verify table schema before performing complex data operations, ensuring
compatibility and data integrity.
AIM: Write a query to show the data of a table with different clauses order by,
ascending and descending order.
The ORDER BY clause is used in SQL to sort the result set of a query based on
one or more columns. By default, SQL sorts data in ascending order, but we can
also specify descending order. This clause is valuable for organizing data in a
readable format, especially when querying large datasets.
You can also sort by multiple columns by separating them with commas.
3. Example Table
Let’s assume we have a table named Employee with the following data:
Expected Output:
Explanation: This query sorts the Employee table data in descending order
by the Age column.
Expected Output:
Expected Output:
Expected Output:
Single Column Sorting: Useful when you only need the data ordered by
one criterion, such as sorting by Salary to identify the highest or lowest
earners.
Multiple Column Sorting: Helpful in cases where sorting by a single
column isn’t sufficient. For example, if there are multiple employees
with the same Salary, you can then sort by Age to further organize the
data.
6. Use Cases for ORDER BY
Null Values: By default, NULL values are treated as the lowest possible
values. They will appear at the beginning in ascending order or at the
end in descending order, depending on the RDBMS.
Index Optimization: Sorting on indexed columns can improve
query performance.
Limit Clause: Often used with ORDER BY to limit the number of
results returned. For example:
Summary
The ORDER BY clause is a versatile tool in SQL that allows you to
control the sorting of query results based on one or more columns in
ascending or descending order. This capability is crucial for organizing
and presenting data, making it a key component in SQL for reporting
and data analysis.
This detailed guide to ORDER BY should help clarify its usage and
functionality for your practical file. Let me know if there’s anything
else you’d like to add!
AIM: Write a query to apply different constraint like primary key and foreign
key.
Primary Key: A unique identifier for each row in a table. It ensures that
no two rows have the same value in the specified column(s) and that
values are not NULL.
Foreign Key: A link between two tables. It creates a relationship by
referencing a primary key in another table, enforcing referential
integrity between the tables.
2. Types of Constraints
1. Primary Key:
o Single Column:
Multiple Columns (Composite Primary Key):
2. Foreign Key:
o Example:
Explanation: Here, EmployeeID and ProjectID together form the
primary key, meaning each employee can only be assigned to a project
once.
Example:
Using primary and foreign keys helps prevent common data entry errors:
Constraints like Primary Key and Foreign Key are essential in SQL for
ensuring data integrity, enforcing uniqueness, and establishing relationships
between tables. By understanding and using these constraints, you can create
robust database structures that support data reliability and consistency across
various operations.
This detailed explanation covers the basics and practical applications of primary
and foreign keys to enhance your understanding and documentation in the
practical file. Let me know if you need further clarification or examples!
AIM: Write a query to show the following function. MIN MAX AVG SUM
1. Overview of Aggregate Functions in SQL
These functions are useful for analyzing and summarizing data in a database.
1. MIN Function:
o Definition: Returns the smallest value in a specified column.
o Syntax:
MAX Function:
AVG Function:
Explanation: This query finds the minimum salary among all employees.
Expected Output:
The MIN function scans the Salary column and returns the smallest
value, which is 48000 in this case.
Expected Output:
The MAX function checks the Salary column and returns the highest
value, which is 60000.
Expected Output:
The AVG function computes the average by summing all the values in Salary
and dividing by the number of rows, resulting in an average of 53250.
Explanation: This query calculates the total sum of salaries for all employees.
Expected Output:
The SUM function adds up all the values in the Salary column,
yielding 213000 as the total.
5. Using Aggregate Functions with the GROUP BY Clause
The GROUP BY clause groups records with similar values before applying
aggregate functions. This is helpful for calculating values based on categories.
Example with GROUP BY: Let’s modify the Employee table to include a
Department column:
Expected Output:
This output shows the total salary for employees in each department.
7. Important Considerations
Summary
The MIN, MAX, AVG, and SUM functions are essential for data analysis and
reporting in SQL. They allow you to find minimum and maximum values,
calculate averages, and add up values across rows. When combined with
GROUP BY, these functions become even more powerful, providing insights on
grouped data.
This detailed explanation should give you a solid understanding of how to use
these aggregate functions effectively. Let me know if you have questions or
need further clarification!
AIM: Write a query to use following operator. Like operator, Not Like
Operator, In and Not in operator, Between operator.
SQL operators such as LIKE, NOT LIKE, IN, NOT IN, and BETWEEN are
used in WHERE clauses to filter data based on specific conditions. These
operators enhance the flexibility of SQL queries by allowing you to search for
patterns, match values from a list, and specify a range of values.
1. LIKE:
o Definition: The LIKE operator is used in SQL to search for a
specified pattern in a column. It’s commonly used with
wildcards (% and _).
%: Represents zero, one, or multiple characters.
_: Represents a single character.
o Syntax:
NOT LIKE:
Definition: The NOT LIKE operator filters out rows that match
a specified pattern, effectively excluding those values.
Syntax:
IN:
Definition: The NOT IN operator filters out rows that match any value
in the specified list.
Syntax:
BETWEEN:
3. Example Table
Expected Output:
Pattern Matching:
Expected Output:
Pattern Exclusion:
Expected Output:
Multiple Values:
Expected Output:
Exclusion of Values:
Expected Output:
Range Filtering:
6. Important Considerations
Summary
Operators like LIKE, NOT LIKE, IN, NOT IN, and BETWEEN provide
powerful ways to filter data based on patterns, lists, or ranges. They enable
flexible data querying and support more nuanced data retrieval, making SQL
queries adaptable to various use cases.
This detailed guide should help you effectively understand and apply these
operators. Let me know if you have any questions!