Structured Query Language OLD Week 1
Structured Query Language OLD Week 1
language (SQL)
What is SQL?
➢It is a computer language widely used for storing, manipulating and retrieving data in
relational database.
➢All relational database management systems like MySQL, MS SQL Server, Postgres
SQL, Oracle etc. use SQL as standard database language.
Most Used Technologies
Stack overflow survey 2021 - Professional
Developers Stack overflow survey 2021 – All Respondents
Reference: https://insights.stackoverflow.com/survey/2021#technology
Data Lifecycle in Machine Learning
RDBMS – Basic concepts
• Relational Database Management System (RDBMS) is a database management system that
stores data in two dimensional tables.
• A table (relation) consists of columns (attributes) and rows (records)
• Tables are related to other tables via relationship
Customer Table
Primary Key
Orders table Foreign Key – to identify which
Customer’s Order
Order_Id Order_date Order_amount Customer_Id Order_status
999901 2022-01-12 5000 1002 Fulfilled
999902 2022-01-15 6500 1002 Fulfilled
Advantages of RDBMS
• Sharing of data across applications and users
• Backup and recovery features
• Ease of use
• Entity Integrity: No two records of a table can be duplicate
• Referential Integrity: For example – Permit the rows to be deleted only if the rows are not
referenced by other tables.
• Access Integrity: Restrict access to functionality (e.g., create new table) and access to data,
based on user role and information confidentiality
• Domain integrity: The columns of a tables have defined data types. Additionally, default
values, permitted value ranges can be defined for columns.
• RDBMS ensures “ACID” (Atomicity, Consistency, Isolation, and Durability) for transaction
processing
Entity Relationship Diagram
SQL Overview
SQL Commands
• SQL commands are mainly categorized into the following categories
• DDL – Data Definition Language
• DML – Data Manipulation Language
• DCL – Data Control Language
• DQL – Data Query Language
• TCL – Transaction Control Language
DDL - Data Definition Language
Command Description
CREATE Creates a new table, a view of a table, or other object in database.
ALTER Modifies an existing database object, such as a table.
DROP Deletes an entire table, a view of a table or other object in the database.
TRUNCATE Deletes the data inside a table, but not the table itself
Command Description
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
Primary key and Unique key
• Primary Key
• Unique identifier for a table
• Primary key columns must be NOT NULL
• Each table has only one primary key
• Unique key
• A table may have zero or multiple unique keys
• Unique key columns can contain NULL values
DML - Data Manipulation Language
Data Manipulation Language (DML)
• Data Manipulation Language (DML) commands deal with the manipulation of data
• Main DML commands are
• INSERT
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
• UPDATE
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
• DELETE
DELETE FROM table_name WHERE condition;
DQL - Data Query Language
Data Query Language
• Data Query Language (DQL) statements are used for querying on the data. It consists of various clauses
• Select statement is used to retrieve data from one or more database objects.
• SELECT clause specifies column(s) to be displayed in result
• FROM clause specifies table(s) from which data is retrieved