SlideShare uma empresa Scribd logo
1 de 29
Structured Query
Language – The Basics
Bhupendra Bahadur Shahi
What We’re Going to Cover
 Overview of SQL (This may be review for some of you)
 Data Definition Language
 Creating tables (we’ll just talk about this)
 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
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);
Inserting Data into a Table
INSERT INTO COURSE
VALUES (‘MIS499’,’ADVANCED ORACLE’,4);
INSERT INTO COURSE
(COURSE_NAME, COURSE_CODE,
CREDIT_HOURS)
VALUES (’ADVANCED ORACLE’,‘MIS499’,4);
More on Inserting Data
COLUMN LIST IS OPTIONAL IF YOU PLAN TO
INSERT A VALUE IN EVERY COLUMN AND IN
THE SAME ORDER AS IN THE TABLE
COLUMN LIST IS NEEDED
TO CHANGE THEORDER
- MUST MATCH VALUE LIST
NOTE - TABLE STILL HAS THE
ORIGINAL COLUMN ORDER
INSERT INTO COURSE
(COURSE_CODE, CREDIT_HOURS)
VALUES (‘MIS499’,4);
INSERT INTO COURSE
VALUES (‘MIS499’,’’,4);
INSERT INTO COURSE
VALUES (‘MIS499’,NULL,4);
COLUMN LIST IS NEEDED IF
YOU PLAN TO LEAVE OUT A
VALUE IN THE VALUE LIST
COLUMN LIST CAN BE OMITTED
IF YOU PUT IN A BLANK VALUE
THE NULL KEYWORD CAN
BE USED TO CREATE A BLANK
COLUMN
ALL OF THESE ASSUME THAT THE DATABASE ALLOWS THE COLUMN TO
BE NULL. YOU CANNOT LEAVE PRIMARY KEYS AND FOREIGN KEYS BLANK
Inserting Null Data
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
found SECTION
CALL_NUMBER KEY
COURSE_CODE
SECTION_TIME
SECTION_DAYS
SECTION_ROOM
INSTRUCTOR_ID
COURSE
COURSE_CODE KEY
COURSE_NAME
CREDIT_HOURS
INSTRUCTOR
INSTRUCTOR_ID KEY
INSTRUCTOR_NAME
INSTRUCTOR_OFFICE
Inserting and Integrity Constraints
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
DELETE COURSE;
DELETES ALL ROWS
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;
Be careful!! This deletes ALL of
the rows in your table. If you
use this command in error, you
can use ROLLBACK to undo
the changes.
Deleting Data
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
Deleting and Integrity Constraints
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 Data
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
Updating and Integrity Constraints
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
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
Rollback and 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.
SELECT * FROM COURSE
WHERE COURSE_CODE LIKE ‘MIS%’;
SELECT * FROM COURSE
WHERE CREDIT HOURS BETWEEN 3 AND 5;
SELECT * FROM CUSTOMER
WHERE BALANCE < CREDIT_LIMIT;
USE % TO SUBSTITUTE FOR
ANY STRING
3 AND 5 ARE INCLUDED
YOU CAN COMPARE TWO
COLUMNS
WHERE Conditions
More WHERE Conditions
SELECT * FROM CUSTOMER
WHERE STATE IN (‘OH’,’WV’,’KY’);
SELECT * FROM CUSTOMER
WHERE (CREDIT_LIMIT - BALANCE) <1000;
LIST OF SPECIFIC VALUES TO
LOOK FOR
CAN MANIPULATE NUMBER
VALUES MATHMATICALLY
SELECT * FROM CUSTOMER
WHERE BALANCE >=500
AND BALANCE<=1000;
SELECT * FROM CUSTOMER
WHERE STATE = ‘OH’
OR CREDIT_LIMIT>1000;
SELECT * FROM CUSTOMER
WHERE NOT (STATE=‘OH’);
TWO COMPARISONS
ON THE SAME COLUMN
TWO COMPARISONS
ON THE DIFFERENT
COLUMNS
SAME AS
STATE<>‘OH’
AND/OR/NOT Conditions
More on AND/OR/NOT
CUST STATE LIMIT BAL
A OH 1000 600
B WV 1000 200
C OH 500 300
D OH 1000 200
E KY 1300 800
F KY 1000 700
G MA 200 100
H NB 1000 100
Use parentheses to
make complex logic
more understandable.
SELECT * FROM CUSTOMER
WHERE STATE = ‘OH’
OR (CREDIT_LIMIT=1000
AND BALANCE <500);
Who will be selected??
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’;
Interesting, right?
This is just a sneak preview of the full presentation. We hope
you like it! To see the rest of it, just click here to view it in full
on PowerShow.com. Then, if you’d like, you can also log in to
PowerShow.com to download the entire presentation for free.

