0% found this document useful (0 votes)
13 views8 pages

DB Short Note All in One

Exit2prep2

Uploaded by

Nardos Tesema
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)
13 views8 pages

DB Short Note All in One

Exit2prep2

Uploaded by

Nardos Tesema
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/ 8

In database design, association, aggregation, composition, and other concepts are used to describe the

relationships between entities or objects. Here's a brief overview of each:


1. Association: An association is a relationship between two or more entities that are not part of a
whole-part relationship. In other words, each entity can exist independently, but they are related
to each other in some way.
Example: A customer can place many orders, and an order is associated with one customer. The customer
and order entities are separate, but they are related through the association.
2. Aggregation: Aggregation is a special type of association where one entity (the whole) is
composed of multiple entities (the parts). The whole entity cannot exist without the parts, but the
parts can exist independently.
Example: A university is composed of multiple departments. The university is the whole, and the
departments are the parts. The university cannot exist without departments, but departments can exist
independently.
3. Composition: Composition is a stronger form of aggregation where the parts cannot exist
independently of the whole. If the whole is deleted, all its parts are also deleted.
Example: A car is composed of wheels, engine, and transmission. If the car is deleted, all its parts
(wheels, engine, transmission) are also deleted.
4. Dependency: Dependency is a relationship where one entity relies on another entity for its
existence or functionality.
Example: A student depends on a teacher for guidance. If the teacher is deleted, the student's guidance is
affected.
5. Generalization: Generalization is a relationship where a more general entity is related to a more
specific entity.
Example: A vehicle is a general entity, and a car is a specific type of vehicle. The car entity inherits the
attributes and behavior of the vehicle entity.
6. Specialization: Specialization is the opposite of generalization, where a more specific entity is
related to a more general entity.
Example: A car is a specific type of vehicle, and a sports car is a specific type of car. The sports car entity
inherits the attributes and behavior of the car entity.
7. Inheritance: Inheritance is a relationship where a child entity inherits the attributes and behavior
of a parent entity.
Example: A square is a type of rectangle, and it inherits the attributes and behavior of a rectangle.
8. Realization: Realization is a relationship where an entity implements or realizes a specific
interface or contract.
Example: A payment gateway realizes a payment interface, which defines the methods for processing
payments.
9. N-ary Association: An n-ary association is a relationship between three or more entities.
Example: A student enrolls in a course, which is taught by a teacher. This is an n-ary association between
student, course, and teacher.
These concepts are essential in database design, as they help model complex relationships between
entities and ensure data consistency and integrity
1. What is a database? Answer: A database is a collection of organized data that is stored in a way that
allows for efficient retrieval and manipulation. Explanation: A database is a system that allows for the
storage, retrieval, and manipulation of data in a structured and controlled manner.
2. What is DBMS? Answer: DBMS stands for Database Management System, which is a software
system that allows for the creation, management, and interaction with databases. Explanation: DBMS
provides a way to define, create, and manipulate databases, as well as control access to the data.
3. What is a schema? Answer: A schema is the overall structure or organization of a database, including
the relationships between different data entities. Explanation: A schema defines the layout of the
database, including the tables, fields, and relationships between them.
4. What is a table? Answer: A table is a collection of related data entries, organized into rows and
columns. Explanation: A table is a fundamental component of a relational database, where each row
represents a single record, and each column represents a field or attribute.
5. What is a field? Answer: A field is a single element of a table, representing a specific attribute or
characteristic of the data. Explanation: A field is a column in a table that contains a specific piece of
information, such as a name, address, or phone number.
6. What is a primary key? Answer: A primary key is a unique identifier for each record in a table, used
to uniquely identify each row. Explanation: A primary key is a field or combination of fields that
uniquely identifies each record in a table, ensuring that no two records have the same value.
7. What is a foreign key? Answer: A foreign key is a field in a table that references the primary key of
another table. Explanation: A foreign key is used to establish relationships between tables, allowing data
to be linked and referenced across multiple tables.
8. What is normalization? Answer: Normalization is the process of organizing data in a database to
minimize data redundancy and improve data integrity. Explanation: Normalization involves dividing
large tables into smaller, related tables to reduce data duplication and improve data consistency.
9. What is denormalization? Answer: Denormalization is the process of intentionally denormalizing a
database to improve performance or simplify data access. Explanation: Denormalization involves
combining multiple tables into a single table, potentially introducing data redundancy, to improve query
performance or simplify data access.
10. What is a query? Answer: A query is a request for specific data or action from a database.
Explanation: A query is a statement that retrieves or manipulates data in a database, using a specific
syntax and structure.
11. What is SQL? Answer: SQL (Structured Query Language) is a standard language for managing and
manipulating data in relational databases. Explanation: SQL is a widely-used language for creating,
modifying, and querying databases, using commands such as SELECT, INSERT, UPDATE, and
DELETE.
12. What is a database index? Answer: A database index is a data structure that improves the speed of
data retrieval by providing a quick way to locate specific data. Explanation: An index is a data structure
that allows the database to quickly locate specific data, improving query performance and reducing search
time.
13. What is a view? Answer: A view is a virtual table based on the result of a query, providing a
simplified way to access and manipulate data. Explanation: A view is a virtual table that is derived from
one or more tables, providing a simplified way to access and manipulate data without modifying the
underlying tables.
14. What is a stored procedure? Answer: A stored procedure is a precompiled SQL program that
performs a specific task or set of tasks. Explanation: A stored procedure is a reusable program that
performs a specific task, such as data validation or data transformation, and can be executed multiple
times.
15. What is a trigger? Answer: A trigger is a set of instructions that are automatically executed in
response to a specific event or action. Explanation: A trigger is a program that is executed in response to a
specific event, such as inserting or updating data, and can perform actions such as data validation or
auditing.
16. What is data integrity? Answer: Data integrity refers to the accuracy, completeness, and consistency
of data in a database. Explanation: Data integrity ensures that data is accurate, complete, and consistent,
and is maintained through the use of constraints, triggers, and other mechanisms.
17. What is data security? Answer: Data security refers to the protection of data from unauthorized
access, use, disclosure, modification, or destruction. Explanation: Data security involves protecting data
from unauthorized access, use, or modification, using mechanisms such as access controls, encryption,
and authentication.
18. What is a transaction? Answer: A transaction is a sequence of operations that are executed as a
single, all-or-nothing unit of work. Explanation: A transaction ensures that multiple operations are
executed as a single unit, ensuring data consistency and integrity.
19. What is ACID? Answer: ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties
that ensure database transactions are processed reliably. Explanation: ACID properties ensure that
database transactions are executed reliably, maintaining data consistency and integrity.
20. What is a deadlock? Answer: A deadlock is a situation where two or more transactions are blocked,
each waiting for the other to release a resource. Explanation: A deadlock occurs when multiple
transactions are blocked, each waiting for the other to release a resource, resulting in a deadlock situation.
21. What is a lock? Answer: A lock is a mechanism that prevents multiple transactions from accessing
the same resource simultaneously. Explanation: A lock ensures that only one transaction can access a
resource at a time, preventing data inconsistencies and ensuring data integrity.
22. What is a cursor? Answer: A cursor is a control structure that allows a program to navigate and
manipulate data in a database. Explanation: A cursor is a control structure that allows a program to
navigate and manipulate data in a database, using commands such as FETCH and UPDATE.
23. What is a subquery? Answer: A subquery is a query nested inside another query, used to retrieve
data from multiple tables. Explanation: A subquery is a query that is nested inside another query,
allowing data to be retrieved from multiple tables and used in a single query.
24. What is a join? Answer: A join is a operation that combines data from two or more tables, based on a
common attribute. Explanation: A join combines data from multiple tables, based on a common attribute,
allowing data to be retrieved from multiple tables in a single query.
25. What is a union? Answer: A union is a operation that combines the result of two or more queries,
removing duplicates. Explanation: A union combines the result of multiple queries, removing duplicates,
and returning a single result set.
26. What is a index scan? Answer: An index scan is a method of retrieving data from a database using
an index. Explanation: An index scan uses an index to quickly locate specific data, improving query
performance and reducing search time.
27. What is a table scan? Answer: A table scan is a method of retrieving data from a database by
scanning the entire table. Explanation: A table scan involves scanning the entire table to retrieve data,
which can be slower than using an index scan.
28. What is a database administrator? Answer: A database administrator (DBA) is a person responsible
for designing, implementing, and maintaining a database. Explanation: A DBA is responsible for ensuring
the database is running efficiently, securely, and reliably, and performs tasks such as database design,
performance tuning, and backup and recovery.
29. What is a data model? Answer: A data model is a conceptual representation of data structures and
relationships. Explanation: A data model provides a conceptual representation of data structures and
relationships, helping to design and implement a database.
30. What is a database design? Answer: A database design is the process of creating a detailed plan for
a database, including the schema, tables, and relationships. Explanation: A database design involves
creating a detailed plan for a database, including the schema, tables, and relationships, to ensure the
database meets the requirements of the application or system.
31 Who created the first DBMS? Charles Bachman
The important features of a database management system are:
 Minimum Duplication and Redundancy of Data
 High Level of Security
 Mulitple-user Access
 Support ACID Property
