SlideShare a Scribd company logo
1 of 43
Download to read offline
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 4
Basic SQL
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Chapter 4 Outline
  SQL Data Definition and Data Types
  Specifying Constraints in SQL
  Basic Retrieval Queries in SQL
  INSERT, DELETE, and UPDATE Statements
in SQL
  Additional Features of SQL
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Basic SQL
  SQL language
  Considered one of the major reasons for the
commercial success of relational databases
  SQL
  Structured Query Language
  Statements for data definitions, queries, and
updates (both DDL and DML)
  Core specification
  Plus specialized extensions
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
SQL Data Definition and Data
Types
  Terminology:
  Table, row, and column used for relational
model terms relation, tuple, and attribute
  CREATE statement
  Main SQL command for data definition
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Schema and Catalog Concepts
in SQL
  SQL schema
  Identified by a schema name
  Includes an authorization identifier and
descriptors for each element
  Schema elements include
  Tables, constraints, views, domains, and other
constructs
  Each statement in SQL ends with a
semicolon
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Schema and Catalog Concepts
in SQL (cont’d.)
  CREATE SCHEMA statement
  CREATE SCHEMA COMPANY AUTHORIZATION
‘Jsmith’;
  Catalog
  Named collection of schemas in an SQL
environment
  SQL environment
  Installation of an SQL-compliant RDBMS on a
computer system
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The CREATE TABLE Command
in SQL
  Specify a new relation
  Provide name
  Specify attributes and initial constraints
  Can optionally specify schema:
  CREATE TABLE COMPANY.EMPLOYEE ...
or
  CREATE TABLE EMPLOYEE ...
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The CREATE TABLE Command
in SQL (cont’d.)
  Base tables (base relations)
  Relation and its tuples are actually created and
stored as a file by the DBMS
  Virtual relations
  Created through the CREATE VIEW statement
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The CREATE TABLE Command
in SQL (cont’d.)
  Some foreign keys may cause errors
  Specified either via:
•  Circular references
•  Or because they refer to a table that has not yet
been created
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Attribute Data Types and
Domains in SQL
  Basic data types
  Numeric data types
•  Integer numbers: INTEGER, INT, and SMALLINT
•  Floating-point (real) numbers: FLOAT or REAL, and
DOUBLE PRECISION
  Character-string data types
•  Fixed length: CHAR(n), CHARACTER(n)
•  Varying length: VARCHAR(n), CHAR VARYING
(n), CHARACTER VARYING(n)
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Attribute Data Types and
Domains in SQL (cont’d.)
  Bit-string data types
•  Fixed length: BIT(n)
•  Varying length: BIT VARYING(n)
  Boolean data type
•  Values of TRUE or FALSE or NULL
  DATE data type
•  Ten positions
•  Components are YEAR, MONTH, and DAY in the form
YYYY-MM-DD
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Attribute Data Types and
Domains in SQL (cont’d.)
  Additional data types
  Timestamp data type (TIMESTAMP)
•  Includes the DATE and TIME fields
•  Plus a minimum of six positions for decimal fractions
of seconds
•  Optional WITH TIME ZONE qualifier
  INTERVAL data type
•  Specifies a relative value that can be used to
increment or decrement an absolute value of a date,
time, or timestamp
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Attribute Data Types and
Domains in SQL (cont’d.)
  Domain
  Name used with the attribute specification
  Makes it easier to change the data type for a
domain that is used by numerous attributes
  Improves schema readability
  Example:
•  CREATE DOMAIN SSN_TYPE AS CHAR(9);
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Specifying Constraints in SQL
  Basic constraints:
  Key and referential integrity constraints
  Restrictions on attribute domains and NULLs
  Constraints on individual tuples within a
