(SQL Notes) - TheTestingAcademy - Pramod - Google Drive
(SQL Notes) - TheTestingAcademy - Pramod - Google Drive
(Pramod Sir)
y
● Collection of Related Information
○ Phone Book
m
○ Shopping List
○ To-do list
de
○ Your Best Friends
● Databases can be stored in different ways
○ ON paper
○ In your mind
ca
○ On A COMPUTER
○ This Google Document
○ Comment Sections
gA
● Computers can store databases in a better way than anything else.
○ E.g. Old bank they kept data in paper What happened?
○ Amazon.com -> Products and Credits Cards, Users and Media, images and
many more from the last 28 + years. (1994)
tin
It typically consists of tables of information, with each table containing rows (records) and
es
Some examples of DBMSs include Oracle, MySQL, and Microsoft SQL Server.
y
m
de
ca
Application Architecture
gA
tin
es
eT
Th
CRUD
Create, Read, Update and Delete
y
m
de
ca
gA
Types of Databases
tin
● Relational Database
es
Relational Database :
eT
A relational database is a type of database that organizes data into tables of rows and
columns, with each table containing data that is related to other tables through key fields.
This allows for efficient querying and manipulation of the data, and is the most common type
Th
y
Relational Database
m
https://drawsql.app/teams/k1-1/diagrams/student-and-course
de
ca
gA
tin
es
eT
Th
What is SQL?
SQL is a standard programming language used to manage and change data in relational
databases.
y
m
de
● Standardized language for interacting with RDBMS
● Used to perform CRUD
● Used to perform Admin type tasks like Backup, Recover extra
● Used to define tables and structures.
ca
gA
tin
es
eT
Th
Instead, data is stored as documents, key-value pairs, or other structures, and is accessed
through APIs or other interface methods.
● Most of the NRDMS will implement their own language for performing the CRUD.
● No standard language.
●
y
m
de
ca
gA
tin
es
eT
Th
y
m
de
ca
gA
Database Queries
● Queries are requests made to the database management system for specific
information.
tin
database.
● Queries are typically written in Structured Query Language (SQL) or a similar
programming language, and are used to specify the criteria for selecting, updating, or
eT
y
m
de
ca
gA
Similar to Excel Sheet we have Databases
tin
es
eT
Th
Table Structure
1. Row
2. Column
3. Attribute
4. Primary Key
5. Tuple / Record
y
Row: A row, also known as a record or tuple, is a single entry in a database table. It consists
m
of a set of values that represent different attributes or fields of a single entity, such as a
person or product.
de
Column: A column, also known as a field or attribute, is a vertical set of values in a
database table. Each column represents a different type of data, such as a person's name or
a product's price.
ca
Attribute: An attribute is a piece of data or a characteristic of an entity represented in a
database. In a table, each column represents a different attribute, and each row represents a
gA
single instance of that attribute.
Primary key: A primary key is a field in a database table that uniquely identifies each row. It
is typically used as the main reference field when linking tables together, and must be unique
tin
Tuple / record: A tuple, also known as a record, is a row in a database table. It consists of a
es
Relationships:
● One-to-one (1-1): One business entity is related to another single business entity.
○ E.g Order to 1 customer
● One-to-many (1-n): One business entity is related to many other business entities.
○ E.g Customer to Many Orders
● Many-to-many (m-n): Many business entities are related to many other business
y
entities.
○ Many Student to Many Courses
m
de
ca
gA
tin
es
eT
Th
y
m
de
SQL Commands
ca
gA
tin
es
eT
Th
Keys
1. Primary Key
y
2. Natural Key
3. Foreign Key
m
de
Keys in Databases
ca
What is the Primary Key?
gA
A primary key is a field or set of fields that uniquely identifies each record in a table.
tin
E.g - a social security number or passport number would be a natural key for identifying a
person in a table.
eT
A foreign key is a field or set of fields in a table that references the primary key of another
table.
It is used to establish a link between the data in two tables and enforce referential integrity
Referential integrity
Referential integrity is a set of rules that ensure the consistency and accuracy of data in a
relational database by enforcing relationships between tables
y
m
de
ca
gA
tin
es
eT
Th
It is a type of primary key that is composed of multiple attributes to identify the unique
records in a table.
y
m
de
ca
gA
● INT: This data type is used to store integer values. It can be specified as INT or
INTEGER, and can be defined with various widths, such as TINYINT, SMALLINT,
MEDIUMINT, INT and BIGINT
eT
● DECIMAL: This data type is used to store decimal (fixed-point) numbers. The
precision and scale can be specified in the definition, for example DECIMAL(10,2)
● FLOAT: This data type is used to store floating-point numbers. It can be specified
with optional precision, such as FLOAT(7,3)
Th
● DOUBLE: This data type is used to store double precision floating-point numbers.
● VARCHAR: This data type is used to store variable-length strings of characters. The
maximum length of the string can be specified in the definition, for example
VARCHAR(255)
● CHAR: This data type is used to store fixed-length strings of characters. The
maximum length of the string can be specified in the definition, for example
CHAR(10)
● DATE: This data type is used to store date values in the format of 'YYYY-MM-DD'
● DATETIME: This data type is used to store date and time values in the format of
'YYYY-MM-DD HH:MM:SS'
● TIMESTAMP: This data type is used to store timestamp values that are based on the
number of seconds since the Unix epoch ('1970-01-01 00:00:00' UTC)
● BLOB and TEXT: These data types are used to store binary and text data,
respectively. They can be used to store large amounts of data, such as images or
text documents.
y
m
de
ca
gA
tin
In addition to these basic data types, MySQL also provides other data types like ENUM,
es
These data types can be used to store different types of data such as enumerated values,
eT
When creating a table, it's important to use the correct data type for each column to ensure
the data is stored correctly and to optimize the performance of the database.
Th
https://www.w3schools.com/sql/sql_datatypes.asp
SQL Operations
y
m
# Count the items
select count(*) from <table_name> where <where_clause>` statement.
de
select count(*) from customers;
select count(*) from customers where name = 'Pramod';
ca
Deleting Rows from Tables
delete from customers where name = 'Pramod';
gA
Update Rows
tin
Like Operator
es
ALTER TABLE
ALTER TABLE customers ADD Email varchar(255);
y
TRUNCATE TABLE
m
TRUNCATE TABLE table_name;
de
ca
gA
tin
es
eT
Th
Mini Project CRUD
describe customers;
y
m
insert into customers(name) values ("Pramod");
insert into customers(name) values ("Dutta");
insert into customers(name) values ("PraD");
de
insert into customers(name) values ("Quick");
ca
select * from customers;
SQL Constraints
SQL constraints are used to specify rules for the data in a table.
AUTO_INCREMENT
y
m
de
Project #1
Create this
ca
gA
tin
es
eT
Th
Solution:
y
CourseId BigInt NOT Null
)
m
de
ALTER TABLE StudentCourses
ADD FOREIGN KEY (StudentId) REFERENCES Students(id);
ca
ALTER TABLE StudentCourses ADD FOREIGN KEY (CourseId) REFERENCES
Courses(id)
gA
INSERT INTO `Courses` (`Name`, `Price`) VALUES ('ATB','10000');
tin