The components of DBMS are as follows:
1) Hardware: Like a hard drive, monitor, etc.
2) Software: Provides a user interface
3) Data Manager: Manages operations of DBMS.
4) Data: The collection of information on the DB is known as data.
5) Data Languages: Languages like DDL, DML, DAL, and DCL allow to perform operations like

Creating, modifying, storing, or retrieving data.


The ability to query data, as well as insert, delete, and alter tuples, is offered by? __DML__________
DML (Data Manipulation Langauge)

A simple way to remember the differences:


 TCL: Transaction Control (manages transactions)
 DCL: Data Control (manages access and privileges)
 DDL: Data Definition (defines database structure)
 DML: Data Manipulation (manipulates data)
A great question about the different languages used in database management systems!
Here's a brief overview of each:
TCL (Transaction Control Language)
 Used to manage transactions, which are a sequence of operations executed as a single, all-or-
nothing unit of work.
 TCL statements control the boundaries of transactions, ensuring data consistency and integrity.
 Examples of TCL statements:
 COMMIT: Saves changes made in a transaction and makes them permanent.
 ROLLBACK: Undoes changes made in a transaction and restores the database to its
previous state.
 SAVEPOINT: Creates a point in a transaction where you can roll back to if needed.
 SET TRANSACTION: Specifies characteristics for the transaction such as isolation
