SlideShare a Scribd company logo
1 of 47
SQL
Basic SQL
1
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
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
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
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
Schema and Catalog Concepts in SQL
Catalog
Named collection of schemas in an SQL environment
SQL environment
Installation of an SQL-compliant RDBMS on a computer
system
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 ...
The CREATE TABLE Command in SQL
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
The CREATE TABLE Command in SQL
The CREATE TABLE Command in SQL
The CREATE TABLE Command in SQL
Some foreign keys may cause errors
Specified either via:
Circular references
Or because they refer to a table that has not yet been created
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)
Attribute Data Types and Domains in
SQL
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
Attribute Data Types and Domains in
SQL
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
Attribute Data Types and Domains in
SQL
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);
Specifying Constraints in SQL
Basic constraints:
Key and referential integrity constraints
Restrictions on attribute domains and NULLs
Constraints on individual tuples within a relation
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);
Specifying Attribute Constraints and
Attribute Defaults
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;
Specifying Key and Referential Integrity
Constraints
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
Giving Names to Constraints
Keyword CONSTRAINT
Name a constraint
Useful for later altering
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);
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
The SELECT-FROM-WHERE Structure of
Basic SQL Queries
Basic form of the SELECT statement:
The SELECT-FROM-WHERE
Structure of Basic SQL Queries
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
The SELECT-FROM-WHERE Structure of
Basic SQL Queries
The SELECT-FROM-WHERE Structure of
Basic SQL Queries
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
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)
Aliasing, and Renaming
Slide 6- 30
Unspecified WHERE Clause
and Use of the Asterisk
Missing WHERE clause
Indicates no condition on tuple selection
CROSS PRODUCT
All possible tuple combinations
Unspecified WHERE Clause
and Use of the Asterisk
Specify an asterisk (*)
Retrieve all the attribute values of the selected tuples
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
Tables as Sets in SQL
Set operations
UNION, EXCEPT (difference), INTERSECT
Corresponding multiset operations: UNION ALL, EXCEPT
ALL, INTERSECT ALL)
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
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
Discussion and Summary
of Basic SQL Retrieval Queries
7.3 Views (Virtual Tables) in SQL
Single table derived from other tables called the defining
tables
Considered to be a virtual table that is not necessarily
populated
Slide 6- 38
Once a View is defined, SQL queries can use the View
relation in the FROM clause
DROP VIEW command
Dispose of a view .
INSERT, DELETE, and UPDATE
Statements in SQL
Three commands used to modify the database:
INSERT, DELETE, and UPDATE
The INSERT Command
Specify the relation name and a list of values for the
tuple
The DELETE Command
Removes tuples from a relation
Includes a WHERE clause to select the tuples to be deleted
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
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
Additional Features of SQL
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
Table 7.2 Summary of SQL Syntax
continued on next slide
Slide 5- 47

More Related Content

Similar to chapter9-SQL.pptx

SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSabrinaShanta2
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSaiMiryala1
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxBhupendraShahi6
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...SakkaravarthiS1
 
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Prosanta Ghosh
 
chap 7.ppt(sql).ppt
chap 7.ppt(sql).pptchap 7.ppt(sql).ppt
chap 7.ppt(sql).pptarjun431527
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptxEllenGracePorras
 
Oracle apps financial online training
Oracle apps financial online trainingOracle apps financial online training
Oracle apps financial online trainingmagnifics
 
6_SQL.pdf
6_SQL.pdf6_SQL.pdf
6_SQL.pdfLPhct2
 

Similar to chapter9-SQL.pptx (20)

Db1 lecture4
Db1 lecture4Db1 lecture4
Db1 lecture4
 
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
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptxSQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
SQL-Tutorial.P1241112567Pczwq.powerpoint.pptx
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 
Sql (DBMS)
Sql (DBMS)Sql (DBMS)
Sql (DBMS)
 
Ch04
Ch04Ch04
Ch04
 
chap 7.ppt(sql).ppt
chap 7.ppt(sql).pptchap 7.ppt(sql).ppt
chap 7.ppt(sql).ppt
 
Chap 7
Chap 7Chap 7
Chap 7
 
SQL
SQLSQL
SQL
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Sql
SqlSql
Sql
 
Oracle apps financial online training
Oracle apps financial online trainingOracle apps financial online training
Oracle apps financial online training
 
6_SQL.pdf
6_SQL.pdf6_SQL.pdf
6_SQL.pdf
 
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
 
UNIT-3.pptx
UNIT-3.pptxUNIT-3.pptx
UNIT-3.pptx
 

Recently uploaded

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 

chapter9-SQL.pptx

  • 2. 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. 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. 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. 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. Schema and Catalog Concepts in SQL Catalog Named collection of schemas in an SQL environment SQL environment Installation of an SQL-compliant RDBMS on a computer system
  • 7. 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. The CREATE TABLE Command in SQL 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. The CREATE TABLE Command in SQL
  • 10. The CREATE TABLE Command in SQL
  • 11. The CREATE TABLE Command in SQL 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. 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. Attribute Data Types and Domains in SQL 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. Attribute Data Types and Domains in SQL 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. Attribute Data Types and Domains in SQL 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. 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. 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. Specifying Attribute Constraints and Attribute Defaults
  • 19. 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. Specifying Key and Referential Integrity Constraints 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. Giving Names to Constraints Keyword CONSTRAINT Name a constraint Useful for later altering
  • 22. 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. 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. The SELECT-FROM-WHERE Structure of Basic SQL Queries Basic form of the SELECT statement:
  • 25. The SELECT-FROM-WHERE Structure of Basic SQL Queries 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. The SELECT-FROM-WHERE Structure of Basic SQL Queries
  • 27. The SELECT-FROM-WHERE Structure of Basic SQL Queries
  • 28. 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. 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)
  • 31. Unspecified WHERE Clause and Use of the Asterisk Missing WHERE clause Indicates no condition on tuple selection CROSS PRODUCT All possible tuple combinations
  • 32. Unspecified WHERE Clause and Use of the Asterisk Specify an asterisk (*) Retrieve all the attribute values of the selected tuples
  • 33. 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
  • 34. Tables as Sets in SQL Set operations UNION, EXCEPT (difference), INTERSECT Corresponding multiset operations: UNION ALL, EXCEPT ALL, INTERSECT ALL)
  • 35. 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
  • 36. 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
  • 37. Discussion and Summary of Basic SQL Retrieval Queries
  • 38. 7.3 Views (Virtual Tables) in SQL Single table derived from other tables called the defining tables Considered to be a virtual table that is not necessarily populated Slide 6- 38
  • 39.
  • 40. Once a View is defined, SQL queries can use the View relation in the FROM clause DROP VIEW command Dispose of a view .
  • 41. INSERT, DELETE, and UPDATE Statements in SQL Three commands used to modify the database: INSERT, DELETE, and UPDATE
  • 42. The INSERT Command Specify the relation name and a list of values for the tuple
  • 43. The DELETE Command Removes tuples from a relation Includes a WHERE clause to select the tuples to be deleted
  • 44. 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
  • 45. 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
  • 46. Additional Features of SQL 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
  • 47. Table 7.2 Summary of SQL Syntax continued on next slide Slide 5- 47

Editor's Notes

  1. We can use this alias-naming or renaming mechanism in any SQL query to specify tuple variables for every table in the clause WHERE, whether or not the same relation needs to be referenced more than once