0% found this document useful (0 votes)
239 views

(SQL Notes) - TheTestingAcademy - Pramod - Google Drive

Uploaded by

reshmamali76
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
239 views

(SQL Notes) - TheTestingAcademy - Pramod - Google Drive

Uploaded by

reshmamali76
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

SQL Notes - TheTestingAcademy

(Pramod Sir)

What is the Database?


A collection of data that is organized and stored in a structured manner, allowing for efficient
retrieval, manipulation, and analysis.

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

○ Security, Backup and Large amount of the database.

It typically consists of tables of information, with each table containing rows (records) and
es

columns (fields) that represent different types of data.

What is a Database Management System?


eT

● A database management system (DBMS) is a software program that is used to


create, manage, and manipulate databases.
Th

● Handles Security, Backups, Import and Export Data


● Concurrency, Interact with Software Applications.
● Manipulate databases with Programming Languages.

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

● Non-Relational (No SQL, Not just SQL)

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

of database in use today.

Non Relational Database :


A non-relational database, also known as a NoSQL database, is a type of database that is
designed to handle large amounts of unstructured data and is not organized into tables of
rows and columns.

Instead, data is stored as documents, key-value pairs, or other structures, and is


accessed through APIs or other interface methods. NoSQL databases are often used in
applications that require high performance and scalability, and are well-suited for handling
big data and real-time data processing. JSON, XML, Graphs, Flexible Tables, Key-value.
Installation of the Tools

● Install MySQL on the System


○ https://dev.mysql.com/downloads/installer/

● Install DB Gate.
○ https://dbgate.org

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

Non-Relational Databases (no SQL, Not just SQL)


A type of database that is designed to handle large amounts of unstructured data and is not
organized into tables of rows and columns like a traditional relational database.

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.

Examples of NoSQL databases include MongoDB, Cassandra, and Redis.

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

● As we have more data, it is difficult to get information, so queries are used.


● e.g Google is a search query.
● Database queries are commands used to retrieve, manipulate, or modify data in a
es

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

deleting data in a database.


Th

Here are some examples of database queries written in SQL:

● Select all rows from a table:


● SELECT * FROM table_name;

● Select specific columns from a table:


● SELECT column_1, column_2 FROM table_name;

● Select rows based on a condition:


● SELECT * FROM table_name WHERE column_name = 'value';

● Select rows based on multiple conditions:


● SELECT * FROM table_name WHERE column_1 = 'value' AND column_2 = 'value';

● Update data in a table:


● UPDATE table_name SET column_name = 'new_value' WHERE column_name =
'old_value';

● Delete rows from a table:


● DELETE FROM table_name WHERE column_name = 'value';

● Join two tables:


● SELECT * FROM table_1 INNER JOIN table_2 ON table_1.column_name =
table_2.column_name;

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

across all rows in the table.

Tuple / record: A tuple, also known as a record, is a row in a database table. It consists of a
es

set of values that represent different attributes or fields of a single entity.


eT
Th

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

Data definition language (DDL)


● This is the set of SQL statements that describe the structure of the data to the server.
This is how the data has to be.
● It is used to create and modify the structure of database objects in the database.

Examples of DDL statements include CREATE, ALTER, and DROP.

Data manipulation language (DML):


This is the set of SQL statements that tell the server what we want to do with the data.
Generally, this is limited to creating, retrieving, updating, and delete commands.

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

What is a Natural Key?


A natural key is a unique identifier that is inherent to the data, rather than being artificially
assigned.
es

E.g - a social security number or passport number would be a natural key for identifying a
person in a table.
eT

What is a Foreign key?


Th

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

What is a composite key?

It is a type of primary key that is composed of multiple attributes to identify the unique
records in a table.

CREATE TABLE Flights (


FlightNumber VARCHAR(6) NOT NULL,
DepartureDate DATE NOT NULL,
DepartureAirport VARCHAR(3) NOT NULL,
DepartureTime TIME NOT NULL,
ArrivalTime TIME NOT NULL,
PRIMARY KEY (FlightNumber, DepartureDate, DepartureAirport)
);

y
m
de
ca
gA

Data Types in SQL


tin

Some of the most commonly used data types in MySQL are:


es

● 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

SET, YEAR, TIME, BIT etc.

These data types can be used to store different types of data such as enumerated values,
eT

sets of values, year values, time values, Bit values etc.

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

Data insertion and CRUD

insert into customers (name) values ('Pramod Dutta');


Showing data and a specific

select * from customers;


select name from customers;

Where clause with operators, Like,


select * from customers where name = 'Pramod';
select * from customers where id = 2;
select * from customers where id = 2 or name = 'Pramod';

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

update customers set name = 'oo' where id = 2;

Like Operator
es

select * from customers where name like '%oo';


eT
Th
DROP DATABASE
DROP DATABASE databasename;
DROP TABLE table_name;

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

CREATE TABLE `customers` (


`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
);

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;

select * from customers where id = 2;


gA
select * from customers where name like 'pra%';
tin
es
eT
Th

SQL Constraints
SQL constraints are used to specify rules for the data in a table.

● NOT NULL - Ensures that a column cannot have a NULL value


● UNIQUE - Ensures that all values in a column are different
● PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies
each row in a table
● FOREIGN KEY - Prevents actions that would destroy links between tables
● CHECK - Ensures that the values in a column satisfies a specific condition
● DEFAULT - Sets a default value for a column if no value is specified
● CREATE INDEX - Used to create and retrieve data from the database very quickly

AUTO_INCREMENT

y
m
de
Project #1

Create this
ca
gA
tin
es
eT
Th

Solution:

CREATE TABLE `Students` (


`id` BIGINT AUTO_INCREMENT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`age` BIGINT NOT NULL,
`phone` INT NOT NULL,
CONSTRAINT `PRIMARY` PRIMARY KEY (`id`)
);
CREATE TABLE Courses (
id bigint NOT NULL AUTO_INCREMENT,
Name varchar(255) NOT NULL,
Price int,
PRIMARY KEY (id)
);

CREATE TABLE StudentCourses (


StudentId BigInt NOT NULL,

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

INSERT INTO `Students` (`name`, `age`, `phone`) VALUES


('Aju','32','989829323');
INSERT INTO `Students` (`name`, `age`, `phone`) VALUES
('Ajim','34','829329323');
es

INSERT INTO `Students` (`name`, `age`, `phone`) VALUES


('Pramod','32','239392323');
eT

SELECT * from Students;


SELECT * from Courses;
Th

SELECT * from StudentCourses;

INSERT INTO `StudentCourses` (`StudentId`, `CourseId`) VALUES ('1','1');


INSERT INTO `StudentCourses` (`StudentId`, `CourseId`) VALUES ('2','1');
INSERT INTO `StudentCourses` (`StudentId`, `CourseId`) VALUES ('3','2');

SELECT * from StudentCourses;

You might also like