SlideShare a Scribd company logo
SQL
DDL and DML command
Categories of SQL Commands
SQL commands can be classified into the following categories:
1. Data Definition Language (DDL) Commands permits database tables to be created
or deleted. It also defines indices (keys), specifies links between tables, and imposes
constraints on tables. Examples of DDL commands in SQL are:
CREATE DATABASE - creates a new database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
2. The Data Manipulation Language (DML) Commands:The query and update
commands form the DML part of SQL:
Examples of DDL commands are:
SELECT - extracts data from a table
UPDATE - updates data in a table
DELETE - deletes data from a table
INSERT INTO - inserts new data into a table
Data type in MySQL
Numeric Data Types:
INTEGER or INT – up to 11 digit number without decimal. SMALLINT –
up to 5 digit number without decimal.
FLOAT (M,D) or DECIMAL(M,D) or NUMERIC(M,D) Stores Real numbers
upto M digit length (including .) with D decimal places. e.g. Float (10,2)
can store 1234567.89
Date & Time Data Types:
DATE - Stores date in YYYY-MM-DD format. TIME - Stores time in
HH:MM:SS format.
String or Text Data Type:
CHAR(Size) A fixed length string up to 255 characters. (default is 1)
VARCHAR(Size) A variable length string up to 255 characters.
RDBMS Terms
• Domain: It is pool of values or the collection (set) of possible values from which
the value for a column is derived.
• Tables or Relation in a Database: Relational Databases store data or information in
tables. A table is similar to a spreadsheet where data is stored in rows and
columns. A table refers to a two- dimensional representation of data using rows
and columns.
• Record - The horizontal subset of the Table is known as a Row/Tuple. Each row
represents a record, which is a collection of data about a particular entity such as
person, place or thing.
• Field - The vertical subset of the Table is known as a Column/Attribute. The term
field is also often used for column. Each column has a unique name and the
content within it must be of the same type.
• For example, consider the following table named Customer with details about
customers:
• MySQL : It is an Open Source RDBMS Software. It is available free of cost.
Keys
Key : A column or a combination of columns which can be used to identify one or more rows (tuples) in a table is
called a key of the table.
Primary Key : The group of one or more columns used to uniquely identify each row of a relation is called its
Primary Key.
Candidate Key : A column or a group of columns which can be used as the primary key of a relation is called a
Candidate key because it is one of the candidates available to be the primary key of the relation.
Alternate Key : A candidate key of a table which is not selected as the primary key is called its Alternate Key.
Example: Consider the following Table, RollNo and Admission_no both may be used to uniquely identify each row
in this Table, so both are candidate keys.
Candidate keys which are not made primary key are called Alternate keys. In the above example, if we use one of
the candidate keys, say, Admission_No as the Primary Key, the other Candidate Key RollNo is the Alternate Key and
vice-versa.
Foreign Key : A primary key of a base table when used in some other table is called as Foreign Key.
For example: Table Employee has columns : EMPID, EMPNAME, ADDRESS, CONTACT NO, DEPTID. And the table
Department has columns : DEPTID, DNAME, CITY. Then the DEPTID column of the Employee table will be known as
Foreign Key because it is declared as Primary Key in the Department table.
Note : The primary key column and the foreign key column must have the same data type andsize.
SQL - commands
• Opening a database
mysql> USE school ;
• Getting listings of database and tables
mysql> SHOW DATABASES;
mysql> SHOW TABLES;
• Deleting a Database and Table
mysql> DROP DATABASE School;
mysql> DROP TABLE STUDENT;
• Viewing Table Structure
mysql> DESC STUDENT;
• Adding constraints in table
Ex1. : CREATE TABLE Shoes(Code CHAR(4), Name VARCHAR(20), type VARCHAR(10),size INT(2),
cost DECIMAL(6,2), margin DECIMAL(4,2), Qty INT(4));
Ex2.: Creating Tables & Inserting records
mysql> Create table Student(ADMNO CHAR(10), NAME CHAR(20), AGE INT(3), PHONE_NO
INT(10));
• Inserting records into the Table
mysql> Insert into Student Values(‘JS0123’, ‘AJAY’, 13, 941234567);
mysql> Insert into Student Values(‘JS0124’, ‘RAJANI’, 12, 991234567);
mysql> Insert into Student Values(‘JS0125’, ‘PURVA’, 12, 900000001);
SQL - commands
• Creating Another Simple Tables:
mysql> CREATE TABLE Empl (empno int(5), ename char(30), Job char(15), mgr int(5), hiredate
Date, Sal float(10,2), comm int(6), deptno int(4));
• Inserting Records:
mysql> INSERT INTO Empl VALUES (7369,’SMITH’,’CLERK’,7902,’1980-12-17’, 800.00,NULL,20);
mysql> INSERT INTO Empl VALUES (7499,’ALLEN’,’SALESMAN’,7698,’1981-02-20’,
1600.00,300.00,30);
mysql> INSERT INTO Empl VALUES (7521,’WARD’,’SALESMAN’,7698,’1981-02-22’,
1250.00,500.00,30);
• Selecting all columns
If you want to view all columns of the student table, then you should give the following command
mysql> SELECT * FROM Empl;
SQL - commands
Selecting columns If you want to view only
ename and job columns of the empl table
MySql>select ename, job from empl;
Modifying the structure of table
When we create a table we define its structure. We can also
change its structure i.e. add, remove or change its column(s)
using the ALTER TABLE statement.ALTER TABLE is used to add a
constraint,to remove a constraint,to remove a column from a
table,to modify a table column.
Syntax:
• ALTER TABLE <table_name> ADD/DROP <column_name> [datatype];
• ALTER TABLE <table> MODIFY <column> <new_definition>;
If we want to add a column named LOCATION in the EMPL table.
mysql> ALTER TABLE EMPL ADD LOCATION CHAR(10);
Modifying the structure of table
Modifying the structure of table
Now, suppose we want to change the newly added LOCATION
column to hold integers(inplace of character data) using ALTER
TABLE statement:
 mysql> ALTER TABLE EMPL MODIFY LOCATION INT(10);