relation
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Specifying Attribute Constraints
and Attribute Defaults
  NOT NULL
  NULL is not permitted for a particular attribute
  Default value
  DEFAULT <value>
  CHECK clause
  Dnumber INT NOT NULL CHECK (Dnumber
> 0 AND Dnumber < 21);
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Specifying Key and Referential
Integrity Constraints
  PRIMARY KEY clause
  Specifies one or more attributes that make up
the primary key of a relation
  Dnumber INT PRIMARY KEY;
  UNIQUE clause
  Specifies alternate (secondary) keys
  Dname VARCHAR(15) UNIQUE;
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Specifying Key and Referential
Integrity Constraints (cont’d.)
  FOREIGN KEY clause
  Default operation: reject update on violation
  Attach referential triggered action clause
•  Options include SET NULL, CASCADE, and SET
DEFAULT
•  Action taken by the DBMS for SET NULL or SET
DEFAULT is the same for both ON DELETE and ON
UPDATE
•  CASCADE option suitable for “relationship” relations
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Giving Names to Constraints
  Keyword CONSTRAINT
  Name a constraint
  Useful for later altering
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Specifying Constraints on Tuples
Using CHECK
  CHECK clauses at the end of a CREATE
TABLE statement
  Apply to each tuple individually
  CHECK (Dept_create_date <=
Mgr_start_date);
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Basic Retrieval Queries in SQL
  SELECT statement
  One basic statement for retrieving information
from a database
  SQL allows a table to have two or more
tuples that are identical in all their attribute
values
  Unlike relational model
  Multiset or bag behavior
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The SELECT-FROM-WHERE
Structure of Basic SQL Queries
  Basic form of the SELECT statement:
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The SELECT-FROM-WHERE
Structure of Basic SQL Queries
(cont’d.)
  Logical comparison operators
  =, <, <=, >, >=, and <>
  Projection attributes
  Attributes whose values are to be retrieved
  Selection condition
  Boolean condition that must be true for any
retrieved tuple
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Ambiguous Attribute Names
  Same name can be used for two (or more)
attributes
  As long as the attributes are in different
relations
  Must qualify the attribute name with the
relation name to prevent ambiguity
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Aliasing, Renaming, and Tuple
Variables
  Aliases or tuple variables
  Declare alternative relation names E and S
  EMPLOYEE AS E(Fn, Mi, Ln, Ssn, Bd,
Addr, Sex, Sal, Sssn, Dno)
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Unspecified WHERE Clause
and Use of the Asterisk
  Missing WHERE clause
  Indicates no condition on tuple selection
  CROSS PRODUCT
  All possible tuple combinations
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Unspecified WHERE Clause
and Use of the Asterisk (cont’d.)
  Specify an asterisk (*)
  Retrieve all the attribute values of the selected
tuples
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Tables as Sets in SQL
  SQL does not automatically eliminate
duplicate tuples in query results
  Use the keyword DISTINCT in the SELECT
clause
  Only distinct tuples should remain in the result
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Tables as Sets in SQL (cont’d.)
  Set operations
  UNION, EXCEPT (difference), INTERSECT
  Corresponding multiset operations: UNION
ALL, EXCEPT ALL, INTERSECT ALL)
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Substring Pattern Matching and
Arithmetic Operators
  LIKE comparison operator
  Used for string pattern matching
  % replaces an arbitrary number of zero or more
characters
  underscore (_) replaces a single character
  Standard arithmetic operators:
  Addition (+), subtraction (–), multiplication (*),
and division (/)
  BETWEEN comparison operator
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Ordering of Query Results
  Use ORDER BY clause
  Keyword DESC to see result in a descending
order of values
  Keyword ASC to specify ascending order
explicitly
  ORDER BY D.Dname DESC, E.Lname ASC,
E.Fname ASC
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Discussion and Summary
of Basic SQL Retrieval Queries
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
INSERT, DELETE, and UPDATE
Statements in SQL
  Three commands used to modify the
database:
  INSERT, DELETE, and UPDATE
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The INSERT Command
  Specify the relation name and a list of