level.
DCL (Data Control Language)
 Used to control access to a database, including user authentication, authorization, and access
privileges.
 DCL statements define the permissions and access rights for users and roles.
 Examples of DCL statements:
 GRANT: Assigns privileges to a user or role.
 REVOKE: Revokes privileges from a user or role.
 CREATE USER: Creates a new database user.
DDL (Data Definition Language)
 Used to define the structure and organization of a database, including the creation and
modification of database objects such as tables, indexes, and views.
 DDL statements define the schema of a database.
 Examples of DDL statements:
 CREATE TABLE: Creates a new table.
 ALTER TABLE: Modifies the structure of an existing table.
 DROP TABLE: Deletes a table.
 TRUNCATE: Deletes all rows from a table but keeps its structure intact.
 RENAME: Renames an existing database object
DML (Data Manipulation Language)
 Used to perform operations on data in a database, including inserting, updating, and deleting data.
 DML statements manipulate the data stored in a database.
 Examples of DML statements:
 INSERT: Adds new data to a table.
 UPDATE: Modifies existing data in a table.
 DELETE: Deletes data from a table.
Here's a simple way to remember the differences:
 TCL: Transaction Control (manages transactions)
 DCL: Data Control (manages access and privileges)
 DDL: Data Definition (defines database structure)
 DML: Data Manipulation (manipulates data)

