SQL
SQL
retrieval and management of data in relational databases like MySQL, MS Access, SQL Server,
Oracle, Sybase, Informix, PostgreSQL etc. SQL is not a database management system, but it
is a query language, which is used to store and retrieve the data from a database or in simple
words, SQL is a language that communicates with databases.
A Data Definition Language (DDL) is a computer language, which is used to create and
modify the structure of database objects, which include tables, views, schemas, and indexes
etc.
Command Description
Transactional Control Commands (TCL) are only used with the DML Commands such as -
INSERT, UPDATE and DELETE. They cannot be used while creating tables or dropping them
because these operations are automatically committed in the database. Following commands
are used to control transactions.
Command Description
Data Integrity
Entity Integrity − This ensures that there are no duplicate rows in a table.
Domain Integrity − Enforces valid entries for a given column by restricting the type,
the format, or the range of values.
Referential integrity − Rows cannot be deleted, which are used by other records.
User-Defined Integrity − Enforces some specific business rules that do not fall into
entity, domain or referential integrity.
Full backup:- When your backup software takes a full backup, it copies the entire dataset,
regardless of whether any changes were made to the data. This type of backup is generally
taken less frequently for practical reasons. For instance, it can be time-consuming and also
take up a large amount of storage space. Alternatives to full data backups include differential
or incremental backups. => ’ BACKUP DATABASE database_name TO medium = 'filepath'
GO
Incremental backup:- An incremental backup only copies modified data since the last
backup. For example, if you took a full backup on Sunday, your incremental backup on
Monday would only copy changes since the Sunday backup. On Tuesday, it would only copy
changes to the backup image file since the Monday backup.
Differential backup:- A differential backup strategy copies only newly added and changed
data since the last full backup. If your last full backup was on Sunday, a backup on Monday
would copy all changes since Sunday. If you took another backup on Tuesday, it would also
copy all changes since Sunday. The backup file size would increase progressively until the
next full backup.=> ‘BACKUP DATABASE my_db TO medium = 'filepath' WITH
DIFFERENTIAL; GO
Transaction Log (T-log) backup:- A transaction log backup includes all the transactions
since the last transaction log backup. BACKUP LOG command is used to perform the
Transaction Log backup.=> ‘BACKUP LOG database_name TO medium = 'filepath'; GO
1. Simple
Description
Data loss
- Yes; we cannot restore the database to an arbitrary point in time. This means, in the
event of a failure, it is only possible to restore database as current as the last full or
differential backup, and data loss is a real possibility
Point-in-time restore
- No
2. Full
Description
- If the database uses the full recovery model, the transaction log would grow infinitely,
and that will be a problem. So, we need to make sure that we take transaction log
backups on a regular basis.
Data loss
Description
- This model is similar to the Full Recovery Model, in that a transaction log is kept, but
certain transactions such as bulk loading operations are minimally logged, although
it logs other transaction. This makes the bulk data imports perform quicker and keeps
the file size of the transaction log down, but does not support the point in time
recovery of the data.
- This can help increase performance bulk load operations.
- Reduces log space usage by using minimal logging for most bulk operations.
Data loss
- If you run transactions under the bulk-logged recovery model that might require a
transaction log restore, these transactions could be exposed to data loss.
Point-in-time restore
SQL Cloning Operation allows to create the exact copy of an existing table along with its
definition. There are three types of cloning possible using SQL in various RDBMS; they are
listed below.
Simple Cloning creates a new replica table from the existing table and copies all the records
in newly created table. To break this process down, a new table is created using the CREATE
TABLE statement; and the data from the existing table, as a result of SELECT statement, is
copied into the new table. Here, clone table inherits only the basic column definitions like
the NULL settings and default values from the original table. It does not inherit the indices
and AUTO_INCREMENT definitions.
Shallow Cloning creates a new replica table from the existing table but does not copy any
data records into newly created table, so only new but empty table is created. Here, the clone
table contains only the structure of the original table along with the column attributes
including indices and AUTO_INCREMENT definition.
Deep Cloning is a combination of simple cloning and shallow cloning. It not only copies the
structure of the existing table but also its data into the newly created table. Hence, the new
table will have all the contents from existing table and all the attributes including indices and
the AUTO_INCREMENT definitions.
Temporary Tables
They are the tables which are created in a database to store temporary data. We can perform
SQL operations similar to the operations on permanent tables like CREATE, UPDATE,
DELETE, INSERT, JOIN, etc. But these tables will be automatically deleted once the current
client session is terminated. In addition to that, they can also be explicitly deleted if the users
decide to drop them manually.
ALTER TABLE command is a part of Data Definition Language (DDL) and modifies the
structure of a table. Add or delete columns, create or destroy indexes, change the type of
existing columns, or rename columns or the table itself.
DROP TABLE statement is a Data Definition Language (DDL) command that is used to
remove a table's definition, and its data, indexes, triggers, constraints and permission
specifications (if any).
T-SQL Vs PL/SQL
PL/SQL T-SQL
PL SQL was developed by Oracle. T-sql was developed by Microsoft.
It is a natural programming which is compatible it provides highest degree of control to
with the sql and also provides better functionality. the programmers
PL sql is good with its performance with oracle T-sql is good with the performance with
database server Microsoft sql server.
here INSERT INTO statement must be used. here SELECT INTO statement must be
used.
PL sql provides OOPs concepts like data- This allows inserting multiple rows into
encapsulation, function overriding etc table using BULK INSERT statement.
OLTP Vs OLAP
ACID Properties
The acronym ACID stands for Atomicity, Consistency, Isolation, and Durability. ACID
properties are essential for ensuring database transactions are reliable and consistent.
Property Description Example
Atomicity Ensures that all parts of a transaction are All items in a customer's order must
completed; if one part fails, the entire be added to the database, or none at
transaction fails. all.
Consistency Ensures that the database remains in a A bank transfer should never result
valid state before and after a transaction. in money disappearing from both
accounts.
Isolation Ensures that concurrent transactions do Two users withdrawing money from
not interfere with each other. an ATM do not affect each other’s
transactions.
Durability Ensures that once a transaction is After a power outage, the bank’s
committed, its effects are permanent, system still shows the correct
even in the case of a crash. account balance.
Indexing in SQL is a technique used to improve the speed of data retrieval from a database
by creating a lookup table for faster access.
Use Indexes, Avoid SELECT *,Limit Results, Use EXISTS Instead of IN, Avoid Functions in
WHERE Clause.
A Deadlock occurs when two or more transactions block each other by holding locks on
resources that the other transactions need.
1. Access Tables in the Same Order: Always access tables in a consistent sequence across
transactions.
2. Minimize Lock Time: Keep transactions short and fast to reduce lock holding time.
3. Use Lower Isolation Levels: Use READ COMMITTED instead of SERIALIZABLE
isolation level if possible.
4. Avoid User Interaction Inside Transactions: Do not wait for user input during a
transaction.
5. Use NOLOCK or Read Uncommitted: Allow non-blocking reads for read-only operations
6. Proper Indexing: Use Indexes to minimize the number of rows locked.
7. Break Large Transactions: Split large transactions into smaller batches
Table Partitioning is a technique used to divide large tables into smaller, more manageable
pieces without changing the table structure. We use partitioning when a table grows so large
that query performance starts to degrade.
Used for:
Types of Partitioning:
Database replication involves copying and maintaining database objects across multiple
servers to ensure data redundancy and high availability. It can be synchronous or
asynchronous.
Synchronous replication ensures that changes are reflected in real time across servers.
1. Master-slave replication: In this setup, one database (the master) handles all write
operations, while one or more replicas (slaves) handle read operations.
2. Master-master replication: In a master-master setup, two or more databases can
handle both read and write operations.
3. Snapshot replication: This involves taking a snapshot of the database at a specific
point in time and copying it to another location.
4. Transactional replication: This method replicates data incrementally as
transactions occur.
What are stored procedures, and how do they improve database performance?
A stored procedure is a precompiled set of SQL statements that can be executed as a unit.
Stored procedures improve performance by reducing the amount of data sent between the
database and the application, as multiple queries can be executed with a single call.
Sharding is a horizontal partitioning strategy where a large database is split into smaller,
more manageable pieces called shards. Sharding is typically used when dealing with large
datasets where the database needs to handle high transaction volumes and millions of users.
1. Vertical scaling: This involves adding more resources, such as CPU, memory, or
storage, to the existing database server.
2. Horizontal scaling (sharding): For larger databases or when dealing with massive
datasets, horizontal scaling, or sharding, is more effective. This involves distributing
the database across multiple servers or nodes, where each shard holds a subset of the
data.
3. Replication: Replication involves copying data to multiple database servers to
distribute the read workload.
4. Database indexing and query optimization: Efficient indexing and query
optimization can significantly improve performance, making the database more
scalable.
5. Caching: Implementing a caching layer, like Redis or Memcached, helps offload
frequently accessed data from the database.
6. Partitioning: Database partitioning involves splitting a large table into smaller, more
manageable pieces, improving query performance and making data management
more efficient.
A View is a virtual table based on the result of a SQL query. It doesn't store data itself but
displays data retrieved from one or more underlying tables. Views also enhance security by
restricting user access to specific data fields without giving them access to the underlying
tables.
Uses for:
Types Of Triggers:
1. A row-level trigger is executed once for each row affected by the triggering event,
which is typically an INSERT, UPDATE, or DELETE statement.
2. A statement-level trigger is executed once for the entire triggering event, instead of
once for each row affected by the event.
Mirroring also known as Shadowing, is the process of creating multiple copies of data and
database. Generally, in mirroring, the database is copied on a very different machine or
location from its main database. Technique used to improve the availability and reliability of
SQL Server databases. This is one of those SQL Server High Availability Solutions that can be
configured on databases with a full recovery model.
1. High Availability: The principle justification for using database mirroring is high
availability.
2. Disaster Recovery: The mirror database will be promoted as principal in a disaster.
This way, businesses can return to operations quickly with minimal data loss.
3. Data Protection: Synchronous mirroring ensures that all transactions are committed
to both the primary and mirror databases. It provides excellent data protection in the
event of a server failure.
4. Scalability: Database mirroring can be configured for numerous mirrors so
businesses can scale up their high-availability solution to meet their needs
SQL Log Shipping is a technique in which there are two or more SQL Server instances that
are copying transaction log files from one SQL server instance to another.
This technology consists of a number of servers known as cluster nodes. All of these servers
have the same hardware and software components to facilitate high availability for the
failover cluster instance.
File Streaming
SQL Server FILESTREAM is a feature that allows you to store large binary data (e.g., images,
videos, and documents) directly in the file system while maintaining transactional
consistency with your SQL Server database. This is particularly beneficial for applications
that require quick access to large binary objects (BLOBs) without the overhead of traditional
SQL Server data types like VARBINARY.
SQL Normalization