Modifying the structure of table
• To delete a column of a table the ALTER TABLE
statement is used with Drop clause.
mysql> ALTER TABLE EMPL DROP LOCATION;
Eliminating Duplicate values in a
column - DISTINCT
• mysql> select distinct job from empl;
Doing simple calculations
We can also perform simple calculations with
SQL Select command. SQL provide a dummy
table named DUAL, which can be used for this
purpose.
mysql> SELECT 4*3 ;
Doing simple calculations
We can also extend this idea with a columns of
the existing table.
mysql> SELECT Name, Sal *12 FROM EMPL ;
Using Column Aliases
We can give a different name to a column or
expression (Alias) in the output of a query.
mysql> SELECT 22/7 AS PI FROM Dual;
mysql> SELECT Name, DOB AS ‘Date of Birth’
FROM Student;
mysql> SELECT Name, Sal*12 AS ‘Annual
Salary’ FROM EMPL;
Handling Nulls
Empty values are represented as Nulls in a table.
mysql> select ename, job, comm from empl;
See the null values appear as Null. If we want to
substitute null with a value in the output, we can
use IFNULL() function.
Handling Nulls IFNULL() Syntax:
IFNULL(columnname, value-to-be-substituted)
• mysql> select ename, job, ifnull(comm, ‘Not
Entitled’) from empl;
Putting text in the query output:
mysql> select ename, ‘ gets the commission ‘, comm from empl;
SQL - commands
• WHERE Clause:
Where clause is used to fetch data based on a criteria/ condition.
• Syntax:
 SELECT <column name1> [,<column name> ,….] FROM <table
name>WHERE <condition>;
For example if we wish to display records of students who have a
got marks greater then we will 100 then following command
needs to be entered:
 mysql> select fname, marks from student-> where marks >
