SQL Unleashed Part1 Part2 1701889964
SQL Unleashed Part1 Part2 1701889964
LinkedIn - https://www.linkedin.com/in/aasthaa1jan/
TOPIC - SQL
Computer
Internet
AI
WHAT IS A DATABASE??
Share collection of logicallyy related data description of these data, designed to meet the
information needs of an organization.
1. Data Storage: A Database is used to store large amounts of structured data, making
it easily accessible, searchable, & retrievable.
2. Data Analysis: A Database can be used to perform complex data analysus, generate
reports, & provide insights into the data.
3. Record Keeping: A Database is often used to keep track of important records, such
as financial transactions, customer information, & inventory levels.
4. Web Applications: Databases are an essential component of many web
applications, providing dynamic content & user management.
CRUD
Create
Retreate
Update
Delete
Types of Databases
1. Relational Databases: Also known as SQL databases, these databases use a
relational model to organize data into tables with rows & columns.
3. Column Databases: These databses store data in columns rather than in rows,
making them well suited for data warehousing & analytical applications. (Amazon
Redshift, Google BigQuery)
4. Graph Databases: These databases store & query graph-structured, such as social
network connections or recommendation systems.
Relational Databases
1. Tabular Data
2. Columns -> Attributes -> Fields
3. Rows -> Tuples -> Records
4. Cardinality of the relation -> No. of records / rows / tuples
5. Degree of the relationn -> No. of attributes / columns / fields
DATABASE MANAGEMENT SYSTEM
DBMS is a software system that provides the interfaces and tools needed to store,
organize, & manage data in the database.
DATABASE KEYS
Let's consider a database table for student information with the following columns:
This column uniquely identifies each student in the table. It ensures that each
student has a unique identifier, and no two students can have the same roll
number. The primary key is essential for data integrity and to establish
relationships with other tables.
2. Name:
This column stores the name of the student. It does not need to be unique, as
different students can have the same name.
3. Branch:
This column represents the branch or major in which the student is enrolled
(e.g., Computer Science, Mechanical Engineering, etc.). The branch information
can be duplicated because multiple students can belong to the same branch.
4. Email (Unique Key):
This column contains the email address of the student. While not every student
may have an email address, for those who do, it's assumed to be unique. Using
a unique key constraint on the email column ensures that each email address is
unique across all records in the table.
Here's an example of how you might fill the table with random data:
Explanation of Keys:
The "Roll No" column serves as the primary key for the table. It uniquely
identifies each student, and no two students can have the same roll number.
The primary key is crucial for establishing relationships with other tables,
ensuring data integrity, and enabling efficient data retrieval.
Unique Key (Email):
The "Email" column is marked as a unique key. This ensures that each email
address in the table is unique. While not every student may have an email
address, for those who do, the email must be unique to avoid duplication.
Unique keys help maintain data integrity by preventing duplicate entries in the
specified column.
These key constraints (primary key and unique key) play a vital role in maintaining the
consistency and uniqueness of data in the database table.
ble.
2. Foreign Key:
Establishes relationships between tables.
Refers to the primary key in another table.
Maintains referential i
ntegrity.
3. Unique Key:
Ensures the uniqueness of values in a column or set of columns.
Allows NULL values (unless specified otherwise).
A table can have multipl
e unique keys.
4. Composite Key:
Consists of multiple columns that, together, uniquely identify a record.
Each individual column in the composite key
5. Candidate Key:
A set of one or more columns that, taken together, can uniquely identify a
record in a table.
It may include more columns than necessary for uniquely identifying a record.
A super key can have redundant or extra attributes beyond what is required for
unique
7. Surrogate Key:
A surrogate key is an artificially generated key used as the primary key for a
table.
It is typically a numeric or alphanumeric identifier assigned to each record.
Surrogate keys have no inherent meaning and are used purely for the purpose
of uniquely identifying records.
Commonly used when a natural key is impractical or when a stable, unchanging
identifier is needed. is needed.te and maintainable data.
CARDINALITY OF RELATIONSHIPS
Cardinality in Database relationships refers to the number of occurances of an entity
in a relationship with another entity.
It defines the number of instances of ana entity that can be associated with the
single instance of the related entity.
Types:
1. One-to-One (1:1) Cardinality:
Definition: Each record in the first table is related to only one record in the
second table, and vice versa.
Example: A relationship where each person has only one passport, and each
passport is issued to only one person.
2. One-to-Many (1:N) Cardinality:
Definition: Each record in the first table can be related to multiple records in
the second table, but each record in the second table is related to only one
record in the first table.
Example: A relationship where one customer can place multiple orders, but
each order is associated with only one customer.
3. Many-to-Many (M:N) Cardinality:
Definition: Each record in the first table can be related to multiple records in
the second table, and vice versa.
Example: A relationship where students can enroll in multiple courses, and each
course can have multiple students.
Examples
DRAWBACKS OF DATABASES
1. Complexity:
LinkedIn - https://www.linkedin.com/in/aasthaa1jan/
SQL
SQL is a programming language used for managing and manipulating data in
relational databases.
It allows you to insert, update, retrieve and delete data in a database.
It is widely used for data management in many applications, websites and
businesses.
In simple terms, it is used to communicate with and control databases.
Removes all records from a table but retains the table structure for reuse.
Syntax: TRUNCATE TABLE table_name;
Example: TRUNCATE TABLE Students;
Let's first learn to insert the data into the table users
Inserting Tuple 1 -
Resulting table with Tuple 1 -
Inserting Tuple 2 -
Resulting table -
Observe That on Dropping the table users , the structure of the table is deleted i.e
existence of the table users has been deleted.
DATA INTEGRITY:
Data integrity refers to the accuracy, consistency, and reliability of data in a database. It
ensures that data remains correct and valid throughout its lifecycle, from creation to
modification and deletion. Maintaining data integrity is crucial for reliable decision-
making, and it is enforced through various mechanisms in a relational database system.
1. Constraints:
Contraints in databases are the rules or conditions that must be met for data to be
inserted, updated, or deleted in a database table. They are used to enforce the integrity
of the data stored in a database & to prevent data from becoming inconsistent or
corrupted .
2. Transactions:
Atomicity:
Ensures that a transaction is treated as a single, indivisible unit. If any part of the
transaction fails, the entire transaction is rolled back to its previous state.
Consistency:
Ensures that a transaction brings the database from one valid state to another,
maintaining integrity constraints.
Isolation:
Ensures that multiple transactions can occur concurrently without interfering
with each other.
Durability:
Ensures that once a transaction is committed, its changes are permanent and
survive system failures.
3. Normalizations:
A design technique that minimizes data redundancy & ensures data consistency by
organizing data into seperate tables.
CONSTRAINTS IN MYSQL
In MySQL, constraints are rules that you can apply to columns in a table to enforce data
integrity. Constraints help ensure that the data stored in the database follows specific
rules, preventing inconsistencies or errors. Here are some common constraints in MySQL:
These constraints play a crucial role in defining the structure of your database and
maintaining the integrity of your data. They help prevent issues such as duplicate
records, inconsistent relationships, and data that doesn't adhere to specified rules.
1. CASCADE:
Similar to SET NULL, but the foreign key values are set to their default values
instead of NULL.
Example:
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT DEFAULT 0,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON
UPDATE SET DEFAULT ON DELETE SET DEFAULT
);
4. RESTRICT:
These referential actions help you define the behavior of the database when changes
occur in the referenced tables, ensuring that relationships between tables remain
consistent and maintain integrity.
DATATYPES IN MYSQL
MySQL supports various data types that allow you to store different kinds of data in your
database tables. Here are some common data types in MySQL:
Numeric Types:
1. INTEGER or INT:
Small integers.
Example: TINYINT
3. SMALLINT:
Medium-sized integers.
Example: SMALLINT
4. MEDIUMINT:
A medium-sized integer.
Example: MEDIUMINT
5. BIGINT:
Large integers.
Example: BIGINT
6. FLOAT:
Fixed-point numbers.
Example: DECIMAL(10,2)
String Types:
9. CHAR:
Other Types:
17. BOOLEAN or BOOL:
Similar to ENUM, but can have multiple values chosen from a predefined list.
Example: SET('Option1', 'Option2', 'Option3')
Binary Types:
20. BINARY:
These are some of the common data types in MySQL. The choice of data type depends
on the nature of the data you are storing and the operations you intend to perform on
that data.
This example demonstrates how to use various constraints to ensure data integrity in a
relational database.
1. Add Columns
2. Delete Columns
3. Modify Columns
Here are some common use cases of the ALTER TABLE command:
Adding a Column:
ALTER TABLE table_name
ADD COLUMN new_column_name INT;
Modifying a Column:
ALTER TABLE table_name
MODIFY COLUMN existing_column_name VARCHAR(50);
Adding an Index:
ALTER TABLE table_name
ADD INDEX index_name (column1, column2);
Renaming a Table:
ALTER TABLE old_table_name
RENAME TO new_table_name;