0% found this document useful (0 votes)
185 views25 pages

Database Server and Client Overview

Uploaded by

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

Database Server and Client Overview

Uploaded by

Vivek Parley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Database Server Vs Database Client

25 February 2023 17:35

1. Database Server: A database server is a software application that provides access


to a database over a network. It manages and processes requests from multiple
clients, and performs tasks such as managing user authentication, concurrency
control, and transaction management. A database server is typically installed on a
dedicated computer or server, and is responsible for storing, managing, and
processing data.

2. Database Client: A database client is a software application that connects to a


database server and sends requests for data or database operations. It is the front-
end interface that allows users to interact with the database, and provides tools
such as query editors, data visualization, and reporting.

3. Database: A database is a collection of related data that is organized and stored in


a structured format. It is designed to efficiently store, retrieve, and manage large
amounts of data. A database can include multiple tables, indexes, and other
objects that are used to manage and manipulate data.

4. DBMS (Database Management System): A DBMS is a software application that


provides tools for creating, managing, and manipulating databases. It includes a
variety of functions and features, such as data storage, data retrieval, data backup
and recovery, user management, and security. The DBMS is responsible for
managing the underlying database, and provides a mechanism for users to interact
with the data.
What are Database Engines
25 February 2023 17:35

A database engine, also known as a database management system (DBMS) engine,


is the software component of a DBMS that is responsible for managing the
storage, organization, and retrieval of data in a database. It is the core component
of a DBMS that provides the necessary tools and services for creating, modifying,
and querying the database.

A database engine typically includes several key components, such as a query


optimizer, transaction manager, storage manager, and buffer manager. The query
optimizer is responsible for optimizing SQL queries to retrieve data from the
database efficiently, while the transaction manager ensures that multiple
transactions are executed correctly and consistently. The storage manager handles
the physical storage of the data on disk or in memory, and the buffer manager is
responsible for managing the cache of data in memory to optimize query
performance.
Components of DBMS
25 February 2023 19:19

1. Database Engine

2. Security and Access Control - used to manage user permissions and access
rights to the database.

3. Backup and Recovery - used to create backups of the database and recover the
data in case of failures.

4. Data Dictionary - used to store metadata about the database schema and data

5. User Interface - used to provide a graphical interface to interact with the


database.
What is Collation
25 February 2023 17:35

Collation refers to the rules and algorithms used to compare and sort
characters in a database. It determines how character strings are
compared and sorted, including the order of the characters, the
treatment of case sensitivity, and the handling of accent marks or other
special characters.

Collation is important in database management because it affects the


way queries are executed and results are returned. If the collation of a
database is not set correctly, queries may return incorrect results or the
database may not sort data properly.

1. Binary: Compares strings byte by byte. It is case-sensitive and accent-


sensitive.
2. Case-insensitive: Compares strings without regard to case, but is
accent-sensitive.
3. Accent-insensitive["café" and "cafe"]: Compares strings without regard
to accents, but is case-sensitive.
4. Case- and accent-insensitive: Compares strings without regard to case
or accents.
5. Unicode: Supports Unicode character sets, and is available in multiple
variants, such as utf8mb4_unicode_ci, utf8mb4_unicode_520_ci,
utf8mb4_unicode_520_ci_ai, etc.
Diff between COUNT(*) and COUNT(col)
25 February 2023 17:35
Dealing with NULL values
25 February 2023 17:36

1. How it deals with NON NULL values

2. Order By

3. Group By - NULL values are treated as a separate group and are not
included in any group that contains non-NULL values.

4. Aggregate
When performing aggregate operations in MySQL, NULL values
are treated differently depending on whether or not the
GROUP BY clause is used.
Without GROUP BY:
 If the aggregate function is SUM, AVG, MAX, MIN, or
COUNT, NULL values are ignored and not included in the
calculation.

 If the aggregate function is GROUP_CONCAT or CONCAT,


NULL values are included in the result, but a NULL value is
returned if all the values being concatenated are NULL.

With GROUP BY:


 If the aggregate function is COUNT, NULL values are not
included in the count for each group. However, if you use
COUNT(*) instead of COUNT(column), then NULL values
are included in the count.

 If the aggregate function is SUM, AVG, MAX, or MIN, NULL


values are ignored and not included in the calculation for
each group. If a group contains only NULL values, then the
result for that group will be NULL.

 If the aggregate function is GROUP_CONCAT or CONCAT,


NULL values are included in the result for each group, but a
NULL value is returned if all the values being concatenated
in a group are NULL.
• How to find null values?
• How to replace null values?
DELETE Vs TRUNCATE
25 February 2023 17:36