100;
SQL - commands
• LIKE keyword : LIKE is used for pattern matching and is very useful. Following characters used for
applying pattern matching:
% percent symbol is used to match none or more characters
_ underscore character is used to match occurrence of one character in the string For
example : To search for records having first name starting with letter ‘R’;
mysql>select * from student where fname like ‘A%’;
• Command to display names that end with letter ‘K’ is
mysql>select * from student where fname like ‘%K’;
• Command to display list of all the students whose name has upto five characters:
mysql>select * from student where fname like ‘_____’;
SQL - commands
Similarly many more patterns may be given as follows :
‘_ _ _ _’ matches any string with exactly four characters
‘S_ _ _ _’ matches any string of length of exactly 5 characters and starts with letter
‘S’
‘S_ _ _ ‘S_ _ _%’ matches any string of length of 4 or more characters and starts with letter
‘S’
‘_ _ _H matches any string of length of exactly 4 characters and terminates with
letter ‘H’ ‘_ _ _ ‘_ _ _%’ matches any string with at least three or more characters
‘%in% matches any string which containing ‘in’
Ad

More Related Content

Similar to SQL PPT.pptx (20)

Chapter8 my sql revision tour
Chapter8 my sql revision tourChapter8 my sql revision tour
Chapter8 my sql revision tour
KV(AFS) Utarlai, Barmer (Rajasthan)
 
MYSQL-database different functions pptx.pdf
MYSQL-database different functions pptx.pdfMYSQL-database different functions pptx.pdf
MYSQL-database different functions pptx.pdf
viluThakkar
 
Data Base Management 1 Database Management.pptx
Data Base Management 1 Database Management.pptxData Base Management 1 Database Management.pptx
Data Base Management 1 Database Management.pptx
PreeTVithule1
 
SQL
SQLSQL
SQL
Vineeta Garg
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
HudaRaghibKadhim
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
pranav kumar verma
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
paddu123
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
paddu123
 
Lab
LabLab
Lab
neelam_rawat
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
Abrar ali
 
SQL
SQLSQL
SQL
Shyam Khant
 
Sql
SqlSql
Sql
Karunakaran Mallikeswaran
 
Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statements
Mohd Tousif
 
SQL python for beginners easy explanation with concepts and output .pptx
SQL python for beginners easy explanation with concepts and output .pptxSQL python for beginners easy explanation with concepts and output .pptx
SQL python for beginners easy explanation with concepts and output .pptx
harshitagrawal2608
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
jainendraKUMAR55
 
MS SQL Server 1
MS SQL Server 1MS SQL Server 1
MS SQL Server 1
Iblesoft
 
98765432345671223Intro-to-PostgreSQL.ppt
98765432345671223Intro-to-PostgreSQL.ppt98765432345671223Intro-to-PostgreSQL.ppt
98765432345671223Intro-to-PostgreSQL.ppt
HastavaramDineshKuma
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
EllenGracePorras
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
Balqees Al.Mubarak
 
MYSQL-database different functions pptx.pdf
MYSQL-database different functions pptx.pdfMYSQL-database different functions pptx.pdf
MYSQL-database different functions pptx.pdf
viluThakkar
 
Data Base Management 1 Database Management.pptx
Data Base Management 1 Database Management.pptxData Base Management 1 Database Management.pptx
Data Base Management 1 Database Management.pptx
PreeTVithule1
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
pranav kumar verma
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
paddu123
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
paddu123
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
Abrar ali
 
Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statements
Mohd Tousif
 
SQL python for beginners easy explanation with concepts and output .pptx
SQL python for beginners easy explanation with concepts and output .pptxSQL python for beginners easy explanation with concepts and output .pptx
SQL python for beginners easy explanation with concepts and output .pptx
harshitagrawal2608
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
jainendraKUMAR55
 
MS SQL Server 1
MS SQL Server 1MS SQL Server 1
MS SQL Server 1
Iblesoft
 
98765432345671223Intro-to-PostgreSQL.ppt
98765432345671223Intro-to-PostgreSQL.ppt98765432345671223Intro-to-PostgreSQL.ppt
98765432345671223Intro-to-PostgreSQL.ppt
HastavaramDineshKuma
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
EllenGracePorras
 

Recently uploaded (20)

Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Massive Power Outage Hits Spain, Portugal, and France: Causes, Impact, and On...
Aqusag Technologies
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Ad

