softcopy of dbms
softcopy of dbms
DATE:
AIM::To understand the basics of mysql
DATA: Data refers to collection of facts, figures, and information that is stored, managed, and
retrieved using the DBMS software. Data in a DBMS is organized in a structured manner to
facilitate efficient access and manipulation.
INFORMATION: Information refers to the processed and organized data that is meaningful
to the users.
DATABASE: A data base is a collection of related data. Database is the place where data gets
stored and the retrieval time is fast.
WHAT IS DBMS?
TYPES OF DBMS:
2.H-DBMS
3.R-DBMS
4.OO-DBMS
5.OR DBMS
AIM::To understand the creation of databases and tables in SQL, perform Data
Manipulation Language (DML) operations like INSERT, UPDATE, DELETE,
and SELECT, and explore Data Definition Language (DDL) commands like
ALTER, DROP, TRUNCATE
1.CREATING DATABASE::
➢ It shows all the database that are present in the mysql nutshell.
➢ SYNTAX: show databases;
• TRUNCUATE
➢ Delete the data from the table and the structure of the table
remans same and the table will be empty
➢ SYNTAX: TRUNCATE tabletable_name;
• DROP
➢ Drop will delete the table from the database when we want to
display it shows the error
➢ SYNTAX: DROP table table_name;
RESULT:
::CREATING DATABASE.
➢ SYNTAX:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY
(column_name);
➢ SYNTAX:
ALTER TABLE table_name
MODIFY column_name datatype NOT NULL;
--Use of not null constraint:
➢ When the null values insert into the not null column the error
should be generated.
➢ SYNTAX:
ALTER TABLEtable_name
ADD CONSTRAINTconstraint_nameUNIQUE(column_name);
--Use of unique constraint:
➢ It is not allowing duplicate values into the unique constraint
column.
➢ the foreign key column takes values from only other column in
other table which has primary key constraint.
➢ SYNTAX:
ALTER TABLEtable_name
ADD CONSTRAINTconstraint_name
FOREIGN KEY (column_name) REFERENCESother_table
(referenced_column);
--Creating an column and make it as default constraint:
➢ The constraint applied to a column such that the column assigns a
default value to the row if now data is passed to that column is
called Default Constraint
➢ SYNTAX:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1,
column2);
--Use of composite primary constraint :
➢ it is combination of two columns or more the duplicate has arises
it raise an error.
RESULT:
Successfully implemented and tested SQL constraints, ensuring data integrity,
uniqueness, and relationships between tables.
TASK-3
DATE:
::OPERARTORS IN DBMS ::
.
4.DISPLAYING THE DETAILS OF THE TABLE ;
*************************OPERATORS***********************
❖ RELATIONAL OPERATOR:
Relational operators in DBMS are used to compare two values. They are
typically used in SQL queries to filter records based on certain conditions.
Types of Relational Operators:
1. =: Equal to
2. != or <>: Not equal to
3. >: Greater than
4. <: Less than
5. >=: Greater than or equal to
6. <=: Less than or equal to
These operators are used to compare columns with values or other columns in
SQL queries.
➢ SYNTAX :SELECT column_names
FROM table_name
WHERE column_namerelational_operatorvalue;
(i) Less than operator(<):
❖ LOGICAL OPERATIONS:
• Logical operators in DBMS are used to combine multiple
conditions in SQL queries. These operators return a boolean value
(TRUE, FALSE, or UNKNOWN) based on the evaluation of the
conditions.
(i).AND OPERATOR:
➢ SYNTAX:
SELECT column_names
FROM table_name
WHERE condition1AND condition2;
(ii).OR OPERATOR:
o Returns TRUE if at least one of the conditions is true.
➢ SYNTAX:
SELECT column_names
FROM table_name
WHERE condition1OR condition2;
(iii).IN OPERATOR:
➢ SYNTAX:
SELECT column_names
FROM table_name
WHERE IN (VALUE1,VALUE2,..,);
➢ SYNTAX:
SELECT column_names
FROM table_name
WHERE NOT IN (VALUE1,VALUE2,..,);
(V).BETWEEN OPERATOR:
➢ SYNTAX:
SELECT column_names
FROM table_name
WHERE BETWEENVALUE1 AND VALUE2;
(VI).LIKE OPERATOR:
➢ SYNTAX :
SELECT column_names
FROM table_name
WHERE column_name LIKE pattern;
TASK-4
DATE:
AIM:: to understand and implement different operations in mysql for data
updation.
BUILT-IN FUNCTIONS in MYSQL::
• We have created a table in the it2b database to perform the numeric
functions,stringfunctions,aggrgrate functions on the table.
❖ Numeric funtions:
• Returns the absolute value of a number.(removes the negative sign if there is any).
➢ The ABS() function returns the absolute value of the number, meaning it removes the
negative sign, if there was one.
➢ Since all the s1_marks values are positive integers, the result will be the same as the
input.
2. ROUND()
➢ The ROUND() function rounds a number to the nearest integer (or specified decimal
places).
➢ Since the s1_marks values are decimal types, the result will be rounded to the nearest
integer.
3.CEIL() (Ceiling):
➢ The CEIL() function returns the smallest integer greater than or equal to the given
number.
➢ The CEIL() function rounds the value up to the nearest whole number. Therefore, the
s1_marks values contain decimal values(91.20,94.50,85,60…..), the result will be the
smallest integer greater than or equal to each original value(92,95,86…)
5. FLOOR() (Floor):
➢ Returns the largest integer value that is <= to a number.
➢ The FLOOR() function returns the largest integer less than or equal to the given
number. As with the CEIL() function, since all values are greater or equal , here the
result now will be the values will be less or equal.
➢ The values of s2_marks like92.43,87.34,85,20…..will changed as 92,87,85,……..etc.
➢ The SQRT() function returns the square root of a given number. It’s used to calculate
the square root of each s1_marks value.
➢ The SQRT() function calculates the square root of each value in the s3_marks column.
For example, the square root of 85 is approximately 9.22.
6. POWER() (Exponentiation):
➢ The POWER() function raises a number to the power of another number. In this case,
the s1_marks values are squared (raised to the power of 2).
➢ The POWER() function squares each of the s1_marks values. For example, the square of
85 is 7225.
7. MOD() (Modulo):
➢ The MOD() function returns the remainder of the division of one number by another.
Here, we're finding the remainder when s1_marks is divided by 5.
➢ The MOD() function calculates the remainder when each value in s1_marks is divided
by 5. For example, 85 % 5 equals 0.
8. LOG() (Logarithm):
➢ The LOG() function returns the logarithm of a number to the base 10. This is useful
for data analysis, such as transforming values for certain types of visualizations or
calculations.
➢ The LOG() function computes the logarithm (base 10) of each value in the s1_marks
column. For instance, the log of 85 is approximately 1.929
9. TRUNCATE() (Truncation):
➢ The EXP() function returns e (Euler’s number, approximately 2.718) raised to the
power of a given number. This is an important mathematical function that is used in
various fields of study, such as statistics, physics, and engineering.
➢ The EXP() function calculates e raised to the power of each value in s1_marks.
Since s1_marks are integers, this results in very large values. For
instance, EXP(85) results in approximately 1.52105E+37.
➢ The CONV() function in MySQL converts a number from one base to another. For
example, converting from decimal (base 10) to binary (base 2). We can convert
the s1_marks values into their binary representations.
➢ The CONV() function converts the decimal values of s1_marks into binary (base 2).
For example, 85 in decimal is 1010101 in binary.
➢ The LN() function calculates the natural logarithm (logarithm base e) of each value in
s1_marks. For instance, the natural logarithm of 85 is approximately 4.442.
String funcions:
ASCII():
➢ The ASCII() function returns the ASCII value of the first character of a string.
➢ For example, if we applied ASCII(sname) to the sname column, it would return the
ASCII value of the first character of each student's name (e.g., ASCII('B') would
return 66 because 'B' is 66 in the ASCII table).
REVERSE():
➢ The REVERSE() function reverses the characters in a string. For example,
applying REVERSE(sname) on the sname column would transform the names like
"Aarav" into "nvaraA" or "Sara" into "araS", reversing the characters of each
name.
CHAR_LENGTH():
UPPER(): (UCASE):
➢ The UPPER() function converts all characters in a string to uppercase. For example,
applying UPPER(sname) would transform "Aarav" into "AARAV" or "Sara" into
"SARA", converting the entire name to uppercase.
SUBSTRING():(substr)
CONCAT():
➢ Adds two or more expressions together.
➢ CONCAT_WS: Adds two or more expressions together with a separator.
REPLACE():
INSTR():
➢ The INSTR() function returns the position of the first occurrence of a substring in a
string.
➢ For example, INSTR(sname, 'a') would return the position of the first 'a' in the
student's name .
➢ (e.g., INSTR('Aarav', 'a') would return 1 because the'a' in "Aarav" is in first position).
LEFT():
➢ The LEFT() function extracts a specified number of characters from the left side of a
string.
➢ For example, LEFT(sname, 3) would return the first 2 characters of the name, so
"Aarav" would become "Aar" and "Sara" would become "Sar".
RIGHT():
FIELD():
➢ The FIELD() function returns the index position of a value in a list of values.
➢ For example, applying FIELD(department_id, 10,20,30,40,) would return the index
of the id’s in the list.
➢ If a student's department id is 30is , FIELD(department_id, 10,20,30,40,) would
return 3.
INSERT():
➢ Inserts a string within a string at the specified position and for a certain number of
characters.
➢ Syntax:INSERT(string, position, number, string2).
➢ The INSERT() function inserts a substring at a specified position within a string.
➢ For example, INSERT(sname, 2, 2, ‘XX') would insert 'XX' at position 3 in the
student's name.
➢ For "Rahul", this would result in "RXXul".
➢ The CURDATE() function returns the current date (without time). For example,
CURDATE() can be used to calculate the number of days a student has been enrolled
since their joining_date by comparing CURDATE() with the joining_date column.
NOW():
➢ Here we created another column ,that returning the current exact time and date of that
time,when we create the table and that instance.
➢ The NOW() function returns the current date and time. By applying NOW() with the
joining_date, you can get the exact duration between the time the student joined and
the current timestamp, which is useful for calculating the time elapsed in terms of
days, hours, or minutes.
DATE():
➢ For instance, DATE(joining_date) will return just the date from the joining_date
column, excluding any time portion, helping to simplify date comparisons or
calculations.
DATEDIFF():
➢ The DATEDIFF() function calculates the difference in days between two dates.
➢ For example, DATEDIFF(CURDATE(), joining_date) will return the number of days
since a student joined,
➢ whereas DATEDIFF(date_of_birth, joining_date) will give the number of days
between their birthdate and joining date.
TIMESTAMPDIFF():
➢ The TIMESTAMPDIFF() function calculates the difference between two dates based
on a specified unit (e.g., year, month, day, hour).
➢ For example, TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) will return the
age of the student in years by comparing their birthdate with the current date.
STR_TO_DATE():
➢ The STR_TO_DATE() function converts a string into a date based on a given format.
For example, STR_TO_DATE('2025-02-02', '%Y-%m-%d') can be used to convert a
date string into a proper DATE format, and similarly, you could use this to convert
user input into a standard date format before storing it in the table.
3) Aggregrate functions:
SUM:
SUM(s2marks): This will calculate the total of all marks in s2marks (subject 2).
SUM(s3marks): This will calculate the total of all marks in s3marks (subject 3).
AS total_s1marks: We are using aliases (total_s1marks, total_s2marks, total_s3marks) to
give more readable names to the results.This output means that:
COUNT:
This function counts the number of rows (or non-NULL entries) in a column.
AVG:
This function calculates the average value of a column.
• This would give you the average marks obtained in each subject (s1marks, s2marks,
and s3marks) across all students.
We computed the average marks for all students in each subject. This tells us the mean
score in each subject, giving us an idea of how students performed on average.
MAX:
MIN:
This function finds the minimum value in a column.
• This would give you the lowest marks obtained in each subject (s1marks, s2marks,
and s3marks).
• We identified the lowest marks obtained in each subject. This tells us the minimum
performance in each subject, indicating the lowest-scoring students.
RESULT:
TASK-5
DATE:
AIM:: To understand and implement different types of joins in mysql.
JOINS:
Creation of the table;
Code::
create table employee(ename varchar(100), eid int primary key, esalary
varchar(10), profile varchar(12), did int);
Insert Data:
• SELECT * FROM employee; retrieves all rows and all columns from the employee table.
• The * is a wildcard that means "select all columns."
Insert Data:
1. Basic Syntax for INSERT INTO:
• INSERT INTO department: This part specifies that you are inserting data into the
department table.
• (dname, did, dlocation): These are the column names in the department table where
data will be inserted.
• VALUES: This keyword introduces the values that you want to insert into the
specified columns.
• (value1, value2, value3): Each set of parentheses represents a new row to be inserted,
where the values correspond to the columns listed earlier.
❖ CROSS JOIN
➔ General Syntax for Selecting Data from Multiple Tables:
This is a cross join (Cartesian product), which will return all combinations of rows from
table1 and table2. No filtering or matching conditions are applied.
❖ EQUI JOIN
Key Points:
• table1.column1, table1.column2, ...: Specifies the columns from the first table you
want to retrieve.
• table2.column1, table2.column2, ...: Specifies the columns from the second table
you want to retrieve.
• FROM table1: The first table you're selecting data from.
• JOIN table2: Specifies the second table to join.
• ON table1.common_column = table2.common_column: The equivalence
condition for the join. The equality operator = is used to match rows from both tables
based on a shared column (the common_column), which typically has the same or
related values in both tables.
❖ LEFT OUTER JOIN :
Key Points:
• table1.column1, table1.column2, ...: Specifies the columns you want to retrieve from
the first table (table1).
• table2.column1, table2.column2, ...: Specifies the columns you want to retrieve from
the second table (table2).
• FROM table1: The first table you're selecting data from.
• LEFT OUTER JOIN table2: Specifies the second table to join with the first table
using a LEFT OUTER JOIN.
o The LEFT OUTER JOIN will return all rows from table1 and the matched
rows from table2. If no match is found, NULL values are returned for columns
from table2.
• ON table1.common_column = table2.common_column: The equivalence
condition where the common_column from both tables is compared for equality
using the = operator. This defines how the rows will be matched between the two
tables.
• table1.column1, table1.column2, ...: Specifies the columns you want to retrieve from
the first table (table1).
• table2.column1, table2.column2, ...: Specifies the columns you want to retrieve from
the second table (table2).
• FROM table1: The first table you're selecting data from.
• RIGHT OUTER JOIN table2: Specifies the second table to join with the first table
using a RIGHT OUTER JOIN.
o The RIGHT OUTER JOIN will return all rows from table2 and the matched
rows from table1. If no match is found, NULL values are returned for columns
from table1.
• ON table1.common_column = table2.common_column: The equivalence condition
where the common_column from both tables is compared for equality using the =
operator. This defines how the rows will be matched between the two tables.
General Syntax for FULL OUTER JOIN in MySQL (Simulated with LEFT JOIN and
RIGHT JOIN):
Key Points:
• LEFT OUTER JOIN: This will return all rows from the first table (table1), and
matching rows from the second table (table2). If no match is found in table2, NULL
values will be returned for the columns from table2.
• RIGHT OUTER JOIN: This will return all rows from the second table (table2), and
matching rows from the first table (table1). If no match is found in table1, NULL
values will be returned for the columns from table1.
• UNION: Combines the result sets of both queries. It removes duplicate rows, thus
giving the effect of a FULL OUTER JOIN by combining rows from both tables that
have matching data as well as rows that don’t match from either table.
Task-6
DATE:
Clauses :
MySQL clauses are the foundational components used to construct database queries.
They define the structure and constraints of SQL statements, enabling efficient data
manipulation and retrieval. Clauses are integral to queries and work in conjunction
with other SQL components like keywords and functions.
Types of Clauses :
o Group by clause
o Having clause
o Order by clause
Create and Insert Values into table :
Group by Clause : The group by clause in SQL is used to arrange identical data
into groups based on one or more columns. This is often done in conjunction with
aggregate functions to perform operations on each group of data.
➢ Displaying average salary and depid from Info table by grouping depid’s.
Having Clause : The having clause in SQL is used to filter the results of a query
after the data has been grouped using the group by clause. It is similar to the where
clause, but while where is used to filter rows before grouping, having is used to filter
groups after the aggregation.
Order by Clause : The order by clause in SQL is used to sort the result set of a
query in either ascending (ASC) or descending (DESC) order. This helps to organize
the data in a meaningful way, either by a single column or multiple columns.
• Corelated Subquery :
If the inner query execution is dependent on outer query execution then it is called corelated
subquery.
➢ Displaying depid from det table of the one who is gaining high or
maximum salary from Info table.
➢ Displaying depname from det table where depid from Info table is having
maximum salary.
➢ Displaying depname from det table where depid from Info table is having
minimum salary.
Co-related Subquery :
➢ Displaying id , name and mobile from Info table where salary is greater
than average of ss
Key points::
Result:: Sucessfully implemented and tested the two different sub queries.
TASK-8
DATE::
Union: It connects all the rows and removes the duplicate values.
Union all: it connects all the rows with the duplicates values.
Except: it outputs the unique values from one table that are not in another table.
In MySQL, comparison operators are used to compare two values. They are commonly used
in SQL queries with WHERE, HAVING, or JOIN clauses to filter data based on certain
conditions. Here are the most common comparison operators:
IN operator: The IN operator allows you to specify multiple values in a WHERE clause.
ANY means that the condition will be true if the operation is true for any of the values in the
range.
EXISTS Operator: The EXISTS operator is used to test for the existence of any record in a
subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.
➢ You can add SQL statements and functions to a view and present the data as if the
data were coming from one single table.
➢ A view is created with the CREATE VIEW statement.
Updation of a View:
➢ A view can be updated with the update statement.
Dropping a View:
Displaying of a view:
SUMMARY ON VIEWS::
Views in SQL:
1. Creation: A view is created using the CREATE VIEW statement, which combines
SQL statements and functions to present data as if from a single table.
2. Updation: A view can be updated using the UPDATE statement, allowing changes to
the underlying data.
4. Displaying: A view can be displayed like a normal table using a SELECT statement.