values for the tuple
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The DELETE Command
  Removes tuples from a relation
  Includes a WHERE clause to select the tuples to
be deleted
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
The UPDATE Command
  Modify attribute values of one or more
selected tuples
  Additional SET clause in the UPDATE
command
  Specifies attributes to be modified and new
values
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Additional Features of SQL
  Techniques for specifying complex retrieval
queries
  Writing programs in various programming
languages that include SQL statements
  Set of commands for specifying physical
database design parameters, file structures
for relations, and access paths
  Transaction control commands
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Additional Features of SQL
(cont’d.)
  Specifying the granting and revoking of
privileges to users
  Constructs for creating triggers
  Enhanced relational systems known as
object-relational
  New technologies such as XML and OLAP
Copyright © 2011 Ramez Elmasri and Shamkant Navathe
Summary
  SQL
  Comprehensive language
  Data definition, queries, updates, constraint
specification, and view definition
  Covered in Chapter 4:
  Data definition commands for creating tables
  Commands for constraint specification
  Simple retrieval queries
  Database update commands

More Related Content

What's hot (20)

Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | Edureka
 
Triggers in SQL | Edureka
Triggers in SQL | EdurekaTriggers in SQL | Edureka
Triggers in SQL | Edureka
 
advanced sql(database)
advanced sql(database)advanced sql(database)
advanced sql(database)
 
Sql commands
Sql commandsSql commands
Sql commands
 
Sql views
Sql viewsSql views
Sql views
 
SQL
SQLSQL
SQL
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 
Primary Key & Foreign Key part10
Primary Key & Foreign Key part10Primary Key & Foreign Key part10
Primary Key & Foreign Key part10
 
Trigger
TriggerTrigger
Trigger
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Cursors
CursorsCursors
Cursors
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
Aggregate Function - Database
Aggregate Function - DatabaseAggregate Function - Database
Aggregate Function - Database
 
SQL Constraints
SQL ConstraintsSQL Constraints
SQL Constraints
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Plsql
PlsqlPlsql
Plsql
 

Similar to Learn SQL basics for database management

Similar to Learn SQL basics for database management (20)

Ch05
Ch05Ch05
Ch05
 
chapter9-SQL.pptx
chapter9-SQL.pptxchapter9-SQL.pptx
chapter9-SQL.pptx
 
Chapter8
Chapter8Chapter8
Chapter8
 
Chapter06.ppt
Chapter06.pptChapter06.ppt
Chapter06.ppt
 
chap1.pdf
chap1.pdfchap1.pdf
chap1.pdf
 
Sql 2006
Sql 2006Sql 2006
Sql 2006
 
lovely
lovelylovely
lovely
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
Sql select statement
Sql select statementSql select statement
Sql select statement
 
chapter-14-sql-commands.pdf
chapter-14-sql-commands.pdfchapter-14-sql-commands.pdf
chapter-14-sql-commands.pdf
 
SQL Query
SQL QuerySQL Query
SQL Query
 
SQL-examples.ppt
SQL-examples.pptSQL-examples.ppt
SQL-examples.ppt
 
SQL-examples.ppt
SQL-examples.pptSQL-examples.ppt
SQL-examples.ppt
 
SQL-examples.ppt
SQL-examples.pptSQL-examples.ppt
SQL-examples.ppt
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
Fg d
Fg dFg d
Fg d
 
ENCh05.ppt
ENCh05.pptENCh05.ppt
ENCh05.ppt
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 

More from cs19club

Podd notes
Podd notesPodd notes
Podd notescs19club
 
Podd note 1
Podd note 1Podd note 1
Podd note 1cs19club
 
Oodp extra2
Oodp extra2Oodp extra2
Oodp extra2cs19club
 
Module5 part2
Module5 part2Module5 part2
Module5 part2cs19club
 
