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

Structured Query Language OLD Week 1

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

Structured Query Language OLD Week 1

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

Structured Query

language (SQL)
What is SQL?

➢Structured Query Language, developed at IBM in early 1970s

➢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

Columns / Attributes / Fields


Customer Table

Customer_Id Name Address City Country Telephone_number


Rows / Records 1001 Akhil Patil Pedar road Mumbai India 123-456-7890
1002 Richa Reddy M G Road Bangalore India 234-567-8901
RDBMS – Basic concepts
• Table have a Primary Key which identifies each row uniquely
• Tables are related to other tables via relationship
• The relationship between tables are established using Foreign Key

Customer Table

Customer_Id Name Address City Country Telephone_number


1001 Akhil Patil Pedar road Mumbai India 123-456-7890
1002 Richa Reddy M G Road Bangalore India 234-567-8901

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

DML - Data Manipulation Language


Command Description
INSERT Creates a record
UPDATE Modifies records
DELETE Deletes records
DCL - Data Control Language
Command Description
GRANT Gives a privilege to user
REVOKE Modifies an existing database object, such as a table.

DQL - Data Query Language


Command Description
Select Retrieves certain records from one or more tables
TCL – Transaction Control Language
Command Description
COMMIT The changes are saved and cannot be rolled back
ROLLBACK Restore the database to previous Commit point or Savepoint
SAVEPOINT Identify a point in a transaction to which you can later rollback
SQL Data Types
Major Data Types Description
CHAR(length) 0 to 255
VARCHAR(length) 0 to 65535
TEXT A string with length 65535 characters
INT(length) Integer
FLOAT Decimal point
DOUBLE Large number with Decimal point
DATE YYYY-MM-DD
DATETIME YYYY-MM-DD HH:MM:SS
TIME HH:MM:SS
SQL Constraints

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 Specifies columns to be displayed in the result

FROM Specifies tables from which data will be queried

WHERE Specifies criteria for filtering records (Optional)

GROUP BY Specifies columns used for grouping the data (Optional)

HAVING Specifies criteria for filtering grouped data (Optional)

ORDER BY Specifies sort order in which result is displayed (Optional)


SELECT and FROM clauses

SELECT Specifies columns to be displayed in the result

FROM Specifies tables from which data will be queried

WHERE Specifies criteria for filtering records (Optional)

GROUP BY Specifies columns used for grouping the data (Optional)

HAVING Specifies criteria for filtering grouped data (Optional)

ORDER BY Specifies sort order in which result is displayed (Optional)


SELECT and FROM 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

SELECT *| column1, column2…


FROM table_name
[WHERE conditions];
THANK YOU

You might also like