• DELETE is a Data Manipulation Language (DML) statement, whereas TRUNCATE is a


Data Definition Language (DDL) statement. This means that TRUNCATE requires
the ALTER TABLE privilege, whereas DELETE requires the DELETE privilege on the
table.

• DELETE can be rolled back using a transaction log, which means that you can undo
the changes made by DELETE if necessary. TRUNCATE, on the other hand, cannot
be rolled back because it does not generate a transaction log.

• DELETE is slower than TRUNCATE because it generates transaction log entries for
each deleted row. If you need to delete a large number of rows, TRUNCATE may
be a better option for performance reasons.

• If you use foreign key constraints in your database, DELETE can cause integrity
issues if you delete rows that are referenced by other tables. In this case, you
should use TRUNCATE or disable the foreign key constraints before using DELETE.
Non-equi joins
25 February 2023 17:37

In a non equi join, the join condition is based on operators other than
equality. Specifically, the join condition can use operators such as
greater than, less than, or not equal to, among others. Non equi joins
are useful when you need to join tables on columns with similar but not
identical data, or when you need to join tables based on a range of
values rather than an exact match.
Natural Joins
25 February 2023 17:37

A natural join is a type of join in SQL where two tables are joined based
on the columns with the same name and data type. In other words, it is
a join where the join condition is implicitly based on the column names
that exist in both tables, and it eliminates the duplicate columns from
the result set.
Anti Joins
25 February 2023 17:37

An excluding join, also known as an anti-join, is a type of join operation in SQL that
returns only the rows from one table that do not have any matching rows in
another table. In other words, it returns the rows that are not included in the
result set of an inner join between the two tables.
All and Any Operators
25 February 2023 17:37
Removing Duplicate Rows
25 February 2023 17:42

1. Find duplicate values


2. Delete duplicate
Metadata Queries
25 February 2023 17:42

- How to see all the tables of a database


- How to print col names
- How to see all the constraints of a table (Homework)
- copy table definition -> How to create empty tables with the same
structure as another table?
Temporal Datatypes
24 February 2023 11:46

In MySQL, there are several temporal data types that can be used to store and
manipulate time and date values. These include:

1. DATE - used for storing date values in the format YYYY-MM-DD.


2. TIME - used for storing time values in the format HH:MM:SS.
3. DATETIME - used for storing date and time values in the format YYYY-MM-
DD HH:MM:SS.
4. TIMESTAMP - used for storing date and time values in the format YYYY-
MM-DD HH:MM:SS. It has a range of 1970-01-01 [Link] UTC to
2038-01-19 [Link] UTC.
5. YEAR - used for storing year values in 2-digit or 4-digit format (YYYY or YY).
If the year is specified with 2 digits, it is assumed to be in the range
1970-2069 (inclusive).
Creating and Populating Temporal Tables
24 February 2023 11:47

1. Uber -> user_id, cab_id, start_time, end_time


DATETIME Functions
24 February 2023 11:50

1. CURR_DATE()
2. CURR_TIME()
3. NOW()
---------------------------------------------------
Extraction Function

1. DATE() and TIME()


2. YEAR()
3. DAY() or DAYOFMONTH()
4. DAYOFWEEK()
5. DAYOFYEAR()
6. MONTH() and MONTHNAME()
7. QUARTER()
8. WEEK() or WEEKOFYEAR()
9. HOUR() -> MINUTE() -> SECOND()
10. LAST_DAY()
Datetime Formatting
24 February 2023 11:48

DATE_FORMAT()
TIME_FORMAT()
Type conversion
24 February 2023 11:48

1. Implicit Type Conversion


2. Explicit Type Conversion -> STR_TO_DATE()
DATETIME Arithmetic
24 February 2023 11:50

1. DATEDIFF()
2. TIMEDIFF()
3. DATE_ADD() and DATE_SUB() INTERVAL
4. ADDTIME() and SUBTIME()
TIMESTAMP VS DATETIME
11 March 2023 11:10

In MySQL, both DATETIME and TIMESTAMP are used to store date and
time values, but they differ in their range, storage format, and
behaviour.

Here are the main differences between DATETIME and TIMESTAMP:

1. Range: DATETIME supports a range of '1000-01-01 [Link]' to


