Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
41 views

Structured Query Language - The Basics: by Ved Prakash

The document provides an overview of structured query language (SQL) including the basics of SQL commands. It discusses the three main types of SQL commands: data definition language (DDL) for defining databases, data manipulation language (DML) for maintaining and querying databases, and data control language (DCL) for administering privileges and committing data. It also covers key SQL concepts like inserting, updating, deleting, and retrieving data from single and multiple tables using joins and subqueries.

Uploaded by

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

Structured Query Language - The Basics: by Ved Prakash

The document provides an overview of structured query language (SQL) including the basics of SQL commands. It discusses the three main types of SQL commands: data definition language (DDL) for defining databases, data manipulation language (DML) for maintaining and querying databases, and data control language (DCL) for administering privileges and committing data. It also covers key SQL concepts like inserting, updating, deleting, and retrieving data from single and multiple tables using joins and subqueries.

Uploaded by

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

Structured Query

Language – The Basics


By Ved Prakash
INTRODUCTION
Microsoft's SQL server is among the biggest and most broadly utilized

database management frameworks in the entire world. Practically the

majority of the greatest names in tech use SQL. Uber, Netflix, Airbnb — the

rundown goes on. Indeed, even inside organizations like Facebook, Google,

and Amazon, which have fabricated their very own superior database

frameworks, information groups use SQL to question information and

perform analytics on it.


What We’re Going to Cover
 Overview of SQL
 Data Definition Language
 Creating tables
 Data Manipulation Language
 Inserting/Updating/Deleting data
 Retrieving data
 Single table queries
 Where
 Joins
 Grouping
SQL
 SQL is a data manipulation language.
 SQL is not a programming language.
 SQL commands are interpreted by the
DBMS engine.
 SQL commands can be used
interactively as a query language within
the DBMS.
 SQL commands can be embedded
within programming languages.
3 Types of SQL Commands
 Data Definition Language (DDL):
 Commands that define a database - Create,
Alter, Drop
 Data Manipulation Language (DML)
 Commands that maintain and query a
database.
 Data Control Language (DCL)
 Commands that control a database, including
administering privileges and committing data.
Data Manipulation Language (DML)
Four basic commands:
 INSERT
 UPDATE
 DELETE
 SELECT
Inserting Data into a Table
INSERT INTO tablename (column-list)
VALUES (value-list)

PUTS ONE ROW INTO A TABLE

INSERT INTO COURSE


(COURSE_CODE, COURSE_NAME,
CREDIT_HOURS)
VALUES (‘MIS499’,’ADVANCED ORACLE’,4);
More on Inserting Data
INSERT INTO COURSE
VALUES (‘MIS499’,’ADVANCED ORACLE’,4);
COLUMN LIST IS OPTIONAL IF YOU PLAN TO
INSERT A VALUE IN EVERY COLUMN AND IN
THE SAME ORDER AS IN THE TABLE

INSERT INTO COURSE


(COURSE_NAME, COURSE_CODE,
CREDIT_HOURS)
VALUES (’ADVANCED ORACLE’,‘MIS499’,4);
COLUMN LIST IS NEEDED
TO CHANGE THEORDER NOTE - TABLE STILL HAS THE
- MUST MATCH VALUE LIST ORIGINAL COLUMN ORDER
Inserting Null Data
INSERT INTO COURSE
(COURSE_CODE, CREDIT_HOURS)
VALUES (‘MIS499’,4); COLUMN LIST IS NEEDED IF
YOU PLAN TO LEAVE OUT A
VALUE IN THE VALUE LIST

INSERT INTO COURSE COLUMN LIST CAN BE OMITTED


VALUES (‘MIS499’,’’,4); IF YOU PUT IN A BLANK VALUE

THE NULL KEYWORD CAN


INSERT INTO COURSE BE USED TO CREATE A BLANK
COLUMN
VALUES (‘MIS499’,NULL,4);

ALL OF THESE ASSUME THAT THE DATABASE ALLOWS THE COLUMN TO