Mais conteúdo relacionado

Semelhante a SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx

My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxMy lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
EliasPetros
 
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptxhjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
EliasPetros
 
Sql intro
Sql introSql intro
Sql intro
glubox
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
PavithSingh
 

Semelhante a SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx (20)

SQL Tutorial for BCA-2
SQL Tutorial for BCA-2SQL Tutorial for BCA-2
SQL Tutorial for BCA-2
 
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxMy lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdf
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Assignment#02
Assignment#02Assignment#02
Assignment#02
 
Oracle Material.pdf
Oracle Material.pdfOracle Material.pdf
Oracle Material.pdf
 
01 basic orders
01   basic orders01   basic orders
01 basic orders
 
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptxhjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
 
chapter9-SQL.pptx
chapter9-SQL.pptxchapter9-SQL.pptx
chapter9-SQL.pptx
 
Ch 9 S Q L
Ch 9  S Q LCh 9  S Q L
Ch 9 S Q L
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
lovely
lovelylovely
lovely
 
Sql intro
Sql introSql intro
Sql intro
 
SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data   SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data
 
Sql
SqlSql
Sql
 
Oracle Baisc Tutorial
Oracle Baisc TutorialOracle Baisc Tutorial
Oracle Baisc Tutorial
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
chap 7.ppt(sql).ppt
chap 7.ppt(sql).pptchap 7.ppt(sql).ppt
chap 7.ppt(sql).ppt
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
 

Último

Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
amitlee9823
 
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
amitlee9823
 
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts ServiceVVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
aroranaina404
 
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman MuscatAbortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion pills in Kuwait Cytotec pills in Kuwait
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
amitlee9823
 
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
nirzagarg
 
infant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptxinfant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptx
suhanimunjal27
 
DESIGN THINKING in architecture- Introduction
DESIGN THINKING in architecture- IntroductionDESIGN THINKING in architecture- Introduction
DESIGN THINKING in architecture- Introduction
sivagami49
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation deck
tbatkhuu1
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
B. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdfB. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdf
University of Wisconsin-Milwaukee
 

Último (20)

UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
 
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
 
Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
 
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts ServiceVVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
 
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman MuscatAbortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
 
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
 
infant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptxinfant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptx
 
DESIGN THINKING in architecture- Introduction
DESIGN THINKING in architecture- IntroductionDESIGN THINKING in architecture- Introduction
DESIGN THINKING in architecture- Introduction
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation deck
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
B. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdfB. Smith. (Architectural Portfolio.).pdf
B. Smith. (Architectural Portfolio.).pdf
 
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
 
Case Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneCase Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, Pune
 

SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx

  • 1. Structured Query Language – The Basics Bhupendra Bahadur Shahi
  • 2. What We’re Going to Cover  Overview of SQL (This may be review for some of you)  Data Definition Language  Creating tables (we’ll just talk about this)  Data Manipulation Language  Inserting/Updating/Deleting data  Retrieving data  Single table queries  Where  Joins  Grouping
  • 3. 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.
  • 4. 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.
  • 5. Data Manipulation Language (DML) Four basic commands:  INSERT  UPDATE  DELETE  SELECT
  • 6. 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); Inserting Data into a Table
  • 7. INSERT INTO COURSE VALUES (‘MIS499’,’ADVANCED ORACLE’,4); INSERT INTO COURSE (COURSE_NAME, COURSE_CODE, CREDIT_HOURS) VALUES (’ADVANCED ORACLE’,‘MIS499’,4); More on Inserting Data COLUMN LIST IS OPTIONAL IF YOU PLAN TO INSERT A VALUE IN EVERY COLUMN AND IN THE SAME ORDER AS IN THE TABLE COLUMN LIST IS NEEDED TO CHANGE THEORDER - MUST MATCH VALUE LIST NOTE - TABLE STILL HAS THE ORIGINAL COLUMN ORDER
  • 8. INSERT INTO COURSE (COURSE_CODE, CREDIT_HOURS) VALUES (‘MIS499’,4); INSERT INTO COURSE VALUES (‘MIS499’,’’,4); INSERT INTO COURSE VALUES (‘MIS499’,NULL,4); COLUMN LIST IS NEEDED IF YOU PLAN TO LEAVE OUT A VALUE IN THE VALUE LIST COLUMN LIST CAN BE OMITTED IF YOU PUT IN A BLANK VALUE THE NULL KEYWORD CAN BE USED TO CREATE A BLANK COLUMN ALL OF THESE ASSUME THAT THE DATABASE ALLOWS THE COLUMN TO BE NULL. YOU CANNOT LEAVE PRIMARY KEYS AND FOREIGN KEYS BLANK Inserting Null Data
  • 9. 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 found SECTION CALL_NUMBER KEY COURSE_CODE SECTION_TIME SECTION_DAYS SECTION_ROOM INSTRUCTOR_ID COURSE COURSE_CODE KEY COURSE_NAME CREDIT_HOURS INSTRUCTOR INSTRUCTOR_ID KEY INSTRUCTOR_NAME INSTRUCTOR_OFFICE Inserting and Integrity Constraints
  • 10. 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
  • 11. DELETE COURSE; DELETES ALL ROWS 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; Be careful!! This deletes ALL of the rows in your table. If you use this command in error, you can use ROLLBACK to undo the changes. Deleting Data
  • 12. 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 Deleting and Integrity Constraints
  • 13. 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 Data
  • 14. 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 Updating and Integrity Constraints
  • 15. 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
  • 16. 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
  • 17. 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 Rollback and Commit
  • 18. 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.
  • 19. 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.
  • 20. SELECT * FROM COURSE WHERE COURSE_CODE LIKE ‘MIS%’; SELECT * FROM COURSE WHERE CREDIT HOURS BETWEEN 3 AND 5; SELECT * FROM CUSTOMER WHERE BALANCE < CREDIT_LIMIT; USE % TO SUBSTITUTE FOR ANY STRING 3 AND 5 ARE INCLUDED YOU CAN COMPARE TWO COLUMNS WHERE Conditions
  • 21. More WHERE Conditions SELECT * FROM CUSTOMER WHERE STATE IN (‘OH’,’WV’,’KY’); SELECT * FROM CUSTOMER WHERE (CREDIT_LIMIT - BALANCE) <1000; LIST OF SPECIFIC VALUES TO LOOK FOR CAN MANIPULATE NUMBER VALUES MATHMATICALLY
  • 22. SELECT * FROM CUSTOMER WHERE BALANCE >=500 AND BALANCE<=1000; SELECT * FROM CUSTOMER WHERE STATE = ‘OH’ OR CREDIT_LIMIT>1000; SELECT * FROM CUSTOMER WHERE NOT (STATE=‘OH’); TWO COMPARISONS ON THE SAME COLUMN TWO COMPARISONS ON THE DIFFERENT COLUMNS SAME AS STATE<>‘OH’ AND/OR/NOT Conditions
  • 23. More on AND/OR/NOT CUST STATE LIMIT BAL A OH 1000 600 B WV 1000 200 C OH 500 300 D OH 1000 200 E KY 1300 800 F KY 1000 700 G MA 200 100 H NB 1000 100 Use parentheses to make complex logic more understandable. SELECT * FROM CUSTOMER WHERE STATE = ‘OH’ OR (CREDIT_LIMIT=1000 AND BALANCE <500); Who will be selected??
  • 24. 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 ...
  • 25. SQL - Other Features  DISTINCT  Arithmetic operators: +, -, *, /  Comparison operators: =, >, >=, <, <=, <>  Concatenation operator: ||  Substring comparisons: %, _  BETWEEN  AND, OR
  • 26. SQL - Other Features  ORDER BY Clause  UNION, EXCEPT, INTERSECT  IN
  • 27. 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.
  • 28. 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’;
  • 29. Interesting, right? This is just a sneak preview of the full presentation. We hope you like it! To see the rest of it, just click here to view it in full on PowerShow.com. Then, if you’d like, you can also log in to PowerShow.com to download the entire presentation for free.