'9999-12-31 [Link]', while TIMESTAMP supports a range of
'1970-01-01 [Link]' UTC to '2038-01-19 [Link]' UTC.
2. Storage format: DATETIME uses 8 bytes to store the date and time
values, while TIMESTAMP uses 4 bytes.
3. Behaviour on insertion/update: DATETIME values are stored as-is,
without any conversion, while TIMESTAMP values are converted
from the current time zone to UTC when inserted, and converted
back to the current time zone when retrieved.
4. Precision: DATETIME can store up to microseconds (6 digits after
the decimal point), while TIMESTAMP can only store up to seconds.
5. Auto-update: TIMESTAMP columns can be set to update
automatically whenever the row is inserted or updated, using the
ON UPDATE CURRENT_TIMESTAMP clause.

In general, you should use DATETIME when you need to store date and
time values outside the range of TIMESTAMP, or when you need to
store values with greater precision than TIMESTAMP. You should use
TIMESTAMP when you need to store values that can be automatically
updated, or when you want to take advantage of its smaller storage
format.
Case Study - Flights
24 February 2023 11:48

1. Find the month with most number of flights


2. Which week day has most costly flights
3. Find number of indigo flights every month
4. Find list of all flights that depart between 10AM and 2PM from
Delhi to Banglore
5. Find the number of flights departing on weekends from Bangalore
6. Calculate the arrival time for all flights by adding the duration to
the departure time.

7. Calculate the arrival date for all the flights


8. Calculate the average duration of flights between two cities.

9. Find all flights that arrive at their destination after midnight

10. Find quarter wise number of flights for each airline


11. Find the longest flight distance(between cities in terms of time) in
India
12. Average time duration for flights that have 1 stop vs more than 1
stops
13. Find all Air India flights in a given date range originating from Delhi
14. Find the longest flight of each airline
15. Find all the pair of cities having average time duration > 3 hours
Stored Procedures
25 March 2023 16:39

A stored procedure is a named block of SQL statements and procedural logic that is stored in a
database and can be executed by a user or application.

Stored procedures are often used to encapsulate business logic and application logic, such as
data validation, data processing, and database updates. By using stored procedures,
developers can separate application logic from the presentation layer and simplify the
application code.

- Create hello world stored procedure


- Create stored procedure to create a new user
- Show error message
- Create stored procedure to show orders placed by 1 single user
- Create a stored procedure to place an order

Some of the benefits of using stored procedures include:

1. Improved performance: Stored procedures are precompiled and optimized, which can
improve performance and reduce network traffic.

2. Enhanced security: Stored procedures can be granted specific permissions and access
rights, which can improve security and limit access to sensitive data.

3. Encapsulation of business logic: Stored procedures allow developers to encapsulate


complex business logic and make it easier to maintain and update.

4. Consistency: Stored procedures ensure that database operations are performed in a


consistent manner, which can help to maintain data integrity.

5. Reduced network traffic: By encapsulating data access and manipulation logic in stored
procedures, developers can reduce the amount of data that needs to be transmitted over
the network.

Session 2 on Advanced SQL Page 1


Transactions
25 March 2023 08:19

What are Transactions?


A database transaction is a sequence of operations that are performed as a single logical unit
of work in a database management system (DBMS). A transaction may consist of one or more
database operations, such as inserts, updates, or deletes, which are treated as a single atomic
operation by the DBMS.

It follows the principle of all or none.

What is Commit, Rollback and Savepoint?


In a database transaction, there are three main commands that are used to manage the
transaction:

1. Commit: A commit command is used to permanently save the changes made by a


transaction to the database. When a transaction is committed, all changes made by the
transaction are made permanent and cannot be rolled back.

2. Rollback: A rollback command is used to undo the changes made by a transaction and
return the database to its state before the transaction began. When a transaction is rolled
back, all changes made by the transaction are discarded and the database is returned to
its previous state.

3. Savepoint: A savepoint command is used to mark a specific point within a transaction


where a rollback can be performed. This allows for partial rollbacks of a transaction,
where only changes made after the savepoint are undone, while changes made before
the savepoint are still committed to the database.

What is Autocommit?

Autocommit is a feature of database management systems (DBMS) that automatically


commits each individual database transaction as soon as it is completed, rather than requiring
an explicit commit command to be issued.

When Autocommit is enabled, each individual SQL statement issued against the database is
treated as a separate transaction and is committed immediately after it is executed. This
means that each SQL statement becomes a separate, independent transaction, and its effects
are immediately visible to other users.

• Each SQL write statement is Autocommit (prove)


• set Autocommit = 0 and (show)
• START TRANSACTION -> show operations without committing
• START TRANSACTION -> with commit
• START TRANSACTION -> all or none with commit
• rollback
• rollback with savepoint
• rollback and commit together

What is ACID properties of a Transaction?