Module 5 part1
Module 5 part1Module 5 part1
Module 5 part1cs19club
 
Floating point arithmetic operations (1)
Floating point arithmetic operations (1)Floating point arithmetic operations (1)
Floating point arithmetic operations (1)cs19club
 
Coa module2
Coa module2Coa module2
Coa module2cs19club
 
Addition and subtraction with signed magnitude data (mano
Addition and subtraction with signed magnitude data (manoAddition and subtraction with signed magnitude data (mano
Addition and subtraction with signed magnitude data (manocs19club
 
Coa module1
Coa module1Coa module1
Coa module1cs19club
 

More from cs19club (16)

Podd notes
Podd notesPodd notes
Podd notes
 
Podd note 1
Podd note 1Podd note 1
Podd note 1
 
Podd mod6
Podd mod6Podd mod6
Podd mod6
 
Module 3
Module 3Module 3
Module 3
 
Oodp extra2
Oodp extra2Oodp extra2
Oodp extra2
 
Oodp mod4
Oodp mod4Oodp mod4
Oodp mod4
 
Module vi
Module viModule vi
Module vi
 
Module5 part2
Module5 part2Module5 part2
Module5 part2
 
Module 5 part1
Module 5 part1Module 5 part1
Module 5 part1
 
Module4
Module4Module4
Module4
 
Io pro
Io proIo pro
Io pro
 
Io pro
Io proIo pro
Io pro
 
Floating point arithmetic operations (1)
Floating point arithmetic operations (1)Floating point arithmetic operations (1)
Floating point arithmetic operations (1)
 
Coa module2
Coa module2Coa module2
Coa module2
 
Addition and subtraction with signed magnitude data (mano
Addition and subtraction with signed magnitude data (manoAddition and subtraction with signed magnitude data (mano
Addition and subtraction with signed magnitude data (mano
 
Coa module1
Coa module1Coa module1
Coa module1
 

Recently uploaded

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 

Learn SQL basics for database management

  • 1. Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL
  • 2. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Chapter 4 Outline   SQL Data Definition and Data Types   Specifying Constraints in SQL   Basic Retrieval Queries in SQL   INSERT, DELETE, and UPDATE Statements in SQL   Additional Features of SQL
  • 3. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Basic SQL   SQL language   Considered one of the major reasons for the commercial success of relational databases   SQL   Structured Query Language   Statements for data definitions, queries, and updates (both DDL and DML)   Core specification   Plus specialized extensions
  • 4. Copyright © 2011 Ramez Elmasri and Shamkant Navathe SQL Data Definition and Data Types   Terminology:   Table, row, and column used for relational model terms relation, tuple, and attribute   CREATE statement   Main SQL command for data definition
  • 5. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Schema and Catalog Concepts in SQL   SQL schema   Identified by a schema name   Includes an authorization identifier and descriptors for each element   Schema elements include   Tables, constraints, views, domains, and other constructs   Each statement in SQL ends with a semicolon
  • 6. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Schema and Catalog Concepts in SQL (cont’d.)   CREATE SCHEMA statement   CREATE SCHEMA COMPANY AUTHORIZATION ‘Jsmith’;   Catalog   Named collection of schemas in an SQL environment   SQL environment   Installation of an SQL-compliant RDBMS on a computer system
  • 7. Copyright © 2011 Ramez Elmasri and Shamkant Navathe The CREATE TABLE Command in SQL   Specify a new relation   Provide name   Specify attributes and initial constraints   Can optionally specify schema:   CREATE TABLE COMPANY.EMPLOYEE ... or   CREATE TABLE EMPLOYEE ...
  • 8. Copyright © 2011 Ramez Elmasri and Shamkant Navathe The CREATE TABLE Command in SQL (cont’d.)   Base tables (base relations)   Relation and its tuples are actually created and stored as a file by the DBMS   Virtual relations   Created through the CREATE VIEW statement
  • 9. Copyright © 2011 Ramez Elmasri and Shamkant Navathe
  • 10. Copyright © 2011 Ramez Elmasri and Shamkant Navathe
  • 11. Copyright © 2011 Ramez Elmasri and Shamkant Navathe The CREATE TABLE Command in SQL (cont’d.)   Some foreign keys may cause errors   Specified either via: •  Circular references •  Or because they refer to a table that has not yet been created
  • 12. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Attribute Data Types and Domains in SQL   Basic data types   Numeric data types •  Integer numbers: INTEGER, INT, and SMALLINT •  Floating-point (real) numbers: FLOAT or REAL, and DOUBLE PRECISION   Character-string data types •  Fixed length: CHAR(n), CHARACTER(n) •  Varying length: VARCHAR(n), CHAR VARYING (n), CHARACTER VARYING(n)
  • 13. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Attribute Data Types and Domains in SQL (cont’d.)   Bit-string data types •  Fixed length: BIT(n) •  Varying length: BIT VARYING(n)   Boolean data type •  Values of TRUE or FALSE or NULL   DATE data type •  Ten positions •  Components are YEAR, MONTH, and DAY in the form YYYY-MM-DD
  • 14. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Attribute Data Types and Domains in SQL (cont’d.)   Additional data types   Timestamp data type (TIMESTAMP) •  Includes the DATE and TIME fields •  Plus a minimum of six positions for decimal fractions of seconds •  Optional WITH TIME ZONE qualifier   INTERVAL data type •  Specifies a relative value that can be used to increment or decrement an absolute value of a date, time, or timestamp
  • 15. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Attribute Data Types and Domains in SQL (cont’d.)   Domain   Name used with the attribute specification   Makes it easier to change the data type for a domain that is used by numerous attributes   Improves schema readability   Example: •  CREATE DOMAIN SSN_TYPE AS CHAR(9);
  • 16. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Specifying Constraints in SQL   Basic constraints:   Key and referential integrity constraints   Restrictions on attribute domains and NULLs   Constraints on individual tuples within a relation
  • 17. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Specifying Attribute Constraints and Attribute Defaults   NOT NULL   NULL is not permitted for a particular attribute   Default value   DEFAULT <value>   CHECK clause   Dnumber INT NOT NULL CHECK (Dnumber > 0 AND Dnumber < 21);
  • 18. Copyright © 2011 Ramez Elmasri and Shamkant Navathe
  • 19. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Specifying Key and Referential Integrity Constraints   PRIMARY KEY clause   Specifies one or more attributes that make up the primary key of a relation   Dnumber INT PRIMARY KEY;   UNIQUE clause   Specifies alternate (secondary) keys   Dname VARCHAR(15) UNIQUE;
  • 20. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Specifying Key and Referential Integrity Constraints (cont’d.)   FOREIGN KEY clause   Default operation: reject update on violation   Attach referential triggered action clause •  Options include SET NULL, CASCADE, and SET DEFAULT •  Action taken by the DBMS for SET NULL or SET DEFAULT is the same for both ON DELETE and ON UPDATE •  CASCADE option suitable for “relationship” relations
  • 21. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Giving Names to Constraints   Keyword CONSTRAINT   Name a constraint   Useful for later altering
  • 22. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Specifying Constraints on Tuples Using CHECK   CHECK clauses at the end of a CREATE TABLE statement   Apply to each tuple individually   CHECK (Dept_create_date <= Mgr_start_date);
  • 23. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Basic Retrieval Queries in SQL   SELECT statement   One basic statement for retrieving information from a database   SQL allows a table to have two or more tuples that are identical in all their attribute values   Unlike relational model   Multiset or bag behavior
  • 24. Copyright © 2011 Ramez Elmasri and Shamkant Navathe The SELECT-FROM-WHERE Structure of Basic SQL Queries   Basic form of the SELECT statement:
  • 25. Copyright © 2011 Ramez Elmasri and Shamkant Navathe The SELECT-FROM-WHERE Structure of Basic SQL Queries (cont’d.)   Logical comparison operators   =, <, <=, >, >=, and <>   Projection attributes   Attributes whose values are to be retrieved   Selection condition   Boolean condition that must be true for any retrieved tuple
  • 26. Copyright © 2011 Ramez Elmasri and Shamkant Navathe
  • 27. Copyright © 2011 Ramez Elmasri and Shamkant Navathe
  • 28. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Ambiguous Attribute Names   Same name can be used for two (or more) attributes   As long as the attributes are in different relations   Must qualify the attribute name with the relation name to prevent ambiguity
  • 29. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Aliasing, Renaming, and Tuple Variables   Aliases or tuple variables   Declare alternative relation names E and S   EMPLOYEE AS E(Fn, Mi, Ln, Ssn, Bd, Addr, Sex, Sal, Sssn, Dno)
  • 30. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Unspecified WHERE Clause and Use of the Asterisk   Missing WHERE clause   Indicates no condition on tuple selection   CROSS PRODUCT   All possible tuple combinations
  • 31. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Unspecified WHERE Clause and Use of the Asterisk (cont’d.)   Specify an asterisk (*)   Retrieve all the attribute values of the selected tuples
  • 32. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Tables as Sets in SQL   SQL does not automatically eliminate duplicate tuples in query results   Use the keyword DISTINCT in the SELECT clause   Only distinct tuples should remain in the result
  • 33. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Tables as Sets in SQL (cont’d.)   Set operations   UNION, EXCEPT (difference), INTERSECT   Corresponding multiset operations: UNION ALL, EXCEPT ALL, INTERSECT ALL)
  • 34. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Substring Pattern Matching and Arithmetic Operators   LIKE comparison operator   Used for string pattern matching   % replaces an arbitrary number of zero or more characters   underscore (_) replaces a single character   Standard arithmetic operators:   Addition (+), subtraction (–), multiplication (*), and division (/)   BETWEEN comparison operator
  • 35. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Ordering of Query Results   Use ORDER BY clause   Keyword DESC to see result in a descending order of values   Keyword ASC to specify ascending order explicitly   ORDER BY D.Dname DESC, E.Lname ASC, E.Fname ASC
  • 36. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Discussion and Summary of Basic SQL Retrieval Queries
  • 37. Copyright © 2011 Ramez Elmasri and Shamkant Navathe INSERT, DELETE, and UPDATE Statements in SQL   Three commands used to modify the database:   INSERT, DELETE, and UPDATE
  • 38. Copyright © 2011 Ramez Elmasri and Shamkant Navathe The INSERT Command   Specify the relation name and a list of values for the tuple
  • 39. Copyright © 2011 Ramez Elmasri and Shamkant Navathe The DELETE Command   Removes tuples from a relation   Includes a WHERE clause to select the tuples to be deleted
  • 40. Copyright © 2011 Ramez Elmasri and Shamkant Navathe The UPDATE Command   Modify attribute values of one or more selected tuples   Additional SET clause in the UPDATE command   Specifies attributes to be modified and new values
  • 41. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Additional Features of SQL   Techniques for specifying complex retrieval queries   Writing programs in various programming languages that include SQL statements   Set of commands for specifying physical database design parameters, file structures for relations, and access paths   Transaction control commands
  • 42. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Additional Features of SQL (cont’d.)   Specifying the granting and revoking of privileges to users   Constructs for creating triggers   Enhanced relational systems known as object-relational   New technologies such as XML and OLAP
  • 43. Copyright © 2011 Ramez Elmasri and Shamkant Navathe Summary   SQL   Comprehensive language   Data definition, queries, updates, constraint specification, and view definition   Covered in Chapter 4:   Data definition commands for creating tables   Commands for constraint specification   Simple retrieval queries   Database update commands