In database systems, the ACID properties are a set of rules that ensure database transactions are
processed reliably and securely. ACID is an acronym that stands for:
A - Atomicity C - Consistency I - Isolation D - Durability
These properties guarantee that database transactions are executed in a way that maintains data integrity,
even in the presence of errors, crashes, or concurrent access.
Let's dive into each property:
A - Atomicity
Atomicity ensures that database transactions are treated as a single, indivisible unit of work. This means
that either all changes are committed, or none are. If any part of the transaction fails, the entire transaction
is rolled back, and the database is returned to its previous state.
Example: When transferring money from one account to another, the transaction should either succeed
entirely or fail entirely. If the transfer fails, the accounts should be restored to their original state.
C - Consistency
Consistency ensures that the database remains in a valid state, even after multiple transactions have been
executed. This means that the database must always satisfy its integrity constraints, such as primary key
uniqueness and referential integrity.
Example: In a banking system, the total amount of money in all accounts should always be consistent
with the total amount of money in the system.
I - Isolation
Isolation ensures that concurrent transactions do not interfere with each other's execution. This means that
each transaction should operate as if it were the only transaction accessing the database.
Example: When multiple users are updating their accounts simultaneously, each transaction should be
isolated from the others, ensuring that the updates are applied correctly and without interference.
D - Durability
Durability ensures that once a transaction has been committed, its effects are permanent and cannot be
rolled back. This means that the database guarantees that the changes made by a transaction will survive
even in the event of a system failure or crash.
Example: Once a payment has been processed and committed, the database should ensure that the
payment is recorded permanently, even if the system crashes or is shut down.
By enforcing these ACID properties, database systems can ensure that transactions are executed reliably,
securely, and with data integrity. This is crucial in many applications, such as financial systems, e-
commerce platforms, and social media networks, where data consistency and reliability are paramount.
Join mechanisms in DB
Inner join: This type of join returns only the rows that have matching values in both tables based on the
join condition.
Outer join: This includes several variations:
 Left outer join: Returns all rows from the left table, and the matched rows from the right table. If
there is no match, NULL values are returned from the right side.
 Right outer join: Returns all rows from the right table, and the matched rows from the left table.
If there is no match, NULL values are returned from the left side.
 Full outer join: Returns all rows when there is a match in either the left or right table.
Cross join: This join produces a Cartesian product of the two tables involved, meaning each row from the
first table is combined with each row from the second table.

An index helps to speed up?


SELECT queries and
WHERE clauses
Index helps to speed up SELECT queries and WHERE clauses, but it slows down data input, with the
UPDATE and the INSERT statements.
Indexes are special lookup tables that the database search engine can use to speed up data retrieval
The basic syntax of a CREATE INDEX is as follows : CREATE INDEX index_name ON table_name;
The basic syntax to drop index is as follows : DROP INDEX index_name;
Indexes should not be used on columns that contain a high number of NULL values.
Indexes should not be used on small tables
Indexes should not be used on Tables that have frequent, large batch updates or insert operations
Columns that are frequently manipulated should not be indexed.
Indexes take memory slots which are located on the disk.
 Single-column index: An index that is created based on only one column of a table. It allows
for efficient retrieval of rows based on the values in that particular column.
//-- Syntax for creating a single-column index in MySQL / MariaDB
CREATE INDEX index_name ON table_name(column_name);
Eg CREATE INDEX idx_name ON employees (last_name);
 Composite index: An index that is created based on more than one column of a table.
//-- Syntax for creating a composite index in MySQL / MariaDB
CREATE INDEX index_name ON table_name(column1, column2, ...);
Eg CREATE INDEX idx_name ON employees(last_name, first_name);
 Unique index: An index that enforces uniqueness on the indexed column or columns,
preventing duplicate values.
//--The basic syntax is CREATE UNIQUE INDEX index_name on table_name (column_name);
Eg CREATE UNIQUE INDEX idx_name ON employees(employee_id);
 Implicit index: This term is less commonly used in the context of SQL indexing; typically,
indexes are explicitly created by SQL statements.
Implicit indexes are indexes that are automatically created by the database server when an object is
created. Indexes are automatically created for primary key constraints and unique constraints
SQL CLAUSES,indexes? SHOULD KNOW

DISTINCT keyword?
DISTINCT removes duplicates based on all the columns in the SELECT clause
DISTINCT keyword can be used in SELECT but not used in WHERE clauses
Usage of DISTINCT should be avoided as far as possible due to performance issues
//SELECT DISTINCT column1, column2, column3 FROM table name;