BE NULL. YOU CANNOT LEAVE PRIMARY KEYS AND FOREIGN KEYS BLANK
Inserting and Integrity Constraints
SQL> INSERT INTO SECTION VALUES
('1234','MIS333','10-12','MW','COPE101','200000000');
INSERT INTO SECTION
VALUES ('1234','MIS333','10-12','MW','COPE101',
*
ERROR at line 1:
ORA-02291: integrity constraint (ORA40.SYS_C00337)
violated - parent key not COURSE

found SECTION COURSE_CODE KEY


COURSE_NAME
CALL_NUMBER KEY CREDIT_HOURS
COURSE_CODE
SECTION_TIME INSTRUCTOR
SECTION_DAYS
SECTION_ROOM INSTRUCTOR_ID KEY
INSTRUCTOR_ID INSTRUCTOR_NAME
INSTRUCTOR_OFFICE
Entity Integrity Problems
SQL> INSERT INTO COURSE
VALUES ('MIS220','NEW',4);

insert into course values ('MIS220','NEW',4)


*
ERROR at line 1:
ORA-00001: unique constraint
(ORA40.SYS_C00335) violated
Deleting Data
Be careful!! This deletes ALL of
DELETE COURSE; the rows in your table. If you
DELETES ALL ROWS use this command in error, you
can use ROLLBACK to undo
the changes.
DELETE COURSE WHERE COURSE_CODE =
‘MIS220’;
DELETES SPECIFIC ROWS (MORE TYPICAL)

DELETE COURSE WHERE HOURS=4;


DELETES A GROUP OF ROWS
DELETE COURSE WHERE HOURS<4;
Deleting and Integrity Constraints
SQL> DELETE COURSE
WHERE COURSE_CODE='MIS220';

DELETE COURSE WHERE


COURSE_CODE='MIS220'
*
ERROR at line 1:
ORA-02292: integrity constraint
(ORA40.SYS_C00341) violated - child record
found
Updating Data
UPDATE COURSE SET HOURS=5;
CHANGES EVERY ROW

UPDATE COURSE SET HOURS=5


WHERE COURSE_CODE=‘MIS220’
CHANGES ONE ROW (MORE TYPICAL)

UPDATE COURSE SET HOURS=3


WHERE COURSE_CODE LIKE ‘MIS%’
CHANGES A GROUP OF ROWS
Updating and Integrity Constraints
YOU CAN CHANGE THE VALUE OF A FOREIGN
KEY AS LONG AS THE NEW VALUE ALSO
COMPLIES WITH REFERENTIAL INTEGRITY
CONSTRAINTS.

PRIMARY KEY VALUES CAN BE UPDATED AS


LONG AS THERE ARE NO ROWS IN OTHER
TABLES WITH FOREIGN KEYS WITH THE
SAME VALUE

DOES NOT MATTER IF CONSTRAINT IS


RESTRICTED OR CASCADED
Integrity Error
SQL> UPDATE COURSE
SET COURSE_CODE='MIS221‘
WHERE COURSE_CODE='MIS220';
UPDATE COURSE
*
ERROR at line 1:
ORA-02292: integrity constraint (ORA40.SYS_C00341)
violated - child record found
Rollback and Commit
CHANGES TO DATA ARE TEMPORARY
DURING YOUR SQLPLUS SESSION

DOES NOT APPLY TO CHANGES IN


DATABASE STRUCTURE - ALTER...

BEFORE LEAVING SQLPLUS, YOU CAN


REVERSE THEM

APPLIES TO INSERTS, UPDATES, AND DELETES


Rollback and Commit
SQL>ROLLBACK;
Rollback complete.
REVERSES ALL CHANGES TO DATA MADE
DURING YOUR SESSION
SQL>COMMIT;
• MAKES ALL CHANGES TO THIS POINT
PERMANENT
• POINTS AT WHICH COMMIT IS ISSUED,
DEFINE EXTENT OF ROLLBACK
• ROLLBACK REVERSES EVERY CHANGE
SINCE THE LAST COMMIT
• EXITING SQLPLUS ISSUES A COMMIT
SQL for Retrieving Data from One
Table
SELECT column_name, column_name, …
FROM table_name
WHERE condition/criteria;

 This statement will retrieve the specified field


values for all rows in the specified table that meet
the specified conditions.
 Every SELECT statement returns a recordset.
Conceptual Evaluation Strategy
 Semantics of an SQL query defined in terms
of the following conceptual evaluation
strategy:
 Compute the cross-product of relation-list.
 Discard resulting tuples if they fail qualifications.
 Delete attributes that are not in target-list.
 If DISTINCT is specified, eliminate duplicate rows.
 This strategy is probably the least efficient
way to compute a query! An optimizer will
find more efficient strategies to compute the
same answers.
WHERE Conditions
SELECT * FROM COURSE
WHERE COURSE_CODE LIKE ‘MIS%’;
USE % TO SUBSTITUTE FOR
ANY STRING

SELECT * FROM COURSE


WHERE CREDIT HOURS BETWEEN 3 AND 5;
3 AND 5 ARE INCLUDED

SELECT * FROM CUSTOMER


WHERE BALANCE < CREDIT_LIMIT;
YOU CAN COMPARE TWO
COLUMNS
More WHERE Conditions
SELECT * FROM CUSTOMER
WHERE STATE IN (‘OH’,’WV’,’KY’);
LIST OF SPECIFIC VALUES TO
LOOK FOR

SELECT * FROM CUSTOMER


WHERE (CREDIT_LIMIT - BALANCE) <1000;

CAN MANIPULATE NUMBER


VALUES MATHMATICALLY
AND/OR/NOT Conditions
SELECT * FROM CUSTOMER
WHERE BALANCE >=500
TWO COMPARISONS
AND BALANCE<=1000;
ON THE SAME COLUMN

SELECT * FROM CUSTOMER TWO COMPARISONS


WHERE STATE = ‘OH’ ON THE DIFFERENT
OR CREDIT_LIMIT>1000; COLUMNS

SELECT * FROM CUSTOMER SAME AS


WHERE NOT (STATE=‘OH’); STATE<>‘OH’
More on AND/OR/NOT
SELECT * FROM CUSTOMER
Use parentheses to
WHERE STATE = ‘OH’
make complex logic
OR (CREDIT_LIMIT=1000
more understandable.
AND BALANCE <500);
CUST STATE LIMIT BAL
A OH 1000 600
B WV 1000 200
C OH 500 300 Who will be selected??
D OH 1000 200
E KY 1300 800
F KY 1000 700
G MA 200 100
H NB 1000 100
SQL - Other Features
 * - All columns in a table
 Aliases
 SELECT EmployeeID, LastName, FirstName,
BirthDate AS DOB FROM Employee;
 SELECT EmployeeID, LastName, FirstName, FROM
Employee AS E;
 Dot Notation - ambiguous attribute names
 SELECT Customer.LName, E.Lname
FROM Customer, Employee AS E
WHERE ...
SQL - Other Features
 DISTINCT
 Arithmetic operators: +, -, *, /
 Comparison operators: =, >, >=, <, <=, <>
 Concatenation operator: ||
 Substring comparisons: %, _
 BETWEEN
 AND, OR
SQL - Other Features
 ORDER BY Clause
 UNION, EXCEPT, INTERSECT
 IN
SQL for Retrieving Data from Two
or More Tables
SQL provides two ways to retrieve data from
related tables:
 Join - When two or more tables are joined by
a common field.
 Subqueries - When one Select command is
nested within another command.
SQL - Joins
Joins:
 The WHERE clause is used to specify the
common field.
 For every relationship among the tables in the
FROM clause, you need one WHERE condition (2
tables - 1 join, 3 tables - 2 joins…)

SELECT C.Cust_ID, Comp_Name, Country,OrderID


FROM Customer AS C, Order AS O
WHERE C.Cust_ID = O.Cust_ID
AND Country = ‘USA’;

You might also like