0% found this document useful (0 votes)
2 views

Kap4-Basic SQL-slides

The document provides an overview of SQL, emphasizing its role in relational databases and detailing various commands such as CREATE, INSERT, DELETE, and UPDATE. It explains schema concepts, attribute data types, constraints, and the structure of basic SQL queries. Additionally, it covers operations for modifying databases and retrieving data, including the use of ORDER BY and DISTINCT clauses.

Uploaded by

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

Kap4-Basic SQL-slides

The document provides an overview of SQL, emphasizing its role in relational databases and detailing various commands such as CREATE, INSERT, DELETE, and UPDATE. It explains schema concepts, attribute data types, constraints, and the structure of basic SQL queries. Additionally, it covers operations for modifying databases and retrieving data, including the use of ORDER BY and DISTINCT clauses.

Uploaded by

psvxdrbmxr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Basic SQL

Kapitulli 4 (Përmbledhje)
Elira Hoxha
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
Schema Concept 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
 CREATE SCHEMA statement
• CREATE SCHEMA COMPANY AUTHORIZATION ‘Jsmith’;
The CREATE TABLE Command in SQL

 Can optionally specify schema:


• CREATE TABLE COMPANY.EMPLOYEE ...
or
• CREATE TABLE EMPLOYEE ...
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 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 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 (2)
 FOREIGN KEY clause
• Default operation: reject update on violation
• Attach referential triggered action clause
o Options include SET NULL, CASCADE, and SET DEFAULT
o Action taken by the DBMS for SET NULL or SET DEFAULT is the same for both ON
DELETE and ON UPDATE
o CASCADE option suitable for “relationship” relations
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);

 Keyword CONSTRAINT
• Name a constraint
• Useful for later altering
The SELECT-FROM-WHERE Structure of
Basic SQL Queries
 Basic form of the SELECT statement:
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)
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 (cont’d.)
 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 (2)

 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
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
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

You might also like