Here's a list of some important SQL keywords and their functions in the context of databases:
1. SELECT: Retrieves data from a database.
o Example: SELECT column1, column2 FROM table_name;
2. DISTINCT: Returns unique values in the result set by eliminating duplicates.
o Example: SELECT DISTINCT column1 FROM table_name;
3. FROM: Specifies the table(s) from which to retrieve data.
o Example: SELECT column1 FROM table_name;
4. WHERE: Filters rows based on a specified condition.
o Example: SELECT column1 FROM table_name WHERE condition;
5. GROUP BY: Groups rows that have the same values into summary rows.
o Example: SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
6. HAVING: Filters groups based on a specified condition when using GROUP BY.
o Example: SELECT column1, COUNT(*) FROM table_name GROUP BY column1
HAVING COUNT(*) > 1;
7. ORDER BY: Sorts the result set in ascending or descending order.
o Example: SELECT column1 FROM table_name ORDER BY column1 ASC;
8. JOIN: Combines rows from two or more tables based on a related column.
o Types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN.
o Example: SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;
9. UNION: Combines the result sets of two or more SELECT statements.
o Example: SELECT column1 FROM table1 UNION SELECT column1 FROM table2;
10. INSERT INTO: Inserts new rows into a table.
o Example: INSERT INTO table_name (column1, column2) VALUES (value1, value2);
11. UPDATE: Modifies existing data in a table.
o Example: UPDATE table_name SET column1 = value1 WHERE condition;
12. DELETE: Deletes rows from a table.
o Example: DELETE FROM table_name WHERE condition;
13. CREATE TABLE: Creates a new table in the database.
o Example: CREATE TABLE table_name (column1 datatype, column2 datatype);
14. ALTER TABLE: Modifies an existing table (e.g., adding columns, modifying constraints).
o Example: ALTER TABLE table_name ADD column3 datatype;
15. DROP TABLE: Deletes a table and its data from the database.
o Example: DROP TABLE table_name;
16. CREATE INDEX: Creates an index on a table column(s) to improve query performance.
o Example: CREATE INDEX idx_column1 ON table_name (column1);
17. DROP INDEX: Deletes an index from a table.
o Example: DROP INDEX idx_column1 ON table_name;
18. TRUNCATE TABLE: Deletes all rows from a table without logging individual row deletions.
o Example: TRUNCATE TABLE table_name;
19. ROLLBACK: Rolls back a transaction to its starting point.
o Example: ROLLBACK;
20. COMMIT: Saves changes made during the current transaction.
o Example: COMMIT;
These keywords and their associated functionalities form the backbone of SQL queries and database
operations, enabling developers and database administrators to manage and manipulate data efficiently
within a relational database management system (RDBMS).

Aggregate functions in SQL are functions that operate on multiple rows of a table to compute a single
result. These functions summarize data from a set of rows according to a specific criterion. Here are the
common aggregate functions used in SQL:
1. COUNT():
o Counts the number of rows that match a specified condition.
o Example: SELECT COUNT(*) FROM employees;
2. SUM():
o Calculates the sum of values in a numeric column.
o Example: SELECT SUM(salary) FROM employees;
3. AVG():
o Calculates the average of values in a numeric column.
o Example: SELECT AVG(age) FROM employees;
4. MIN():
o Finds the minimum value in a column.
o Example: SELECT MIN(salary) FROM employees;
5. MAX():
o Finds the maximum value in a column.
o Example: SELECT MAX(salary) FROM employees;
Usage Notes:
 Aggregate functions are typically used with the SELECT statement to compute summary
information.
 They can be used with GROUP BY to calculate summary values for groups of rows.
 NULL values in columns are generally ignored by aggregate functions, except for COUNT(*),
which counts all rows regardless of NULL values
Example
//-- Count the number of employees
SELECT COUNT(*) FROM employees;

//-- Calculate the total salary of all employees


SELECT SUM(salary) FROM employees;

//-- Calculate the average salary of employees in each department


SELECT department, AVG(salary) FROM employees GROUP BY department;

//-- Find the minimum salary among all employees


SELECT MIN(salary) FROM employees;

//-- Find the maximum salary among all employees


SELECT MAX(salary) FROM employees;
TYPES OF QUERY: Master query, Sub query, Super query, Multi-query

You might also like