SQL PPT.pptx

  • 1. SQL
  • 2. DDL and DML command Categories of SQL Commands SQL commands can be classified into the following categories: 1. Data Definition Language (DDL) Commands permits database tables to be created or deleted. It also defines indices (keys), specifies links between tables, and imposes constraints on tables. Examples of DDL commands in SQL are: CREATE DATABASE - creates a new database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table 2. The Data Manipulation Language (DML) Commands:The query and update commands form the DML part of SQL: Examples of DDL commands are: SELECT - extracts data from a table UPDATE - updates data in a table DELETE - deletes data from a table INSERT INTO - inserts new data into a table
  • 3. Data type in MySQL Numeric Data Types: INTEGER or INT – up to 11 digit number without decimal. SMALLINT – up to 5 digit number without decimal. FLOAT (M,D) or DECIMAL(M,D) or NUMERIC(M,D) Stores Real numbers upto M digit length (including .) with D decimal places. e.g. Float (10,2) can store 1234567.89 Date & Time Data Types: DATE - Stores date in YYYY-MM-DD format. TIME - Stores time in HH:MM:SS format. String or Text Data Type: CHAR(Size) A fixed length string up to 255 characters. (default is 1) VARCHAR(Size) A variable length string up to 255 characters.
  • 4. RDBMS Terms • Domain: It is pool of values or the collection (set) of possible values from which the value for a column is derived. • Tables or Relation in a Database: Relational Databases store data or information in tables. A table is similar to a spreadsheet where data is stored in rows and columns. A table refers to a two- dimensional representation of data using rows and columns. • Record - The horizontal subset of the Table is known as a Row/Tuple. Each row represents a record, which is a collection of data about a particular entity such as person, place or thing. • Field - The vertical subset of the Table is known as a Column/Attribute. The term field is also often used for column. Each column has a unique name and the content within it must be of the same type. • For example, consider the following table named Customer with details about customers: • MySQL : It is an Open Source RDBMS Software. It is available free of cost.
  • 5. Keys Key : A column or a combination of columns which can be used to identify one or more rows (tuples) in a table is called a key of the table. Primary Key : The group of one or more columns used to uniquely identify each row of a relation is called its Primary Key. Candidate Key : A column or a group of columns which can be used as the primary key of a relation is called a Candidate key because it is one of the candidates available to be the primary key of the relation. Alternate Key : A candidate key of a table which is not selected as the primary key is called its Alternate Key. Example: Consider the following Table, RollNo and Admission_no both may be used to uniquely identify each row in this Table, so both are candidate keys. Candidate keys which are not made primary key are called Alternate keys. In the above example, if we use one of the candidate keys, say, Admission_No as the Primary Key, the other Candidate Key RollNo is the Alternate Key and vice-versa. Foreign Key : A primary key of a base table when used in some other table is called as Foreign Key. For example: Table Employee has columns : EMPID, EMPNAME, ADDRESS, CONTACT NO, DEPTID. And the table Department has columns : DEPTID, DNAME, CITY. Then the DEPTID column of the Employee table will be known as Foreign Key because it is declared as Primary Key in the Department table. Note : The primary key column and the foreign key column must have the same data type andsize.
  • 6. SQL - commands • Opening a database mysql> USE school ; • Getting listings of database and tables mysql> SHOW DATABASES; mysql> SHOW TABLES; • Deleting a Database and Table mysql> DROP DATABASE School; mysql> DROP TABLE STUDENT; • Viewing Table Structure mysql> DESC STUDENT; • Adding constraints in table Ex1. : CREATE TABLE Shoes(Code CHAR(4), Name VARCHAR(20), type VARCHAR(10),size INT(2), cost DECIMAL(6,2), margin DECIMAL(4,2), Qty INT(4)); Ex2.: Creating Tables & Inserting records mysql> Create table Student(ADMNO CHAR(10), NAME CHAR(20), AGE INT(3), PHONE_NO INT(10)); • Inserting records into the Table mysql> Insert into Student Values(‘JS0123’, ‘AJAY’, 13, 941234567); mysql> Insert into Student Values(‘JS0124’, ‘RAJANI’, 12, 991234567); mysql> Insert into Student Values(‘JS0125’, ‘PURVA’, 12, 900000001);
  • 7. SQL - commands • Creating Another Simple Tables: mysql> CREATE TABLE Empl (empno int(5), ename char(30), Job char(15), mgr int(5), hiredate Date, Sal float(10,2), comm int(6), deptno int(4)); • Inserting Records: mysql> INSERT INTO Empl VALUES (7369,’SMITH’,’CLERK’,7902,’1980-12-17’, 800.00,NULL,20); mysql> INSERT INTO Empl VALUES (7499,’ALLEN’,’SALESMAN’,7698,’1981-02-20’, 1600.00,300.00,30); mysql> INSERT INTO Empl VALUES (7521,’WARD’,’SALESMAN’,7698,’1981-02-22’, 1250.00,500.00,30); • Selecting all columns If you want to view all columns of the student table, then you should give the following command mysql> SELECT * FROM Empl;
  • 8. SQL - commands Selecting columns If you want to view only ename and job columns of the empl table MySql>select ename, job from empl;
  • 9. Modifying the structure of table When we create a table we define its structure. We can also change its structure i.e. add, remove or change its column(s) using the ALTER TABLE statement.ALTER TABLE is used to add a constraint,to remove a constraint,to remove a column from a table,to modify a table column. Syntax: • ALTER TABLE <table_name> ADD/DROP <column_name> [datatype]; • ALTER TABLE <table> MODIFY <column> <new_definition>; If we want to add a column named LOCATION in the EMPL table. mysql> ALTER TABLE EMPL ADD LOCATION CHAR(10);
  • 11. Modifying the structure of table Now, suppose we want to change the newly added LOCATION column to hold integers(inplace of character data) using ALTER TABLE statement:  mysql> ALTER TABLE EMPL MODIFY LOCATION INT(10);
  • 12. Modifying the structure of table • To delete a column of a table the ALTER TABLE statement is used with Drop clause. mysql> ALTER TABLE EMPL DROP LOCATION;
  • 13. Eliminating Duplicate values in a column - DISTINCT • mysql> select distinct job from empl;
  • 14. Doing simple calculations We can also perform simple calculations with SQL Select command. SQL provide a dummy table named DUAL, which can be used for this purpose. mysql> SELECT 4*3 ;
  • 15. Doing simple calculations We can also extend this idea with a columns of the existing table. mysql> SELECT Name, Sal *12 FROM EMPL ;
  • 16. Using Column Aliases We can give a different name to a column or expression (Alias) in the output of a query. mysql> SELECT 22/7 AS PI FROM Dual; mysql> SELECT Name, DOB AS ‘Date of Birth’ FROM Student; mysql> SELECT Name, Sal*12 AS ‘Annual Salary’ FROM EMPL;
  • 17. Handling Nulls Empty values are represented as Nulls in a table. mysql> select ename, job, comm from empl; See the null values appear as Null. If we want to substitute null with a value in the output, we can use IFNULL() function. Handling Nulls IFNULL() Syntax: IFNULL(columnname, value-to-be-substituted) • mysql> select ename, job, ifnull(comm, ‘Not Entitled’) from empl;
  • 18. Putting text in the query output: mysql> select ename, ‘ gets the commission ‘, comm from empl;
  • 19. SQL - commands • WHERE Clause: Where clause is used to fetch data based on a criteria/ condition. • Syntax:  SELECT <column name1> [,<column name> ,….] FROM <table name>WHERE <condition>; For example if we wish to display records of students who have a got marks greater then we will 100 then following command needs to be entered:  mysql> select fname, marks from student-> where marks > 100;
  • 20. SQL - commands • LIKE keyword : LIKE is used for pattern matching and is very useful. Following characters used for applying pattern matching: % percent symbol is used to match none or more characters _ underscore character is used to match occurrence of one character in the string For example : To search for records having first name starting with letter ‘R’; mysql>select * from student where fname like ‘A%’; • Command to display names that end with letter ‘K’ is mysql>select * from student where fname like ‘%K’; • Command to display list of all the students whose name has upto five characters: mysql>select * from student where fname like ‘_____’;
  • 21. SQL - commands Similarly many more patterns may be given as follows : ‘_ _ _ _’ matches any string with exactly four characters ‘S_ _ _ _’ matches any string of length of exactly 5 characters and starts with letter ‘S’ ‘S_ _ _ ‘S_ _ _%’ matches any string of length of 4 or more characters and starts with letter ‘S’ ‘_ _ _H matches any string of length of exactly 4 characters and terminates with letter ‘H’ ‘_ _ _ ‘_ _ _%’ matches any string with at least three or more characters ‘%in% matches any string which containing ‘in’