ACID is an acronym that stands for Atomicity, Consistency, Isolation, and Durability, which are
a set of properties that ensure reliable database transactions:

1. Atomicity: This property ensures that a transaction is treated as a single, indivisible unit
of work. This means that either all of the changes made by a transaction are committed to
the database, or none of them are. If any part of the transaction fails, the entire
transaction is rolled back, and all changes are undone.

2. Consistency: This property ensures that a transaction takes the database from one valid
state to another valid state. It requires that all data in the database must conform to a set
of rules, or constraints, which ensure data integrity.

3. Isolation: This property ensures that concurrent transactions do not interfere with each
other. It requires that each transaction executes as if it were the only transaction
executing against the database, even if multiple transactions are executing at the same
time.

4. Durability: This property ensures that once a transaction is committed, its changes are
permanently stored in the database, even in the event of a system failure or power
outage. This is typically achieved through the use of database backups, replication, or
other forms of data redundancy.

Together, these properties ensure that database transactions are reliable, consistent, and
accurate, and that the data stored in a database is both protected and available at all times.
The ACID properties are essential for mission-critical applications that require high levels of
data integrity and availability, such as banking, finance, and healthcare systems.

Session 2 on Advanced SQL Page 2


Session 2 on Advanced SQL Page 3

Common questions

Powered by AI

Collation determines how strings are compared and sorted in a database, affecting the execution of queries and the accuracy of results. Incorrect collation can cause queries to return incorrect results or improper data sorting due to variations in case sensitivity, accent handling, and character order. For example, binary collation compares strings byte by byte, making it both case-sensitive and accent-sensitive, which could lead to different sorting and comparison outcomes compared to a case-insensitive collation .

SUM and AVG ignore NULL values during calculation, resulting in aggregate values from non-NULL entries only. On the other hand, CONCAT and GROUP_CONCAT include NULLs in the result pool but return NULL if all concatenated values are NULL. These handling differences affect the outcome of calculations and the interpretation of results when aggregates involve NULLs .

Stored procedures enhance management by encapsulating business logic, improving performance through precompilation, and ensuring consistency by standardizing complex operations. They provide security enhancements by restricting direct SQL execution and offer reduced network traffic by localizing data processing, beneficial for both database maintenance and application development .

DELETE allows for selective row deletion and can be rolled back using a transaction log, making it useful for transactions requiring potential rollback. However, DELETE is slower due to generating log entries for each row. TRUNCATE is faster as it does not generate log entries for each row, but it cannot be rolled back and might cause foreign key constraints to fail if not managed properly. TRUNCATE is suitable for quickly deleting all rows in large tables but requires ALTER TABLE privileges .

Savepoints allow finer control over transactions by marking points within them to which a rollback can occur. This enables partial transaction rollbacks, preserving some changes while discarding others, optimizing workflow by avoiding complete transaction failures and improving efficiency by reducing rework from failed processes. They support complex transaction logic by enabling staged transactions and controlled error handling .

COUNT(*) counts all rows, including those with NULL values, whereas COUNT(column) excludes NULL values in the specified column. When using GROUP BY, COUNT(column) provides counts of non-NULL values per group, while COUNT(*) includes NULLs, which affects group size calculation and can lead to different results between both functions .

A natural join automatically joins two tables based on columns with the same name and data type, removing duplicate columns in the result. Unlike other joins that require explicit conditions (e.g., INNER JOIN with ON clause), natural joins use implicit conditions based on matching column names, simplifying syntax but offering less control over join criteria .

TIMESTAMP uses 4 bytes and supports a range limited to '1970-01-01' to '2038-01-19', storing values in UTC and auto-updating on record changes, ideal for recording event times. DATETIME uses 8 bytes, supports a broader range from '1000-01-01' to '9999-12-31', and stores local date-time values precisely, making it suitable for applications needing high precision or dates outside TIMESTAMP's range .

The ACID properties—Atomicity, Consistency, Isolation, and Durability—ensure reliable database transactions. Atomicity guarantees that a transaction is all-or-nothing, ensuring system robustness in failure scenarios. Consistency maintains data integrity by keeping the database in valid states. Isolation prevents concurrent transactions from interfering, allowing each to operate as if it were the only transaction. Durability ensures transaction permanence through system failures, preserving committed changes through backups and replication .

Non-equi joins are preferred when the join condition requires comparisons other than equality, such as greater than, less than, or not equal to. They are useful for joining tables on similar but not identical data or when needing to join based on a value range. For example, you might use non-equi joins to find overlapping intervals in scheduling tables or to match records with conditional thresholds .

You might also like