Académique Documents
Professionnel Documents
Culture Documents
These slides are a modified version of the ones prepared by the authors
INTRODUCTION
SQL stands for Structured Query Language
SEQUEL
The SQL standard is maintained by the International Organization for Standards (ISO)
SQL:2019 is the most recent
No SQL implementation fully adheres to the standard
Most syntax is compatible between vendors
Data types may mismatch or not be implemented at all
What is created with CREATE can be modified with ALTER and removed with DROP
What is added with INSERT can be modified with UPDATE and removed with DELETE
Data retrievals (also part of DML) are done using SELECT, which will be covered later
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
CREATE SCHEMA statement:
CREATE SCHEMA COMPANY AUTHORIZATION ‘Jsmith’;
Catalog: Named collection of schemas in an SQL environment
USER DEFINED DATA TYPES
The CREATE command is used to create any schema construct
For example, creating Domains
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);
They may also be troublesome if you want to load the DB for the first time
They enforce a certain order on data entry
Insert referenced tuples first
SPECIFYING CONSTRAINTS IN SQL
Relational Model has 3 basic constraint types that are supported in SQL:
Key constraint: A primary key value cannot be duplicated
Entity Integrity Constraint: A primary key value cannot be null
Referential integrity constraints : The “foreign key” must have a value that is already present as a
primary key or may be null.
Check constraints can be used to check the values of an attribute in a certain relation, or
to check the values in individual tuples
CHECK clauses at the end of a CREATE TABLE statement
Apply to each tuple individually
CHECK (Dept_create_date <= Mgr_start_date);
This constraint verifies that the manager start date is after the department creation date
IN-CLASS EXERCISE:
Consider the following tables that describe two mini-world entities
Employee(SSN, Name, phone)
Department(Dno, Dname)
Modify table definitions given the following information, then write the needed SQL
statements to create them:
Many employees may work in the same department, while an employee works in one department only
How will table definitions change if an employee can have more than phone number
INSERTING DATA INTO TABLES
There are two ways to use the INSERT command: CREATE TABLE CAR(VIN INT
CONSTRAINT CAR_PK PRIMARY KEY,
The shorthand version: MAKE VARCHAR(20) NOT NULL,
MODEL VARCHAR(20),
INSERT INTO CAR VALUES(123,'HONDA', 'ACCORD', 5) NO_OF_SEATS INT
-- THE FOLLOWING WILL CAUSE AN ERROR CONSTRAINT SEATS_DEF DEFAULT 5);
INSERT INTO CAR VALUES(321,'TOYOTA', 'CAMREY')
The full (long) version:
INSERT INTO CAR(VIN, MAKE, MODEL) VALUES (321,'TOYOTA', 'CAMREY')
INSERT INTO CAR(MAKE, VIN) VALUES ('NISSAN', 111)
INSERTING DATA INTO TABLES
In the full version, you can choose which
attributes to assign values to in any order
you like.
The attributes that are not assigned values
will have either NULLs or their default values
Remember that the following insertions were
made:
INSERT INTO CAR VALUES(123,'HONDA', 'ACCORD', 5)
INSERT INTO CAR(VIN, MAKE, MODEL) VALUES (321,'TOYOTA', 'CAMREY')
INSERT INTO CAR(MAKE, VIN) VALUES ('NISSAN', 111)
DELETING FROM TABLES
The DELETE command can be used to delete rows from tables.
Usually used with a WHERE clause to choose which rows to delete
Otherwise, all table content will be deleted. The table will not be removed.
Examples:
DELETE CAR WHERE VIN = 111
DELETE CAR
UPDATING DATA
The UPDATE command is used to modify the values for one or more attributes in one
or more rows
Like DELETE, UPDATE is usually used with a WHERE clause to specify which rows to
update. Otherwise, the values in the whole column will be updated.
SQL allows a table to have two or more tuples that are identical in all their attribute
values
Unlike relational model (relational model is strictly set-theory based)
Multiset or bag behavior
Tuple-id may be used as a key
THE SELECT STATEMENT
Basic form of the SELECT statement:
THE SELECT STATEMENT
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. Selection conditions include join conditions
(see Ch.8) when multiple relations are involved.
BASIC RETRIEVAL QUERIES
From the query you should be able
to identify:
The needed attributes
The conditions that the values must meet
Query 8. For each employee, retrieve the employee’s first and last name and the first and last
name of his or her immediate supervisor.
-- OK
INSERT INTO TEST VALUES ('ALI', '(123) 456-7890', 20)
ARITHMETIC OPERATIONS
Standard arithmetic operators:
Addition (+), subtraction (–), multiplication (*), and division (/) may be included as a part of SELECT
Query 13. Show the resulting salaries if every employee working on the ‘ProductX’ project is
given a 10 percent raise.
In this example, tuples will be in descending order of first name only if they have
equal salaries. That is the second ordering criterion will not be used unless a tie in the
first occurred.